Skip to content

Commit 3dd647a

Browse files
committed
Add autogen field completions and tests for cabal files
1 parent 32f7800 commit 3dd647a

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ import Ide.Plugin.Cabal.LicenseSuggest (licenseNames)
2323
-- Completion Data
2424
-- ----------------------------------------------------------------
2525

26-
supportedCabalVersions :: [CabalSpecVersion]
27-
supportedCabalVersions = [CabalSpecV2_2 .. maxBound]
28-
2926
-- | Keyword for cabal version; required to be the top line in a cabal file
3027
cabalVersionKeyword :: Map KeyWordName Completer
3128
cabalVersionKeyword =
3229
Map.singleton "cabal-version:" $
3330
constantCompleter $
3431
-- We only suggest cabal versions newer than 2.2
3532
-- since we don't recommend using older ones.
36-
map (T.pack . showCabalSpecVersion) supportedCabalVersions
33+
map (T.pack . showCabalSpecVersion) [CabalSpecV2_2 .. maxBound]
3734

3835
-- | Top level keywords of a cabal file.
3936
--
@@ -90,6 +87,7 @@ libraryFields =
9087
("visibility:", constantCompleter ["private", "public"]),
9188
("reexported-modules:", noopCompleter),
9289
("signatures:", noopCompleter),
90+
("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary),
9391
("other-modules:", modulesCompleter sourceDirsExtractionLibrary)
9492
]
9593

@@ -98,13 +96,15 @@ executableFields =
9896
Map.fromList
9997
[ ("main-is:", mainIsCompleter sourceDirsExtractionExecutable),
10098
("scope:", constantCompleter ["public", "private"]),
99+
("autogen-modules:", modulesCompleter sourceDirsExtractionExecutable),
101100
("other-modules:", modulesCompleter sourceDirsExtractionExecutable)
102101
]
103102

104103
testSuiteFields :: Map KeyWordName Completer
105104
testSuiteFields =
106105
Map.fromList
107106
[ ("type:", constantCompleter ["exitcode-stdio-1.0", "detailed-0.9"]),
107+
("autogen-modules:", modulesCompleter sourceDirsExtractionTestSuite),
108108
("main-is:", mainIsCompleter sourceDirsExtractionTestSuite),
109109
("other-modules:", modulesCompleter sourceDirsExtractionTestSuite)
110110
]
@@ -113,6 +113,7 @@ benchmarkFields :: Map KeyWordName Completer
113113
benchmarkFields =
114114
Map.fromList
115115
[ ("type:", noopCompleter),
116+
("autogen-modules:", modulesCompleter sourceDirsExtractionBenchmark),
116117
("main-is:", mainIsCompleter sourceDirsExtractionBenchmark),
117118
("other-modules:", modulesCompleter sourceDirsExtractionBenchmark)
118119
]
@@ -165,8 +166,7 @@ flagFields =
165166
libExecTestBenchCommons :: Map KeyWordName Completer
166167
libExecTestBenchCommons =
167168
Map.fromList
168-
[ ("import:", importCompleter),
169-
("build-depends:", noopCompleter),
169+
[ ("build-depends:", noopCompleter),
170170
("hs-source-dirs:", directoryCompleter),
171171
("default-extensions:", noopCompleter),
172172
("other-extensions:", noopCompleter),
@@ -181,6 +181,7 @@ libExecTestBenchCommons =
181181
("ghcjs-prof-options:", constantCompleter ghcOptions),
182182
("ghcjs-shared-options:", constantCompleter ghcOptions),
183183
("includes:", filePathCompleter),
184+
("autogen-includes:", filePathCompleter),
184185
("install-includes:", filePathCompleter),
185186
("include-dirs:", directoryCompleter),
186187
("c-sources:", filePathCompleter),
@@ -264,3 +265,6 @@ weightedLicenseNames =
264265

265266
ghcOptions :: [T.Text]
266267
ghcOptions = map T.pack $ flagsForCompletion False
268+
269+
supportedCabalVersions :: [CabalSpecVersion]
270+
supportedCabalVersions = [CabalSpecV2_2 .. maxBound]

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Completer where
77

88
import Control.Lens ((^.), (^?))
99
import Control.Lens.Prism
10+
import Control.Monad (forM_)
1011
import qualified Data.ByteString as ByteString
1112
import qualified Data.ByteString.Char8 as BS8
1213
import Data.Maybe (mapMaybe)
@@ -40,7 +41,8 @@ completerTests =
4041
completionHelperTests,
4142
filePathExposedModulesTests,
4243
exposedModuleCompleterTests,
43-
importCompleterTests
44+
importCompleterTests,
45+
autogenFieldCompletionTests
4446
]
4547

4648
basicCompleterTests :: TestTree
@@ -336,6 +338,27 @@ importCompleterTests =
336338
[Syntax.SecArgName (Syntax.Position row (col + 7)) (BS8.pack name)]
337339
[]
338340

341+
autogenFieldCompletionTests :: TestTree
342+
autogenFieldCompletionTests =
343+
testGroup "Autogen Field Completer Tests"
344+
[ testAutogenField "library" "autogen-completion/autogen-completion.cabal" (Position 6 9) ["autogen-modules:", "autogen-includes:"]
345+
, testAutogenField "executable" "autogen-completion/autogen-completion.cabal" (Position 11 9) ["autogen-modules:", "autogen-includes:"]
346+
, testAutogenField "test-suite" "autogen-completion/autogen-completion.cabal" (Position 16 9) ["autogen-modules:", "autogen-includes:"]
347+
, testAutogenField "benchmark" "autogen-completion/autogen-completion.cabal" (Position 21 9) ["autogen-modules:", "autogen-includes:"]
348+
, testAutogenField "foreign-library" "autogen-completion/autogen-completion.cabal" (Position 26 9) ["autogen-includes:"]
349+
, testAutogenField "common" "autogen-completion/autogen-completion.cabal" (Position 29 9) ["autogen-includes:"]
350+
]
351+
352+
where
353+
testAutogenField :: String -> FilePath -> Position -> [T.Text] -> TestTree
354+
testAutogenField section file pos expected = runCabalTestCaseSession ("autogen-modules completion in " <> section) "" $ do
355+
doc <- openDoc file "cabal"
356+
items <- getCompletions doc pos
357+
let labels = map (^. L.label) items
358+
liftIO $ forM_ expected $ \expect ->
359+
assertBool (T.unpack expect <> " not found in " <> section) $
360+
any (expect `T.isInfixOf`) labels
361+
339362
simpleCompleterData :: Maybe StanzaName -> FilePath -> T.Text -> CompleterData
340363
simpleCompleterData sName dir pref = do
341364
CompleterData
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cabal-version: 3.0
2+
name: autogen-completion
3+
version: 0.1.0.0
4+
5+
library
6+
hs-source-dirs: src
7+
autogen-
8+
9+
executable autoexe
10+
main-is: Main.hs
11+
hs-source-dirs: src
12+
autogen-
13+
14+
test-suite autotest
15+
type: exitcode-stdio-1.0
16+
hs-source-dirs: src
17+
autogen-
18+
19+
benchmark autobench
20+
type: exitcode-stdio-1.0
21+
hs-source-dirs: src
22+
autogen-
23+
24+
foreign-library autoforeign
25+
type: native-shared
26+
hs-source-dirs: src
27+
autogen-
28+
29+
common defaults
30+
autogen-

0 commit comments

Comments
 (0)