Skip to content

Improve parsing of import suggestions extending multiple multiline imports (fixes #4175) #4177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1973,15 +1973,18 @@ regexSingleMatch msg regex = case matchRegexUnifySpaces msg regex of
_ -> Nothing

-- | Process a list of (module_name, filename:src_span) values
-- | Eg. [(Data.Map, app/ModuleB.hs:2:1-18), (Data.HashMap.Strict, app/ModuleB.hs:3:1-29)]
--
-- Eg. [(Data.Map, app/ModuleB.hs:2:1-18), (Data.HashMap.Strict, app/ModuleB.hs:3:1-29)]
regExImports :: T.Text -> Maybe [(T.Text, T.Text)]
regExImports msg
| Just mods' <- allMatchRegex msg "‘([^’]*)’"
, Just srcspans' <- allMatchRegex msg
-- This regex has to be able to deal both with single-line srcpans like "(/path/to/File.hs:2:1-18)"
-- as well as multi-line srcspans like "(/path/to/File.hs:(3,1)-(5,2))"
#if MIN_VERSION_ghc(9,7,0)
"\\(at ([^)]*)\\)"
Copy link
Collaborator Author

@jhrcek jhrcek Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original regex didn't correctly deal with import lists spanning multiple lines.
Instead of extracting the whole of

"(/path/to/File.hs:(3,1)-(5,2))"

it would only extract

"(/path/to/File.hs:(3,1)"
                       ^ -only up to first `)`

which led to errors in src span parser described in #4175

"\\(at ([^:]+:[^ ]+)\\)"
#else
"\\(([^)]*)\\)"
"\\(([^:]+:[^ ]+)\\)"
#endif
, mods <- [mod | [_,mod] <- mods']
, srcspans <- [srcspan | [_,srcspan] <- srcspans']
Expand Down
24 changes: 24 additions & 0 deletions plugins/hls-refactor-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,30 @@ extendImportTests = testGroup "extend import actions"
, "f :: Foo"
, "f = undefined"
])
, testSession "data constructor with two multiline import lists that can be extended with it" $ template
[]
("A.hs", T.unlines
[ "module A where"
, "import Prelude ("
, " )"
, "import Data.Maybe ("
, " )"
, "f = Nothing"
])
(Range (Position 5 5) (Position 5 6))
[ "Add Maybe(..) to the import list of Data.Maybe"
, "Add Maybe(..) to the import list of Prelude"
, "Add Maybe(Nothing) to the import list of Data.Maybe"
, "Add Maybe(Nothing) to the import list of Prelude"
]
(T.unlines
["module A where"
, "import Prelude ("
, " )"
, "import Data.Maybe (Maybe (..)"
, " )"
, "f = Nothing"
])
]
where
codeActionTitle CodeAction{_title=x} = x
Expand Down
Loading