From 3dd647a148022ac80cc7ab69de012ea244286e6c Mon Sep 17 00:00:00 2001 From: rm41339 Date: Wed, 2 Apr 2025 11:31:38 -0400 Subject: [PATCH 1/5] Add autogen field completions and tests for cabal files --- .../src/Ide/Plugin/Cabal/Completion/Data.hs | 16 ++++++---- plugins/hls-cabal-plugin/test/Completer.hs | 25 +++++++++++++++- .../autogen-completion.cabal | 30 +++++++++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal diff --git a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs index c27568d692..ac4510b141 100644 --- a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs +++ b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs @@ -23,9 +23,6 @@ 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 = @@ -33,7 +30,7 @@ cabalVersionKeyword = 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. -- @@ -90,6 +87,7 @@ libraryFields = ("visibility:", constantCompleter ["private", "public"]), ("reexported-modules:", noopCompleter), ("signatures:", noopCompleter), + ("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary), ("other-modules:", modulesCompleter sourceDirsExtractionLibrary) ] @@ -98,6 +96,7 @@ executableFields = Map.fromList [ ("main-is:", mainIsCompleter sourceDirsExtractionExecutable), ("scope:", constantCompleter ["public", "private"]), + ("autogen-modules:", modulesCompleter sourceDirsExtractionExecutable), ("other-modules:", modulesCompleter sourceDirsExtractionExecutable) ] @@ -105,6 +104,7 @@ 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) ] @@ -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) ] @@ -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), @@ -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), @@ -264,3 +265,6 @@ weightedLicenseNames = ghcOptions :: [T.Text] ghcOptions = map T.pack $ flagsForCompletion False + +supportedCabalVersions :: [CabalSpecVersion] +supportedCabalVersions = [CabalSpecV2_2 .. maxBound] diff --git a/plugins/hls-cabal-plugin/test/Completer.hs b/plugins/hls-cabal-plugin/test/Completer.hs index 6b1f772af0..d88b81128f 100644 --- a/plugins/hls-cabal-plugin/test/Completer.hs +++ b/plugins/hls-cabal-plugin/test/Completer.hs @@ -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) @@ -40,7 +41,8 @@ completerTests = completionHelperTests, filePathExposedModulesTests, exposedModuleCompleterTests, - importCompleterTests + importCompleterTests, + autogenFieldCompletionTests ] basicCompleterTests :: TestTree @@ -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 diff --git a/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal b/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal new file mode 100644 index 0000000000..1815b4d512 --- /dev/null +++ b/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal @@ -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- From 9b618969fa31d988a7ff1ccd0adaf786bc0b754e Mon Sep 17 00:00:00 2001 From: rm41339 Date: Wed, 2 Apr 2025 12:46:49 -0400 Subject: [PATCH 2/5] fix tests, edit autogen-modules completion to remove redundancy --- .../src/Ide/Plugin/Cabal/Completion/Data.hs | 18 ++++++++---------- plugins/hls-cabal-plugin/test/Completer.hs | 3 +-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs index ac4510b141..2f9ed8df89 100644 --- a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs +++ b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs @@ -23,6 +23,9 @@ 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 = @@ -30,7 +33,7 @@ cabalVersionKeyword = constantCompleter $ -- We only suggest cabal versions newer than 2.2 -- since we don't recommend using older ones. - map (T.pack . showCabalSpecVersion) [CabalSpecV2_2 .. maxBound] + map (T.pack . showCabalSpecVersion) supportedCabalVersions -- | Top level keywords of a cabal file. -- @@ -87,7 +90,6 @@ libraryFields = ("visibility:", constantCompleter ["private", "public"]), ("reexported-modules:", noopCompleter), ("signatures:", noopCompleter), - ("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary), ("other-modules:", modulesCompleter sourceDirsExtractionLibrary) ] @@ -96,7 +98,6 @@ executableFields = Map.fromList [ ("main-is:", mainIsCompleter sourceDirsExtractionExecutable), ("scope:", constantCompleter ["public", "private"]), - ("autogen-modules:", modulesCompleter sourceDirsExtractionExecutable), ("other-modules:", modulesCompleter sourceDirsExtractionExecutable) ] @@ -104,7 +105,6 @@ 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) ] @@ -113,7 +113,6 @@ benchmarkFields :: Map KeyWordName Completer benchmarkFields = Map.fromList [ ("type:", noopCompleter), - ("autogen-modules:", modulesCompleter sourceDirsExtractionBenchmark), ("main-is:", mainIsCompleter sourceDirsExtractionBenchmark), ("other-modules:", modulesCompleter sourceDirsExtractionBenchmark) ] @@ -166,7 +165,8 @@ flagFields = libExecTestBenchCommons :: Map KeyWordName Completer libExecTestBenchCommons = Map.fromList - [ ("build-depends:", noopCompleter), + [ ("import:", importCompleter), + ("build-depends:", noopCompleter), ("hs-source-dirs:", directoryCompleter), ("default-extensions:", noopCompleter), ("other-extensions:", noopCompleter), @@ -181,9 +181,10 @@ libExecTestBenchCommons = ("ghcjs-prof-options:", constantCompleter ghcOptions), ("ghcjs-shared-options:", constantCompleter ghcOptions), ("includes:", filePathCompleter), - ("autogen-includes:", filePathCompleter), ("install-includes:", filePathCompleter), ("include-dirs:", directoryCompleter), + ("autogen-includes:", filePathCompleter), + ("autogen-modules:", filePathCompleter), ("c-sources:", filePathCompleter), ("cxx-sources:", filePathCompleter), ("asm-sources:", filePathCompleter), @@ -265,6 +266,3 @@ weightedLicenseNames = ghcOptions :: [T.Text] ghcOptions = map T.pack $ flagsForCompletion False - -supportedCabalVersions :: [CabalSpecVersion] -supportedCabalVersions = [CabalSpecV2_2 .. maxBound] diff --git a/plugins/hls-cabal-plugin/test/Completer.hs b/plugins/hls-cabal-plugin/test/Completer.hs index d88b81128f..b37c7245f5 100644 --- a/plugins/hls-cabal-plugin/test/Completer.hs +++ b/plugins/hls-cabal-plugin/test/Completer.hs @@ -345,8 +345,7 @@ autogenFieldCompletionTests = , 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:"] + , testAutogenField "common" "autogen-completion/autogen-completion.cabal" (Position 29 9) ["autogen-modules:", "autogen-includes:"] ] where From 897c28013958f53b2009b0563cbfc8a8532d7bee Mon Sep 17 00:00:00 2001 From: rm41339 Date: Wed, 2 Apr 2025 12:50:34 -0400 Subject: [PATCH 3/5] edit test data to reflect latest changes --- plugins/hls-cabal-plugin/test/Completer.hs | 2 +- .../testdata/autogen-completion/autogen-completion.cabal | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/plugins/hls-cabal-plugin/test/Completer.hs b/plugins/hls-cabal-plugin/test/Completer.hs index b37c7245f5..9a2a570a57 100644 --- a/plugins/hls-cabal-plugin/test/Completer.hs +++ b/plugins/hls-cabal-plugin/test/Completer.hs @@ -345,7 +345,7 @@ autogenFieldCompletionTests = , 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 "common" "autogen-completion/autogen-completion.cabal" (Position 29 9) ["autogen-modules:", "autogen-includes:"] + , testAutogenField "common" "autogen-completion/autogen-completion.cabal" (Position 24 9) ["autogen-modules:", "autogen-includes:"] ] where diff --git a/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal b/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal index 1815b4d512..dd5c86d339 100644 --- a/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal +++ b/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal @@ -21,10 +21,5 @@ benchmark autobench hs-source-dirs: src autogen- -foreign-library autoforeign - type: native-shared - hs-source-dirs: src - autogen- - common defaults autogen- From 9aa5c009bc605c6d7478fbb30bd5244b5d12586c Mon Sep 17 00:00:00 2001 From: rm41339 Date: Wed, 2 Apr 2025 13:25:36 -0400 Subject: [PATCH 4/5] fix autogen-modules completer type --- .../hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs index 2f9ed8df89..637800e3f9 100644 --- a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs +++ b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Data.hs @@ -184,7 +184,7 @@ libExecTestBenchCommons = ("install-includes:", filePathCompleter), ("include-dirs:", directoryCompleter), ("autogen-includes:", filePathCompleter), - ("autogen-modules:", filePathCompleter), + ("autogen-modules:", modulesCompleter sourceDirsExtractionLibrary), ("c-sources:", filePathCompleter), ("cxx-sources:", filePathCompleter), ("asm-sources:", filePathCompleter), From 4f5e61729cd0aca41122f94ca2c6665683d88ee3 Mon Sep 17 00:00:00 2001 From: rm41339 Date: Wed, 2 Apr 2025 14:21:34 -0400 Subject: [PATCH 5/5] rename testdata/completion --- plugins/hls-cabal-plugin/test/Completer.hs | 10 +++++----- .../autogen-completion.cabal | 0 2 files changed, 5 insertions(+), 5 deletions(-) rename plugins/hls-cabal-plugin/test/testdata/{autogen-completion => completion}/autogen-completion.cabal (100%) diff --git a/plugins/hls-cabal-plugin/test/Completer.hs b/plugins/hls-cabal-plugin/test/Completer.hs index 9a2a570a57..ab7165b1ac 100644 --- a/plugins/hls-cabal-plugin/test/Completer.hs +++ b/plugins/hls-cabal-plugin/test/Completer.hs @@ -341,11 +341,11 @@ importCompleterTests = 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 "common" "autogen-completion/autogen-completion.cabal" (Position 24 9) ["autogen-modules:", "autogen-includes:"] + [ testAutogenField "library" "completion/autogen-completion.cabal" (Position 6 9) ["autogen-modules:", "autogen-includes:"] + , testAutogenField "executable" "completion/autogen-completion.cabal" (Position 11 9) ["autogen-modules:", "autogen-includes:"] + , testAutogenField "test-suite" "completion/autogen-completion.cabal" (Position 16 9) ["autogen-modules:", "autogen-includes:"] + , testAutogenField "benchmark" "completion/autogen-completion.cabal" (Position 21 9) ["autogen-modules:", "autogen-includes:"] + , testAutogenField "common" "completion/autogen-completion.cabal" (Position 24 9) ["autogen-modules:", "autogen-includes:"] ] where diff --git a/plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal b/plugins/hls-cabal-plugin/test/testdata/completion/autogen-completion.cabal similarity index 100% rename from plugins/hls-cabal-plugin/test/testdata/autogen-completion/autogen-completion.cabal rename to plugins/hls-cabal-plugin/test/testdata/completion/autogen-completion.cabal