Skip to content

Commit

Permalink
Remove suggestion of stanzas inside of stanza context (#3761)
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryMilkyJoe authored Aug 21, 2023
1 parent 3ffde0d commit af6387c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions plugins/hls-cabal-plugin/test/Completer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,14 +26,32 @@ completerTests :: TestTree
completerTests =
testGroup
"Completer Tests"
[ fileCompleterTests,
[ basicCompleterTests,
fileCompleterTests,
filePathCompletionContextTests,
directoryCompleterTests,
completionHelperTests,
filePathExposedModulesTests,
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
Expand Down
19 changes: 1 addition & 18 deletions plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
14 changes: 14 additions & 0 deletions plugins/hls-cabal-plugin/test/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions plugins/hls-cabal-plugin/test/testdata/completer.cabal
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit af6387c

Please sign in to comment.