Skip to content

Cabal autogen-modules and autogen-includes completion #4534

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23,17 +23,14 @@ import Ide.Plugin.Cabal.LicenseSuggest (licenseNames)
-- Completion Data
-- ----------------------------------------------------------------

supportedCabalVersions :: [CabalSpecVersion]
supportedCabalVersions = [CabalSpecV2_2 .. maxBound]

-- | Keyword for cabal version; required to be the top line in a cabal file
cabalVersionKeyword :: Map KeyWordName Completer
cabalVersionKeyword =
Map.singleton "cabal-version:" $
constantCompleter $
-- We only suggest cabal versions newer than 2.2
-- since we don't recommend using older ones.
map (T.pack . showCabalSpecVersion) supportedCabalVersions
map (T.pack . showCabalSpecVersion) [CabalSpecV2_2 .. maxBound]

-- | Top level keywords of a cabal file.
--
Expand Down Expand Up @@ -90,6 +87,7 @@ libraryFields =
("visibility:", constantCompleter ["private", "public"]),
("reexported-modules:", noopCompleter),
("signatures:", noopCompleter),
("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary),
("other-modules:", modulesCompleter sourceDirsExtractionLibrary)
]

Expand All @@ -98,13 +96,15 @@ executableFields =
Map.fromList
[ ("main-is:", mainIsCompleter sourceDirsExtractionExecutable),
("scope:", constantCompleter ["public", "private"]),
("autogen-modules:", modulesCompleter sourceDirsExtractionExecutable),
("other-modules:", modulesCompleter sourceDirsExtractionExecutable)
]

testSuiteFields :: Map KeyWordName Completer
testSuiteFields =
Map.fromList
[ ("type:", constantCompleter ["exitcode-stdio-1.0", "detailed-0.9"]),
("autogen-modules:", modulesCompleter sourceDirsExtractionTestSuite),
("main-is:", mainIsCompleter sourceDirsExtractionTestSuite),
("other-modules:", modulesCompleter sourceDirsExtractionTestSuite)
]
Expand All @@ -113,6 +113,7 @@ benchmarkFields :: Map KeyWordName Completer
benchmarkFields =
Map.fromList
[ ("type:", noopCompleter),
("autogen-modules:", modulesCompleter sourceDirsExtractionBenchmark),
("main-is:", mainIsCompleter sourceDirsExtractionBenchmark),
("other-modules:", modulesCompleter sourceDirsExtractionBenchmark)
]
Expand Down Expand Up @@ -165,8 +166,7 @@ flagFields =
libExecTestBenchCommons :: Map KeyWordName Completer
libExecTestBenchCommons =
Map.fromList
[ ("import:", importCompleter),
("build-depends:", noopCompleter),
[ ("build-depends:", noopCompleter),
("hs-source-dirs:", directoryCompleter),
("default-extensions:", noopCompleter),
("other-extensions:", noopCompleter),
Expand All @@ -181,6 +181,7 @@ libExecTestBenchCommons =
("ghcjs-prof-options:", constantCompleter ghcOptions),
("ghcjs-shared-options:", constantCompleter ghcOptions),
("includes:", filePathCompleter),
("autogen-includes:", filePathCompleter),
("install-includes:", filePathCompleter),
("include-dirs:", directoryCompleter),
("c-sources:", filePathCompleter),
Expand Down Expand Up @@ -264,3 +265,6 @@ weightedLicenseNames =

ghcOptions :: [T.Text]
ghcOptions = map T.pack $ flagsForCompletion False

supportedCabalVersions :: [CabalSpecVersion]
supportedCabalVersions = [CabalSpecV2_2 .. maxBound]
25 changes: 24 additions & 1 deletion plugins/hls-cabal-plugin/test/Completer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Completer where

import Control.Lens ((^.), (^?))
import Control.Lens.Prism
import Control.Monad (forM_)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Char8 as BS8
import Data.Maybe (mapMaybe)
Expand Down Expand Up @@ -40,7 +41,8 @@ completerTests =
completionHelperTests,
filePathExposedModulesTests,
exposedModuleCompleterTests,
importCompleterTests
importCompleterTests,
autogenFieldCompletionTests
]

basicCompleterTests :: TestTree
Expand Down Expand Up @@ -336,6 +338,27 @@ importCompleterTests =
[Syntax.SecArgName (Syntax.Position row (col + 7)) (BS8.pack name)]
[]

autogenFieldCompletionTests :: TestTree
autogenFieldCompletionTests =
testGroup "Autogen Field Completer Tests"
[ testAutogenField "library" "autogen-completion/autogen-completion.cabal" (Position 6 9) ["autogen-modules:", "autogen-includes:"]
, testAutogenField "executable" "autogen-completion/autogen-completion.cabal" (Position 11 9) ["autogen-modules:", "autogen-includes:"]
, testAutogenField "test-suite" "autogen-completion/autogen-completion.cabal" (Position 16 9) ["autogen-modules:", "autogen-includes:"]
, testAutogenField "benchmark" "autogen-completion/autogen-completion.cabal" (Position 21 9) ["autogen-modules:", "autogen-includes:"]
, testAutogenField "foreign-library" "autogen-completion/autogen-completion.cabal" (Position 26 9) ["autogen-includes:"]
, testAutogenField "common" "autogen-completion/autogen-completion.cabal" (Position 29 9) ["autogen-includes:"]
]

where
testAutogenField :: String -> FilePath -> Position -> [T.Text] -> TestTree
testAutogenField section file pos expected = runCabalTestCaseSession ("autogen-modules completion in " <> section) "" $ do
doc <- openDoc file "cabal"
items <- getCompletions doc pos
let labels = map (^. L.label) items
liftIO $ forM_ expected $ \expect ->
assertBool (T.unpack expect <> " not found in " <> section) $
any (expect `T.isInfixOf`) labels

simpleCompleterData :: Maybe StanzaName -> FilePath -> T.Text -> CompleterData
simpleCompleterData sName dir pref = do
CompleterData
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cabal-version: 3.0
name: autogen-completion
version: 0.1.0.0

library
hs-source-dirs: src
autogen-

executable autoexe
main-is: Main.hs
hs-source-dirs: src
autogen-

test-suite autotest
type: exitcode-stdio-1.0
hs-source-dirs: src
autogen-

benchmark autobench
type: exitcode-stdio-1.0
hs-source-dirs: src
autogen-

foreign-library autoforeign
type: native-shared
hs-source-dirs: src
autogen-

common defaults
autogen-
Loading