Ensure a minimum selection width on commands that need it.

pull/376/head
Nathan Vegdahl 3 years ago
parent 7c7be6d583
commit e725957704

@ -308,7 +308,7 @@ impl Selection {
} }
/// Normalizes a `Selection`. /// Normalizes a `Selection`.
pub fn normalize(mut self) -> Self { fn normalize(mut self) -> Self {
let primary = self.ranges[self.primary_index]; let primary = self.ranges[self.primary_index];
self.ranges.sort_unstable_by_key(Range::from); self.ranges.sort_unstable_by_key(Range::from);
self.primary_index = self self.primary_index = self
@ -363,7 +363,12 @@ impl Selection {
*range = f(*range) *range = f(*range)
} }
self self.normalize()
}
/// A convenience short-cut for `transform(|r| r.min_width_1(text))`.
pub fn min_width_1(mut self, text: RopeSlice) -> Self {
self.transform(|r| r.min_width_1(text))
} }
pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a { pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {

@ -718,12 +718,14 @@ fn replace(cx: &mut Context) {
_ => None, _ => None,
}; };
let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().min_width_1(text);
if let Some(ch) = ch { if let Some(ch) = ch {
let transaction = let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { if !range.is_empty() {
let max_to = rope_end_without_line_ending(&doc.text().slice(..)); let text: String =
let to = std::cmp::min(max_to, range.to() + 1); RopeGraphemes::new(doc.text().slice(range.from()..range.to()))
let text: String = RopeGraphemes::new(doc.text().slice(range.from()..to))
.map(|g| { .map(|g| {
let cow: Cow<str> = g.into(); let cow: Cow<str> = g.into();
if str_is_line_ending(&cow) { if str_is_line_ending(&cow) {
@ -734,7 +736,11 @@ fn replace(cx: &mut Context) {
}) })
.collect(); .collect();
(range.from(), to, Some(text.into())) (range.from(), range.to(), Some(text.into()))
} else {
// No change.
(range.from(), range.to(), None)
}
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
@ -1050,23 +1056,17 @@ fn extend_line(cx: &mut Context) {
} }
fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) { fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) {
// first yank the selection let text = doc.text().slice(..);
let values: Vec<String> = doc let selection = doc.selection(view_id).clone().min_width_1(text);
.selection(view_id)
.fragments(doc.text().slice(..))
.map(Cow::into_owned)
.collect();
// first yank the selection
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
reg.write(values); reg.write(values);
// then delete // then delete
let transaction = let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| { let line = text.char_to_line(range.head);
let alltext = doc.text().slice(..); (range.from(), range.to(), None)
let line = alltext.char_to_line(range.head);
let max_to = rope_end_without_line_ending(&alltext);
let to = std::cmp::min(max_to, range.to() + 1);
(range.from(), to, None)
}); });
doc.apply(&transaction, view_id); doc.apply(&transaction, view_id);
} }
@ -1513,11 +1513,13 @@ mod cmd {
match cx.editor.clipboard_provider.get_contents() { match cx.editor.clipboard_provider.get_contents() {
Ok(contents) => { Ok(contents) => {
let selection = doc
.selection(view.id)
.clone()
.min_width_1(doc.text().slice(..));
let transaction = let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { Transaction::change_by_selection(doc.text(), &selection, |range| {
let max_to = rope_end_without_line_ending(&doc.text().slice(..)); (range.from(), range.to(), Some(contents.as_str().into()))
let to = std::cmp::min(max_to, range.to() + 1);
(range.from(), to, Some(contents.as_str().into()))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
@ -2864,17 +2866,18 @@ fn paste_impl(
let mut values = values.iter().cloned().map(Tendril::from).chain(repeat); let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
let text = doc.text(); let text = doc.text();
let selection = doc.selection(view.id).clone().min_width_1(text.slice(..));
let transaction = Transaction::change_by_selection(text, doc.selection(view.id), |range| { let transaction = Transaction::change_by_selection(text, &selection, |range| {
let pos = match (action, linewise) { let pos = match (action, linewise) {
// paste linewise before // paste linewise before
(Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())), (Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())),
// paste linewise after // paste linewise after
(Paste::After, true) => text.line_to_char(text.char_to_line(range.to()) + 1), (Paste::After, true) => text.line_to_char(text.char_to_line(range.to())),
// paste insert // paste insert
(Paste::Before, false) => range.from(), (Paste::Before, false) => range.from(),
// paste append // paste append
(Paste::After, false) => range.to() + 1, (Paste::After, false) => range.to(),
}; };
(pos, pos, Some(values.next().unwrap())) (pos, pos, Some(values.next().unwrap()))
}); });
@ -2914,11 +2917,16 @@ fn replace_with_yanked(cx: &mut Context) {
if let Some(values) = registers.read(reg_name) { if let Some(values) = registers.read(reg_name) {
if let Some(yank) = values.first() { if let Some(yank) = values.first() {
let transaction = let selection = doc
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { .selection(view.id)
let max_to = rope_end_without_line_ending(&doc.text().slice(..)); .clone()
let to = std::cmp::min(max_to, range.to() + 1); .min_width_1(doc.text().slice(..));
(range.from(), to, Some(yank.as_str().into())) let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
if !range.is_empty() {
(range.from(), range.to(), Some(yank.as_str().into()))
} else {
(range.from(), range.to(), None)
}
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
@ -2932,11 +2940,12 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
match editor.clipboard_provider.get_contents() { match editor.clipboard_provider.get_contents() {
Ok(contents) => { Ok(contents) => {
let transaction = let selection = doc
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { .selection(view.id)
let max_to = rope_end_without_line_ending(&doc.text().slice(..)); .clone()
let to = std::cmp::min(max_to, range.to() + 1); .min_width_1(doc.text().slice(..));
(range.from(), to, Some(contents.as_str().into())) let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
(range.from(), range.to(), Some(contents.as_str().into()))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
@ -3575,18 +3584,13 @@ fn surround_add(cx: &mut Context) {
{ {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id); let selection = doc.selection(view.id).clone().min_width_1(text);
let (open, close) = surround::get_pair(ch); let (open, close) = surround::get_pair(ch);
let mut changes = Vec::new(); let mut changes = Vec::new();
for (i, range) in selection.iter().enumerate() { for (i, range) in selection.iter().enumerate() {
let from = range.from(); changes.push((range.from(), range.from(), Some(Tendril::from_char(open))));
let line = text.char_to_line(range.to()); changes.push((range.to(), range.to(), Some(Tendril::from_char(close))));
let max_to = rope_end_without_line_ending(&text);
let to = std::cmp::min(range.to() + 1, max_to);
changes.push((from, from, Some(Tendril::from_char(open))));
changes.push((to, to, Some(Tendril::from_char(close))));
} }
let transaction = Transaction::change(doc.text(), changes.into_iter()); let transaction = Transaction::change(doc.text(), changes.into_iter());
@ -3612,9 +3616,9 @@ fn surround_replace(cx: &mut Context) {
{ {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id); let selection = doc.selection(view.id).clone().min_width_1(text);
let change_pos = match surround::get_surround_pos(text, selection, from, count) let change_pos = match surround::get_surround_pos(text, &selection, from, count)
{ {
Some(c) => c, Some(c) => c,
None => return, None => return,
@ -3646,9 +3650,9 @@ fn surround_delete(cx: &mut Context) {
{ {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id); let selection = doc.selection(view.id).clone().min_width_1(text);
let change_pos = match surround::get_surround_pos(text, selection, ch, count) { let change_pos = match surround::get_surround_pos(text, &selection, ch, count) {
Some(c) => c, Some(c) => c,
None => return, None => return,
}; };

Loading…
Cancel
Save