From 33e3e5410d7bb319408f7417be5985da88b61d78 Mon Sep 17 00:00:00 2001 From: niels <33730531+yndolg@users.noreply.github.com> Date: Sat, 12 Oct 2024 17:35:30 +0200 Subject: [PATCH] Cabal ignore if for completions If conditions were previously handled as normal sections, for which we don't have any completion information. Now, we ignore the if section and use the parent section for completion purposes. Co-authored-by: SamuelLess --- .../src/Ide/Plugin/Cabal/Completion/Completions.hs | 1 + plugins/hls-cabal-plugin/test/Context.hs | 11 +++++++++++ 2 files changed, 12 insertions(+) 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 04b6562270..566828db82 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 @@ -140,6 +140,7 @@ findCursorContext cursor parentHistory prefixText fields = Just field@(Syntax.Field _ _) -> classifyFieldContext parentHistory cursor field Just section@(Syntax.Section _ args sectionFields) | inSameLineAsSectionName section -> (stanzaCtx, None) -- TODO: test whether keyword in same line is parsed correctly + | getFieldName section == "if" -> findCursorContext cursor parentHistory prefixText sectionFields -- Ignore if conditionals, they are not real sections | otherwise -> findCursorContext cursor (NE.cons (Syntax.positionCol (getAnnotation section) + 1, Stanza (getFieldName section) (getOptionalSectionName args)) parentHistory) diff --git a/plugins/hls-cabal-plugin/test/Context.hs b/plugins/hls-cabal-plugin/test/Context.hs index 82d50ccf14..73c15274ac 100644 --- a/plugins/hls-cabal-plugin/test/Context.hs +++ b/plugins/hls-cabal-plugin/test/Context.hs @@ -154,6 +154,9 @@ getContextTests = , testCase "Top level - cursor in later line with partially written value" $ do ctx <- callGetContext (Position 5 13) "eee" topLevelData ctx @?= (TopLevel, KeyWord "name:") + , testCase "If is ignored" $ do + ctx <- callGetContext (Position 5 18) "" ifData + ctx @?= (Stanza "library" Nothing, KeyWord "buildable:") , testCase "Named Stanza" $ do ctx <- callGetContext (Position 2 18) "" executableStanzaData ctx @?= (TopLevel, None) @@ -237,6 +240,14 @@ name: eee |] +ifData :: T.Text +ifData = [trimming| +cabal-version: 3.0 +name: simple-cabal +library + if os(windows) + buildable: +|] multiLineOptsData :: T.Text multiLineOptsData = [trimming| cabal-version: 3.0