Skip to content

Code action to remove redundant record field import (fixes #4220) #4308

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
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 @@ -422,7 +422,7 @@ suggestRemoveRedundantImport ParsedModule{pm_parsed_source = L _ HsModule{hsmod
| Just [_, bindings] <- matchRegexUnifySpaces _message "The( qualified)? import of ‘([^’]*)’ from module [^ ]* is redundant"
, Just (L _ impDecl) <- find (\(L (locA -> l) _) -> _start _range `isInsideSrcSpan` l && _end _range `isInsideSrcSpan` l ) hsmodImports
, Just c <- contents
, ranges <- map (rangesForBindingImport impDecl . T.unpack) (T.splitOn ", " bindings)
, ranges <- map (rangesForBindingImport impDecl . T.unpack) (T.splitOn ", " bindings >>= trySplitIntoOriginalAndRecordField)
, ranges' <- extendAllToIncludeCommaIfPossible False (indexedByPosition $ T.unpack c) (concat ranges)
, not (null ranges')
= [( "Remove " <> bindings <> " from import" , [ TextEdit r "" | r <- ranges' ] )]
Expand All @@ -434,6 +434,15 @@ suggestRemoveRedundantImport ParsedModule{pm_parsed_source = L _ HsModule{hsmod
| _message =~ ("The( qualified)? import of [^ ]* is redundant" :: String)
= [("Remove import", [TextEdit (extendToWholeLineIfPossible contents _range) ""])]
| otherwise = []
where
-- In case of an unused record field import, the binding from the message will not match any import directly
-- In this case, we try if we can additionally extract a record field name
-- Example: The import of ‘B(b2)’ from module ‘ModuleB’ is redundant
trySplitIntoOriginalAndRecordField :: T.Text -> [T.Text]
trySplitIntoOriginalAndRecordField binding =
case matchRegexUnifySpaces binding "([^ ]+)\\(([^)]+)\\)" of
Just [_, fields] -> [binding, fields]
_ -> [binding]

diagInRange :: Diagnostic -> Range -> Bool
diagInRange Diagnostic {_range = dr} r = dr `subRange` extendedRange
Expand Down
70 changes: 70 additions & 0 deletions plugins/hls-refactor-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,76 @@ removeImportTests = testGroup "remove import actions"
, "x = a -- Must use something from module A, but not (@.)"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "remove redundant record field import" $ do
let contentA = T.unlines
[ "module ModuleA where"
, "data A = A {"
, " a1 :: String,"
, " a2 :: Int"
, "}"
, "newA = A \"foo\" 42"
]
_docA <- createDoc "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA"
, " ( A (a1, a2),"
, " newA"
, " )"
, "x = a1 newA"
]
docB <- createDoc "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
action <- pickActionWithTitle "Remove A(a2) from import" =<< getCodeActions docB (R 2 0 5 3)
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA"
, " ( A (a1),"
, " newA"
, " )"
, "x = a1 newA"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
, testSession "remove multiple redundant record field imports" $ do
let contentA = T.unlines
[ "module ModuleA where"
, "data A = A {"
, " a1 :: String,"
, " a2 :: Int,"
, " a3 :: Int,"
, " a4 :: Int"
, "}"
, "newA = A \"foo\" 2 3 4"
]
_docA <- createDoc "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA"
, " ( A (a1, a2, a3, a4),"
, " newA"
, " )"
, "x = a2 newA"
]
docB <- createDoc "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
action <- pickActionWithTitle "Remove A(a1), A(a3), A(a4) from import" =<< getCodeActions docB (R 2 0 5 3)
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA"
, " ( A (a2),"
, " newA"
, " )"
, "x = a2 newA"
]
liftIO $ expectedContentAfterAction @=? contentAfterAction
]

extendImportTests :: TestTree
Expand Down
Loading