From 752f7cfd77a4710fa7c57435a35d0653bb7bccaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Hr=C4=8Dek?= Date: Mon, 15 Apr 2024 07:35:24 +0200 Subject: [PATCH 1/2] Fix parsing of import suggestions extending multiple multiline imports --- .../src/Development/IDE/Plugin/CodeAction.hs | 9 ++++--- plugins/hls-refactor-plugin/test/Main.hs | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs index 23607bae8b..35c738e6f4 100644 --- a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs +++ b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs @@ -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 ([^)]*)\\)" + "\\(at ([^:]+:[^ ]+)\\)" #else - "\\(([^)]*)\\)" + "\\(([^:]+:[^ ]+)\\)" #endif , mods <- [mod | [_,mod] <- mods'] , srcspans <- [srcspan | [_,srcspan] <- srcspans'] diff --git a/plugins/hls-refactor-plugin/test/Main.hs b/plugins/hls-refactor-plugin/test/Main.hs index 09635e898a..092cd6ef0b 100644 --- a/plugins/hls-refactor-plugin/test/Main.hs +++ b/plugins/hls-refactor-plugin/test/Main.hs @@ -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 From d320e1cfd0e643027b5baf50d8a51dc3fdf9661e Mon Sep 17 00:00:00 2001 From: Jan Hrcek <2716069+jhrcek@users.noreply.github.com> Date: Mon, 15 Apr 2024 07:43:16 +0200 Subject: [PATCH 2/2] Remove extraneous space --- .../src/Development/IDE/Plugin/CodeAction.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs index 35c738e6f4..c3dbca86f8 100644 --- a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs +++ b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs @@ -1984,7 +1984,7 @@ regExImports msg #if MIN_VERSION_ghc(9,7,0) "\\(at ([^:]+:[^ ]+)\\)" #else - "\\(([^:]+:[^ ]+)\\)" + "\\(([^:]+:[^ ]+)\\)" #endif , mods <- [mod | [_,mod] <- mods'] , srcspans <- [srcspan | [_,srcspan] <- srcspans']