Skip to content

Commit 46c7634

Browse files
sheafcocreature
authored andcommitted
use GHC language extension names (#362)
1 parent 742df7d commit 46c7634

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

src/Development/IDE/GHC/Compat.hs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module Development.IDE.GHC.Compat(
1616
includePathsGlobal,
1717
includePathsQuote,
1818
addIncludePathsQuote,
19-
ghcEnumerateExtensions,
2019
pattern DerivD,
2120
pattern ForD,
2221
pattern InstD,
@@ -31,11 +30,6 @@ module Development.IDE.GHC.Compat(
3130
import StringBuffer
3231
import DynFlags
3332
import FieldLabel
34-
import GHC.LanguageExtensions.Type
35-
36-
#if MIN_GHC_API_VERSION(8,8,0)
37-
import Data.List.Extra (enumerate)
38-
#endif
3933

4034
import qualified GHC
4135
import GHC hiding (ClassOpSig, DerivD, ForD, IEThingWith, InstD, TyClD, ValD)
@@ -88,15 +82,6 @@ addIncludePathsQuote path x = x{includePaths = f $ includePaths x}
8882
addIncludePathsQuote path x = x{includePaths = path : includePaths x}
8983
#endif
9084

91-
ghcEnumerateExtensions :: [Extension]
92-
#if MIN_GHC_API_VERSION(8,8,0)
93-
ghcEnumerateExtensions = enumerate
94-
#elif MIN_GHC_API_VERSION(8,6,0)
95-
ghcEnumerateExtensions = [Cpp .. StarIsType]
96-
#else
97-
ghcEnumerateExtensions = [Cpp .. EmptyDataDeriving]
98-
#endif
99-
10085
pattern DerivD :: DerivDecl p -> HsDecl p
10186
pattern DerivD x <-
10287
#if MIN_GHC_API_VERSION(8,6,0)

src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import Development.IDE.LSP.Server
2121
import Development.IDE.Types.Location
2222
import Development.IDE.Types.Options
2323
import qualified Data.HashMap.Strict as Map
24-
import qualified Data.HashSet as Set
2524
import qualified Language.Haskell.LSP.Core as LSP
2625
import Language.Haskell.LSP.VFS
2726
import Language.Haskell.LSP.Messages
@@ -32,9 +31,12 @@ import Data.Char
3231
import Data.Maybe
3332
import Data.List.Extra
3433
import qualified Data.Text as T
34+
import Data.Tuple.Extra ((&&&))
3535
import Text.Regex.TDFA ((=~), (=~~))
3636
import Text.Regex.TDFA.Text()
3737
import Outputable (ppr, showSDocUnsafe)
38+
import DynFlags (xFlags, FlagSpec(..))
39+
import GHC.LanguageExtensions.Type (Extension)
3840

3941
plugin :: Plugin
4042
plugin = codeActionPlugin codeAction <> Plugin mempty setHandlersCodeLens
@@ -210,10 +212,14 @@ suggestAddExtension Diagnostic{_range=_range@Range{..},..}
210212
-- * In the context: a ~ ()
211213
-- While checking an instance declaration
212214
-- In the instance declaration for `Unit (m a)'
213-
| exts@(_:_) <- filter (`Set.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
215+
| exts@(_:_) <- filter (`Map.member` ghcExtensions) $ T.split (not . isAlpha) $ T.replace "-X" "" _message
214216
= [("Add " <> x <> " extension", [TextEdit (Range (Position 0 0) (Position 0 0)) $ "{-# LANGUAGE " <> x <> " #-}\n"]) | x <- exts]
215217
| otherwise = []
216218

219+
-- | All the GHC extensions
220+
ghcExtensions :: Map.HashMap T.Text Extension
221+
ghcExtensions = Map.fromList . map ( ( T.pack . flagSpecName ) &&& flagSpecFlag ) $ xFlags
222+
217223
suggestModuleTypo :: Diagnostic -> [(T.Text, [TextEdit])]
218224
suggestModuleTypo Diagnostic{_range=_range@Range{..},..}
219225
-- src/Development/IDE/Core/Compile.hs:58:1: error:
@@ -367,10 +373,6 @@ extendToWholeLineIfPossible contents range@Range{..} =
367373
extend = newlineAfter && _character _start == 0 -- takes up an entire line, so remove the whole line
368374
in if extend then Range _start (Position (_line _end + 1) 0) else range
369375

370-
-- | All the GHC extensions
371-
ghcExtensions :: Set.HashSet T.Text
372-
ghcExtensions = Set.fromList $ map (T.pack . show) ghcEnumerateExtensions
373-
374376
splitTextAtPosition :: Position -> T.Text -> (T.Text, T.Text)
375377
splitTextAtPosition (Position row col) x
376378
| (preRow, mid:postRow) <- splitAt row $ T.splitOn "\n" x

test/exe/Main.hs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ codeActionTests = testGroup "code actions"
392392
, typeWildCardActionTests
393393
, removeImportTests
394394
, extendImportTests
395+
, addExtensionTests
395396
, fixConstructorImportTests
396397
, importRenameActionTests
397398
, fillTypedHoleTests
@@ -806,6 +807,62 @@ extendImportTests = testGroup "extend import actions"
806807
contentAfterAction <- documentContents docB
807808
liftIO $ expectedContentB @=? contentAfterAction
808809

810+
addExtensionTests :: TestTree
811+
addExtensionTests = testGroup "add language extension actions"
812+
[ testSession "add NamedFieldPuns language extension" $ template
813+
(T.unlines
814+
[ "module Module where"
815+
, ""
816+
, "data A = A { getA :: Bool }"
817+
, ""
818+
, "f :: A -> Bool"
819+
, "f A { getA } = getA"
820+
])
821+
(Range (Position 0 0) (Position 0 0))
822+
"Add NamedFieldPuns extension"
823+
(T.unlines
824+
[ "{-# LANGUAGE NamedFieldPuns #-}"
825+
, "module Module where"
826+
, ""
827+
, "data A = A { getA :: Bool }"
828+
, ""
829+
, "f :: A -> Bool"
830+
, "f A { getA } = getA"
831+
])
832+
, testSession "add RecordWildCards language extension" $ template
833+
(T.unlines
834+
[ "module Module where"
835+
, ""
836+
, "data A = A { getA :: Bool }"
837+
, ""
838+
, "f :: A -> Bool"
839+
, "f A { .. } = getA"
840+
])
841+
(Range (Position 0 0) (Position 0 0))
842+
"Add RecordWildCards extension"
843+
(T.unlines
844+
[ "{-# LANGUAGE RecordWildCards #-}"
845+
, "module Module where"
846+
, ""
847+
, "data A = A { getA :: Bool }"
848+
, ""
849+
, "f :: A -> Bool"
850+
, "f A { .. } = getA"
851+
])
852+
]
853+
where
854+
template initialContent range expectedAction expectedContents = do
855+
doc <- openDoc' "Module.hs" "haskell" initialContent
856+
_ <- waitForDiagnostics
857+
CACodeAction action@CodeAction { _title = actionTitle } : _
858+
<- sortOn (\(CACodeAction CodeAction{_title=x}) -> x) <$>
859+
getCodeActions doc range
860+
liftIO $ expectedAction @=? actionTitle
861+
executeCodeAction action
862+
contentAfterAction <- documentContents doc
863+
liftIO $ expectedContents @=? contentAfterAction
864+
865+
809866
insertNewDefinitionTests :: TestTree
810867
insertNewDefinitionTests = testGroup "insert new definition actions"
811868
[ testSession "insert new function definition" $ do

0 commit comments

Comments
 (0)