-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat: support setting cursor position in text edits #2389
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,9 +50,11 @@ def format_document(text_command: LspTextCommand, formatter: Optional[str] = Non | |
return Promise.resolve(None) | ||
|
||
|
||
def apply_text_edits_to_view(response: Optional[List[TextEdit]], view: sublime.View) -> None: | ||
def apply_text_edits_to_view( | ||
response: Optional[List[TextEdit]], view: sublime.View, *, process_placeholders: bool = False | ||
) -> None: | ||
edits = list(parse_text_edit(change) for change in response) if response else [] | ||
view.run_command('lsp_apply_document_edit', {'changes': edits}) | ||
view.run_command('lsp_apply_document_edit', {'changes': edits, 'process_placeholders': process_placeholders}) | ||
Comment on lines
+53
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit confusing that the I think it would be better if LspApplyDocumentEditCommand would simply take a list of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why is that. I imagine potential reasons could be to optimize the payload size or avoid passing unserializable data but neither of those is relevant here IMO. As for public vs. non-public, we both know that there are many "non-public" APIs that are used from plugins. People will use whatever they need since we don't prevent them :) But yeah, I agree with you that the command could take the original edits. |
||
|
||
|
||
class WillSaveWaitTask(SaveTask): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't taken a closer look to understand every part of this PR, but I wonder is there a particular reason for all this manual snippet handling? Also can it handle snippets with multiple tabstops correctly? If I understand the code above correcty, it manually just adds a selection for each tabstop. But this is not how snippets work, if there are multiple tabstops it should set a single cursor and then with tab you can jump to the next one.
I think instead of
view.insert(...)
the logic to apply text edits should better move the curser and then use the built-in commands instead (at least for snippets, to handle them correctly). Like inLSP/plugin/completion.py
Lines 354 to 357 in 36871c2
(perhaps only in the snippets case with tabstops, otherwise the cursor position probably shouldn't change I guess)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's obviously tailored for rust-analyzer and taken from it so this implementation assumes that there is only a single placeholder (specifically
$0
or${0:...}
). So no multiple tab stops. But yes, those are not so much snippets but more a functionality to set cursor(s), only using a snippet-like placeholders for that. The rust-analyzer-initiated LSP protocol feature request even calls it "Allow CodeActions to specify cursor position".I remember pretty well that we've tried
insert_snippet
(or @rwols did) when implementing the original code and it didn't work at all since it contains a lot of extra magic that for example auto-figures indentation. We need to do a raw insert or replace that won't do any of that.And we need to set the cursor after applying the edit. Otherwise the selection will shift randomly.
I can do some better naming here to reflect what it really is. Maybe "process_selection_placeholders"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I guess it could be tried like this for now, but I just read through that thread and I saw that there was already an example in microsoft/language-server-protocol#724 (comment) where they consider to use it as a "regular" snippet with multiple tab stops. So dependent on how this will end up in the specs, it's probably only a matter of time until a server will use the snippet in this way. For the autocompletion the indentation problem is solved by client announcing insertTextMode capability, where this client only supports
adjustIndentation
. I guess something like this would be needed as well for the snippet text edits then. Or alternatively they should introduce a new "simplified snippet" structure that only supports a single tab marker (or guarantee this in some other way in the specs).I wonder don't we need to set
{ "snippetTextEdit": boolean }
experimental client capability for this to work, which is mentioned in the docs from rust-analyzer?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like they don't explicitly check for the capability for this "move item" functionality (https://github.com/rust-lang/rust-analyzer/blob/f8eac19b3354722a6fa0177968af54a58bb5b9e1/crates/ide/src/move_item.rs#L139-L165). They do check it many other places but in that case I wouldn't go and enable it without first checking that we correctly handle it in all cases (which we probably don't).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I also realized that the proper place would probably be in the "experimental_capabilities" client config for rust-analyzer anyway.