Add SnippetEdit
to be alongside source changes
Rendering of snippet edits is deferred to places using source change
This commit is contained in:
parent
75ac37f317
commit
89f7bf7411
@ -191,7 +191,7 @@ fn check_with_config(
|
||||
&& source_change.file_system_edits.len() == 0;
|
||||
|
||||
let mut buf = String::new();
|
||||
for (file_id, edit) in source_change.source_file_edits {
|
||||
for (file_id, (edit, _snippet_edit)) in source_change.source_file_edits {
|
||||
let mut text = db.file_text(file_id).as_ref().to_owned();
|
||||
edit.apply(&mut text);
|
||||
if !skip_header {
|
||||
@ -485,7 +485,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "let $0var_name = 5;\n ",
|
||||
@ -497,6 +498,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [],
|
||||
is_snippet: true,
|
||||
@ -544,7 +547,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "let $0var_name = 5;\n ",
|
||||
@ -556,6 +560,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [],
|
||||
is_snippet: true,
|
||||
@ -581,7 +587,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "fun_name()",
|
||||
@ -593,6 +600,8 @@ pub fn test_some_range(a: int) -> bool {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [],
|
||||
is_snippet: true,
|
||||
|
@ -7,6 +7,7 @@ use std::{collections::hash_map::Entry, iter, mem};
|
||||
|
||||
use crate::SnippetCap;
|
||||
use base_db::{AnchoredPathBuf, FileId};
|
||||
use itertools::Itertools;
|
||||
use nohash_hasher::IntMap;
|
||||
use stdx::never;
|
||||
use syntax::{
|
||||
@ -17,7 +18,7 @@ use text_edit::{TextEdit, TextEditBuilder};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct SourceChange {
|
||||
pub source_file_edits: IntMap<FileId, TextEdit>,
|
||||
pub source_file_edits: IntMap<FileId, (TextEdit, Option<SnippetEdit>)>,
|
||||
pub file_system_edits: Vec<FileSystemEdit>,
|
||||
pub is_snippet: bool,
|
||||
}
|
||||
@ -26,7 +27,7 @@ impl SourceChange {
|
||||
/// Creates a new SourceChange with the given label
|
||||
/// from the edits.
|
||||
pub fn from_edits(
|
||||
source_file_edits: IntMap<FileId, TextEdit>,
|
||||
source_file_edits: IntMap<FileId, (TextEdit, Option<SnippetEdit>)>,
|
||||
file_system_edits: Vec<FileSystemEdit>,
|
||||
) -> Self {
|
||||
SourceChange { source_file_edits, file_system_edits, is_snippet: false }
|
||||
@ -34,7 +35,7 @@ impl SourceChange {
|
||||
|
||||
pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self {
|
||||
SourceChange {
|
||||
source_file_edits: iter::once((file_id, edit)).collect(),
|
||||
source_file_edits: iter::once((file_id, (edit, None))).collect(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@ -42,12 +43,31 @@ impl SourceChange {
|
||||
/// Inserts a [`TextEdit`] for the given [`FileId`]. This properly handles merging existing
|
||||
/// edits for a file if some already exist.
|
||||
pub fn insert_source_edit(&mut self, file_id: FileId, edit: TextEdit) {
|
||||
self.insert_source_and_snippet_edit(file_id, edit, None)
|
||||
}
|
||||
|
||||
/// Inserts a [`TextEdit`] and potentially a [`SnippetEdit`] for the given [`FileId`].
|
||||
/// This properly handles merging existing edits for a file if some already exist.
|
||||
pub fn insert_source_and_snippet_edit(
|
||||
&mut self,
|
||||
file_id: FileId,
|
||||
edit: TextEdit,
|
||||
snippet_edit: Option<SnippetEdit>,
|
||||
) {
|
||||
match self.source_file_edits.entry(file_id) {
|
||||
Entry::Occupied(mut entry) => {
|
||||
never!(entry.get_mut().union(edit).is_err(), "overlapping edits for same file");
|
||||
let value = entry.get_mut();
|
||||
never!(value.0.union(edit).is_err(), "overlapping edits for same file");
|
||||
never!(
|
||||
value.1.is_some() && snippet_edit.is_some(),
|
||||
"overlapping snippet edits for same file"
|
||||
);
|
||||
if value.1.is_none() {
|
||||
value.1 = snippet_edit;
|
||||
}
|
||||
}
|
||||
Entry::Vacant(entry) => {
|
||||
entry.insert(edit);
|
||||
entry.insert((edit, snippet_edit));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -57,7 +77,7 @@ impl SourceChange {
|
||||
}
|
||||
|
||||
pub fn get_source_edit(&self, file_id: FileId) -> Option<&TextEdit> {
|
||||
self.source_file_edits.get(&file_id)
|
||||
self.source_file_edits.get(&file_id).map(|(edit, _)| edit)
|
||||
}
|
||||
|
||||
pub fn merge(mut self, other: SourceChange) -> SourceChange {
|
||||
@ -70,7 +90,18 @@ impl SourceChange {
|
||||
|
||||
impl Extend<(FileId, TextEdit)> for SourceChange {
|
||||
fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) {
|
||||
iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit));
|
||||
self.extend(iter.into_iter().map(|(file_id, edit)| (file_id, (edit, None))))
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<(FileId, (TextEdit, Option<SnippetEdit>))> for SourceChange {
|
||||
fn extend<T: IntoIterator<Item = (FileId, (TextEdit, Option<SnippetEdit>))>>(
|
||||
&mut self,
|
||||
iter: T,
|
||||
) {
|
||||
iter.into_iter().for_each(|(file_id, (edit, snippet_edit))| {
|
||||
self.insert_source_and_snippet_edit(file_id, edit, snippet_edit)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,6 +113,14 @@ impl Extend<FileSystemEdit> for SourceChange {
|
||||
|
||||
impl From<IntMap<FileId, TextEdit>> for SourceChange {
|
||||
fn from(source_file_edits: IntMap<FileId, TextEdit>) -> SourceChange {
|
||||
let source_file_edits =
|
||||
source_file_edits.into_iter().map(|(file_id, edit)| (file_id, (edit, None))).collect();
|
||||
SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntMap<FileId, (TextEdit, Option<SnippetEdit>)>> for SourceChange {
|
||||
fn from(source_file_edits: IntMap<FileId, (TextEdit, Option<SnippetEdit>)>) -> SourceChange {
|
||||
SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
|
||||
}
|
||||
}
|
||||
@ -94,6 +133,73 @@ impl FromIterator<(FileId, TextEdit)> for SourceChange {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<(FileId, (TextEdit, Option<SnippetEdit>))> for SourceChange {
|
||||
fn from_iter<T: IntoIterator<Item = (FileId, (TextEdit, Option<SnippetEdit>))>>(
|
||||
iter: T,
|
||||
) -> Self {
|
||||
let mut this = SourceChange::default();
|
||||
this.extend(iter);
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SnippetEdit(Vec<(u32, TextRange)>);
|
||||
|
||||
impl SnippetEdit {
|
||||
fn new(snippets: Vec<Snippet>) -> Self {
|
||||
let mut snippet_ranges = snippets
|
||||
.into_iter()
|
||||
.zip(1..)
|
||||
.with_position()
|
||||
.map(|pos| {
|
||||
let (snippet, index) = match pos {
|
||||
itertools::Position::First(it) | itertools::Position::Middle(it) => it,
|
||||
// last/only snippet gets index 0
|
||||
itertools::Position::Last((snippet, _))
|
||||
| itertools::Position::Only((snippet, _)) => (snippet, 0),
|
||||
};
|
||||
|
||||
let range = match snippet {
|
||||
Snippet::Tabstop(pos) => TextRange::empty(pos),
|
||||
Snippet::Placeholder(range) => range,
|
||||
};
|
||||
(index, range)
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
snippet_ranges.sort_by_key(|(_, range)| range.start());
|
||||
|
||||
// Ensure that none of the ranges overlap
|
||||
let disjoint_ranges =
|
||||
snippet_ranges.windows(2).all(|ranges| ranges[0].1.end() <= ranges[1].1.start());
|
||||
stdx::always!(disjoint_ranges);
|
||||
|
||||
SnippetEdit(snippet_ranges)
|
||||
}
|
||||
|
||||
/// Inserts all of the snippets into the given text.
|
||||
pub fn apply(&self, text: &mut String) {
|
||||
// Start from the back so that we don't have to adjust ranges
|
||||
for (index, range) in self.0.iter().rev() {
|
||||
if range.is_empty() {
|
||||
// is a tabstop
|
||||
text.insert_str(range.start().into(), &format!("${index}"));
|
||||
} else {
|
||||
// is a placeholder
|
||||
text.insert(range.end().into(), '}');
|
||||
text.insert_str(range.start().into(), &format!("${{{index}:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the underlying snippet index + text range
|
||||
/// Tabstops are represented by an empty range, and placeholders use the range that they were given
|
||||
pub fn into_edit_ranges(self) -> Vec<(u32, TextRange)> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SourceChangeBuilder {
|
||||
pub edit: TextEditBuilder,
|
||||
pub file_id: FileId,
|
||||
@ -275,6 +381,16 @@ impl SourceChangeBuilder {
|
||||
|
||||
pub fn finish(mut self) -> SourceChange {
|
||||
self.commit();
|
||||
|
||||
// Only one file can have snippet edits
|
||||
stdx::never!(self
|
||||
.source_change
|
||||
.source_file_edits
|
||||
.iter()
|
||||
.filter(|(_, (_, snippet_edit))| snippet_edit.is_some())
|
||||
.at_most_one()
|
||||
.is_err());
|
||||
|
||||
mem::take(&mut self.source_change)
|
||||
}
|
||||
}
|
||||
@ -296,6 +412,13 @@ impl From<FileSystemEdit> for SourceChange {
|
||||
}
|
||||
}
|
||||
|
||||
enum Snippet {
|
||||
/// A tabstop snippet (e.g. `$0`).
|
||||
Tabstop(TextSize),
|
||||
/// A placeholder snippet (e.g. `${0:placeholder}`).
|
||||
Placeholder(TextRange),
|
||||
}
|
||||
|
||||
enum PlaceSnippet {
|
||||
/// Place a tabstop before an element
|
||||
Before(SyntaxElement),
|
||||
|
@ -49,8 +49,11 @@ fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||
let file_id = *source_change.source_file_edits.keys().next().unwrap();
|
||||
let mut actual = db.file_text(file_id).to_string();
|
||||
|
||||
for edit in source_change.source_file_edits.values() {
|
||||
for (edit, snippet_edit) in source_change.source_file_edits.values() {
|
||||
edit.apply(&mut actual);
|
||||
if let Some(snippet_edit) = snippet_edit {
|
||||
snippet_edit.apply(&mut actual);
|
||||
}
|
||||
}
|
||||
actual
|
||||
};
|
||||
|
@ -367,7 +367,7 @@ mod tests {
|
||||
let mut file_id: Option<FileId> = None;
|
||||
for edit in source_change.source_file_edits {
|
||||
file_id = Some(edit.0);
|
||||
for indel in edit.1.into_iter() {
|
||||
for indel in edit.1 .0.into_iter() {
|
||||
text_edit_builder.replace(indel.delete, indel.insert);
|
||||
}
|
||||
}
|
||||
@ -895,7 +895,8 @@ mod foo$0;
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
1,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo2",
|
||||
@ -903,6 +904,8 @@ mod foo$0;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -944,7 +947,8 @@ use crate::foo$0::FooContent;
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "quux",
|
||||
@ -952,9 +956,12 @@ use crate::foo$0::FooContent;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
FileId(
|
||||
2,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "quux",
|
||||
@ -962,6 +969,8 @@ use crate::foo$0::FooContent;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -997,7 +1006,8 @@ mod fo$0o;
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo2",
|
||||
@ -1005,6 +1015,8 @@ mod fo$0o;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveDir {
|
||||
@ -1047,7 +1059,8 @@ mod outer { mod fo$0o; }
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "bar",
|
||||
@ -1055,6 +1068,8 @@ mod outer { mod fo$0o; }
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -1120,7 +1135,8 @@ pub mod foo$0;
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo2",
|
||||
@ -1128,9 +1144,12 @@ pub mod foo$0;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
FileId(
|
||||
1,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo2",
|
||||
@ -1138,6 +1157,8 @@ pub mod foo$0;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -1187,7 +1208,8 @@ mod quux;
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo2",
|
||||
@ -1195,6 +1217,8 @@ mod quux;
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -1325,7 +1349,8 @@ pub fn baz() {}
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "r#fn",
|
||||
@ -1337,6 +1362,8 @@ pub fn baz() {}
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
@ -1395,7 +1422,8 @@ pub fn baz() {}
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "foo",
|
||||
@ -1407,6 +1435,8 @@ pub fn baz() {}
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [
|
||||
MoveFile {
|
||||
|
@ -126,7 +126,8 @@ mod tests {
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "3",
|
||||
@ -134,6 +135,8 @@ mod tests {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [],
|
||||
is_snippet: false,
|
||||
@ -163,7 +166,8 @@ mod tests {
|
||||
source_file_edits: {
|
||||
FileId(
|
||||
0,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "3",
|
||||
@ -171,9 +175,12 @@ mod tests {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
FileId(
|
||||
1,
|
||||
): TextEdit {
|
||||
): (
|
||||
TextEdit {
|
||||
indels: [
|
||||
Indel {
|
||||
insert: "3",
|
||||
@ -181,6 +188,8 @@ mod tests {
|
||||
},
|
||||
],
|
||||
},
|
||||
None,
|
||||
),
|
||||
},
|
||||
file_system_edits: [],
|
||||
is_snippet: false,
|
||||
|
@ -353,7 +353,8 @@ pub(crate) fn handle_on_type_formatting(
|
||||
};
|
||||
|
||||
// This should be a single-file edit
|
||||
let (_, text_edit) = edit.source_file_edits.into_iter().next().unwrap();
|
||||
let (_, (text_edit, snippet_edit)) = edit.source_file_edits.into_iter().next().unwrap();
|
||||
stdx::never!(snippet_edit.is_none(), "on type formatting shouldn't use structured snippets");
|
||||
|
||||
let change = to_proto::snippet_text_edit_vec(&line_index, edit.is_snippet, text_edit);
|
||||
Ok(Some(change))
|
||||
|
@ -973,7 +973,7 @@ pub(crate) fn snippet_workspace_edit(
|
||||
let ops = snippet_text_document_ops(snap, op)?;
|
||||
document_changes.extend_from_slice(&ops);
|
||||
}
|
||||
for (file_id, edit) in source_change.source_file_edits {
|
||||
for (file_id, (edit, _snippet_edit)) in source_change.source_file_edits {
|
||||
let edit = snippet_text_document_edit(snap, source_change.is_snippet, file_id, edit)?;
|
||||
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user