Record start columns when writing to the clipboard from Zed

This commit is contained in:
Max Brunsfeld
2022-07-28 22:13:32 -07:00
parent 2d05f906f1
commit 7a26fa18c7
5 changed files with 214 additions and 45 deletions
+129 -3
View File
@@ -879,6 +879,7 @@ struct ActiveDiagnosticGroup {
pub struct ClipboardSelection {
pub len: usize,
pub is_entire_line: bool,
pub first_line_indent: u32,
}
#[derive(Debug)]
@@ -1926,7 +1927,7 @@ impl Editor {
old_selections
.iter()
.map(|s| (s.start..s.end, text.clone())),
Some(AutoindentMode::Block),
Some(AutoindentMode::Independent),
cx,
);
anchors
@@ -2368,7 +2369,7 @@ impl Editor {
this.buffer.update(cx, |buffer, cx| {
buffer.edit(
ranges.iter().map(|range| (range.clone(), text)),
Some(AutoindentMode::Block),
Some(AutoindentMode::Independent),
cx,
);
});
@@ -3512,6 +3513,10 @@ impl Editor {
clipboard_selections.push(ClipboardSelection {
len,
is_entire_line,
first_line_indent: cmp::min(
selection.start.column,
buffer.indent_size_for_line(selection.start.row).len,
),
});
}
}
@@ -3549,6 +3554,10 @@ impl Editor {
clipboard_selections.push(ClipboardSelection {
len,
is_entire_line,
first_line_indent: cmp::min(
start.column,
buffer.indent_size_for_line(start.row).len,
),
});
}
}
@@ -3583,18 +3592,22 @@ impl Editor {
let snapshot = buffer.read(cx);
let mut start_offset = 0;
let mut edits = Vec::new();
let mut start_columns = Vec::new();
let line_mode = this.selections.line_mode;
for (ix, selection) in old_selections.iter().enumerate() {
let to_insert;
let entire_line;
let start_column;
if let Some(clipboard_selection) = clipboard_selections.get(ix) {
let end_offset = start_offset + clipboard_selection.len;
to_insert = &clipboard_text[start_offset..end_offset];
entire_line = clipboard_selection.is_entire_line;
start_offset = end_offset;
start_column = clipboard_selection.first_line_indent;
} else {
to_insert = clipboard_text.as_str();
entire_line = all_selections_were_entire_line;
start_column = 0;
}
// If the corresponding selection was empty when this slice of the
@@ -3610,9 +3623,10 @@ impl Editor {
};
edits.push((range, to_insert));
start_columns.push(start_column);
}
drop(snapshot);
buffer.edit(edits, Some(AutoindentMode::Block), cx);
buffer.edit(edits, Some(AutoindentMode::Block { start_columns }), cx);
});
let selections = this.selections.all::<usize>(cx);
@@ -8649,6 +8663,118 @@ mod tests {
t|he lazy dog"});
}
#[gpui::test]
async fn test_paste_multiline(cx: &mut gpui::TestAppContext) {
let mut cx = EditorTestContext::new(cx).await;
let language = Arc::new(Language::new(
LanguageConfig::default(),
Some(tree_sitter_rust::language()),
));
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
// Cut an indented block, without the leading whitespace.
cx.set_state(indoc! {"
const a = (
b(),
[c(
d,
e
)}
);
"});
cx.update_editor(|e, cx| e.cut(&Cut, cx));
cx.assert_editor_state(indoc! {"
const a = (
b(),
|
);
"});
// Paste it at the same position.
cx.update_editor(|e, cx| e.paste(&Paste, cx));
cx.assert_editor_state(indoc! {"
const a = (
b(),
c(
d,
e
)|
);
"});
// Paste it at a line with a lower indent level.
cx.update_editor(|e, cx| e.paste(&Paste, cx));
cx.set_state(indoc! {"
|
const a = (
b(),
);
"});
cx.update_editor(|e, cx| e.paste(&Paste, cx));
cx.assert_editor_state(indoc! {"
c(
d,
e
)|
const a = (
b(),
);
"});
// Cut an indented block, with the leading whitespace.
cx.set_state(indoc! {"
const a = (
b(),
[ c(
d,
e
)
});
"});
cx.update_editor(|e, cx| e.cut(&Cut, cx));
cx.assert_editor_state(indoc! {"
const a = (
b(),
|);
"});
// Paste it at the same position.
cx.update_editor(|e, cx| e.paste(&Paste, cx));
cx.assert_editor_state(indoc! {"
const a = (
b(),
c(
d,
e
)
|);
"});
// Paste it at a line with a higher indent level.
cx.set_state(indoc! {"
const a = (
b(),
c(
d,
e|
)
);
"});
cx.update_editor(|e, cx| e.paste(&Paste, cx));
cx.set_state(indoc! {"
const a = (
b(),
c(
d,
ec(
d,
e
)|
)
);
"});
}
#[gpui::test]
fn test_select_all(cx: &mut gpui::MutableAppContext) {
cx.set_global(Settings::test(cx));
+39 -12
View File
@@ -305,7 +305,7 @@ impl MultiBuffer {
pub fn edit<I, S, T>(
&mut self,
edits: I,
autoindent_mode: Option<AutoindentMode>,
mut autoindent_mode: Option<AutoindentMode>,
cx: &mut ModelContext<Self>,
) where
I: IntoIterator<Item = (Range<S>, T)>,
@@ -331,11 +331,17 @@ impl MultiBuffer {
});
}
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, Arc<str>, bool)>> =
let indent_start_columns = match &mut autoindent_mode {
Some(AutoindentMode::Block { start_columns }) => mem::take(start_columns),
_ => Default::default(),
};
let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, Arc<str>, bool, u32)>> =
Default::default();
let mut cursor = snapshot.excerpts.cursor::<usize>();
for (range, new_text) in edits {
for (ix, (range, new_text)) in edits.enumerate() {
let new_text: Arc<str> = new_text.into();
let start_column = indent_start_columns.get(ix).copied().unwrap_or(0);
cursor.seek(&range.start, Bias::Right, &());
if cursor.item().is_none() && range.start == *cursor.start() {
cursor.prev(&());
@@ -366,7 +372,7 @@ impl MultiBuffer {
buffer_edits
.entry(start_excerpt.buffer_id)
.or_insert(Vec::new())
.push((buffer_start..buffer_end, new_text, true));
.push((buffer_start..buffer_end, new_text, true, start_column));
} else {
let start_excerpt_range = buffer_start
..start_excerpt
@@ -383,11 +389,11 @@ impl MultiBuffer {
buffer_edits
.entry(start_excerpt.buffer_id)
.or_insert(Vec::new())
.push((start_excerpt_range, new_text.clone(), true));
.push((start_excerpt_range, new_text.clone(), true, start_column));
buffer_edits
.entry(end_excerpt.buffer_id)
.or_insert(Vec::new())
.push((end_excerpt_range, new_text.clone(), false));
.push((end_excerpt_range, new_text.clone(), false, start_column));
cursor.seek(&range.start, Bias::Right, &());
cursor.next(&());
@@ -402,6 +408,7 @@ impl MultiBuffer {
excerpt.range.context.to_offset(&excerpt.buffer),
new_text.clone(),
false,
start_column,
));
cursor.next(&());
}
@@ -409,19 +416,21 @@ impl MultiBuffer {
}
for (buffer_id, mut edits) in buffer_edits {
edits.sort_unstable_by_key(|(range, _, _)| range.start);
edits.sort_unstable_by_key(|(range, _, _, _)| range.start);
self.buffers.borrow()[&buffer_id]
.buffer
.update(cx, |buffer, cx| {
let mut edits = edits.into_iter().peekable();
let mut insertions = Vec::new();
let mut insertion_start_columns = Vec::new();
let mut deletions = Vec::new();
let empty_str: Arc<str> = "".into();
while let Some((mut range, new_text, mut is_insertion)) = edits.next() {
while let Some((next_range, _, next_is_insertion)) = edits.peek() {
while let Some((mut range, new_text, mut is_insertion, start_column)) =
edits.next()
{
while let Some((next_range, _, next_is_insertion, _)) = edits.peek() {
if range.end >= next_range.start {
range.end = cmp::max(next_range.end, range.end);
is_insertion |= *next_is_insertion;
edits.next();
} else {
@@ -430,6 +439,7 @@ impl MultiBuffer {
}
if is_insertion {
insertion_start_columns.push(start_column);
insertions.push((
buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
new_text.clone(),
@@ -442,8 +452,25 @@ impl MultiBuffer {
}
}
buffer.edit(deletions, autoindent_mode, cx);
buffer.edit(insertions, autoindent_mode, cx);
let deletion_autoindent_mode =
if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
Some(AutoindentMode::Block {
start_columns: Default::default(),
})
} else {
None
};
let insertion_autoindent_mode =
if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
Some(AutoindentMode::Block {
start_columns: insertion_start_columns,
})
} else {
None
};
buffer.edit(deletions, deletion_autoindent_mode, cx);
buffer.edit(insertions, insertion_autoindent_mode, cx);
})
}
}