@@ -17,7 +17,6 @@ import qualified Data.HashMap.Strict as H
17
17
import qualified Data.Text as T
18
18
import Development.IDE as D
19
19
import qualified GHC.Generics as Generics
20
- import Ide.Plugin
21
20
import Ide.Types
22
21
import Language.Haskell.LSP.Types
23
22
import qualified Language.Haskell.LSP.Types as J
@@ -32,19 +31,12 @@ import qualified Language.Haskell.LSP.VFS as VFS
32
31
33
32
descriptor :: PluginId -> PluginDescriptor
34
33
descriptor plId = (defaultPluginDescriptor plId)
35
- { pluginCommands = commands
36
- , pluginCodeActionProvider = Just codeActionProvider
34
+ { pluginCodeActionProvider = Just codeActionProvider
37
35
, pluginCompletionProvider = Just completion
38
36
}
39
37
40
38
-- ---------------------------------------------------------------------
41
39
42
- commands :: [PluginCommand ]
43
- commands = [ PluginCommand " addPragma" " add the given pragma" addPragmaCmd
44
- ]
45
-
46
- -- ---------------------------------------------------------------------
47
-
48
40
-- | Parameters for the addPragma PluginCommand.
49
41
data AddPragmaParams = AddPragmaParams
50
42
{ file :: J. Uri -- ^ Uri of the file to add the pragma to
@@ -56,9 +48,9 @@ data AddPragmaParams = AddPragmaParams
56
48
-- Pragma is added to the first line of the Uri.
57
49
-- It is assumed that the pragma name is a valid pragma,
58
50
-- thus, not validated.
59
- addPragmaCmd :: CommandFunction AddPragmaParams
60
- addPragmaCmd _lf _ide ( AddPragmaParams uri pragmaName) = do
61
- let
51
+ -- mkPragmaEdit :: CommandFunction AddPragmaParams
52
+ mkPragmaEdit :: Uri -> T. Text -> WorkspaceEdit
53
+ mkPragmaEdit uri pragmaName = res where
62
54
pos = J. Position 0 0
63
55
textEdits = J. List
64
56
[J. TextEdit (J. Range pos pos)
@@ -67,33 +59,29 @@ addPragmaCmd _lf _ide (AddPragmaParams uri pragmaName) = do
67
59
res = J. WorkspaceEdit
68
60
(Just $ H. singleton uri textEdits)
69
61
Nothing
70
- return (Right Null , Just (WorkspaceApplyEdit , ApplyWorkspaceEditParams res))
71
62
72
63
-- ---------------------------------------------------------------------
73
64
-- | Offer to add a missing Language Pragma to the top of a file.
74
65
-- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
75
66
codeActionProvider :: CodeActionProvider
76
- codeActionProvider _ state plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
67
+ codeActionProvider _ state _plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
77
68
let mFile = docId ^. J. uri & uriToFilePath <&> toNormalizedFilePath'
78
69
pm <- fmap join $ runAction " addPragma" state $ getParsedModule `traverse` mFile
79
70
let dflags = ms_hspp_opts . pm_mod_summary <$> pm
80
71
-- Filter diagnostics that are from ghcmod
81
72
ghcDiags = filter (\ d -> d ^. J. source == Just " typecheck" ) diags
82
73
-- Get all potential Pragmas for all diagnostics.
83
74
pragmas = concatMap (\ d -> genPragma dflags (d ^. J. message)) ghcDiags
84
- -- cmds <- mapM mkCommand ("FooPragma":pragmas)
85
- cmds <- mapM mkCommand pragmas
75
+ cmds <- mapM mkCodeAction pragmas
86
76
return $ Right $ List cmds
87
77
where
88
- mkCommand pragmaName = do
78
+ mkCodeAction pragmaName = do
89
79
let
90
- -- | Code Action for the given command.
91
- codeAction :: J. Command -> J. CAResult
92
- codeAction cmd = J. CACodeAction $ J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) Nothing (Just cmd)
80
+ codeAction = J. CACodeAction $ J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) (Just edit) Nothing
93
81
title = " Add \" " <> pragmaName <> " \" "
94
- cmdParams = [toJSON ( AddPragmaParams ( docId ^. J. uri) pragmaName)]
95
- cmd <- mkLspCommand plId " addPragma " title ( Just cmdParams)
96
- return $ codeAction cmd
82
+ edit = mkPragmaEdit ( docId ^. J. uri) pragmaName
83
+ return codeAction
84
+
97
85
genPragma mDynflags target
98
86
| Just dynFlags <- mDynflags,
99
87
-- GHC does not export 'OnOff', so we have to view it as string
0 commit comments