Skip to content

Commit 51b657b

Browse files
committed
Refactor autogen completions using libExecTestBenchCommons and consolidate test data
1 parent aa7244e commit 51b657b

File tree

8 files changed

+59
-83
lines changed

8 files changed

+59
-83
lines changed

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

+24-19
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ cabalKeywords =
7171
stanzaKeywordMap :: Map StanzaType (Map KeyWordName Completer)
7272
stanzaKeywordMap =
7373
Map.fromList
74-
[ ("library", libraryFields <> libExecTestBenchCommons),
75-
("executable", executableFields <> libExecTestBenchCommons),
76-
("test-suite", testSuiteFields <> libExecTestBenchCommons),
77-
("benchmark", benchmarkFields <> libExecTestBenchCommons),
78-
("foreign-library", foreignLibraryFields <> libExecTestBenchCommons),
79-
("common", libExecTestBenchCommons),
74+
[ ("library", libraryFields <> libExecTestBenchCommons "library"),
75+
("executable", executableFields <> libExecTestBenchCommons "executable"),
76+
("test-suite", testSuiteFields <> libExecTestBenchCommons "test-suite"),
77+
("benchmark", benchmarkFields <> libExecTestBenchCommons "benchmark"),
78+
("foreign-library", foreignLibraryFields <> libExecTestBenchCommons "benchmark"),
79+
("common", libExecTestBenchCommons "library"),
8080
("flag", flagFields),
8181
("source-repository", sourceRepositoryFields)
8282
]
@@ -90,9 +90,7 @@ libraryFields =
9090
("visibility:", constantCompleter ["private", "public"]),
9191
("reexported-modules:", noopCompleter),
9292
("signatures:", noopCompleter),
93-
("other-modules:", modulesCompleter sourceDirsExtractionLibrary),
94-
("autogen-includes:", filePathCompleter),
95-
("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary)
93+
("other-modules:", modulesCompleter sourceDirsExtractionLibrary)
9694

9795
]
9896

@@ -101,28 +99,23 @@ executableFields =
10199
Map.fromList
102100
[ ("main-is:", mainIsCompleter sourceDirsExtractionExecutable),
103101
("scope:", constantCompleter ["public", "private"]),
104-
("other-modules:", modulesCompleter sourceDirsExtractionExecutable),
105-
("autogen-modules:", modulesCompleter sourceDirsExtractionExecutable)
102+
("other-modules:", modulesCompleter sourceDirsExtractionExecutable)
106103
]
107104

108105
testSuiteFields :: Map KeyWordName Completer
109106
testSuiteFields =
110107
Map.fromList
111108
[ ("type:", constantCompleter ["exitcode-stdio-1.0", "detailed-0.9"]),
112109
("main-is:", mainIsCompleter sourceDirsExtractionTestSuite),
113-
("other-modules:", modulesCompleter sourceDirsExtractionTestSuite),
114-
("autogen-modules:", modulesCompleter sourceDirsExtractionTestSuite),
115-
("autogen-includes:", filePathCompleter)
110+
("other-modules:", modulesCompleter sourceDirsExtractionTestSuite)
116111
]
117112

118113
benchmarkFields :: Map KeyWordName Completer
119114
benchmarkFields =
120115
Map.fromList
121116
[ ("type:", noopCompleter),
122117
("main-is:", mainIsCompleter sourceDirsExtractionBenchmark),
123-
("other-modules:", modulesCompleter sourceDirsExtractionBenchmark),
124-
("autogen-modules:", modulesCompleter sourceDirsExtractionBenchmark),
125-
("autogen-includes:", filePathCompleter)
118+
("other-modules:", modulesCompleter sourceDirsExtractionBenchmark)
126119
]
127120

128121
foreignLibraryFields :: Map KeyWordName Completer
@@ -170,8 +163,20 @@ flagFields =
170163
("lib-version-linux:", noopCompleter)
171164
]
172165

173-
libExecTestBenchCommons :: Map KeyWordName Completer
174-
libExecTestBenchCommons =
166+
libExecTestBenchCommons :: StanzaType -> Map KeyWordName Completer
167+
libExecTestBenchCommons stanza =
168+
Map.insert "autogen-modules:" (modulesCompleter extractor) $
169+
Map.insert "autogen-includes:" filePathCompleter baseBenchCommons
170+
where
171+
extractor = case stanza of
172+
"library" -> sourceDirsExtractionLibrary
173+
"executable" -> sourceDirsExtractionExecutable
174+
"test-suite" -> sourceDirsExtractionTestSuite
175+
"benchmark" -> sourceDirsExtractionBenchmark
176+
_ -> sourceDirsExtractionLibrary
177+
178+
baseBenchCommons :: Map KeyWordName Completer
179+
baseBenchCommons =
175180
Map.fromList
176181
[ ("import:", importCompleter),
177182
("build-depends:", noopCompleter),

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

+15-34
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,22 @@ autogenModulesCompletionTests :: TestTree
4848
autogenModulesCompletionTests =
4949
testGroup
5050
"Autogen modules completion"
51-
[ runCabalTestCaseSession "Completes autogen-modules in library stanza" "" $ do
52-
doc <- openDoc "cabal-files/autogen-completion.cabal" "cabal"
53-
completions <- getCompletions doc (Position 5 4)
54-
liftIO $ do
55-
print (map (^. L.label) completions)
56-
assertBool "autogen-modules: should be in completions" $
57-
any (\c -> T.isInfixOf "autogen-modules:" (c ^. L.label)) completions
58-
assertBool "autogen-includes: should be in completions" $
59-
any (\c -> T.isInfixOf "autogen-includes:" (c ^. L.label)) completions
60-
61-
, runCabalTestCaseSession "Completes autogen-modules in executable stanza" "" $ do
62-
doc <- openDoc "cabal-files/autogen-completion-exe.cabal" "cabal"
63-
completions <- getCompletions doc (Position 5 4)
64-
liftIO $ do
65-
assertBool "autogen-modules: should be in completions" $
66-
any (\c -> T.isInfixOf "autogen-modules:" (c ^. L.label)) completions
67-
68-
, runCabalTestCaseSession "Completes autogen fields in test-suite stanza" "" $ do
69-
doc <- openDoc "cabal-files/autogen-completion-test.cabal" "cabal"
70-
completions <- getCompletions doc (Position 6 4)
71-
liftIO $ do
72-
assertBool "autogen-modules: should be in completions" $
73-
any (\c -> T.isInfixOf "autogen-modules:" (c ^. L.label)) completions
74-
assertBool "autogen-includes: should be in completions" $
75-
any (\c -> T.isInfixOf "autogen-includes:" (c ^. L.label)) completions
76-
77-
, runCabalTestCaseSession "Completes autogen fields in benchmark stanza" "" $ do
78-
doc <- openDoc "cabal-files/autogen-completion-bench.cabal" "cabal"
79-
completions <- getCompletions doc (Position 6 4)
80-
liftIO $ do
81-
assertBool "autogen-modules: should be in completions" $
82-
any (\c -> T.isInfixOf "autogen-modules:" (c ^. L.label)) completions
83-
assertBool "autogen-includes: should be in completions" $
84-
any (\c -> T.isInfixOf "autogen-includes:" (c ^. L.label)) completions
51+
[ checkCompletion "library" (Position 4 4) ["autogen-modules:", "autogen-includes:"]
52+
, checkCompletion "executable" (Position 8 4) ["autogen-modules:"]
53+
, checkCompletion "test-suite" (Position 13 4) ["autogen-modules:", "autogen-includes:"]
54+
, checkCompletion "benchmark" (Position 18 4) ["autogen-modules:", "autogen-includes:"]
8555
]
56+
where
57+
checkCompletion :: String -> Position -> [T.Text] -> TestTree
58+
checkCompletion name pos expectedLabels =
59+
runCabalTestCaseSession ("Completes autogen fields in " ++ name ++ " stanza") "" $ do
60+
doc <- openDoc "completions/autogen-completions.cabal" "cabal"
61+
completions <- getCompletions doc pos
62+
liftIO $ mapM_ (\label ->
63+
assertBool (T.unpack label ++ " should be in completions") $
64+
any (\c -> T.isInfixOf label (c ^. L.label)) completions
65+
) expectedLabels
66+
8667

8768

8869
basicCompleterTests :: TestTree

plugins/hls-cabal-plugin/test/testdata/cabal-files/autogen-completion-bench.cabal

-8
This file was deleted.

plugins/hls-cabal-plugin/test/testdata/cabal-files/autogen-completion-exe.cabal

-7
This file was deleted.

plugins/hls-cabal-plugin/test/testdata/cabal-files/autogen-completion-test.cabal

-8
This file was deleted.

plugins/hls-cabal-plugin/test/testdata/cabal-files/autogen-completion.cabal

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cabal-version: 3.2
2+
name: test-project
3+
4+
library
5+
autogen
6+
7+
executable exe
8+
main-is: Main.hs
9+
autogen
10+
11+
test-suite test
12+
type: exitcode-stdio-1.0
13+
main-is: Test.hs
14+
autogen
15+
16+
benchmark bench
17+
type: exitcode-stdio-1.0
18+
main-is: Bench.hs
19+
autogen

plugins/hls-module-name-plugin/src/Ide/Plugin/ModuleName.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pathModuleNames recorder state normFilePath filePath
150150
-- TODO, this can be avoid if the filePath is already absolute,
151151
-- we can avoid the toAbsolute call in the future.
152152
-- see Note [Root Directory]
153-
let mdlPath = (toAbsolute $ rootDir state) filePath
153+
let mdlPath = normalise $ (toAbsolute $ rootDir state) filePath
154154
logWith recorder Debug (AbsoluteFilePath mdlPath)
155155

156156
let suffixes = mapMaybe (`stripPrefix` mdlPath) paths

0 commit comments

Comments
 (0)