Skip to content

Commit e6aa0aa

Browse files
drsoochpepeiborramergify[bot]
authored
Fix hanging redundant import on Unicode function (#2870)
* Revert partial changes from #2483 Instead of using a low-level `is_ident` that only works on ascii characters. Instead match directly on the '_' character. The original functionality has been restored (using `isAlpha` instead). This commit removes the now unneccessary `is_ident` compat layer * Add regression test for unicode functions Co-authored-by: Pepe Iborra <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 1473e95 commit e6aa0aa

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ module Development.IDE.GHC.Compat.Util (
6969
stringToStringBuffer,
7070
nextChar,
7171
atEnd,
72-
-- * Char
73-
is_ident
7472
) where
7573

7674
#if MIN_VERSION_ghc(9,0,0)
@@ -83,7 +81,6 @@ import GHC.Data.FastString
8381
import GHC.Data.Maybe
8482
import GHC.Data.Pair
8583
import GHC.Data.StringBuffer
86-
import GHC.Parser.CharClass (is_ident)
8784
import GHC.Types.Unique
8885
import GHC.Types.Unique.DFM
8986
import GHC.Utils.Fingerprint
@@ -93,7 +90,6 @@ import GHC.Utils.Panic hiding (try)
9390
#else
9491
import Bag
9592
import BooleanFormula
96-
import Ctype (is_ident)
9793
import EnumSet
9894
import qualified Exception
9995
import FastString

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import qualified Data.Rope.UTF16 as Rope
4141
import qualified Data.Set as S
4242
import qualified Data.Text as T
4343
import Data.Tuple.Extra (fst3)
44-
import Development.IDE.Core.RuleTypes
4544
import Development.IDE.Core.Rules
45+
import Development.IDE.Core.RuleTypes
4646
import Development.IDE.Core.Service
4747
import Development.IDE.GHC.Compat
4848
import Development.IDE.GHC.Compat.Util
@@ -1631,8 +1631,11 @@ rangesForBindingImport _ _ = []
16311631
wrapOperatorInParens :: String -> String
16321632
wrapOperatorInParens x =
16331633
case uncons x of
1634-
Just (h, _t) -> if is_ident h then x else "(" <> x <> ")"
1635-
Nothing -> mempty
1634+
-- see #2483 and #2859
1635+
-- common lens functions use the _ prefix, and should not be wrapped in parens
1636+
Just ('_', _t) -> x
1637+
Just (h, _t) -> if isAlpha h then x else "(" <> x <> ")"
1638+
Nothing -> mempty
16361639

16371640
smallerRangesForBindingExport :: [LIE GhcPs] -> String -> [Range]
16381641
smallerRangesForBindingExport lies b =
@@ -1788,8 +1791,8 @@ renderImportStyle (ImportAllConstructors p) = p <> "(..)"
17881791

17891792
-- | Used for extending import lists
17901793
unImportStyle :: ImportStyle -> (Maybe String, String)
1791-
unImportStyle (ImportTopLevel x) = (Nothing, T.unpack x)
1792-
unImportStyle (ImportViaParent x y) = (Just $ T.unpack y, T.unpack x)
1794+
unImportStyle (ImportTopLevel x) = (Nothing, T.unpack x)
1795+
unImportStyle (ImportViaParent x y) = (Just $ T.unpack y, T.unpack x)
17931796
unImportStyle (ImportAllConstructors x) = (Just $ T.unpack x, wildCardSymbol)
17941797

17951798

ghcide/test/exe/Main.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,34 @@ removeImportTests = testGroup "remove import actions"
13431343
, "main = print stuffB"
13441344
]
13451345
liftIO $ expectedContentAfterAction @=? contentAfterAction
1346+
, testSession "redundant binding - unicode regression " $ do
1347+
let contentA = T.unlines
1348+
[ "module ModuleA where"
1349+
, "data A = A"
1350+
, "ε :: Double"
1351+
, "ε = 0.5"
1352+
]
1353+
_docA <- createDoc "ModuleA.hs" "haskell" contentA
1354+
let contentB = T.unlines
1355+
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
1356+
, "module ModuleB where"
1357+
, "import ModuleA (A(..), ε)"
1358+
, "a = A"
1359+
]
1360+
docB <- createDoc "ModuleB.hs" "haskell" contentB
1361+
_ <- waitForDiagnostics
1362+
[InR action@CodeAction { _title = actionTitle }, _]
1363+
<- getCodeActions docB (Range (Position 2 0) (Position 2 5))
1364+
liftIO $ "Remove ε from import" @=? actionTitle
1365+
executeCodeAction action
1366+
contentAfterAction <- documentContents docB
1367+
let expectedContentAfterAction = T.unlines
1368+
[ "{-# OPTIONS_GHC -Wunused-imports #-}"
1369+
, "module ModuleB where"
1370+
, "import ModuleA (A(..))"
1371+
, "a = A"
1372+
]
1373+
liftIO $ expectedContentAfterAction @=? contentAfterAction
13461374
, testSession "redundant operator" $ do
13471375
let contentA = T.unlines
13481376
[ "module ModuleA where"

0 commit comments

Comments
 (0)