Potential character range mis-calculation leading to erroneous text replacement
rust-analyzer version: rust-analyzer 1.65.0-nightly (2befdefd 2022-08-06)
rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)
relevant settings: Editor is BBEdit version 14.6.4
Reproduction example: https://github.com/fspeirs/bbedit-rust-analyzer-bug/ @ 5fc0a1b
I noticed an issue in BBEdit when using rust-analyzer when I wanted to change L1 in bbedit_test::main.rs.
use bbedit_lib::add;
becomes:
use bbedit_lib::{bbedit_do_thing, add};
I can reproduce the bug by:
- Select the text "add" in the import line.
- Type { to wrap it in braces
- Left arrow to move to after the opening brace
- Type-select "bbedi" to import "bbedit_do_thing"
- Tab to accept the completion.
Expected result: that the braces would contain {bbedit_do_thing, add}
Result: add is overwritten with bbedit_do_thing.
Feedback from BareBones tech support:
When requesting the completion, BBEdit sends the following position parameter in the request:
position = {
character = 22;
line = 0;
};
Character position 22 corresponds to where the insertion point is after typing "bbedi" just inside the open brace.
The response from the server includes an entry for bbedit_do_thing:
{
additionalTextEdits = (
);
deprecated = 0;
detail = "fn()";
filterText = "bbedit_do_thing";
kind = 3;
label = "bbedit_do_thing";
preselect = 1;
sortText = ffffffef;
textEdit = {
newText = "bbedit_do_thing";
range = {
end = {
character = 25;
line = 0;
};
start = {
character = 17;
line = 0;
};
};
};
}
The textEdit value is an instruction to replace characters 17 through 25 on the first line with "bbedit_do_thing". This range includes the "add" that was already there.
When BBEdit follows the instruction specified by the server, the practical effect is to replace the add with bbedit_do_thing.
This is intentional, we default to replace behavior. If the client wants to choose between insert and replace it should set the insertReplaceSupport capability.
I'm sorry to say that I'm not understanding you here.
As I read the specification, the insertReplaceSupport client capability is intended to signal that the client supports the InsertReplaceEdit operation structure 1. As far as I can tell, the presence of InsertReplaceEdit support on the client is not intended to alter the decisions the server makes about what transformations it wants the client to make when processing a CompletionItem.
Put another way: if the client doesn't support InsertReplaceEdit, the client can (and, I contend, should) perform the same transformations using a TextEdit structure (and/or an array of them in CompletionItem.additionalTextEdits) as it would have specified using an InsertReplaceEdit 2. That way the end user gets consistent completion behavior across clients, whether or not they support the (newer) InsertReplaceEdit.
Thus it seems odd to me that advertising support for an alternative representation of a completion's edits would address this symptom.
Is there a runtime option for configuring this behavior in rust-analyzer? Some servers accept server-specific parameters in InitializeParams.initializationOptions 3; that might be a good place for clients (or customers) with alternative preferences to express them, if so.
(I don't have a strong opinion on whether rust-analyzer's default behavior is correct; but clearly in this case there is at least one user who found it surprising to the point of believing that it was incorrect. I will say that I found it surprising as well, and until this had never encountered a server which replaced existing text ahead of the insertion point in this fashion.)
If the client reports insertReplaceSupport, then the server is allowed to return an InsertReplaceEdit instead of a TextEdit. This allows the client itself to choose whether to replace or insert as this type of text edit contains both behaviors. The relevant docs for that are on https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem specifically the textEdit field:
/** * An edit which is applied to a document when selecting this completion. * When an edit is provided the value of
insertTextis ignored. * * Note: The range of the edit must be a single line range and it must * contain the position at which completion has been requested. * * Most editors support two different operations when accepting a completion * item. One is to insert a completion text and the other is to replace an * existing text with a completion text. Since this can usually not be * predetermined by a server it can report both ranges. Clients need to * signal support forInsertReplaceEdits via the *textDocument.completion.completionItem.insertReplaceSupportclient * capability property. * * Note 1: The text edit's range as well as both ranges from an insert * replace edit must be a [single line] and they must contain the position * at which completion has been requested. * Note 2: If anInsertReplaceEditis returned the edit's insert range * must be a prefix of the edit's replace range, that means it must be * contained and starting at the same position. * * since 3.16.0 additional typeInsertReplaceEdit*/
Is there a runtime option for configuring this behavior in
rust-analyzer?
There isn't, and in my opinion there is no need to as the client can offer the configuration by signaling insertReplaceSupport and then doing the decision itself instead of leaving to the server.
The relevant docs for that are on
I'm aware, thanks; I linked to them. :-)
There isn't, and in my opinion there is no need to as the client can offer the configuration by signaling insertReplaceSupport and then doing the decision itself instead of leaving to the server.
OK. This looks like yet another area in which the LSP spec doesn't provide any client behavior guidance because it (still) seems to assume that VS Code is the only client; but I've gone ahead and advertised insertReplaceSupport for completions and made it work.