Skip to content

Commit f6114fa

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 f6114fa

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+2
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

+21
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ 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) "" conditionalData
159+
ctx @?= (Stanza "library" Nothing, None)
160+
, testCase "Elif is ignored" $ do
161+
ctx <- callGetContext (Position 7 18) "" conditionalData
162+
ctx @?= (Stanza "library" Nothing, None)
163+
, testCase "Else is ignored" $ do
164+
ctx <- callGetContext (Position 9 18) "" conditionalData
165+
ctx @?= (Stanza "library" Nothing, KeyWord "buildable:")
157166
, testCase "Named Stanza" $ do
158167
ctx <- callGetContext (Position 2 18) "" executableStanzaData
159168
ctx @?= (TopLevel, None)
@@ -237,6 +246,18 @@ name:
237246
eee
238247
|]
239248

249+
conditionalData :: T.Text
250+
conditionalData = [trimming|
251+
cabal-version: 3.0
252+
name: simple-cabal
253+
library
254+
if os(windows)
255+
buildable:
256+
elif os(linux)
257+
buildable:
258+
else
259+
buildable:
260+
|]
240261
multiLineOptsData :: T.Text
241262
multiLineOptsData = [trimming|
242263
cabal-version: 3.0

0 commit comments

Comments
 (0)