@@ -32,12 +32,17 @@ pragmasDescriptor plId = PluginDescriptor
32
32
33
33
-- ---------------------------------------------------------------------
34
34
35
+ -- | Parameters for the addPragma PluginCommand.
35
36
data 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
38
39
}
39
40
deriving (Show , Eq , Generics.Generic , ToJSON , FromJSON )
40
41
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.
41
46
addPragmaCmd :: CommandFunc AddPragmaParams J. WorkspaceEdit
42
47
addPragmaCmd = CmdSync $ \ (AddPragmaParams uri pragmaName) -> do
43
48
let
@@ -53,15 +58,20 @@ addPragmaCmd = CmdSync $ \(AddPragmaParams uri pragmaName) -> do
53
58
54
59
-- ---------------------------------------------------------------------
55
60
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'.
56
63
codeActionProvider :: CodeActionProvider
57
64
codeActionProvider plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
58
65
cmds <- mapM mkCommand pragmas
59
66
return $ IdeResultOk cmds
60
67
where
68
+ -- Filter diagnostics that are from ghcmod
61
69
ghcDiags = filter (\ d -> d ^. J. source == Just " ghcmod" ) diags
70
+ -- Get all potential Pragmas for all diagnostics.
62
71
pragmas = concatMap (\ d -> findPragma (d ^. J. message)) ghcDiags
63
72
mkCommand pragmaName = do
64
73
let
74
+ -- | Code Action for the given command.
65
75
codeAction :: J. Command -> J. CodeAction
66
76
codeAction cmd = J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) Nothing (Just cmd)
67
77
title = " Add \" " <> pragmaName <> " \" "
@@ -71,13 +81,16 @@ codeActionProvider plId docId _ (J.CodeActionContext (J.List diags) _monly) = do
71
81
72
82
-- ---------------------------------------------------------------------
73
83
84
+ -- | Find all Pragmas are an infix of the search term.
74
85
findPragma :: T. Text -> [T. Text ]
75
86
findPragma str = concatMap check possiblePragmas
76
87
where
77
88
check p = [p | T. isInfixOf p str]
78
89
79
90
-- ---------------------------------------------------------------------
80
91
92
+ -- | Possible Pragma names.
93
+ -- Is non-exhaustive, and may be extended.
81
94
possiblePragmas :: [T. Text ]
82
95
possiblePragmas =
83
96
[
0 commit comments