@@ -32,12 +32,17 @@ pragmasDescriptor plId = PluginDescriptor
3232
3333-- ---------------------------------------------------------------------
3434
35+ -- | Parameters for the addPragma PluginCommand.
3536data AddPragmaParams = AddPragmaParams
36- { file :: Uri
37- , pragma :: T. Text
37+ { file :: Uri -- ^ Uri of the file to add the pragma to
38+ , pragma :: T. Text -- ^ Name of the Pragma to add
3839 }
3940 deriving (Show , Eq , Generics.Generic , ToJSON , FromJSON )
4041
42+ -- | Add a Pragma to the given URI at the top of the file.
43+ -- Pragma is added to the first line of the Uri.
44+ -- It is assumed that the pragma name is a valid pragma,
45+ -- thus, not validated.
4146addPragmaCmd :: CommandFunc AddPragmaParams J. WorkspaceEdit
4247addPragmaCmd = CmdSync $ \ (AddPragmaParams uri pragmaName) -> do
4348 let
@@ -53,15 +58,22 @@ addPragmaCmd = CmdSync $ \(AddPragmaParams uri pragmaName) -> do
5358
5459-- ---------------------------------------------------------------------
5560
61+ -- | Offer to add a missing Language Pragma to the top of a file.
62+ -- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
63+ -- May also offer to add unknown pragmas, if the pragma is a subexpression of
64+ -- an existing plugin.
5665codeActionProvider :: CodeActionProvider
5766codeActionProvider plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
5867 cmds <- mapM mkCommand pragmas
5968 return $ IdeResultOk cmds
6069 where
70+ -- Filter diagnostics that are from ghcmod
6171 ghcDiags = filter (\ d -> d ^. J. source == Just " ghcmod" ) diags
72+ -- Get all potential Pragmas for all diagnostics.
6273 pragmas = concatMap (\ d -> findPragma (d ^. J. message)) ghcDiags
6374 mkCommand pragmaName = do
6475 let
76+ -- | Code Action for the given command.
6577 codeAction :: J. Command -> J. CodeAction
6678 codeAction cmd = J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) Nothing (Just cmd)
6779 title = " Add \" " <> pragmaName <> " \" "
@@ -71,13 +83,16 @@ codeActionProvider plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
7183
7284-- ---------------------------------------------------------------------
7385
86+ -- | Find all Pragmas that have the search term as infix.
7487findPragma :: T. Text -> [T. Text ]
7588findPragma str = concatMap check possiblePragmas
7689 where
7790 check p = [p | T. isInfixOf p str]
7891
7992-- ---------------------------------------------------------------------
8093
94+ -- | Possible Pragama names.
95+ -- Is non-exhaustive, and may be extended.
8196possiblePragmas :: [T. Text ]
8297possiblePragmas =
8398 [
0 commit comments