Skip to content
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

Cabal ignore if for completions (#4289) #4427

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -140,13 +140,15 @@ 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 `elem` conditionalKeywords -> 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)
prefixText sectionFields
where
inSameLineAsSectionName section = Syntax.positionRow (getAnnotation section) == Syntax.positionRow cursor
stanzaCtx = snd $ NE.head parentHistory
conditionalKeywords = ["if", "elif", "else"]

-- | Finds the cursor's context, where the cursor is already found to be in a specific field
--
Expand Down
21 changes: 21 additions & 0 deletions plugins/hls-cabal-plugin/test/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ 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) "" conditionalData
ctx @?= (Stanza "library" Nothing, None)
, testCase "Elif is ignored" $ do
ctx <- callGetContext (Position 7 18) "" conditionalData
ctx @?= (Stanza "library" Nothing, None)
, testCase "Else is ignored" $ do
ctx <- callGetContext (Position 9 18) "" conditionalData
ctx @?= (Stanza "library" Nothing, KeyWord "buildable:")
, testCase "Named Stanza" $ do
ctx <- callGetContext (Position 2 18) "" executableStanzaData
ctx @?= (TopLevel, None)
Expand Down Expand Up @@ -237,6 +246,18 @@ name:
eee
|]

conditionalData :: T.Text
conditionalData = [trimming|
cabal-version: 3.0
name: simple-cabal
library
if os(windows)
buildable:
elif os(linux)
buildable:
else
buildable:
|]
multiLineOptsData :: T.Text
multiLineOptsData = [trimming|
cabal-version: 3.0
Expand Down
Loading