@@ -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,20 @@ 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'.
5663codeActionProvider :: CodeActionProvider
5764codeActionProvider plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
5865 cmds <- mapM mkCommand pragmas
5966 return $ IdeResultOk cmds
6067 where
68+ -- Filter diagnostics that are from ghcmod
6169 ghcDiags = filter (\ d -> d ^. J. source == Just " ghcmod" ) diags
70+ -- Get all potential Pragmas for all diagnostics.
6271 pragmas = concatMap (\ d -> findPragma (d ^. J. message)) ghcDiags
6372 mkCommand pragmaName = do
6473 let
74+ -- | Code Action for the given command.
6575 codeAction :: J. Command -> J. CodeAction
6676 codeAction cmd = J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) Nothing (Just cmd)
6777 title = " Add \" " <> pragmaName <> " \" "
@@ -71,13 +81,16 @@ codeActionProvider plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
7181
7282-- ---------------------------------------------------------------------
7383
84+ -- | Find all Pragmas are an infix of the search term.
7485findPragma :: T. Text -> [T. Text ]
7586findPragma str = concatMap check possiblePragmas
7687 where
7788 check p = [p | T. isInfixOf p str]
7889
7990-- ---------------------------------------------------------------------
8091
92+ -- | Possible Pragma names.
93+ -- Is non-exhaustive, and may be extended.
8194possiblePragmas :: [T. Text ]
8295possiblePragmas =
8396 [
0 commit comments