apple / sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code folding issue with trailing closures

NSExceptional opened this issue · comments

The following code only gets one folding expression, when it should have two, and it should ideally stop folding so that line 57 is not part of the folded range, since it has an opening brace on it as well, so you'd need to be able to show that line to be able to fold it too.

Screenshot 2024-01-24 at 11 55 12 PM

I would like to make a contribution to fix this myself, but I'm not sure where to start except that I know this is probably where the issue lies:

if let braced = node.asProtocol(BracedSyntax.self) {
return self.addFoldingRange(
start: braced.leftBrace.endPositionBeforeTrailingTrivia,
end: braced.rightBrace.positionAfterSkippingLeadingTrivia
)
}

Imo, an easy fix—which would also address my personal opinion that we should mimic how Xcode code folds by not consuming the line with the closing } on it—would be to just adjust those lines to always stop on the line above the trailing brace. Then we wouldn't have to special-case this fix, either. Thoughts?

Tracked in Apple’s issue tracker as rdar://121580050

The following code only gets one folding expression, when it should have two

Just to be clear, you’re saying that it should have one for each trailing closure, right?

Imo, an easy fix—which would also address my personal opinion that we should mimic how Xcode code folds by not consuming the line with the closing } on it—would be to just adjust those lines to always stop on the line above the trailing brace. Then we wouldn't have to special-case this fix, either. Thoughts?

That sounds reasonable to me.

I would like to make a contribution to fix this myself, but I'm not sure where to start except that I know this is probably where the issue lies:

Thank you 🙏🏽 You’re already at the correct place for the implementation. I would start by adding a test case to https://github.com/apple/sourcekit-lsp/blob/main/Tests/SourceKitLSPTests/FoldingRangeTests.swift. Once you have that, you should be able to set breakpoints in FoldingRangeFinder to figure out the fix. My suspicion is that we need to change some positionAfterSkippingLeadingTrivia to position in there. But I might be wrong.

Just to be clear, you’re saying that it should have one for each trailing closure, right?

Correct 👍🏻

an easy fix would be to just adjust those lines to always stop on the line above the trailing brace

Upon further thinking, this only fixes the issue of it covering up another brace... it wouldn't necessarily cause another folding range to be added for the closure. Right?

I'll get to debugging :)

I think (haven’t verified) that sourcekit-lsp does report a folding range for the second closure as well but VS Code just doesn’t display it because it overlaps with the folding range of the previous closure.

if let braced = node.asProtocol(BracedSyntax.self) {
return self.addFoldingRange(
start: braced.leftBrace.endPositionBeforeTrailingTrivia,
end: braced.rightBrace.positionAfterSkippingLeadingTrivia
)
}
should be generating the folding range for any closure.

@NSExceptional Are you still interested in pursuing this issue?

Hey, yes, I'm sorry, I've just had a lot on my plate recently. I will try to get to it ASAP.

Not an issue at all. Just wanted to check.

I just checked and it looks like I fixed this in #1385.