Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Fix for #45 - remove redundant symbols from imports #290

Merged
merged 7 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ library
prettyprinter-ansi-terminal,
prettyprinter-ansi-terminal,
prettyprinter,
regex-tdfa >= 1.3.1.0,
rope-utf16-splay,
safe-exceptions,
shake >= 0.17.5,
Expand Down
53 changes: 50 additions & 3 deletions src/Development/IDE/LSP/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import Data.Char
import Data.Maybe
import Data.List.Extra
import qualified Data.Text as T
import Text.Regex.TDFA ((=~), (=~~))
import Text.Regex.TDFA.Text()

-- | Generate code actions.
codeAction
Expand Down Expand Up @@ -85,14 +87,18 @@ executeAddSignatureCommand _lsp _ideState ExecuteCommandParams{..}

suggestAction :: Maybe T.Text -> Diagnostic -> [(T.Text, [TextEdit])]
suggestAction contents diag@Diagnostic{_range=_range@Range{..},..}
-- The qualified import of ‘many’ from module ‘Control.Applicative’ is redundant
| Just [_, bindings] <- matchRegex _message ("The( qualified)? import of ‘([^’]*)’ from module [^ ]* is redundant")
, Just c <- contents
, importLine <- textInRange _range c
= [( "Remove " <> bindings <> " from import"
, [TextEdit _range (dropBindingsFromImportLine (T.splitOn "," bindings) importLine)])]

-- File.hs:16:1: warning:
-- The import of `Data.List' is redundant
-- 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
| _message =~ ("The( qualified)? import of [^ ]* is redundant" :: String)
= [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])]

-- File.hs:52:41: error:
Expand Down Expand Up @@ -293,6 +299,47 @@ textInRange (Range (Position startRow startCol) (Position endRow endCol)) text =
where
linesBeginningWithStartLine = drop startRow (T.splitOn "\n" text)

-- | Drop all occurrences of a binding in an import line.
-- Preserves well-formedness but not whitespace between bindings.
--
-- >>> dropBindingsFromImportLine ["bA", "bC"] "import A(bA, bB,bC ,bA)"
-- "import A(bB)"
--
-- >>> dropBindingsFromImportLine ["+"] "import "P" qualified A as B ((+))"
-- "import "P" qualified A() as B hiding (bB)"
dropBindingsFromImportLine :: [T.Text] -> T.Text -> T.Text
dropBindingsFromImportLine bindings_ importLine =
importPre <> "(" <> importRest'
where
bindings = map
(\binding ->
if isAlpha (T.head binding) then binding else "(" <> binding <> ")"
)
bindings_
(importPre, importRest) = T.breakOn "(" importLine

importRest' =
T.intercalate ","
$ joinCloseParens
$ mapMaybe (filtering . T.strip)
$ T.splitOn ","
$ T.tail importRest

filtering x = case () of
() | x `elem` bindings -> Nothing
() | x `elem` (map (<> ")") bindings) -> Just ")"
_ -> Just x

joinCloseParens (x : ")" : rest) = (x <> ")") : joinCloseParens rest
joinCloseParens (x : rest) = x : joinCloseParens rest
joinCloseParens [] = []

-- | Returns Just (the submatches) for the first capture, or Nothing.
matchRegex :: T.Text -> T.Text -> Maybe [T.Text]
matchRegex message regex = case message =~~ regex of
Just (_ :: T.Text, _ :: T.Text, _ :: T.Text, bindings) -> Just bindings
Nothing -> Nothing

setHandlersCodeAction :: PartialHandlers
setHandlersCodeAction = PartialHandlers $ \WithMessage{..} x -> return x{
LSP.codeActionHandler = withResponse RspCodeAction codeAction
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ extra-deps:
- lsp-test-0.9.0.0
- hie-bios-0.3.0
- fuzzy-0.1.0.0
- regex-tdfa-1.3.1.0
nix:
packages: [zlib]
1 change: 1 addition & 0 deletions stack84.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extra-deps:
- js-dgtable-0.5.2
- hie-bios-0.3.0
- fuzzy-0.1.0.0
- regex-tdfa-1.3.1.0
nix:
packages: [zlib]
allow-newer: true
1 change: 1 addition & 0 deletions stack88.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extra-deps:
- lsp-test-0.9.0.0
- hie-bios-0.3.0
- fuzzy-0.1.0.0
- regex-tdfa-1.3.1.0
allow-newer: true
nix:
packages: [zlib]
56 changes: 56 additions & 0 deletions test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,62 @@ removeImportTests = testGroup "remove import actions"
, "stuffB = 123"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "redundant binding" $ do
let contentA = T.unlines
[ "module ModuleA where"
, "stuffA = False"
, "stuffB :: Integer"
, "stuffB = 123"
]
_docA <- openDoc' "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA (stuffA, stuffB)"
, "main = print stuffB"
]
docB <- openDoc' "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
[CACodeAction action@CodeAction { _title = actionTitle }]
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
liftIO $ "Remove stuffA from import" @=? actionTitle
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA (stuffB)"
, "main = print stuffB"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "redundant symbol binding" $ do
let contentA = T.unlines
[ "module ModuleA where"
, "a !! b = a"
, "stuffB :: Integer"
, "stuffB = 123"
]
_docA <- openDoc' "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import qualified ModuleA as A ((!!), stuffB, (!!))"
, "main = print A.stuffB"
]
docB <- openDoc' "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
[CACodeAction action@CodeAction { _title = actionTitle }]
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
liftIO $ "Remove !! from import" @=? actionTitle
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import qualified ModuleA as A (stuffB)"
, "main = print A.stuffB"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
]

importRenameActionTests :: TestTree
Expand Down