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

Handle "textDocument/rangeFormatting" and "textDocument/onTypeFormatting" #1805

Closed
MahdiBM opened this issue Nov 5, 2024 · 3 comments · Fixed by #1806 · May be fixed by #1815
Closed

Handle "textDocument/rangeFormatting" and "textDocument/onTypeFormatting" #1805

MahdiBM opened this issue Nov 5, 2024 · 3 comments · Fixed by #1806 · May be fixed by #1815
Labels
enhancement New feature or request

Comments

@MahdiBM
Copy link
Contributor

MahdiBM commented Nov 5, 2024

Description

Now that swift-format has some support for range formatting, sourcekit-lsp can implement handling these requests as well.

This will elevate the VSCode formatting experience since the VSCode extension is calling through to sourcekit-lsp for formatting.

sourcekit-lsp seems to already have some support for these. Just the logic isn't implemented.
For example there are some existing DocumentRangeFormattingRequest and DocumentOnTypeFormattingRequest types.

@ahoppen
Copy link
Member

ahoppen commented Nov 5, 2024

Synced to Apple’s issue tracker as rdar://139287738

@MahdiBM
Copy link
Contributor Author

MahdiBM commented Nov 7, 2024

@ahoppen How would you like OnTypeFormatting to be implemented?

I quite like the Xcode formatting behavior, so not sure if any of your colleagues working on Xcode can provide some tips about this.
Also not sure that swift-format will have the ability to do those formatting anyway, even if we knew Xcode's algorithm.

What I did for the vknabel formatter extensions was to perform a format of the whole file after hitting "return" on non-empty lines. But that formatted the whole file, not just some certain lines (this was before the selection-formatting PR being merged into swift-format).

I guess we could start with that "perform a format of the whole file after hitting "return" on non-empty lines" although I'd like to leverage the selection-formatting capabilities.

@ahoppen
Copy link
Member

ahoppen commented Nov 7, 2024

What do you think of running swift-format on the line that we receive the textDocument/onTypeFormatting request for if ch is a newline? I am a little doubtful how well this will work though because swift-format can’t format if you have syntax errors and I assume there will likely be syntax errors during typing.

Anyway, I think we would need to get a feel for this, so I’d implement it behind an experimental feature and then use it for a while to see how it feels and if we have improvement ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants