Skip to content

Commit 5d5f7e4

Browse files
ozkutukmichaelpj
andauthored
Make redundant import removal work on PatSyn imports (#3377)
* Make redundant import removal work on patsyn imports * Make stylish-haskell happy Co-authored-by: Michael Peyton Jones <me@michaelpj.com>
1 parent cc1b90d commit 5d5f7e4

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,19 @@ smallerRangesForBindingExport lies b =
18411841
ranges' _ = []
18421842

18431843
rangesForBinding' :: String -> LIE GhcPs -> [SrcSpan]
1844-
rangesForBinding' b (L (locA -> l) x@IEVar{}) | T.unpack (printOutputable x) == b = [l]
1844+
#if !MIN_VERSION_ghc(9,2,0)
1845+
rangesForBinding' b (L (locA -> l) (IEVar _ nm))
1846+
| L _ (IEPattern (L _ b')) <- nm
1847+
, T.unpack (printOutputable b') == b
1848+
= [l]
1849+
#else
1850+
rangesForBinding' b (L (locA -> l) (IEVar _ nm))
1851+
| L _ (IEPattern _ (L _ b')) <- nm
1852+
, T.unpack (printOutputable b') == b
1853+
= [l]
1854+
#endif
1855+
rangesForBinding' b (L (locA -> l) x@IEVar{})
1856+
| T.unpack (printOutputable x) == b = [l]
18451857
rangesForBinding' b (L (locA -> l) x@IEThingAbs{}) | T.unpack (printOutputable x) == b = [l]
18461858
rangesForBinding' b (L (locA -> l) (IEThingAll _ x)) | T.unpack (printOutputable x) == b = [l]
18471859
#if !MIN_VERSION_ghc(9,2,0)

test/functional/FunctionalCodeAction.hs

+19-4
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,21 @@ redundantImportTests = testGroup "redundant import code actions" [
176176
doc <- openDoc "src/CodeActionRedundant.hs" "haskell"
177177

178178
diags <- waitForDiagnosticsFromSource doc "typecheck"
179-
liftIO $ expectDiagnostic diags ["The import of", "Data.List", "is redundant"]
179+
liftIO $ expectDiagnostic diags [ "The import of", "Data.List", "is redundant" ]
180+
liftIO $ expectDiagnostic diags [ "Empty", "from module", "Data.Sequence" ]
180181

181182
mActions <- getAllCodeActions doc
182183

183184
let allActions = map fromAction mActions
184185
actionTitles = map (view L.title) allActions
185186

186-
liftIO $ actionTitles `shouldContain` ["Remove import", "Remove all redundant imports"]
187+
liftIO $ actionTitles `shouldContain`
188+
[ "Remove import"
189+
, "Remove Empty from import"
190+
, "Remove all redundant imports"
191+
]
187192

188-
let mbRemoveAction = find (\x -> x ^. L.title == "Remove import") allActions
193+
let mbRemoveAction = find (\x -> x ^. L.title == "Remove all redundant imports") allActions
189194

190195
case mbRemoveAction of
191196
Just removeAction -> do
@@ -202,7 +207,17 @@ redundantImportTests = testGroup "redundant import code actions" [
202207
-- provides workspace edit property which skips round trip to
203208
-- the server
204209
contents <- documentContents doc
205-
liftIO $ contents @?= "{-# OPTIONS_GHC -Wunused-imports #-}\nmodule CodeActionRedundant where\nmain :: IO ()\nmain = putStrLn \"hello\"\n"
210+
liftIO $ contents @?= T.unlines
211+
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
212+
, "{-# LANGUAGE PatternSynonyms #-}"
213+
, "module CodeActionRedundant where"
214+
, "-- We need a non-reduntant import in the import list"
215+
, "-- to properly test the removal of the singular redundant item"
216+
, "import Data.Sequence (singleton)"
217+
, "main :: IO ()"
218+
, "main = putStrLn \"hello\""
219+
, " where unused = Data.Sequence.singleton 42"
220+
]
206221

207222
, testCase "doesn't touch other imports" $ runSession hlsCommand noLiteralCaps "test/testdata/redundantImportTest/" $ do
208223
doc <- openDoc "src/MultipleImports.hs" "haskell"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{-# OPTIONS_GHC -Wunused-imports #-}
2+
{-# LANGUAGE PatternSynonyms #-}
23
module CodeActionRedundant where
34
import Data.List
5+
-- We need a non-reduntant import in the import list
6+
-- to properly test the removal of the singular redundant item
7+
import Data.Sequence (pattern Empty, singleton)
48
main :: IO ()
59
main = putStrLn "hello"
10+
where unused = Data.Sequence.singleton 42

0 commit comments

Comments
 (0)