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

Code folding issue with trailing closures #1035

Open
NSExceptional opened this issue Jan 25, 2024 · 7 comments
Open

Code folding issue with trailing closures #1035

NSExceptional opened this issue Jan 25, 2024 · 7 comments
Assignees

Comments

@NSExceptional
Copy link

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?

@ahoppen
Copy link
Collaborator

ahoppen commented Jan 25, 2024

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

@ahoppen
Copy link
Collaborator

ahoppen commented Jan 26, 2024

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.

@NSExceptional
Copy link
Author

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 :)

@ahoppen
Copy link
Collaborator

ahoppen commented Jan 26, 2024

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.

@ahoppen
Copy link
Collaborator

ahoppen commented Feb 21, 2024

@NSExceptional Are you still interested in pursuing this issue?

@NSExceptional
Copy link
Author

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

@ahoppen
Copy link
Collaborator

ahoppen commented Feb 21, 2024

Not an issue at all. Just wanted to check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants