Repository navigation
In review
Labels
Milestone
Description
What
Since 3.16, a WorkspaceEdit can carry changeAnnotations: a map from an ID to a ChangeAnnotation with { label, needsConfirmation, description }.
- Text edits (
AnnotatedTextEdit) and file operations (create, rename, delete) refer to an annotation by itsannotationId. - An annotation with
needsConfirmation: trueasks the client to let the user confirm those changes before they are applied. - The client announces it with:
workspace.workspaceEdit.changeAnnotationSupport(groupsOnLabeloptional);textDocument.rename.honorsChangeAnnotations;textDocument.codeAction.honorsChangeAnnotations.
Why
- rust-analyzer: marks edits and file renames outside the workspace, for example in a dependency's sources, with an annotation "Edit outside of the workspace" (
outside_workspace_annotation_id(),crates/rust-analyzer/src/lsp/to_proto.rs). It does this only when the client announceschangeAnnotationSupport.
I found no other mainstream server that uses annotations without looking further. VS Code shows a refactor preview whenever an annotation needs confirmation.
What users miss:
- Renaming a symbol that is also used in a library's source edits that library without asking.
- No grouping or labels for the parts of a large edit.
Today
- Not announced (ClientCapabilitiesFactory.java#L72-L81, #L264-L266, #L142-L154).
getAnnotationId()is not read anywhere.changeAnnotationsis only checked to see whether a rename edit is non-empty (LSPRenameSupport.java#L108).- So a server that sends annotations anyway gets its edits applied with no confirmation. Per the spec, that is allowed for a client that doesn't announce support.
How it could be done
- In the paths that apply a server's
WorkspaceEdit:LSPIJUtils.applyWorkspaceEdit, called for rename, code actions andworkspace/applyEdit;- collect the changes whose annotation has
needsConfirmation: true.
- If there are any, show one dialog before applying anything:
- the changes grouped by annotation
label(anddescriptionas a tooltip); - a checkbox per group, as VS Code's preview does;
- OK applies the checked groups and the unannotated changes, and Cancel applies nothing.
- the changes grouped by annotation
- A plain
DialogWrapperwith aCheckboxTreeis enough. IntelliJ's ownUsageViewpreview may be too heavy for this. - For
workspace/applyEdit, unchecked changes still count as applied. The spec leaves it to the client; VS Code answersapplied: truetoo. Cancel answersapplied: falsewith afailureReason. - Then announce
changeAnnotationSupport: { groupsOnLabel: true }and bothhonorsChangeAnnotations.
Test
A rename fixture test with one annotated edit with needsConfirmation and one plain edit: accept, uncheck, and cancel.
Activity