From fdefb6bc6d6655f115a6ad2b4fc8adb4fd7ada16 Mon Sep 17 00:00:00 2001 From: Olle Fredriksson Date: Thu, 12 Sep 2019 22:47:50 +0200 Subject: [PATCH 1/2] Add a test for removing an unused module --- test/exe/Main.hs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/exe/Main.hs b/test/exe/Main.hs index 5a7a66eb..9e9c1a2a 100644 --- a/test/exe/Main.hs +++ b/test/exe/Main.hs @@ -212,6 +212,7 @@ codeActionTests :: TestTree codeActionTests = testGroup "code actions" [ renameActionTests , typeWildCardActionTests + , removeImportTests ] renameActionTests :: TestTree @@ -360,6 +361,34 @@ 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 + ] + ---------------------------------------------------------------------- -- Utils From 8d0e4a2ca0956af5b1b823f9ac344b83410074b7 Mon Sep 17 00:00:00 2001 From: Olle Fredriksson Date: Thu, 12 Sep 2019 22:51:46 +0200 Subject: [PATCH 2/2] Add support for removing redundant qualified imports --- src/Development/IDE/LSP/CodeAction.hs | 1 + test/exe/Main.hs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Development/IDE/LSP/CodeAction.hs b/src/Development/IDE/LSP/CodeAction.hs index 223cb8eb..3430deb9 100644 --- a/src/Development/IDE/LSP/CodeAction.hs +++ b/src/Development/IDE/LSP/CodeAction.hs @@ -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) ""])] diff --git a/test/exe/Main.hs b/test/exe/Main.hs index 9e9c1a2a..2376dad3 100644 --- a/test/exe/Main.hs +++ b/test/exe/Main.hs @@ -387,6 +387,30 @@ removeImportTests = testGroup "remove import actions" , "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 ] ----------------------------------------------------------------------