Ensure editor clipboard contains \n too

This commit is contained in:
Conrad Irwin 2023-08-22 13:51:11 -06:00
parent 33d7fe02ee
commit 27ba77b16f

View file

@ -4766,6 +4766,7 @@ impl Editor {
let mut clipboard_selections = Vec::with_capacity(selections.len()); let mut clipboard_selections = Vec::with_capacity(selections.len());
{ {
let max_point = buffer.max_point(); let max_point = buffer.max_point();
let mut is_first = true;
for selection in &mut selections { for selection in &mut selections {
let is_entire_line = selection.is_empty() || self.selections.line_mode; let is_entire_line = selection.is_empty() || self.selections.line_mode;
if is_entire_line { if is_entire_line {
@ -4773,6 +4774,11 @@ impl Editor {
selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0)); selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0));
selection.goal = SelectionGoal::None; selection.goal = SelectionGoal::None;
} }
if is_first {
is_first = false;
} else {
text += "\n";
}
let mut len = 0; let mut len = 0;
for chunk in buffer.text_for_range(selection.start..selection.end) { for chunk in buffer.text_for_range(selection.start..selection.end) {
text.push_str(chunk); text.push_str(chunk);
@ -4803,6 +4809,7 @@ impl Editor {
let mut clipboard_selections = Vec::with_capacity(selections.len()); let mut clipboard_selections = Vec::with_capacity(selections.len());
{ {
let max_point = buffer.max_point(); let max_point = buffer.max_point();
let mut is_first = true;
for selection in selections.iter() { for selection in selections.iter() {
let mut start = selection.start; let mut start = selection.start;
let mut end = selection.end; let mut end = selection.end;
@ -4811,6 +4818,11 @@ impl Editor {
start = Point::new(start.row, 0); start = Point::new(start.row, 0);
end = cmp::min(max_point, Point::new(end.row + 1, 0)); end = cmp::min(max_point, Point::new(end.row + 1, 0));
} }
if is_first {
is_first = false;
} else {
text += "\n";
}
let mut len = 0; let mut len = 0;
for chunk in buffer.text_for_range(start..end) { for chunk in buffer.text_for_range(start..end) {
text.push_str(chunk); text.push_str(chunk);
@ -4830,7 +4842,7 @@ impl Editor {
pub fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) { pub fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
if let Some(item) = cx.read_from_clipboard() { if let Some(item) = cx.read_from_clipboard() {
let mut clipboard_text = Cow::Borrowed(item.text()); let clipboard_text = Cow::Borrowed(item.text());
if let Some(mut clipboard_selections) = item.metadata::<Vec<ClipboardSelection>>() { if let Some(mut clipboard_selections) = item.metadata::<Vec<ClipboardSelection>>() {
let old_selections = this.selections.all::<usize>(cx); let old_selections = this.selections.all::<usize>(cx);
let all_selections_were_entire_line = let all_selections_were_entire_line =
@ -4838,18 +4850,7 @@ impl Editor {
let first_selection_indent_column = let first_selection_indent_column =
clipboard_selections.first().map(|s| s.first_line_indent); clipboard_selections.first().map(|s| s.first_line_indent);
if clipboard_selections.len() != old_selections.len() { if clipboard_selections.len() != old_selections.len() {
let mut newline_separated_text = String::new(); clipboard_selections.drain(..);
let mut clipboard_selections = clipboard_selections.drain(..).peekable();
let mut ix = 0;
while let Some(clipboard_selection) = clipboard_selections.next() {
newline_separated_text
.push_str(&clipboard_text[ix..ix + clipboard_selection.len]);
ix += clipboard_selection.len;
if clipboard_selections.peek().is_some() {
newline_separated_text.push('\n');
}
}
clipboard_text = Cow::Owned(newline_separated_text);
} }
this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
@ -4865,8 +4866,9 @@ impl Editor {
if let Some(clipboard_selection) = clipboard_selections.get(ix) { if let Some(clipboard_selection) = clipboard_selections.get(ix) {
let end_offset = start_offset + clipboard_selection.len; let end_offset = start_offset + clipboard_selection.len;
to_insert = &clipboard_text[start_offset..end_offset]; to_insert = &clipboard_text[start_offset..end_offset];
dbg!(start_offset, end_offset, &clipboard_text, &to_insert);
entire_line = clipboard_selection.is_entire_line; entire_line = clipboard_selection.is_entire_line;
start_offset = end_offset; start_offset = end_offset + 1;
original_indent_column = original_indent_column =
Some(clipboard_selection.first_line_indent); Some(clipboard_selection.first_line_indent);
} else { } else {