Skip to content

Commit 42a5261

Browse files
authored
Support PackageImports in hiddenPackageSuggestion (#4537)
* Support PackageImports in hiddenPackageSuggestion Fix: #4479 * Stop using error in hiddenPackageSuggestion
1 parent 51b6475 commit 42a5261

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath caba
190190
-- > It is a member of the hidden package ‘split-0.2.5’.
191191
-- > Perhaps you need to add ‘split’ to the build-depends in your .cabal file."
192192
--
193+
-- or this if PackageImports extension is used:
194+
--
195+
-- > "Could not find module ‘Data.List.Split’
196+
-- > Perhaps you meant
197+
-- > Data.List.Split (needs flag -package-id split-0.2.5)"
198+
--
193199
-- It extracts mentioned package names and version numbers.
194200
-- In this example, it will be @[("split", "0.2.5")]@
195201
--
@@ -204,13 +210,18 @@ hiddenPackageSuggestion diag = getMatch (msg =~ regex)
204210
msg :: T.Text
205211
msg = _message diag
206212
regex :: T.Text -- TODO: Support multiple packages suggestion
207-
regex = "It is a member of the hidden package [\8216']([a-zA-Z0-9-]*[a-zA-Z0-9])(-([0-9\\.]*))?[\8217']"
213+
regex =
214+
let regex' = "([a-zA-Z0-9-]*[a-zA-Z0-9])(-([0-9\\.]*))?"
215+
in "It is a member of the hidden package [\8216']" <> regex' <> "[\8217']"
216+
<> "|"
217+
<> "needs flag -package-id " <> regex'
208218
-- Have to do this matching because `Regex.TDFA` doesn't(?) support
209219
-- not-capturing groups like (?:message)
210220
getMatch :: (T.Text, T.Text, T.Text, [T.Text]) -> [(T.Text, T.Text)]
211221
getMatch (_, _, _, []) = []
212-
getMatch (_, _, _, [dependency, _, cleanVersion]) = [(dependency, cleanVersion)]
213-
getMatch (_, _, _, _) = error "Impossible pattern matching case"
222+
getMatch (_, _, _, [dependency, _, cleanVersion, "", "", ""]) = [(dependency, cleanVersion)]
223+
getMatch (_, _, _, ["", "", "", dependency, _, cleanVersion]) = [(dependency, cleanVersion)]
224+
getMatch (_, _, _, _) = []
214225

215226
command :: Recorder (WithPriority Log) -> CommandFunction IdeState CabalAddCommandParams
216227
command recorder state _ params@(CabalAddCommandParams {cabalPath = path, verTxtDocId = verTxtDocId, buildTarget = target, dependency = dep, version = mbVer}) = do

plugins/hls-cabal-plugin/test/CabalAdd.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ cabalAddTests =
3333
(generateAddDependencyTestSession "cabal-add-lib.cabal" ("src" </> "MyLib.hs") "split" [348])
3434
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test" ("cabal-add-testdata" </> "cabal-add-tests")
3535
(generateAddDependencyTestSession "cabal-add-tests.cabal" ("test" </> "Main.hs") "split" [478])
36+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test with PackageImports" ("cabal-add-testdata" </> "cabal-add-tests")
37+
(generateAddDependencyTestSession "cabal-add-tests.cabal" ("test" </> "MainPackageImports.hs") "split" [731])
3638
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark" ("cabal-add-testdata" </> "cabal-add-bench")
3739
(generateAddDependencyTestSession "cabal-add-bench.cabal" ("bench" </> "Main.hs") "split" [403])
3840

@@ -122,6 +124,23 @@ cabalAddTests =
122124
[ ("3d-graphics-examples", T.empty)
123125
, ("3d-graphics-examples", "1.1.6")
124126
]
127+
, testHiddenPackageSuggestions "Check CabalAdd's parser, with version, with PackageImports"
128+
[ "(needs flag -package-id base-0.1.0.0)"
129+
, "(needs flag -package-id Blammo-wai-0.11.0)"
130+
, "(needs flag -package-id BlastHTTP-2.6.4.3)"
131+
, "(needs flag -package-id CC-delcont-ref-tf-0.0.0.2)"
132+
, "(needs flag -package-id 3d-graphics-examples-1.1.6)"
133+
, "(needs flag -package-id AAI-0.1)"
134+
, "(needs flag -package-id AWin32Console-1.19.1)"
135+
]
136+
[ ("base","0.1.0.0")
137+
, ("Blammo-wai", "0.11.0")
138+
, ("BlastHTTP", "2.6.4.3")
139+
, ("CC-delcont-ref-tf", "0.0.0.2")
140+
, ("3d-graphics-examples", "1.1.6")
141+
, ("AAI", "0.1")
142+
, ("AWin32Console", "1.19.1")
143+
]
125144
]
126145
where
127146
generateAddDependencyTestSession :: FilePath -> FilePath -> T.Text -> [Int] -> Session ()

plugins/hls-cabal-plugin/test/testdata/cabal-add-testdata/cabal-add-tests/cabal-add-tests.cabal

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ test-suite cabal-add-tests-test
1616
hs-source-dirs: test
1717
main-is: Main.hs
1818
build-depends: base
19+
20+
test-suite cabal-add-tests-test-package-imports
21+
import: warnings
22+
default-language: Haskell2010
23+
type: exitcode-stdio-1.0
24+
hs-source-dirs: test
25+
main-is: MainPackageImports.hs
26+
build-depends: base
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE PackageImports #-}
2+
3+
module Main (main) where
4+
5+
import "split" Data.List.Split
6+
7+
main :: IO ()
8+
main = putStrLn "Test suite not yet implemented."

0 commit comments

Comments
 (0)