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

Add support for multi-range formatting #2299

Merged
merged 3 commits into from
Aug 6, 2023

Conversation

jwortmann
Copy link
Member

This should add support for the new textDocument/rangesFormatting request from the LSP 3.18 specs, see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_rangeFormatting.

(I didn't add a new command for it, but instead made it a special case of the lsp_format_document_range command, because I think it would be unnecessary to expose this difference to users.)

Disclaimer: untested due to lack of server support at the moment

return Request("textDocument/rangesFormatting", {
"textDocument": text_document_identifier(view),
"options": formatting_options(view.settings()),
"ranges": [region_to_range(view, region) for region in view.sel() if not region.empty()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec doesn't seem to specify that range can't be empty. I would think that it could potentially be useful to place multiple cursors in multiple places and expect the code around those places to be formatted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would understand the specs in the way that servers must respond with TextEdits which are only allowed to modify the document within the given range(s).

The document range formatting request is sent from the client to the server to format a given range in a document.

So if the range is empty, there is nothing useful a server could do, other than sending an empty/null response. Do you know of any server which behaves different?

In fact, LSP already prevents empty selections for range formatting at the moment:

LSP/plugin/formatting.py

Lines 117 to 119 in e608f87

def is_enabled(self, event: Optional[dict] = None, point: Optional[int] = None) -> bool:
if super().is_enabled(event, point):
return has_single_nonempty_selection(self.view)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on me just trying LSP-json and using Format Selection, it seems like you are right. I haven't tried with empty selection of course since that is disabled, but I've tried with very small selection to see if server expands formatting beyond the selection but it seemed to only format within the selection.

Maybe worth trying some more servers before agreeing here...

@rchl rchl merged commit 1ac2b3e into sublimelsp:main Aug 6, 2023
4 checks passed
@jwortmann jwortmann deleted the ranges-formatting branch August 7, 2023 01:46
@jwortmann jwortmann mentioned this pull request Aug 22, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants