Skip to content

Commit 97a6fa5

Browse files
committed
internal: Defer rendering of structured snippets
This ensures that any assist using structured snippets won't accidentally remove bits interpreted as snippet bits.
1 parent 89f7bf7 commit 97a6fa5

File tree

4 files changed

+161
-78
lines changed

4 files changed

+161
-78
lines changed

crates/ide-assists/src/tests.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,13 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
132132
.filter(|it| !it.source_file_edits.is_empty() || !it.file_system_edits.is_empty())
133133
.expect("Assist did not contain any source changes");
134134
let mut actual = before;
135-
if let Some(source_file_edit) = source_change.get_source_edit(file_id) {
135+
if let Some((source_file_edit, snippet_edit)) =
136+
source_change.get_source_and_snippet_edit(file_id)
137+
{
136138
source_file_edit.apply(&mut actual);
139+
if let Some(snippet_edit) = snippet_edit {
140+
snippet_edit.apply(&mut actual);
141+
}
137142
}
138143
actual
139144
};
@@ -191,9 +196,12 @@ fn check_with_config(
191196
&& source_change.file_system_edits.len() == 0;
192197

193198
let mut buf = String::new();
194-
for (file_id, (edit, _snippet_edit)) in source_change.source_file_edits {
199+
for (file_id, (edit, snippet_edit)) in source_change.source_file_edits {
195200
let mut text = db.file_text(file_id).as_ref().to_owned();
196201
edit.apply(&mut text);
202+
if let Some(snippet_edit) = snippet_edit {
203+
snippet_edit.apply(&mut text);
204+
}
197205
if !skip_header {
198206
let sr = db.file_source_root(file_id);
199207
let sr = db.source_root(sr);

crates/ide-db/src/source_change.rs

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use itertools::Itertools;
1111
use nohash_hasher::IntMap;
1212
use stdx::never;
1313
use syntax::{
14-
algo, ast, ted, AstNode, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange,
15-
TextSize,
14+
algo, AstNode, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
1615
};
1716
use text_edit::{TextEdit, TextEditBuilder};
1817

@@ -76,8 +75,11 @@ impl SourceChange {
7675
self.file_system_edits.push(edit);
7776
}
7877

79-
pub fn get_source_edit(&self, file_id: FileId) -> Option<&TextEdit> {
80-
self.source_file_edits.get(&file_id).map(|(edit, _)| edit)
78+
pub fn get_source_and_snippet_edit(
79+
&self,
80+
file_id: FileId,
81+
) -> Option<&(TextEdit, Option<SnippetEdit>)> {
82+
self.source_file_edits.get(&file_id)
8183
}
8284

8385
pub fn merge(mut self, other: SourceChange) -> SourceChange {
@@ -258,24 +260,19 @@ impl SourceChangeBuilder {
258260
}
259261

260262
fn commit(&mut self) {
261-
// Render snippets first so that they get bundled into the tree diff
262-
if let Some(mut snippets) = self.snippet_builder.take() {
263-
// Last snippet always has stop index 0
264-
let last_stop = snippets.places.pop().unwrap();
265-
last_stop.place(0);
266-
267-
for (index, stop) in snippets.places.into_iter().enumerate() {
268-
stop.place(index + 1)
269-
}
270-
}
263+
let snippet_edit = self.snippet_builder.take().map(|builder| {
264+
SnippetEdit::new(
265+
builder.places.into_iter().map(PlaceSnippet::finalize_position).collect_vec(),
266+
)
267+
});
271268

272269
if let Some(tm) = self.mutated_tree.take() {
273-
algo::diff(&tm.immutable, &tm.mutable_clone).into_text_edit(&mut self.edit)
270+
algo::diff(&tm.immutable, &tm.mutable_clone).into_text_edit(&mut self.edit);
274271
}
275272

276273
let edit = mem::take(&mut self.edit).finish();
277-
if !edit.is_empty() {
278-
self.source_change.insert_source_edit(self.file_id, edit);
274+
if !edit.is_empty() || snippet_edit.is_some() {
275+
self.source_change.insert_source_and_snippet_edit(self.file_id, edit, snippet_edit);
279276
}
280277
}
281278

@@ -429,57 +426,11 @@ enum PlaceSnippet {
429426
}
430427

431428
impl PlaceSnippet {
432-
/// Places the snippet before or over an element with the given tab stop index
433-
fn place(self, order: usize) {
434-
// ensure the target element is still attached
435-
match &self {
436-
PlaceSnippet::Before(element)
437-
| PlaceSnippet::After(element)
438-
| PlaceSnippet::Over(element) => {
439-
// element should still be in the tree, but if it isn't
440-
// then it's okay to just ignore this place
441-
if stdx::never!(element.parent().is_none()) {
442-
return;
443-
}
444-
}
445-
}
446-
429+
fn finalize_position(self) -> Snippet {
447430
match self {
448-
PlaceSnippet::Before(element) => {
449-
ted::insert_raw(ted::Position::before(&element), Self::make_tab_stop(order));
450-
}
451-
PlaceSnippet::After(element) => {
452-
ted::insert_raw(ted::Position::after(&element), Self::make_tab_stop(order));
453-
}
454-
PlaceSnippet::Over(element) => {
455-
let position = ted::Position::before(&element);
456-
element.detach();
457-
458-
let snippet = ast::SourceFile::parse(&format!("${{{order}:_}}"))
459-
.syntax_node()
460-
.clone_for_update();
461-
462-
let placeholder =
463-
snippet.descendants().find_map(ast::UnderscoreExpr::cast).unwrap();
464-
ted::replace(placeholder.syntax(), element);
465-
466-
ted::insert_raw(position, snippet);
467-
}
431+
PlaceSnippet::Before(it) => Snippet::Tabstop(it.text_range().start()),
432+
PlaceSnippet::After(it) => Snippet::Tabstop(it.text_range().end()),
433+
PlaceSnippet::Over(it) => Snippet::Placeholder(it.text_range()),
468434
}
469435
}
470-
471-
fn make_tab_stop(order: usize) -> SyntaxNode {
472-
let stop = ast::SourceFile::parse(&format!("stop!(${order})"))
473-
.syntax_node()
474-
.descendants()
475-
.find_map(ast::TokenTree::cast)
476-
.unwrap()
477-
.syntax()
478-
.clone_for_update();
479-
480-
stop.first_token().unwrap().detach();
481-
stop.last_token().unwrap().detach();
482-
483-
stop
484-
}
485436
}

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub use ide_db::{
127127
label::Label,
128128
line_index::{LineCol, LineIndex},
129129
search::{ReferenceCategory, SearchScope},
130-
source_change::{FileSystemEdit, SourceChange},
130+
source_change::{FileSystemEdit, SnippetEdit, SourceChange},
131131
symbol_index::Query,
132132
RootDatabase, SymbolKind,
133133
};

crates/rust-analyzer/src/to_proto.rs

Lines changed: 132 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use ide::{
1010
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
1111
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel, InlayHint,
1212
InlayHintLabel, InlayHintLabelPart, InlayKind, Markup, NavigationTarget, ReferenceCategory,
13-
RenameError, Runnable, Severity, SignatureHelp, SourceChange, StructureNodeKind, SymbolKind,
14-
TextEdit, TextRange, TextSize,
13+
RenameError, Runnable, Severity, SignatureHelp, SnippetEdit, SourceChange, StructureNodeKind,
14+
SymbolKind, TextEdit, TextRange, TextSize,
1515
};
1616
use itertools::Itertools;
1717
use serde_json::to_value;
@@ -22,7 +22,7 @@ use crate::{
2222
config::{CallInfoConfig, Config},
2323
global_state::GlobalStateSnapshot,
2424
line_index::{LineEndings, LineIndex, PositionEncoding},
25-
lsp_ext,
25+
lsp_ext::{self, SnippetTextEdit},
2626
lsp_utils::invalid_params_error,
2727
semantic_tokens::{self, standard_fallback_type},
2828
};
@@ -884,16 +884,135 @@ fn outside_workspace_annotation_id() -> String {
884884
String::from("OutsideWorkspace")
885885
}
886886

887+
fn merge_text_and_snippet_edit(
888+
line_index: &LineIndex,
889+
edit: TextEdit,
890+
snippet_edit: Option<SnippetEdit>,
891+
) -> Vec<SnippetTextEdit> {
892+
let Some(snippet_edit) = snippet_edit else {
893+
return edit.into_iter().map(|it| snippet_text_edit(&line_index, false, it)).collect();
894+
};
895+
896+
let mut edits: Vec<SnippetTextEdit> = vec![];
897+
let mut snippets = snippet_edit.into_edit_ranges().into_iter().peekable();
898+
let mut text_edits = edit.into_iter();
899+
900+
while let Some(current_indel) = text_edits.next() {
901+
let new_range = {
902+
let insert_len =
903+
TextSize::try_from(current_indel.insert.len()).unwrap_or(TextSize::from(u32::MAX));
904+
TextRange::at(current_indel.delete.start(), insert_len)
905+
};
906+
907+
// insert any snippets before the text edit
908+
let first_snippet_in_or_after_edit = loop {
909+
let Some((snippet_index, snippet_range)) = snippets.peek() else { break None };
910+
911+
// check if we're entirely before the range
912+
// only possible for tabstops
913+
if snippet_range.end() < new_range.start()
914+
&& stdx::always!(
915+
snippet_range.is_empty(),
916+
"placeholder range is before any text edits"
917+
)
918+
{
919+
let range = range(&line_index, *snippet_range);
920+
let new_text = format!("${snippet_index}");
921+
922+
edits.push(SnippetTextEdit {
923+
range,
924+
new_text,
925+
insert_text_format: Some(lsp_types::InsertTextFormat::SNIPPET),
926+
annotation_id: None,
927+
})
928+
} else {
929+
break Some((snippet_index, snippet_range));
930+
}
931+
};
932+
933+
if first_snippet_in_or_after_edit
934+
.is_some_and(|(_, range)| new_range.intersect(*range).is_some())
935+
{
936+
// at least one snippet edit intersects this text edit,
937+
// so gather all of the edits that intersect this text edit
938+
let mut all_snippets = snippets
939+
.take_while_ref(|(_, range)| new_range.intersect(*range).is_some())
940+
.collect_vec();
941+
942+
// ensure all of the ranges are wholly contained inside of the new range
943+
all_snippets.retain(|(_, range)| {
944+
stdx::always!(
945+
new_range.contains_range(*range),
946+
"found placeholder range {:?} which wasn't fully inside of text edit's new range {:?}", range, new_range
947+
)
948+
});
949+
950+
let mut text_edit = text_edit(line_index, current_indel);
951+
952+
// escape out snippet text
953+
stdx::replace(&mut text_edit.new_text, '\\', r"\\");
954+
stdx::replace(&mut text_edit.new_text, '$', r"\$");
955+
956+
// ...and apply!
957+
for (index, range) in all_snippets.iter().rev() {
958+
let start = (range.start() - new_range.start()).into();
959+
let end = (range.end() - new_range.start()).into();
960+
961+
if range.is_empty() {
962+
text_edit.new_text.insert_str(start, &format!("${index}"));
963+
} else {
964+
text_edit.new_text.insert(end, '}');
965+
text_edit.new_text.insert_str(start, &format!("${{{index}"));
966+
}
967+
}
968+
969+
edits.push(SnippetTextEdit {
970+
range: text_edit.range,
971+
new_text: text_edit.new_text,
972+
insert_text_format: Some(lsp_types::InsertTextFormat::SNIPPET),
973+
annotation_id: None,
974+
})
975+
} else {
976+
// snippet edit was beyond the current one
977+
// since it wasn't consumed, it's available for the next pass
978+
edits.push(snippet_text_edit(line_index, false, current_indel));
979+
}
980+
}
981+
982+
// insert any remaining edits
983+
// either one of the two or both should've run out at this point,
984+
// so it's either a tail of text edits or tabstops
985+
edits.extend(text_edits.map(|indel| snippet_text_edit(line_index, false, indel)));
986+
edits.extend(snippets.map(|(snippet_index, snippet_range)| {
987+
stdx::always!(
988+
snippet_range.is_empty(),
989+
"found placeholder snippet {:?} without a text edit",
990+
snippet_range
991+
);
992+
993+
let range = range(&line_index, snippet_range);
994+
let new_text = format!("${snippet_index}");
995+
996+
SnippetTextEdit {
997+
range,
998+
new_text,
999+
insert_text_format: Some(lsp_types::InsertTextFormat::SNIPPET),
1000+
annotation_id: None,
1001+
}
1002+
}));
1003+
1004+
edits
1005+
}
1006+
8871007
pub(crate) fn snippet_text_document_edit(
8881008
snap: &GlobalStateSnapshot,
889-
is_snippet: bool,
8901009
file_id: FileId,
8911010
edit: TextEdit,
1011+
snippet_edit: Option<SnippetEdit>,
8921012
) -> Cancellable<lsp_ext::SnippetTextDocumentEdit> {
8931013
let text_document = optional_versioned_text_document_identifier(snap, file_id);
8941014
let line_index = snap.file_line_index(file_id)?;
895-
let mut edits: Vec<_> =
896-
edit.into_iter().map(|it| snippet_text_edit(&line_index, is_snippet, it)).collect();
1015+
let mut edits = merge_text_and_snippet_edit(&line_index, edit, snippet_edit);
8971016

8981017
if snap.analysis.is_library_file(file_id)? && snap.config.change_annotation_support() {
8991018
for edit in &mut edits {
@@ -973,8 +1092,13 @@ pub(crate) fn snippet_workspace_edit(
9731092
let ops = snippet_text_document_ops(snap, op)?;
9741093
document_changes.extend_from_slice(&ops);
9751094
}
976-
for (file_id, (edit, _snippet_edit)) in source_change.source_file_edits {
977-
let edit = snippet_text_document_edit(snap, source_change.is_snippet, file_id, edit)?;
1095+
for (file_id, (edit, snippet_edit)) in source_change.source_file_edits {
1096+
let edit = snippet_text_document_edit(
1097+
snap,
1098+
file_id,
1099+
edit,
1100+
snippet_edit.filter(|_| source_change.is_snippet),
1101+
)?;
9781102
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
9791103
}
9801104
let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {

0 commit comments

Comments
 (0)