From af6387cf22d18d0ec618a09345d46e0a64466037 Mon Sep 17 00:00:00 2001 From: VeryMilkyJoe Date: Mon, 21 Aug 2023 10:07:55 +0200 Subject: [PATCH] Remove suggestion of stanzas inside of stanza context (#3761) --- .../Plugin/Cabal/Completion/Completions.hs | 2 +- plugins/hls-cabal-plugin/test/Completer.hs | 27 ++++++++++++++++--- plugins/hls-cabal-plugin/test/Main.hs | 19 +------------ plugins/hls-cabal-plugin/test/Utils.hs | 14 ++++++++++ .../test/testdata/completer.cabal | 12 +++++++++ 5 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 plugins/hls-cabal-plugin/test/testdata/completer.cabal diff --git a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completions.hs b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completions.hs index 840dc44e50..69c5fa6598 100644 --- a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completions.hs +++ b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completions.hs @@ -49,7 +49,7 @@ contextToCompleter (TopLevel, KeyWord kw) = contextToCompleter (Stanza s _, None) = case Map.lookup s stanzaKeywordMap of Nothing -> errorNoopCompleter (LogUnknownStanzaNameInContextError s) - Just l -> constantCompleter $ Map.keys l ++ Map.keys stanzaKeywordMap + Just l -> constantCompleter $ Map.keys l -- if we are in a stanza's keyword's context we can complete possible values of that keyword contextToCompleter (Stanza s _, KeyWord kw) = case Map.lookup s stanzaKeywordMap of diff --git a/plugins/hls-cabal-plugin/test/Completer.hs b/plugins/hls-cabal-plugin/test/Completer.hs index d2d28fc07b..4dfa0f18eb 100644 --- a/plugins/hls-cabal-plugin/test/Completer.hs +++ b/plugins/hls-cabal-plugin/test/Completer.hs @@ -3,15 +3,18 @@ module Completer where -import Control.Lens ((^.)) +import Control.Lens ((^.), (^?)) +import Control.Lens.Prism import qualified Data.ByteString as ByteString +import Data.Maybe (mapMaybe) import qualified Data.Text as T import Distribution.PackageDescription.Parsec (parseGenericPackageDescriptionMaybe) import Ide.Plugin.Cabal.Completion.Completer.FilePath import Ide.Plugin.Cabal.Completion.Completer.Module import Ide.Plugin.Cabal.Completion.Completer.Types (CompleterData (..)) import Ide.Plugin.Cabal.Completion.Completions -import Ide.Plugin.Cabal.Completion.Types +import Ide.Plugin.Cabal.Completion.Types (CabalPrefixInfo (..), + StanzaName) import Ide.Plugin.Cabal.Parse (GenericPackageDescription) import qualified Language.LSP.Protocol.Lens as L import qualified Language.LSP.VFS as VFS @@ -23,7 +26,8 @@ completerTests :: TestTree completerTests = testGroup "Completer Tests" - [ fileCompleterTests, + [ basicCompleterTests, + fileCompleterTests, filePathCompletionContextTests, directoryCompleterTests, completionHelperTests, @@ -31,6 +35,23 @@ completerTests = exposedModuleCompleterTests ] +basicCompleterTests :: TestTree +basicCompleterTests = + testGroup + "Basic Completer Tests" + [ runCabalTestCaseSession "In stanza context - stanza should not be suggested" "" $ do + doc <- openDoc "completer.cabal" "cabal" + compls <- getCompletions doc (Position 11 7) + let complTexts = mapMaybe (^? L.textEdit . _Just . _L . L.newText) compls + liftIO $ assertBool "does not suggest library" $ "library" `notElem` complTexts + liftIO $ assertBool "suggests library keyword" $ "extra-libraries:" `elem` complTexts + , runCabalTestCaseSession "In top level context - stanza should be suggested" "" $ do + doc <- openDoc "completer.cabal" "cabal" + compls <- getCompletions doc (Position 8 2) + let complTexts = mapMaybe (^? L.textEdit . _Just . _L . L.newText) compls + liftIO $ assertBool "suggests benchmark" $ "benchmark" `elem` complTexts + ] + fileCompleterTests :: TestTree fileCompleterTests = testGroup diff --git a/plugins/hls-cabal-plugin/test/Main.hs b/plugins/hls-cabal-plugin/test/Main.hs index b2b32f6258..4ee8afac28 100644 --- a/plugins/hls-cabal-plugin/test/Main.hs +++ b/plugins/hls-cabal-plugin/test/Main.hs @@ -17,15 +17,12 @@ import Data.Either (isRight) import Data.Row import qualified Data.Text as T import qualified Data.Text as Text -import Ide.Plugin.Cabal import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion) import qualified Ide.Plugin.Cabal.Parse as Lib import qualified Language.LSP.Protocol.Lens as L import System.FilePath import Test.Hls - -cabalPlugin :: PluginTestDescriptor Ide.Plugin.Cabal.Log -cabalPlugin = mkPluginTestDescriptor descriptor "cabal" +import Utils main :: IO () main = do @@ -195,17 +192,3 @@ pluginTests = InR action@CodeAction{_title} <- codeActions guard (_title == "Replace with " <> license) pure action - --- ------------------------------------------------------------------------ --- Runner utils --- ------------------------------------------------------------------------ - -runCabalTestCaseSession :: TestName -> FilePath -> Session () -> TestTree -runCabalTestCaseSession title subdir = testCase title . runCabalSession subdir - -runCabalSession :: FilePath -> Session a -> IO a -runCabalSession subdir = - failIfSessionTimeout . runSessionWithServer cabalPlugin (testDataDir subdir) - -testDataDir :: FilePath -testDataDir = "test" "testdata" diff --git a/plugins/hls-cabal-plugin/test/Utils.hs b/plugins/hls-cabal-plugin/test/Utils.hs index 6974b9e188..bf1606cba8 100644 --- a/plugins/hls-cabal-plugin/test/Utils.hs +++ b/plugins/hls-cabal-plugin/test/Utils.hs @@ -5,11 +5,15 @@ module Utils where import Data.List (sort) import qualified Data.Text as T +import Ide.Plugin.Cabal (descriptor) +import qualified Ide.Plugin.Cabal import Ide.Plugin.Cabal.Completion.Types import System.Directory (getCurrentDirectory) import System.FilePath import Test.Hls +cabalPlugin :: PluginTestDescriptor Ide.Plugin.Cabal.Log +cabalPlugin = mkPluginTestDescriptor descriptor "cabal" simpleCabalPrefixInfoFromPos :: Position -> T.Text -> CabalPrefixInfo simpleCabalPrefixInfoFromPos pos prefix = @@ -43,6 +47,16 @@ getFilePathComplTestDir = do testDir <- getTestDir pure $ addTrailingPathSeparator $ testDir "filepath-completions" +runCabalTestCaseSession :: TestName -> FilePath -> Session () -> TestTree +runCabalTestCaseSession title subdir = testCase title . runCabalSession subdir + +runCabalSession :: FilePath -> Session a -> IO a +runCabalSession subdir = + failIfSessionTimeout . runSessionWithServer cabalPlugin (testDataDir subdir) + +testDataDir :: FilePath +testDataDir = "test" "testdata" + -- | list comparison where the order in the list is irrelevant (@?==) :: (HasCallStack, Ord a, Show a) => [a] -> [a] -> Assertion (@?==) l1 l2 = sort l1 @?= sort l2 diff --git a/plugins/hls-cabal-plugin/test/testdata/completer.cabal b/plugins/hls-cabal-plugin/test/testdata/completer.cabal new file mode 100644 index 0000000000..cd7c697026 --- /dev/null +++ b/plugins/hls-cabal-plugin/test/testdata/completer.cabal @@ -0,0 +1,12 @@ +cabal-version: 3.4 +name: test-hls +version: 0.1.0.0 +maintainer: milky +synopsis: example cabal file :) +license: Apache-2.0 +build-type: Simple + +be + +library + lib