Skip to content

Fix redundant import actions for names starting with _ #2483

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 4 commits into from
Dec 14, 2021
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
6 changes: 5 additions & 1 deletion ghcide/src/Development/IDE/GHC/Compat/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ module Development.IDE.GHC.Compat.Util (
hGetStringBuffer,
stringToStringBuffer,
nextChar,
atEnd
atEnd,
-- * Char
is_ident
) where

#if MIN_VERSION_ghc(9,0,0)
Expand All @@ -81,6 +83,7 @@ import GHC.Data.FastString
import GHC.Data.Maybe
import GHC.Data.Pair
import GHC.Data.StringBuffer
import GHC.Parser.CharClass (is_ident)
import GHC.Types.Unique
import GHC.Types.Unique.DFM
import GHC.Utils.Fingerprint
Expand All @@ -90,6 +93,7 @@ import GHC.Utils.Panic hiding (try)
#else
import Bag
import BooleanFormula
import Ctype (is_ident)
import EnumSet
import qualified Exception
import FastString
Expand Down
2 changes: 1 addition & 1 deletion ghcide/src/Development/IDE/Plugin/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ rangesForBindingImport _ _ = []
wrapOperatorInParens :: String -> String
wrapOperatorInParens x =
case uncons x of
Just (h, _t) -> if isAlpha h then x else "(" <> x <> ")"
Just (h, _t) -> if is_ident h then x else "(" <> x <> ")"
Nothing -> mempty

smallerRangesForBindingExport :: [LIE GhcPs] -> String -> [Range]
Expand Down
5 changes: 3 additions & 2 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,19 +1276,20 @@ removeImportTests = testGroup "remove import actions"
, "stuffB :: Integer"
, "stuffB = 123"
, "stuffC = ()"
, "_stuffD = '_'"
]
_docA <- createDoc "ModuleA.hs" "haskell" contentA
let contentB = T.unlines
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
, "module ModuleB where"
, "import ModuleA (stuffA, stuffB, stuffC, stuffA)"
, "import ModuleA (stuffA, stuffB, _stuffD, stuffC, stuffA)"
, "main = print stuffB"
]
docB <- createDoc "ModuleB.hs" "haskell" contentB
_ <- waitForDiagnostics
[InR action@CodeAction { _title = actionTitle }, _]
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
liftIO $ "Remove stuffA, stuffC from import" @=? actionTitle
liftIO $ "Remove _stuffD, stuffA, stuffC from import" @=? actionTitle
executeCodeAction action
contentAfterAction <- documentContents docB
let expectedContentAfterAction = T.unlines
Expand Down