Skip to content

Commit 64e0acf

Browse files
authored
Improve parsing of import suggestions extending multiple multiline imports (fixes #4175) (#4177)
1 parent 97aac54 commit 64e0acf

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1973,15 +1973,18 @@ regexSingleMatch msg regex = case matchRegexUnifySpaces msg regex of
19731973
_ -> Nothing
19741974

19751975
-- | Process a list of (module_name, filename:src_span) values
1976-
-- | Eg. [(Data.Map, app/ModuleB.hs:2:1-18), (Data.HashMap.Strict, app/ModuleB.hs:3:1-29)]
1976+
--
1977+
-- Eg. [(Data.Map, app/ModuleB.hs:2:1-18), (Data.HashMap.Strict, app/ModuleB.hs:3:1-29)]
19771978
regExImports :: T.Text -> Maybe [(T.Text, T.Text)]
19781979
regExImports msg
19791980
| Just mods' <- allMatchRegex msg "‘([^’]*)’"
19801981
, Just srcspans' <- allMatchRegex msg
1982+
-- This regex has to be able to deal both with single-line srcpans like "(/path/to/File.hs:2:1-18)"
1983+
-- as well as multi-line srcspans like "(/path/to/File.hs:(3,1)-(5,2))"
19811984
#if MIN_VERSION_ghc(9,7,0)
1982-
"\\(at ([^)]*)\\)"
1985+
"\\(at ([^:]+:[^ ]+)\\)"
19831986
#else
1984-
"\\(([^)]*)\\)"
1987+
"\\(([^:]+:[^ ]+)\\)"
19851988
#endif
19861989
, mods <- [mod | [_,mod] <- mods']
19871990
, srcspans <- [srcspan | [_,srcspan] <- srcspans']

plugins/hls-refactor-plugin/test/Main.hs

+24
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,30 @@ extendImportTests = testGroup "extend import actions"
15011501
, "f :: Foo"
15021502
, "f = undefined"
15031503
])
1504+
, testSession "data constructor with two multiline import lists that can be extended with it" $ template
1505+
[]
1506+
("A.hs", T.unlines
1507+
[ "module A where"
1508+
, "import Prelude ("
1509+
, " )"
1510+
, "import Data.Maybe ("
1511+
, " )"
1512+
, "f = Nothing"
1513+
])
1514+
(Range (Position 5 5) (Position 5 6))
1515+
[ "Add Maybe(..) to the import list of Data.Maybe"
1516+
, "Add Maybe(..) to the import list of Prelude"
1517+
, "Add Maybe(Nothing) to the import list of Data.Maybe"
1518+
, "Add Maybe(Nothing) to the import list of Prelude"
1519+
]
1520+
(T.unlines
1521+
["module A where"
1522+
, "import Prelude ("
1523+
, " )"
1524+
, "import Data.Maybe (Maybe (..)"
1525+
, " )"
1526+
, "f = Nothing"
1527+
])
15041528
]
15051529
where
15061530
codeActionTitle CodeAction{_title=x} = x

0 commit comments

Comments
 (0)