Skip to content

Commit bb73e32

Browse files
authored
Fix redundant import actions for names starting with _ (#2483)
* Fix redundant import actions for names starting with _ * Move CPP into compat layer
1 parent d95d0ee commit bb73e32

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

ghcide/src/Development/IDE/GHC/Compat/Util.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ module Development.IDE.GHC.Compat.Util (
6868
hGetStringBuffer,
6969
stringToStringBuffer,
7070
nextChar,
71-
atEnd
71+
atEnd,
72+
-- * Char
73+
is_ident
7274
) where
7375

7476
#if MIN_VERSION_ghc(9,0,0)
@@ -81,6 +83,7 @@ import GHC.Data.FastString
8183
import GHC.Data.Maybe
8284
import GHC.Data.Pair
8385
import GHC.Data.StringBuffer
86+
import GHC.Parser.CharClass (is_ident)
8487
import GHC.Types.Unique
8588
import GHC.Types.Unique.DFM
8689
import GHC.Utils.Fingerprint
@@ -90,6 +93,7 @@ import GHC.Utils.Panic hiding (try)
9093
#else
9194
import Bag
9295
import BooleanFormula
96+
import Ctype (is_ident)
9397
import EnumSet
9498
import qualified Exception
9599
import FastString

ghcide/src/Development/IDE/Plugin/CodeAction.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ rangesForBindingImport _ _ = []
15251525
wrapOperatorInParens :: String -> String
15261526
wrapOperatorInParens x =
15271527
case uncons x of
1528-
Just (h, _t) -> if isAlpha h then x else "(" <> x <> ")"
1528+
Just (h, _t) -> if is_ident h then x else "(" <> x <> ")"
15291529
Nothing -> mempty
15301530

15311531
smallerRangesForBindingExport :: [LIE GhcPs] -> String -> [Range]

ghcide/test/exe/Main.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1276,19 +1276,20 @@ removeImportTests = testGroup "remove import actions"
12761276
, "stuffB :: Integer"
12771277
, "stuffB = 123"
12781278
, "stuffC = ()"
1279+
, "_stuffD = '_'"
12791280
]
12801281
_docA <- createDoc "ModuleA.hs" "haskell" contentA
12811282
let contentB = T.unlines
12821283
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
12831284
, "module ModuleB where"
1284-
, "import ModuleA (stuffA, stuffB, stuffC, stuffA)"
1285+
, "import ModuleA (stuffA, stuffB, _stuffD, stuffC, stuffA)"
12851286
, "main = print stuffB"
12861287
]
12871288
docB <- createDoc "ModuleB.hs" "haskell" contentB
12881289
_ <- waitForDiagnostics
12891290
[InR action@CodeAction { _title = actionTitle }, _]
12901291
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
1291-
liftIO $ "Remove stuffA, stuffC from import" @=? actionTitle
1292+
liftIO $ "Remove _stuffD, stuffA, stuffC from import" @=? actionTitle
12921293
executeCodeAction action
12931294
contentAfterAction <- documentContents docB
12941295
let expectedContentAfterAction = T.unlines

0 commit comments

Comments
 (0)