Skip to content

refactor plugin: fix regex for extracting import suggestions #4080

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
Show file tree
Hide file tree
Changes from all commits
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 @@ -931,9 +931,9 @@ suggestExtendImport exportsMap (L _ HsModule {hsmodImports}) Diagnostic{_range=_
| Just [binding, mod, srcspan] <-
matchRegexUnifySpaces _message
#if MIN_VERSION_ghc(9,7,0)
"Add ‘([^’]*)’ to the import list in the import of ‘([^’]*)’ *\\(at (.*)\\)."
"Add ‘([^’]*)’ to the import list in the import of ‘([^’]*)’ *\\(at (.*)\\)\\."
#else
"Perhaps you want to add ‘([^’]*)’ to the import list in the import of ‘([^’]*)’ *\\((.*)\\)."
"Perhaps you want to add ‘([^’]*)’ to the import list in the import of ‘([^’]*)’ *\\((.*)\\)\\."
#endif
= suggestions hsmodImports binding mod srcspan
| Just (binding, mod_srcspan) <-
Expand Down
18 changes: 18 additions & 0 deletions plugins/hls-refactor-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,21 @@ extendImportTests = testGroup "extend import actions"
, "b :: A"
, "b = ConstructorFoo"
])
, brokenForGHC92 "On GHC 9.2, the error doesn't contain \"perhaps you want ...\" part from which import suggestion can be extracted." $
testSession "extend single line import in presence of extra parens" $ template
[]
("Main.hs", T.unlines
[ "import Data.Monoid (First)"
, "f = (First Nothing) <> mempty" -- parens tripped up the regex extracting import suggestions
])
(Range (Position 1 6) (Position 1 7))
[ "Add First(..) to the import list of Data.Monoid"
, "Add First(First) to the import list of Data.Monoid"
]
(T.unlines
[ "import Data.Monoid (First (..))"
, "f = (First Nothing) <> mempty"
])
, brokenForGHC94 "On GHC 9.4, the error messages with -fdefer-type-errors don't have necessary imported target srcspan info." $
testSession "extend single line qualified import with value" $ template
[("ModuleA.hs", T.unlines
Expand Down Expand Up @@ -3735,3 +3750,6 @@ withTempDir f = System.IO.Extra.withTempDir $ \dir ->

brokenForGHC94 :: String -> TestTree -> TestTree
brokenForGHC94 = knownBrokenForGhcVersions [GHC94]

brokenForGHC92 :: String -> TestTree -> TestTree
brokenForGHC92 = knownBrokenForGhcVersions [GHC92]