Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Haskell/Ide/Engine/Plugin/Pragmas.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ pragmasDescriptor plId = PluginDescriptor

-- ---------------------------------------------------------------------

-- | Parameters for the addPragma PluginCommand.
data AddPragmaParams = AddPragmaParams
{ file :: Uri
, pragma :: T.Text
{ file :: Uri -- ^ Uri of the file to add the pragma to
, pragma :: T.Text -- ^ Name of the Pragma to add
}
deriving (Show, Eq, Generics.Generic, ToJSON, FromJSON)

-- | Add a Pragma to the given URI at the top of the file.
-- Pragma is added to the first line of the Uri.
-- It is assumed that the pragma name is a valid pragma,
-- thus, not validated.
addPragmaCmd :: CommandFunc AddPragmaParams J.WorkspaceEdit
addPragmaCmd = CmdSync $ \(AddPragmaParams uri pragmaName) -> do
let
Expand All @@ -53,15 +58,20 @@ addPragmaCmd = CmdSync $ \(AddPragmaParams uri pragmaName) -> do

-- ---------------------------------------------------------------------

-- | Offer to add a missing Language Pragma to the top of a file.
-- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
codeActionProvider :: CodeActionProvider
codeActionProvider plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
cmds <- mapM mkCommand pragmas
return $ IdeResultOk cmds
where
-- Filter diagnostics that are from ghcmod
ghcDiags = filter (\d -> d ^. J.source == Just "ghcmod") diags
-- Get all potential Pragmas for all diagnostics.
pragmas = concatMap (\d -> findPragma (d ^. J.message)) ghcDiags
mkCommand pragmaName = do
let
-- | Code Action for the given command.
codeAction :: J.Command -> J.CodeAction
codeAction cmd = J.CodeAction title (Just J.CodeActionQuickFix) (Just (J.List [])) Nothing (Just cmd)
title = "Add \"" <> pragmaName <> "\""
Expand All @@ -71,13 +81,16 @@ codeActionProvider plId docId _ (J.CodeActionContext (J.List diags) _monly) = do

-- ---------------------------------------------------------------------

-- | Find all Pragmas are an infix of the search term.
findPragma :: T.Text -> [T.Text]
findPragma str = concatMap check possiblePragmas
where
check p = [p | T.isInfixOf p str]

-- ---------------------------------------------------------------------

-- | Possible Pragma names.
-- Is non-exhaustive, and may be extended.
possiblePragmas :: [T.Text]
possiblePragmas =
[
Expand Down