Merge pull request #51 from ollef/master

Add support for removing redundant qualified imports
This commit is contained in:
Moritz Kiefer 2019-09-13 06:20:11 +02:00 committed by GitHub
commit 8a71bfaa81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -48,6 +48,7 @@ suggestAction contents Diagnostic{_range=_range@Range{..},..}
-- except perhaps to import instances from `Data.List'
-- To import instances alone, use: import Data.List()
| "The import of " `T.isInfixOf` _message
|| "The qualified import of " `T.isInfixOf` _message
, " is redundant" `T.isInfixOf` _message
= [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])]

View File

@ -212,6 +212,7 @@ codeActionTests :: TestTree
codeActionTests = testGroup "code actions"
[ renameActionTests
, typeWildCardActionTests
, removeImportTests
]
renameActionTests :: TestTree
@ -360,6 +361,58 @@ typeWildCardActionTests = testGroup "type wildcard actions"
liftIO $ expectedContentAfterAction @=? contentAfterAction
]
removeImportTests :: TestTree
removeImportTests = testGroup "remove import actions"
[ testSession "redundant" $ do
let contentA = T.unlines
[ "module ModuleA where"
]
docA <- openDoc' "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA"
, "stuffB = 123"
]
docB <- openDoc' "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"
, "stuffB = 123"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "qualified redundant" $ do
let contentA = T.unlines
[ "module ModuleA where"
]
docA <- openDoc' "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import qualified ModuleA"
, "stuffB = 123"
]
docB <- openDoc' "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"
, "stuffB = 123"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
]
----------------------------------------------------------------------
-- Utils