Skip to content

Commit 05049ba

Browse files
yndolgNiels Glodny
authored andcommitted
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: Niels Glodny <[email protected]>
1 parent 559e294 commit 05049ba

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/Completions.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ findCursorContext cursor parentHistory prefixText fields =
140140
Just field@(Syntax.Field _ _) -> classifyFieldContext parentHistory cursor field
141141
Just section@(Syntax.Section _ args sectionFields)
142142
| inSameLineAsSectionName section -> (stanzaCtx, None) -- TODO: test whether keyword in same line is parsed correctly
143+
| getFieldName section `elem` conditionalKeywords -> findCursorContext cursor parentHistory prefixText sectionFields -- Ignore if conditionals, they are not real sections
143144
| otherwise ->
144145
findCursorContext cursor
145146
(NE.cons (Syntax.positionCol (getAnnotation section) + 1, Stanza (getFieldName section) (getOptionalSectionName args)) parentHistory)
146147
prefixText sectionFields
147148
where
148149
inSameLineAsSectionName section = Syntax.positionRow (getAnnotation section) == Syntax.positionRow cursor
149150
stanzaCtx = snd $ NE.head parentHistory
151+
conditionalKeywords = ["if", "elif", "else"]
150152

151153
-- | Finds the cursor's context, where the cursor is already found to be in a specific field
152154
--

plugins/hls-cabal-plugin/test/Context.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ getContextTests =
154154
, testCase "Top level - cursor in later line with partially written value" $ do
155155
ctx <- callGetContext (Position 5 13) "eee" topLevelData
156156
ctx @?= (TopLevel, KeyWord "name:")
157+
, testCase "If is ignored" $ do
158+
ctx <- callGetContext (Position 5 18) "" ifData
159+
ctx @?= (Stanza "library" Nothing, KeyWord "buildable:")
157160
, testCase "Named Stanza" $ do
158161
ctx <- callGetContext (Position 2 18) "" executableStanzaData
159162
ctx @?= (TopLevel, None)
@@ -237,6 +240,14 @@ name:
237240
eee
238241
|]
239242

243+
ifData :: T.Text
244+
ifData = [trimming|
245+
cabal-version: 3.0
246+
name: simple-cabal
247+
library
248+
if os(windows)
249+
buildable:
250+
|]
240251
multiLineOptsData :: T.Text
241252
multiLineOptsData = [trimming|
242253
cabal-version: 3.0

0 commit comments

Comments
 (0)