1
- {-# LANGUAGE DeriveAnyClass #-}
2
- {-# LANGUAGE DeriveGeneric #-}
3
- {-# LANGUAGE OverloadedStrings #-}
1
+ {-# LANGUAGE DeriveAnyClass #-}
2
+ {-# LANGUAGE DeriveGeneric #-}
4
3
{-# LANGUAGE DuplicateRecordFields #-}
4
+ {-# LANGUAGE OverloadedStrings #-}
5
5
6
6
-- | Provides code actions to add missing pragmas (whenever GHC suggests to)
7
7
module Ide.Plugin.Pragmas
@@ -10,20 +10,22 @@ module Ide.Plugin.Pragmas
10
10
-- , commands -- TODO: get rid of this
11
11
) where
12
12
13
- import Control.Lens hiding (List )
13
+ import Control.Lens hiding (List )
14
14
import Data.Aeson
15
15
import qualified Data.HashMap.Strict as H
16
16
import qualified Data.Text as T
17
+ import Development.IDE as D
18
+ import qualified GHC.Generics as Generics
17
19
import Ide.Plugin
18
20
import Ide.Types
19
- import qualified GHC.Generics as Generics
21
+ import Language.Haskell.LSP.Types
20
22
import qualified Language.Haskell.LSP.Types as J
21
23
import qualified Language.Haskell.LSP.Types.Lens as J
22
- import Development.IDE as D
23
- import Language.Haskell.LSP.Types
24
24
25
- import qualified Language.Haskell.LSP.Core as LSP
26
- import qualified Language.Haskell.LSP.VFS as VFS
25
+ import Control.Monad (join )
26
+ import Development.IDE.GHC.Compat
27
+ import qualified Language.Haskell.LSP.Core as LSP
28
+ import qualified Language.Haskell.LSP.VFS as VFS
27
29
28
30
-- ---------------------------------------------------------------------
29
31
@@ -67,28 +69,38 @@ addPragmaCmd _lf _ide (AddPragmaParams uri pragmaName) = do
67
69
return (Right Null , Just (WorkspaceApplyEdit , ApplyWorkspaceEditParams res))
68
70
69
71
-- ---------------------------------------------------------------------
70
-
72
+ -- ms_hspp_opts
71
73
-- | Offer to add a missing Language Pragma to the top of a file.
72
74
-- Pragmas are defined by a curated list of known pragmas, see 'possiblePragmas'.
73
75
codeActionProvider :: CodeActionProvider
74
- codeActionProvider _ _ plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
75
- cmds <- mapM mkCommand pragmas
76
- -- cmds <- mapM mkCommand ("FooPragma":pragmas)
77
- return $ Right $ List cmds
78
- where
76
+ codeActionProvider _ state plId docId _ (J. CodeActionContext (J. List diags) _monly) = do
77
+ let mFile = docId ^. J. uri & uriToFilePath <&> toNormalizedFilePath'
78
+ pm <- fmap join $ runAction " addPragma" state $ getParsedModule `traverse` mFile
79
+ let dflags = ms_hspp_opts . pm_mod_summary <$> pm
79
80
-- Filter diagnostics that are from ghcmod
80
- ghcDiags = filter (\ d -> d ^. J. source == Just " typecheck" ) diags
81
+ ghcDiags = filter (\ d -> d ^. J. source == Just " typecheck" ) diags
81
82
-- Get all potential Pragmas for all diagnostics.
82
- pragmas = concatMap (\ d -> findPragma (d ^. J. message)) ghcDiags
83
- mkCommand pragmaName = do
84
- let
85
- -- | Code Action for the given command.
86
- codeAction :: J. Command -> J. CAResult
87
- codeAction cmd = J. CACodeAction $ J. CodeAction title (Just J. CodeActionQuickFix ) (Just (J. List [] )) Nothing (Just cmd)
88
- title = " Add \" " <> pragmaName <> " \" "
89
- cmdParams = [toJSON (AddPragmaParams (docId ^. J. uri) pragmaName )]
90
- cmd <- mkLspCommand plId " addPragma" title (Just cmdParams)
91
- return $ codeAction cmd
83
+ pragmas = concatMap (\ d -> genPragma dflags (d ^. J. message)) ghcDiags
84
+ -- cmds <- mapM mkCommand ("FooPragma":pragmas)
85
+ cmds <- mapM mkCommand pragmas
86
+ return $ Right $ List cmds
87
+ where
88
+ mkCommand pragmaName = do
89
+ 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)
93
+ title = " Add \" " <> pragmaName <> " \" "
94
+ cmdParams = [toJSON (AddPragmaParams (docId ^. J. uri) pragmaName)]
95
+ cmd <- mkLspCommand plId " addPragma" title (Just cmdParams)
96
+ return $ codeAction cmd
97
+ genPragma mDynflags target
98
+ | Just dynFlags <- mDynflags,
99
+ -- GHC does not export 'OnOff', so we have to convert it into string
100
+ disabled <- [ e | Just e <- T. stripPrefix " Off " . T. pack . prettyPrint <$> extensions dynFlags]
101
+ = [ r | r <- findPragma target, r `notElem` disabled]
102
+ | otherwise = []
103
+
92
104
93
105
-- ---------------------------------------------------------------------
94
106
0 commit comments