From 19d1bc5fdf618a0520b78249a7e94dfa581ce7c1 Mon Sep 17 00:00:00 2001 From: Junyoung/Clare Jang Date: Mon, 13 Dec 2021 15:48:33 -0500 Subject: [PATCH 1/2] Fix redundant import actions for names starting with _ --- ghcide/src/Development/IDE/Plugin/CodeAction.hs | 8 +++++++- ghcide/test/exe/Main.hs | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/CodeAction.hs b/ghcide/src/Development/IDE/Plugin/CodeAction.hs index 70d2d6da98..b6a85aefed 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction.hs @@ -24,6 +24,9 @@ import Control.Arrow (second, import Control.Concurrent.STM.Stats (atomically) import Control.Monad (guard, join) import Control.Monad.IO.Class +#if !MIN_VERSION_ghc(9,0,0) +import Ctype (is_ident) +#endif import Data.Char import qualified Data.DList as DL import Data.Function @@ -57,6 +60,9 @@ import Development.IDE.Types.Exports import Development.IDE.Types.Location import Development.IDE.Types.Options import qualified GHC.LanguageExtensions as Lang +#if MIN_VERSION_ghc(9,0,0) +import GHC.Parser.CharClass (is_ident) +#endif import Ide.PluginUtils (subRange) import Ide.Types import qualified Language.LSP.Server as LSP @@ -1525,7 +1531,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 From ebd0912c3fe902b1d99fdf8a956e5671906bc7f6 Mon Sep 17 00:00:00 2001 From: Junyoung/Clare Jang Date: Mon, 13 Dec 2021 16:05:10 -0500 Subject: [PATCH 2/2] Move CPP into compat layer --- ghcide/src/Development/IDE/GHC/Compat/Util.hs | 6 +++++- ghcide/src/Development/IDE/Plugin/CodeAction.hs | 6 ------ 2 files changed, 5 insertions(+), 7 deletions(-) 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 b6a85aefed..6a7a636e55 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction.hs @@ -24,9 +24,6 @@ import Control.Arrow (second, import Control.Concurrent.STM.Stats (atomically) import Control.Monad (guard, join) import Control.Monad.IO.Class -#if !MIN_VERSION_ghc(9,0,0) -import Ctype (is_ident) -#endif import Data.Char import qualified Data.DList as DL import Data.Function @@ -60,9 +57,6 @@ import Development.IDE.Types.Exports import Development.IDE.Types.Location import Development.IDE.Types.Options import qualified GHC.LanguageExtensions as Lang -#if MIN_VERSION_ghc(9,0,0) -import GHC.Parser.CharClass (is_ident) -#endif import Ide.PluginUtils (subRange) import Ide.Types import qualified Language.LSP.Server as LSP