on master help sign in
Ensure a minimum selection width on commands that need it.
commite725957704274b1ec814a34ddf6f75faf35358e7 authorNathan Vegdahl <cessen@cessen.com> dateThu 1 Jul 2021, 09:51 (5 years ago) parent7c7be6d
2 files changed+73−64
helix-core/src/selection.rs+7−2 helix-term/src/commands.rs+66−62
helix-core/src/selection.rs+7−2
@@ -308,7 +308,7 @@ impl Selection {
308308 }
309309
310310 /// Normalizes a `Selection`.
311− pub fn normalize(mut self) -> Self {
311+ fn normalize(mut self) -> Self {
312312 let primary = self.ranges[self.primary_index];
313313 self.ranges.sort_unstable_by_key(Range::from);
314314 self.primary_index = self
@@ -363,7 +363,12 @@ impl Selection {
363363 *range = f(*range)
364364 }
365365
366− self
366+ self.normalize()
367+ }
368+
369+ /// A convenience short-cut for `transform(|r| r.min_width_1(text))`.
370+ pub fn min_width_1(mut self, text: RopeSlice) -> Self {
371+ self.transform(|r| r.min_width_1(text))
367372 }
368373
369374 pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
helix-term/src/commands.rs+66−62
@@ -718,24 +718,30 @@ fn replace(cx: &mut Context) {
718718 _ => None,
719719 };
720720
721− if let Some(ch) = ch {
722− let transaction =
723− Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
724− let max_to = rope_end_without_line_ending(&doc.text().slice(..));
725− let to = std::cmp::min(max_to, range.to() + 1);
726− let text: String = RopeGraphemes::new(doc.text().slice(range.from()..to))
727− .map(|g| {
728− let cow: Cow<str> = g.into();
729− if str_is_line_ending(&cow) {
730− cow
731− } else {
732− ch.into()
733− }
734− })
735− .collect();
721+ let text = doc.text().slice(..);
722+ let selection = doc.selection(view.id).clone().min_width_1(text);
736723
737− (range.from(), to, Some(text.into()))
738− });
724+ if let Some(ch) = ch {
725+ let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
726+ if !range.is_empty() {
727+ let text: String =
728+ RopeGraphemes::new(doc.text().slice(range.from()..range.to()))
729+ .map(|g| {
730+ let cow: Cow<str> = g.into();
731+ if str_is_line_ending(&cow) {
732+ cow
733+ } else {
734+ ch.into()
735+ }
736+ })
737+ .collect();
738+
739+ (range.from(), range.to(), Some(text.into()))
740+ } else {
741+ // No change.
742+ (range.from(), range.to(), None)
743+ }
744+ });
739745
740746 doc.apply(&transaction, view.id);
741747 doc.append_changes_to_history(view.id);
@@ -1050,24 +1056,18 @@ fn extend_line(cx: &mut Context) {
10501056}
10511057
10521058fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) {
1053− // first yank the selection
1054− let values: Vec<String> = doc
1055− .selection(view_id)
1056− .fragments(doc.text().slice(..))
1057− .map(Cow::into_owned)
1058− .collect();
1059+ let text = doc.text().slice(..);
1060+ let selection = doc.selection(view_id).clone().min_width_1(text);
10591061
1062+ // first yank the selection
1063+ let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
10601064 reg.write(values);
10611065
10621066 // then delete
1063− let transaction =
1064− Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1065− let alltext = doc.text().slice(..);
1066− let line = alltext.char_to_line(range.head);
1067− let max_to = rope_end_without_line_ending(&alltext);
1068− let to = std::cmp::min(max_to, range.to() + 1);
1069− (range.from(), to, None)
1070− });
1067+ let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
1068+ let line = text.char_to_line(range.head);
1069+ (range.from(), range.to(), None)
1070+ });
10711071 doc.apply(&transaction, view_id);
10721072}
10731073
@@ -1513,11 +1513,13 @@ mod cmd {
15131513
15141514 match cx.editor.clipboard_provider.get_contents() {
15151515 Ok(contents) => {
1516+ let selection = doc
1517+ .selection(view.id)
1518+ .clone()
1519+ .min_width_1(doc.text().slice(..));
15161520 let transaction =
1517− Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1518− let max_to = rope_end_without_line_ending(&doc.text().slice(..));
1519− let to = std::cmp::min(max_to, range.to() + 1);
1520− (range.from(), to, Some(contents.as_str().into()))
1521+ Transaction::change_by_selection(doc.text(), &selection, |range| {
1522+ (range.from(), range.to(), Some(contents.as_str().into()))
15211523 });
15221524
15231525 doc.apply(&transaction, view.id);
@@ -2864,17 +2866,18 @@ fn paste_impl(
28642866 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
28652867
28662868 let text = doc.text();
2869+ let selection = doc.selection(view.id).clone().min_width_1(text.slice(..));
28672870
2868− let transaction = Transaction::change_by_selection(text, doc.selection(view.id), |range| {
2871+ let transaction = Transaction::change_by_selection(text, &selection, |range| {
28692872 let pos = match (action, linewise) {
28702873 // paste linewise before
28712874 (Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())),
28722875 // paste linewise after
2873− (Paste::After, true) => text.line_to_char(text.char_to_line(range.to()) + 1),
2876+ (Paste::After, true) => text.line_to_char(text.char_to_line(range.to())),
28742877 // paste insert
28752878 (Paste::Before, false) => range.from(),
28762879 // paste append
2877− (Paste::After, false) => range.to() + 1,
2880+ (Paste::After, false) => range.to(),
28782881 };
28792882 (pos, pos, Some(values.next().unwrap()))
28802883 });
@@ -2914,12 +2917,17 @@ fn replace_with_yanked(cx: &mut Context) {
29142917
29152918 if let Some(values) = registers.read(reg_name) {
29162919 if let Some(yank) = values.first() {
2917− let transaction =
2918− Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
2919− let max_to = rope_end_without_line_ending(&doc.text().slice(..));
2920− let to = std::cmp::min(max_to, range.to() + 1);
2921− (range.from(), to, Some(yank.as_str().into()))
2922− });
2920+ let selection = doc
2921+ .selection(view.id)
2922+ .clone()
2923+ .min_width_1(doc.text().slice(..));
2924+ let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
2925+ if !range.is_empty() {
2926+ (range.from(), range.to(), Some(yank.as_str().into()))
2927+ } else {
2928+ (range.from(), range.to(), None)
2929+ }
2930+ });
29232931
29242932 doc.apply(&transaction, view.id);
29252933 doc.append_changes_to_history(view.id);
@@ -2932,12 +2940,13 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
29322940
29332941 match editor.clipboard_provider.get_contents() {
29342942 Ok(contents) => {
2935− let transaction =
2936− Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
2937− let max_to = rope_end_without_line_ending(&doc.text().slice(..));
2938− let to = std::cmp::min(max_to, range.to() + 1);
2939− (range.from(), to, Some(contents.as_str().into()))
2940− });
2943+ let selection = doc
2944+ .selection(view.id)
2945+ .clone()
2946+ .min_width_1(doc.text().slice(..));
2947+ let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
2948+ (range.from(), range.to(), Some(contents.as_str().into()))
2949+ });
29412950
29422951 doc.apply(&transaction, view.id);
29432952 doc.append_changes_to_history(view.id);
@@ -3575,18 +3584,13 @@ fn surround_add(cx: &mut Context) {
35753584 {
35763585 let (view, doc) = current!(cx.editor);
35773586 let text = doc.text().slice(..);
3578− let selection = doc.selection(view.id);
3587+ let selection = doc.selection(view.id).clone().min_width_1(text);
35793588 let (open, close) = surround::get_pair(ch);
35803589
35813590 let mut changes = Vec::new();
35823591 for (i, range) in selection.iter().enumerate() {
3583− let from = range.from();
3584− let line = text.char_to_line(range.to());
3585− let max_to = rope_end_without_line_ending(&text);
3586− let to = std::cmp::min(range.to() + 1, max_to);
3587−
3588− changes.push((from, from, Some(Tendril::from_char(open))));
3589− changes.push((to, to, Some(Tendril::from_char(close))));
3592+ changes.push((range.from(), range.from(), Some(Tendril::from_char(open))));
3593+ changes.push((range.to(), range.to(), Some(Tendril::from_char(close))));
35903594 }
35913595
35923596 let transaction = Transaction::change(doc.text(), changes.into_iter());
@@ -3612,9 +3616,9 @@ fn surround_replace(cx: &mut Context) {
36123616 {
36133617 let (view, doc) = current!(cx.editor);
36143618 let text = doc.text().slice(..);
3615− let selection = doc.selection(view.id);
3619+ let selection = doc.selection(view.id).clone().min_width_1(text);
36163620
3617− let change_pos = match surround::get_surround_pos(text, selection, from, count)
3621+ let change_pos = match surround::get_surround_pos(text, &selection, from, count)
36183622 {
36193623 Some(c) => c,
36203624 None => return,
@@ -3646,9 +3650,9 @@ fn surround_delete(cx: &mut Context) {
36463650 {
36473651 let (view, doc) = current!(cx.editor);
36483652 let text = doc.text().slice(..);
3649− let selection = doc.selection(view.id);
3653+ let selection = doc.selection(view.id).clone().min_width_1(text);
36503654
3651− let change_pos = match surround::get_surround_pos(text, selection, ch, count) {
3655+ let change_pos = match surround::get_surround_pos(text, &selection, ch, count) {
36523656 Some(c) => c,
36533657 None => return,
36543658 };