Skip to content

Commit

Permalink
Fix redundant import actions for names starting with _ (#2483)
Browse files Browse the repository at this point in the history
* Fix redundant import actions for names starting with _

* Move CPP into compat layer
  • Loading branch information
Ailrun authored Dec 14, 2021
1 parent d95d0ee commit bb73e32
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
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

0 comments on commit bb73e32

Please sign in to comment.