on master help sign in
Merge pull request #51 from ollef/master

Add support for removing redundant qualified imports

commit8a71bfaa81fee8fbad541ce1fbf16548bcf48b60 authorMoritz Kiefer <moritz.kiefer@purelyfunctional.org> dateFri 13 Sep 2019, 06:20 (7 years ago) parent14b0b168d0e4a2
2 files changed+54
src/Development/IDE/LSP/CodeAction.hs+1 test/exe/Main.hs+53
src/Development/IDE/LSP/CodeAction.hs+1
@@ -48,6 +48,7 @@ suggestAction contents Diagnostic{_range=_range@Range{..},..}
4848-- except perhaps to import instances from `Data.List'
4949-- To import instances alone, use: import Data.List()
5050 | "The import of " `T.isInfixOf` _message
51+ || "The qualified import of " `T.isInfixOf` _message
5152 , " is redundant" `T.isInfixOf` _message
5253 = [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])]
5354
test/exe/Main.hs+53
@@ -212,6 +212,7 @@ codeActionTests :: TestTree
212212codeActionTests = testGroup "code actions"
213213 [ renameActionTests
214214 , typeWildCardActionTests
215+ , removeImportTests
215216 ]
216217
217218renameActionTests :: TestTree
@@ -360,6 +361,58 @@ typeWildCardActionTests = testGroup "type wildcard actions"
360361 liftIO $ expectedContentAfterAction @=? contentAfterAction
361362 ]
362363
364+removeImportTests :: TestTree
365+removeImportTests = testGroup "remove import actions"
366+ [ testSession "redundant" $ do
367+ let contentA = T.unlines
368+ [ "module ModuleA where"
369+ ]
370+ docA <- openDoc' "ModuleA.hs" "haskell" contentA
371+ let contentB = T.unlines
372+ [ "{-# OPTIONS_GHC -Wunused-imports #-}"
373+ , "module ModuleB where"
374+ , "import ModuleA"
375+ , "stuffB = 123"
376+ ]
377+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
378+ _ <- waitForDiagnostics
379+ [CACodeAction action@CodeAction { _title = actionTitle }]
380+ <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
381+ liftIO $ "Remove import" @=? actionTitle
382+ executeCodeAction action
383+ contentAfterAction <- documentContents docB
384+ let expectedContentAfterAction = T.unlines
385+ [ "{-# OPTIONS_GHC -Wunused-imports #-}"
386+ , "module ModuleB where"
387+ , "stuffB = 123"
388+ ]
389+ liftIO $ expectedContentAfterAction @=? contentAfterAction
390+ , testSession "qualified redundant" $ do
391+ let contentA = T.unlines
392+ [ "module ModuleA where"
393+ ]
394+ docA <- openDoc' "ModuleA.hs" "haskell" contentA
395+ let contentB = T.unlines
396+ [ "{-# OPTIONS_GHC -Wunused-imports #-}"
397+ , "module ModuleB where"
398+ , "import qualified ModuleA"
399+ , "stuffB = 123"
400+ ]
401+ docB <- openDoc' "ModuleB.hs" "haskell" contentB
402+ _ <- waitForDiagnostics
403+ [CACodeAction action@CodeAction { _title = actionTitle }]
404+ <- getCodeActions docB (Range (Position 2 0) (Position 2 5))
405+ liftIO $ "Remove import" @=? actionTitle
406+ executeCodeAction action
407+ contentAfterAction <- documentContents docB
408+ let expectedContentAfterAction = T.unlines
409+ [ "{-# OPTIONS_GHC -Wunused-imports #-}"
410+ , "module ModuleB where"
411+ , "stuffB = 123"
412+ ]
413+ liftIO $ expectedContentAfterAction @=? contentAfterAction
414+ ]
415+
363416----------------------------------------------------------------------
364417-- Utils
365418