Skip to content

fix: Improve brace handling in Cabal fields #4549

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -52,20 +52,24 @@
--
-- This only looks at the row of the cursor and not at the cursor's
-- position within the row.
--
-- TODO: we do not handle braces correctly. Add more tests!
findFieldSection :: Syntax.Position -> [Syntax.Field Syntax.Position] -> Maybe (Syntax.Field Syntax.Position)
findFieldSection _cursor [] = Nothing
findFieldSection _cursor [x] =
-- Last field. We decide later, whether we are starting
-- a new section.
Just x
findFieldSection _cursor [x] = Just x
findFieldSection cursor (x:y:ys)
| Syntax.positionRow (getAnnotation x) <= cursorLine && cursorLine < Syntax.positionRow (getAnnotation y)
= Just x
| Syntax.positionRow (getAnnotation x) == cursorLine && Syntax.positionRow (getAnnotation y) == cursorLine
= case (x, y) of
(Syntax.Field _ fieldLines, Syntax.Field _ nextFieldLines) ->
-- Handle multi-line fields with braces
if any (isBraceField . Syntax.fieldLineName) fieldLines

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / flags (9.12, ubuntu-latest)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / bench_init (9.10, ubuntu-latest)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / flags (9.10, ubuntu-latest)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.12, ubuntu-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.10, ubuntu-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.8, macOS-latest, false)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.4, macOS-latest, false)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.4, ubuntu-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.6, ubuntu-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / bench_init (9.8, ubuntu-latest)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.8, ubuntu-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.6, macOS-latest, false)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.12, macOS-latest, false)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.8, windows-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.10, macOS-latest, false)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.6, windows-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.4, windows-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.12, windows-latest, true)

Not in scope: ‘Syntax.fieldLineName’

Check failure on line 65 in plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Completion/CabalFields.hs

View workflow job for this annotation

GitHub Actions / test (9.10, windows-latest, true)

Not in scope: ‘Syntax.fieldLineName’
then Just x
else findFieldSection cursor (y:ys)
_ -> findFieldSection cursor (y:ys)
| otherwise = findFieldSection cursor (y:ys)
where
cursorLine = Syntax.positionRow cursor
isBraceField name = name == "extra-libraries:" || name == "extra-frameworks:" || name == "extra-lib-dirs:"

-- | Determine the field line the cursor is currently a part of.
--
Expand Down Expand Up @@ -301,3 +305,7 @@
where
startLSPPos = cabalPositionToLSPPosition $ getAnnotation field
endLSPPos = cabalPositionToLSPPosition $ getFieldEndPosition field

-- | Helper function to check if a field line contains a brace
isBraceField :: T.Text -> Bool
isBraceField name = name `elem` ["extra-libraries:", "extra-frameworks:", "extra-lib-dirs:", "extra-framework-dirs:"]
12 changes: 12 additions & 0 deletions plugins/hls-cabal-plugin/test/Completer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ basicCompleterTests =
let complTexts = getTextEditTexts compls
liftIO $ assertBool "suggests f2" $ "f2.hs" `elem` complTexts
liftIO $ assertBool "does not suggest" $ "Content.hs" `notElem` complTexts
, runCabalTestCaseSession "Brace handling in multi-line fields" "brace-handling" $ do
doc <- openDoc "brace-handling.cabal" "cabal"
-- Test completion in a multi-line field with braces
compls <- getCompletions doc (Position 15 8) -- Position in extra-libraries field
let complTexts = getTextEditTexts compls
liftIO $ assertBool "suggests correct library names" $
all (`elem` complTexts) ["pthread", "dl"]
-- Test completion in a field with multiple lines and braces
compls2 <- getCompletions doc (Position 25 8) -- Position in extra-frameworks field
let complTexts2 = getTextEditTexts compls2
liftIO $ assertBool "suggests correct framework names" $
all (`elem` complTexts2) ["CoreFoundation", "CoreServices"]
]
where
getTextEditTexts :: [CompletionItem] -> [T.Text]
Expand Down
31 changes: 31 additions & 0 deletions plugins/hls-cabal-plugin/test/testdata/brace-handling.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: brace-handling-test
version: 0.1.0.0

library
exposed-modules:
MyLib
build-depends:
base >= 4.7 && < 5
, text
, containers
default-language: GHC2021
ghc-options: -Wall
cpp-options: -DDEBUG
extra-libraries:
, pthread
, dl
extra-lib-dirs:
, /usr/local/lib
, /opt/local/lib
extra-framework-dirs:
, /Library/Frameworks
, /System/Library/Frameworks
extra-frameworks:
, CoreFoundation
, CoreServices
extra-lib-dirs:
, /usr/local/lib
, /opt/local/lib
extra-libraries:
, pthread
, dl
Loading