diff --git a/ghcide/src/Development/IDE/GHC/Compat/Util.hs b/ghcide/src/Development/IDE/GHC/Compat/Util.hs index 0bc37618c5..d12be88560 100644 --- a/ghcide/src/Development/IDE/GHC/Compat/Util.hs +++ b/ghcide/src/Development/IDE/GHC/Compat/Util.hs @@ -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) @@ -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 @@ -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 diff --git a/ghcide/src/Development/IDE/Plugin/CodeAction.hs b/ghcide/src/Development/IDE/Plugin/CodeAction.hs index 70d2d6da98..6a7a636e55 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction.hs @@ -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] diff --git a/ghcide/test/exe/Main.hs b/ghcide/test/exe/Main.hs index 1eb0e9dd00..dfb6e8e026 100644 --- a/ghcide/test/exe/Main.hs +++ b/ghcide/test/exe/Main.hs @@ -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