Skip to content

Commit

Permalink
Remove Strict from the language extensions used for code actions (#638
Browse files Browse the repository at this point in the history
)

Since the code action for language extension suggestions uses substring
matching, the presence of the literal name of an extension can trigger
a false positive.

`Strict` is an identifier that occurs frequently in imports, causing
the extension to be suggested rather than the removal of a redundant
import.
  • Loading branch information
tek authored Jun 17, 2020
1 parent 71631d8 commit 0d806c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Development/IDE/Plugin/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ suggestAddExtension Diagnostic{_range=_range,..}

-- | All the GHC extensions
ghcExtensions :: Map.HashMap T.Text Extension
ghcExtensions = Map.fromList . map ( ( T.pack . flagSpecName ) &&& flagSpecFlag ) $ xFlags
ghcExtensions = Map.fromList . filter notStrictFlag . map ( ( T.pack . flagSpecName ) &&& flagSpecFlag ) $ xFlags
where
-- Strict often causes false positives, as in Data.Map.Strict imports.
-- See discussion at https://github.com/digital-asset/ghcide/pull/638
notStrictFlag (name, _) = name /= "Strict"

suggestModuleTypo :: Diagnostic -> [(T.Text, [TextEdit])]
suggestModuleTypo Diagnostic{_range=_range,..}
Expand Down
22 changes: 22 additions & 0 deletions test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,28 @@ removeImportTests = testGroup "remove import actions"
, "main = B"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "import containing the identifier Strict" $ do
let contentA = T.unlines
[ "module Strict where"
]
_docA <- createDoc "Strict.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import Strict"
]
docB <- createDoc "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
[CACodeAction action@CodeAction { _title = actionTitle }]
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
liftIO $ "Remove import" @=? actionTitle
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
]

extendImportTests :: TestTree
Expand Down

0 comments on commit 0d806c3

Please sign in to comment.