Skip to content

Make redundant import removal work on PatSyn imports #3377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,19 @@ smallerRangesForBindingExport lies b =
ranges' _ = []

rangesForBinding' :: String -> LIE GhcPs -> [SrcSpan]
rangesForBinding' b (L (locA -> l) x@IEVar{}) | T.unpack (printOutputable x) == b = [l]
#if !MIN_VERSION_ghc(9,2,0)
rangesForBinding' b (L (locA -> l) (IEVar _ nm))
| L _ (IEPattern (L _ b')) <- nm
, T.unpack (printOutputable b') == b
= [l]
#else
rangesForBinding' b (L (locA -> l) (IEVar _ nm))
| L _ (IEPattern _ (L _ b')) <- nm
, T.unpack (printOutputable b') == b
= [l]
#endif
rangesForBinding' b (L (locA -> l) x@IEVar{})
| T.unpack (printOutputable x) == b = [l]
rangesForBinding' b (L (locA -> l) x@IEThingAbs{}) | T.unpack (printOutputable x) == b = [l]
rangesForBinding' b (L (locA -> l) (IEThingAll _ x)) | T.unpack (printOutputable x) == b = [l]
#if !MIN_VERSION_ghc(9,2,0)
Expand Down
23 changes: 19 additions & 4 deletions test/functional/FunctionalCodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,21 @@ redundantImportTests = testGroup "redundant import code actions" [
doc <- openDoc "src/CodeActionRedundant.hs" "haskell"

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

mActions <- getAllCodeActions doc

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

liftIO $ actionTitles `shouldContain` ["Remove import", "Remove all redundant imports"]
liftIO $ actionTitles `shouldContain`
[ "Remove import"
, "Remove Empty from import"
, "Remove all redundant imports"
]

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

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

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