on master help sign in
Separate jump behavior from increment/decrement (#4123)

increment/decrement (C-a/C-x) had some buggy behavior where selections could be offset incorrectly or the editor could panic with some edits that changed the number of characters in a number or date. These stemmed from the automatic jumping behavior which attempted to find the next date or integer to increment. The jumping behavior also complicated the code quite a bit and made the behavior somewhat difficult to predict when using many cursors. This change removes the automatic jumping behavior and only increments or decrements when the full text in a range of a selection is a number or date. This simplifies the code and fixes the panics and buggy behaviors from changing the number of characters.

commit60f84be40c1c488dacf823f791ca33f43b5d28d8 authorgreg-enbala <66078183+greg-enbala@users.noreply.github.com> dateMon 16 Jan 2023, 11:15 (4 years ago) parent97083f8
5 files changed+349−839
helix-core/src/increment/date_time.rs+75−246 helix-core/src/increment/integer.rs+235 helix-core/src/increment/mod.rs+7−5 helix-core/src/increment/number.rs−507 helix-term/src/commands.rs+32−81
helix-core/src/increment/date_time.rs+75−246
@@ -1,114 +1,53 @@
1−use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
1+use chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime};
22use once_cell::sync::Lazy;
33use regex::Regex;
4−use ropey::RopeSlice;
5−
6−use std::borrow::Cow;
7−use std::cmp;
84use std::fmt::Write;
95
10−use super::Increment;
11−use crate::{Range, Tendril};
6+/// Increment a Date or DateTime
7+///
8+/// If just a Date is selected the day will be incremented.
9+/// If a DateTime is selected the second will be incremented.
10+pub fn increment(selected_text: &str, amount: i64) -> Option<String> {
11+ if selected_text.is_empty() {
12+ return None;
13+ }
1214
13−#[derive(Debug, PartialEq, Eq)]
14−pub struct DateTimeIncrementor {
15− date_time: NaiveDateTime,
16− range: Range,
17− fmt: &'static str,
18− field: DateField,
19−}
15+ FORMATS.iter().find_map(|format| {
16+ let captures = format.regex.captures(selected_text)?;
17+ if captures.len() - 1 != format.fields.len() {
18+ return None;
19+ }
2020
21−impl DateTimeIncrementor {
22− pub fn from_range(text: RopeSlice, range: Range) -> Option<DateTimeIncrementor> {
23− let range = if range.is_empty() {
24− if range.anchor < text.len_chars() {
25− // Treat empty range as a cursor range.
26− range.put_cursor(text, range.anchor + 1, true)
27− } else {
28− // The range is empty and at the end of the text.
29− return None;
21+ let date_time = captures.get(0)?;
22+ let has_date = format.fields.iter().any(|f| f.unit.is_date());
23+ let has_time = format.fields.iter().any(|f| f.unit.is_time());
24+ let date_time = &selected_text[date_time.start()..date_time.end()];
25+ match (has_date, has_time) {
26+ (true, true) => {
27+ let date_time = NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?;
28+ Some(
29+ date_time
30+ .checked_add_signed(Duration::minutes(amount))?
31+ .format(format.fmt)
32+ .to_string(),
33+ )
3034 }
31− } else {
32− range
33− };
34−
35− FORMATS.iter().find_map(|format| {
36− let from = range.from().saturating_sub(format.max_len);
37− let to = (range.from() + format.max_len).min(text.len_chars());
38−
39− let (from_in_text, to_in_text) = (range.from() - from, range.to() - from);
40− let text: Cow<str> = text.slice(from..to).into();
41−
42− let captures = format.regex.captures(&text)?;
43− if captures.len() - 1 != format.fields.len() {
44− return None;
35+ (true, false) => {
36+ let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?;
37+ Some(
38+ date.checked_add_signed(Duration::days(amount))?
39+ .format(format.fmt)
40+ .to_string(),
41+ )
4542 }
46−
47− let date_time = captures.get(0)?;
48− let offset = range.from() - from_in_text;
49− let range = Range::new(date_time.start() + offset, date_time.end() + offset);
50−
51− let field = captures
52− .iter()
53− .skip(1)
54− .enumerate()
55− .find_map(|(i, capture)| {
56− let capture = capture?;
57− let capture_range = capture.range();
58−
59− if capture_range.contains(&from_in_text)
60− && capture_range.contains(&(to_in_text - 1))
61− {
62− Some(format.fields[i])
63− } else {
64− None
65− }
66− })?;
67−
68− let has_date = format.fields.iter().any(|f| f.unit.is_date());
69− let has_time = format.fields.iter().any(|f| f.unit.is_time());
70−
71− let date_time = &text[date_time.start()..date_time.end()];
72− let date_time = match (has_date, has_time) {
73− (true, true) => NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?,
74− (true, false) => {
75− let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?;
76−
77− date.and_hms_opt(0, 0, 0).unwrap()
78− }
79− (false, true) => {
80− let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?;
81−
82− NaiveDate::from_ymd_opt(0, 1, 1).unwrap().and_time(time)
83− }
84− (false, false) => return None,
85− };
86−
87− Some(DateTimeIncrementor {
88− date_time,
89− range,
90− fmt: format.fmt,
91− field,
92− })
93− })
94− }
95−}
96−
97−impl Increment for DateTimeIncrementor {
98− fn increment(&self, amount: i64) -> (Range, Tendril) {
99− let date_time = match self.field.unit {
100− DateUnit::Years => add_years(self.date_time, amount),
101− DateUnit::Months => add_months(self.date_time, amount),
102− DateUnit::Days => add_duration(self.date_time, Duration::days(amount)),
103− DateUnit::Hours => add_duration(self.date_time, Duration::hours(amount)),
104− DateUnit::Minutes => add_duration(self.date_time, Duration::minutes(amount)),
105− DateUnit::Seconds => add_duration(self.date_time, Duration::seconds(amount)),
106− DateUnit::AmPm => toggle_am_pm(self.date_time),
43+ (false, true) => {
44+ let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?;
45+ let (adjusted_time, _) = time.overflowing_add_signed(Duration::minutes(amount));
46+ Some(adjusted_time.format(format.fmt).to_string())
47+ }
48+ (false, false) => None,
10749 }
108− .unwrap_or(self.date_time);
109−
110− (self.range, date_time.format(self.fmt).to_string().into())
111− }
50+ })
11251}
11352
11453static FORMATS: Lazy<Vec<Format>> = Lazy::new(|| {
@@ -144,7 +83,7 @@ impl Format {
14483 fn new(fmt: &'static str) -> Self {
14584 let mut remaining = fmt;
14685 let mut fields = Vec::new();
147− let mut regex = String::new();
86+ let mut regex = "^".to_string();
14887 let mut max_len = 0;
14988
15089 while let Some(i) = remaining.find('%') {
@@ -166,6 +105,7 @@ impl Format {
166105 write!(regex, "({})", field.regex).unwrap();
167106 remaining = &after[spec_len..];
168107 }
108+ regex += "$";
169109
170110 let regex = Regex::new(&regex).unwrap();
171111
@@ -305,155 +245,47 @@ impl DateUnit {
305245 }
306246}
307247
308−fn ndays_in_month(year: i32, month: u32) -> u32 {
309− // The first day of the next month...
310− let (y, m) = if month == 12 {
311− (year + 1, 1)
312− } else {
313− (year, month + 1)
314− };
315− let d = NaiveDate::from_ymd_opt(y, m, 1).unwrap();
316−
317− // ...is preceded by the last day of the original month.
318− d.pred_opt().unwrap().day()
319−}
320−
321−fn add_months(date_time: NaiveDateTime, amount: i64) -> Option<NaiveDateTime> {
322− let month = (date_time.month0() as i64).checked_add(amount)?;
323− let year = date_time.year() + i32::try_from(month / 12).ok()?;
324− let year = if month.is_negative() { year - 1 } else { year };
325−
326− // Normalize month
327− let month = month % 12;
328− let month = if month.is_negative() {
329− month + 12
330− } else {
331− month
332− } as u32
333− + 1;
334−
335− let day = cmp::min(date_time.day(), ndays_in_month(year, month));
336−
337− NaiveDate::from_ymd_opt(year, month, day).map(|date| date.and_time(date_time.time()))
338−}
339−
340−fn add_years(date_time: NaiveDateTime, amount: i64) -> Option<NaiveDateTime> {
341− let year = i32::try_from((date_time.year() as i64).checked_add(amount)?).ok()?;
342− let ndays = ndays_in_month(year, date_time.month());
343−
344− if date_time.day() > ndays {
345− NaiveDate::from_ymd_opt(year, date_time.month(), ndays)
346− .and_then(|date| date.succ_opt().map(|date| date.and_time(date_time.time())))
347− } else {
348− date_time.with_year(year)
349− }
350−}
351−
352−fn add_duration(date_time: NaiveDateTime, duration: Duration) -> Option<NaiveDateTime> {
353− date_time.checked_add_signed(duration)
354−}
355−
356−fn toggle_am_pm(date_time: NaiveDateTime) -> Option<NaiveDateTime> {
357− if date_time.hour() < 12 {
358− add_duration(date_time, Duration::hours(12))
359− } else {
360− add_duration(date_time, Duration::hours(-12))
361− }
362−}
363−
364248#[cfg(test)]
365249mod test {
366250 use super::*;
367− use crate::Rope;
368251
369252 #[test]
370253 fn test_increment_date_times() {
371254 let tests = [
372255 // (original, cursor, amount, expected)
373− ("2020-02-28", 0, 1, "2021-02-28"),
374− ("2020-02-29", 0, 1, "2021-03-01"),
375− ("2020-01-31", 5, 1, "2020-02-29"),
376− ("2020-01-20", 5, 1, "2020-02-20"),
377− ("2021-01-01", 5, -1, "2020-12-01"),
378− ("2021-01-31", 5, -2, "2020-11-30"),
379− ("2020-02-28", 8, 1, "2020-02-29"),
380− ("2021-02-28", 8, 1, "2021-03-01"),
381− ("2021-02-28", 0, -1, "2020-02-28"),
382− ("2021-03-01", 0, -1, "2020-03-01"),
383− ("2020-02-29", 5, -1, "2020-01-29"),
384− ("2020-02-20", 5, -1, "2020-01-20"),
385− ("2020-02-29", 8, -1, "2020-02-28"),
386− ("2021-03-01", 8, -1, "2021-02-28"),
387− ("1980/12/21", 8, 100, "1981/03/31"),
388− ("1980/12/21", 8, -100, "1980/09/12"),
389− ("1980/12/21", 8, 1000, "1983/09/17"),
390− ("1980/12/21", 8, -1000, "1978/03/27"),
391− ("2021-11-24 07:12:23", 0, 1, "2022-11-24 07:12:23"),
392− ("2021-11-24 07:12:23", 5, 1, "2021-12-24 07:12:23"),
393− ("2021-11-24 07:12:23", 8, 1, "2021-11-25 07:12:23"),
394− ("2021-11-24 07:12:23", 11, 1, "2021-11-24 08:12:23"),
395− ("2021-11-24 07:12:23", 14, 1, "2021-11-24 07:13:23"),
396− ("2021-11-24 07:12:23", 17, 1, "2021-11-24 07:12:24"),
397− ("2021/11/24 07:12:23", 0, 1, "2022/11/24 07:12:23"),
398− ("2021/11/24 07:12:23", 5, 1, "2021/12/24 07:12:23"),
399− ("2021/11/24 07:12:23", 8, 1, "2021/11/25 07:12:23"),
400− ("2021/11/24 07:12:23", 11, 1, "2021/11/24 08:12:23"),
401− ("2021/11/24 07:12:23", 14, 1, "2021/11/24 07:13:23"),
402− ("2021/11/24 07:12:23", 17, 1, "2021/11/24 07:12:24"),
403− ("2021-11-24 07:12", 0, 1, "2022-11-24 07:12"),
404− ("2021-11-24 07:12", 5, 1, "2021-12-24 07:12"),
405− ("2021-11-24 07:12", 8, 1, "2021-11-25 07:12"),
406− ("2021-11-24 07:12", 11, 1, "2021-11-24 08:12"),
407− ("2021-11-24 07:12", 14, 1, "2021-11-24 07:13"),
408− ("2021/11/24 07:12", 0, 1, "2022/11/24 07:12"),
409− ("2021/11/24 07:12", 5, 1, "2021/12/24 07:12"),
410− ("2021/11/24 07:12", 8, 1, "2021/11/25 07:12"),
411− ("2021/11/24 07:12", 11, 1, "2021/11/24 08:12"),
412− ("2021/11/24 07:12", 14, 1, "2021/11/24 07:13"),
413− ("Wed Nov 24 2021", 0, 1, "Thu Nov 25 2021"),
414− ("Wed Nov 24 2021", 4, 1, "Fri Dec 24 2021"),
415− ("Wed Nov 24 2021", 8, 1, "Thu Nov 25 2021"),
416− ("Wed Nov 24 2021", 11, 1, "Thu Nov 24 2022"),
417− ("24-Nov-2021", 0, 1, "25-Nov-2021"),
418− ("24-Nov-2021", 3, 1, "24-Dec-2021"),
419− ("24-Nov-2021", 7, 1, "24-Nov-2022"),
420− ("2021 Nov 24", 0, 1, "2022 Nov 24"),
421− ("2021 Nov 24", 5, 1, "2021 Dec 24"),
422− ("2021 Nov 24", 9, 1, "2021 Nov 25"),
423− ("Nov 24, 2021", 0, 1, "Dec 24, 2021"),
424− ("Nov 24, 2021", 4, 1, "Nov 25, 2021"),
425− ("Nov 24, 2021", 8, 1, "Nov 24, 2022"),
426− ("7:21:53 am", 0, 1, "8:21:53 am"),
427− ("7:21:53 am", 3, 1, "7:22:53 am"),
428− ("7:21:53 am", 5, 1, "7:21:54 am"),
429− ("7:21:53 am", 8, 1, "7:21:53 pm"),
430− ("7:21:53 AM", 0, 1, "8:21:53 AM"),
431− ("7:21:53 AM", 3, 1, "7:22:53 AM"),
432− ("7:21:53 AM", 5, 1, "7:21:54 AM"),
433− ("7:21:53 AM", 8, 1, "7:21:53 PM"),
434− ("7:21 am", 0, 1, "8:21 am"),
435− ("7:21 am", 3, 1, "7:22 am"),
436− ("7:21 am", 5, 1, "7:21 pm"),
437− ("7:21 AM", 0, 1, "8:21 AM"),
438− ("7:21 AM", 3, 1, "7:22 AM"),
439− ("7:21 AM", 5, 1, "7:21 PM"),
440− ("23:24:23", 1, 1, "00:24:23"),
441− ("23:24:23", 3, 1, "23:25:23"),
442− ("23:24:23", 6, 1, "23:24:24"),
443− ("23:24", 1, 1, "00:24"),
444− ("23:24", 3, 1, "23:25"),
256+ ("2020-02-28", 1, "2020-02-29"),
257+ ("2020-02-29", 1, "2020-03-01"),
258+ ("2020-01-31", 1, "2020-02-01"),
259+ ("2020-01-20", 1, "2020-01-21"),
260+ ("2021-01-01", -1, "2020-12-31"),
261+ ("2021-01-31", -2, "2021-01-29"),
262+ ("2020-02-28", 1, "2020-02-29"),
263+ ("2021-02-28", 1, "2021-03-01"),
264+ ("2021-03-01", -1, "2021-02-28"),
265+ ("2020-02-29", -1, "2020-02-28"),
266+ ("2020-02-20", -1, "2020-02-19"),
267+ ("2021-03-01", -1, "2021-02-28"),
268+ ("1980/12/21", 100, "1981/03/31"),
269+ ("1980/12/21", -100, "1980/09/12"),
270+ ("1980/12/21", 1000, "1983/09/17"),
271+ ("1980/12/21", -1000, "1978/03/27"),
272+ ("2021-11-24 07:12:23", 1, "2021-11-24 07:13:23"),
273+ ("2021-11-24 07:12", 1, "2021-11-24 07:13"),
274+ ("Wed Nov 24 2021", 1, "Thu Nov 25 2021"),
275+ ("24-Nov-2021", 1, "25-Nov-2021"),
276+ ("2021 Nov 24", 1, "2021 Nov 25"),
277+ ("Nov 24, 2021", 1, "Nov 25, 2021"),
278+ ("7:21:53 am", 1, "7:22:53 am"),
279+ ("7:21:53 AM", 1, "7:22:53 AM"),
280+ ("7:21 am", 1, "7:22 am"),
281+ ("23:24:23", 1, "23:25:23"),
282+ ("23:24", 1, "23:25"),
283+ ("23:59", 1, "00:00"),
284+ ("23:59:59", 1, "00:00:59"),
445285 ];
446286
447− for (original, cursor, amount, expected) in tests {
448− let rope = Rope::from_str(original);
449− let range = Range::new(cursor, cursor + 1);
450− assert_eq!(
451− DateTimeIncrementor::from_range(rope.slice(..), range)
452− .unwrap()
453− .increment(amount)
454− .1,
455− Tendril::from(expected)
456− );
287+ for (original, amount, expected) in tests {
288+ assert_eq!(increment(original, amount).unwrap(), expected);
457289 }
458290 }
459291
@@ -482,10 +314,7 @@ mod test {
482314 ];
483315
484316 for invalid in tests {
485− let rope = Rope::from_str(invalid);
486− let range = Range::new(0, 1);
487−
488− assert_eq!(DateTimeIncrementor::from_range(rope.slice(..), range), None)
317+ assert_eq!(increment(invalid, 1), None)
489318 }
490319 }
491320}
helix-core/src/increment/integer.rsnew file+235
@@ -0,0 +1,235 @@
1+const SEPARATOR: char = '_';
2+
3+/// Increment an integer.
4+///
5+/// Supported bases:
6+/// 2 with prefix 0b
7+/// 8 with prefix 0o
8+/// 10 with no prefix
9+/// 16 with prefix 0x
10+///
11+/// An integer can contain `_` as a separator but may not start or end with a separator.
12+/// Base 10 integers can go negative, but bases 2, 8, and 16 cannot.
13+/// All addition and subtraction is saturating.
14+pub fn increment(selected_text: &str, amount: i64) -> Option<String> {
15+ if selected_text.is_empty()
16+ || selected_text.ends_with(SEPARATOR)
17+ || selected_text.starts_with(SEPARATOR)
18+ {
19+ return None;
20+ }
21+
22+ let radix = if selected_text.starts_with("0x") {
23+ 16
24+ } else if selected_text.starts_with("0o") {
25+ 8
26+ } else if selected_text.starts_with("0b") {
27+ 2
28+ } else {
29+ 10
30+ };
31+
32+ // Get separator indexes from right to left.
33+ let separator_rtl_indexes: Vec<usize> = selected_text
34+ .chars()
35+ .rev()
36+ .enumerate()
37+ .filter_map(|(i, c)| if c == SEPARATOR { Some(i) } else { None })
38+ .collect();
39+
40+ let word: String = selected_text.chars().filter(|&c| c != SEPARATOR).collect();
41+
42+ let mut new_text = if radix == 10 {
43+ let number = &word;
44+ let value = i128::from_str_radix(number, radix).ok()?;
45+ let new_value = value.saturating_add(amount as i128);
46+
47+ let format_length = match (value.is_negative(), new_value.is_negative()) {
48+ (true, false) => number.len() - 1,
49+ (false, true) => number.len() + 1,
50+ _ => number.len(),
51+ } - separator_rtl_indexes.len();
52+
53+ if number.starts_with('0') || number.starts_with("-0") {
54+ format!("{:01$}", new_value, format_length)
55+ } else {
56+ format!("{}", new_value)
57+ }
58+ } else {
59+ let number = &word[2..];
60+ let value = u128::from_str_radix(number, radix).ok()?;
61+ let new_value = (value as i128).saturating_add(amount as i128);
62+ let new_value = if new_value < 0 { 0 } else { new_value };
63+ let format_length = selected_text.len() - 2 - separator_rtl_indexes.len();
64+
65+ match radix {
66+ 2 => format!("0b{:01$b}", new_value, format_length),
67+ 8 => format!("0o{:01$o}", new_value, format_length),
68+ 16 => {
69+ let (lower_count, upper_count): (usize, usize) =
70+ number.chars().fold((0, 0), |(lower, upper), c| {
71+ (
72+ lower + c.is_ascii_lowercase().then(|| 1).unwrap_or(0),
73+ upper + c.is_ascii_uppercase().then(|| 1).unwrap_or(0),
74+ )
75+ });
76+ if upper_count > lower_count {
77+ format!("0x{:01$X}", new_value, format_length)
78+ } else {
79+ format!("0x{:01$x}", new_value, format_length)
80+ }
81+ }
82+ _ => unimplemented!("radix not supported: {}", radix),
83+ }
84+ };
85+
86+ // Add separators from original number.
87+ for &rtl_index in &separator_rtl_indexes {
88+ if rtl_index < new_text.len() {
89+ let new_index = new_text.len().saturating_sub(rtl_index);
90+ if new_index > 0 {
91+ new_text.insert(new_index, SEPARATOR);
92+ }
93+ }
94+ }
95+
96+ // Add in additional separators if necessary.
97+ if new_text.len() > selected_text.len() && !separator_rtl_indexes.is_empty() {
98+ let spacing = match separator_rtl_indexes.as_slice() {
99+ [.., b, a] => a - b - 1,
100+ _ => separator_rtl_indexes[0],
101+ };
102+
103+ let prefix_length = if radix == 10 { 0 } else { 2 };
104+ if let Some(mut index) = new_text.find(SEPARATOR) {
105+ while index - prefix_length > spacing {
106+ index -= spacing;
107+ new_text.insert(index, SEPARATOR);
108+ }
109+ }
110+ }
111+
112+ Some(new_text)
113+}
114+
115+#[cfg(test)]
116+mod test {
117+ use super::*;
118+
119+ #[test]
120+ fn test_increment_basic_decimal_numbers() {
121+ let tests = [
122+ ("100", 1, "101"),
123+ ("100", -1, "99"),
124+ ("99", 1, "100"),
125+ ("100", 1000, "1100"),
126+ ("100", -1000, "-900"),
127+ ("-1", 1, "0"),
128+ ("-1", 2, "1"),
129+ ("1", -1, "0"),
130+ ("1", -2, "-1"),
131+ ];
132+
133+ for (original, amount, expected) in tests {
134+ assert_eq!(increment(original, amount).unwrap(), expected);
135+ }
136+ }
137+
138+ #[test]
139+ fn test_increment_basic_hexadecimal_numbers() {
140+ let tests = [
141+ ("0x0100", 1, "0x0101"),
142+ ("0x0100", -1, "0x00ff"),
143+ ("0x0001", -1, "0x0000"),
144+ ("0x0000", -1, "0x0000"),
145+ ("0xffffffffffffffff", 1, "0x10000000000000000"),
146+ ("0xffffffffffffffff", 2, "0x10000000000000001"),
147+ ("0xffffffffffffffff", -1, "0xfffffffffffffffe"),
148+ ("0xABCDEF1234567890", 1, "0xABCDEF1234567891"),
149+ ("0xabcdef1234567890", 1, "0xabcdef1234567891"),
150+ ];
151+
152+ for (original, amount, expected) in tests {
153+ assert_eq!(increment(original, amount).unwrap(), expected);
154+ }
155+ }
156+
157+ #[test]
158+ fn test_increment_basic_octal_numbers() {
159+ let tests = [
160+ ("0o0107", 1, "0o0110"),
161+ ("0o0110", -1, "0o0107"),
162+ ("0o0001", -1, "0o0000"),
163+ ("0o7777", 1, "0o10000"),
164+ ("0o1000", -1, "0o0777"),
165+ ("0o0107", 10, "0o0121"),
166+ ("0o0000", -1, "0o0000"),
167+ ("0o1777777777777777777777", 1, "0o2000000000000000000000"),
168+ ("0o1777777777777777777777", 2, "0o2000000000000000000001"),
169+ ("0o1777777777777777777777", -1, "0o1777777777777777777776"),
170+ ];
171+
172+ for (original, amount, expected) in tests {
173+ assert_eq!(increment(original, amount).unwrap(), expected);
174+ }
175+ }
176+
177+ #[test]
178+ fn test_increment_basic_binary_numbers() {
179+ let tests = [
180+ ("0b00000100", 1, "0b00000101"),
181+ ("0b00000100", -1, "0b00000011"),
182+ ("0b00000100", 2, "0b00000110"),
183+ ("0b00000100", -2, "0b00000010"),
184+ ("0b00000001", -1, "0b00000000"),
185+ ("0b00111111", 10, "0b01001001"),
186+ ("0b11111111", 1, "0b100000000"),
187+ ("0b10000000", -1, "0b01111111"),
188+ ("0b0000", -1, "0b0000"),
189+ (
190+ "0b1111111111111111111111111111111111111111111111111111111111111111",
191+ 1,
192+ "0b10000000000000000000000000000000000000000000000000000000000000000",
193+ ),
194+ (
195+ "0b1111111111111111111111111111111111111111111111111111111111111111",
196+ 2,
197+ "0b10000000000000000000000000000000000000000000000000000000000000001",
198+ ),
199+ (
200+ "0b1111111111111111111111111111111111111111111111111111111111111111",
201+ -1,
202+ "0b1111111111111111111111111111111111111111111111111111111111111110",
203+ ),
204+ ];
205+
206+ for (original, amount, expected) in tests {
207+ assert_eq!(increment(original, amount).unwrap(), expected);
208+ }
209+ }
210+
211+ #[test]
212+ fn test_increment_with_separators() {
213+ let tests = [
214+ ("999_999", 1, "1_000_000"),
215+ ("1_000_000", -1, "999_999"),
216+ ("-999_999", -1, "-1_000_000"),
217+ ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
218+ ("0x0000_0000", -1, "0x0000_0000"),
219+ ("0x0000_0000_0000", -1, "0x0000_0000_0000"),
220+ ("0b01111111_11111111", 1, "0b10000000_00000000"),
221+ ("0b11111111_11111111", 1, "0b1_00000000_00000000"),
222+ ];
223+
224+ for (original, amount, expected) in tests {
225+ assert_eq!(increment(original, amount).unwrap(), expected);
226+ }
227+ }
228+
229+ #[test]
230+ fn test_leading_and_trailing_separators_arent_a_match() {
231+ assert_eq!(increment("9_", 1), None);
232+ assert_eq!(increment("_9", 1), None);
233+ assert_eq!(increment("_9_", 1), None);
234+ }
235+}
helix-core/src/increment/mod.rs+7−5
@@ -1,8 +1,10 @@
1−pub mod date_time;
2−pub mod number;
1+mod date_time;
2+mod integer;
33
4−use crate::{Range, Tendril};
4+pub fn integer(selected_text: &str, amount: i64) -> Option<String> {
5+ integer::increment(selected_text, amount)
6+}
57
6−pub trait Increment {
7− fn increment(&self, amount: i64) -> (Range, Tendril);
8+pub fn date_time(selected_text: &str, amount: i64) -> Option<String> {
9+ date_time::increment(selected_text, amount)
810}
helix-core/src/increment/number.rsdeleted−507
@@ -1,507 +0,0 @@
1−use std::borrow::Cow;
2−
3−use ropey::RopeSlice;
4−
5−use super::Increment;
6−
7−use crate::{
8− textobject::{textobject_word, TextObject},
9− Range, Tendril,
10−};
11−
12−#[derive(Debug, PartialEq, Eq)]
13−pub struct NumberIncrementor<'a> {
14− value: i64,
15− radix: u32,
16− range: Range,
17−
18− text: RopeSlice<'a>,
19−}
20−
21−impl<'a> NumberIncrementor<'a> {
22− /// Return information about number under rang if there is one.
23− pub fn from_range(text: RopeSlice, range: Range) -> Option<NumberIncrementor> {
24− // If the cursor is on the minus sign of a number we want to get the word textobject to the
25− // right of it.
26− let range = if range.to() < text.len_chars()
27− && range.to() - range.from() <= 1
28− && text.char(range.from()) == '-'
29− {
30− Range::new(range.from() + 1, range.to() + 1)
31− } else {
32− range
33− };
34−
35− let range = textobject_word(text, range, TextObject::Inside, 1, false);
36−
37− // If there is a minus sign to the left of the word object, we want to include it in the range.
38− let range = if range.from() > 0 && text.char(range.from() - 1) == '-' {
39− range.extend(range.from() - 1, range.from())
40− } else {
41− range
42− };
43−
44− let word: String = text
45− .slice(range.from()..range.to())
46− .chars()
47− .filter(|&c| c != '_')
48− .collect();
49− let (radix, prefixed) = if word.starts_with("0x") {
50− (16, true)
51− } else if word.starts_with("0o") {
52− (8, true)
53− } else if word.starts_with("0b") {
54− (2, true)
55− } else {
56− (10, false)
57− };
58−
59− let number = if prefixed { &word[2..] } else { &word };
60−
61− let value = i128::from_str_radix(number, radix).ok()?;
62− if (value.is_positive() && value.leading_zeros() < 64)
63− || (value.is_negative() && value.leading_ones() < 64)
64− {
65− return None;
66− }
67−
68− let value = value as i64;
69− Some(NumberIncrementor {
70− range,
71− value,
72− radix,
73− text,
74− })
75− }
76−}
77−
78−impl<'a> Increment for NumberIncrementor<'a> {
79− fn increment(&self, amount: i64) -> (Range, Tendril) {
80− let old_text: Cow<str> = self.text.slice(self.range.from()..self.range.to()).into();
81− let old_length = old_text.len();
82− let new_value = self.value.wrapping_add(amount);
83−
84− // Get separator indexes from right to left.
85− let separator_rtl_indexes: Vec<usize> = old_text
86− .chars()
87− .rev()
88− .enumerate()
89− .filter_map(|(i, c)| if c == '_' { Some(i) } else { None })
90− .collect();
91−
92− let format_length = if self.radix == 10 {
93− match (self.value.is_negative(), new_value.is_negative()) {
94− (true, false) => old_length - 1,
95− (false, true) => old_length + 1,
96− _ => old_text.len(),
97− }
98− } else {
99− old_text.len() - 2
100− } - separator_rtl_indexes.len();
101−
102− let mut new_text = match self.radix {
103− 2 => format!("0b{:01$b}", new_value, format_length),
104− 8 => format!("0o{:01$o}", new_value, format_length),
105− 10 if old_text.starts_with('0') || old_text.starts_with("-0") => {
106− format!("{:01$}", new_value, format_length)
107− }
108− 10 => format!("{}", new_value),
109− 16 => {
110− let (lower_count, upper_count): (usize, usize) =
111− old_text.chars().skip(2).fold((0, 0), |(lower, upper), c| {
112− (
113− lower + usize::from(c.is_ascii_lowercase()),
114− upper + usize::from(c.is_ascii_uppercase()),
115− )
116− });
117− if upper_count > lower_count {
118− format!("0x{:01$X}", new_value, format_length)
119− } else {
120− format!("0x{:01$x}", new_value, format_length)
121− }
122− }
123− _ => unimplemented!("radix not supported: {}", self.radix),
124− };
125−
126− // Add separators from original number.
127− for &rtl_index in &separator_rtl_indexes {
128− if rtl_index < new_text.len() {
129− let new_index = new_text.len() - rtl_index;
130− new_text.insert(new_index, '_');
131− }
132− }
133−
134− // Add in additional separators if necessary.
135− if new_text.len() > old_length && !separator_rtl_indexes.is_empty() {
136− let spacing = match separator_rtl_indexes.as_slice() {
137− [.., b, a] => a - b - 1,
138− _ => separator_rtl_indexes[0],
139− };
140−
141− let prefix_length = if self.radix == 10 { 0 } else { 2 };
142− if let Some(mut index) = new_text.find('_') {
143− while index - prefix_length > spacing {
144− index -= spacing;
145− new_text.insert(index, '_');
146− }
147− }
148− }
149−
150− (self.range, new_text.into())
151− }
152−}
153−
154−#[cfg(test)]
155−mod test {
156− use super::*;
157− use crate::Rope;
158−
159− #[test]
160− fn test_decimal_at_point() {
161− let rope = Rope::from_str("Test text 12345 more text.");
162− let range = Range::point(12);
163− assert_eq!(
164− NumberIncrementor::from_range(rope.slice(..), range),
165− Some(NumberIncrementor {
166− range: Range::new(10, 15),
167− value: 12345,
168− radix: 10,
169− text: rope.slice(..),
170− })
171− );
172− }
173−
174− #[test]
175− fn test_uppercase_hexadecimal_at_point() {
176− let rope = Rope::from_str("Test text 0x123ABCDEF more text.");
177− let range = Range::point(12);
178− assert_eq!(
179− NumberIncrementor::from_range(rope.slice(..), range),
180− Some(NumberIncrementor {
181− range: Range::new(10, 21),
182− value: 0x123ABCDEF,
183− radix: 16,
184− text: rope.slice(..),
185− })
186− );
187− }
188−
189− #[test]
190− fn test_lowercase_hexadecimal_at_point() {
191− let rope = Rope::from_str("Test text 0xfa3b4e more text.");
192− let range = Range::point(12);
193− assert_eq!(
194− NumberIncrementor::from_range(rope.slice(..), range),
195− Some(NumberIncrementor {
196− range: Range::new(10, 18),
197− value: 0xfa3b4e,
198− radix: 16,
199− text: rope.slice(..),
200− })
201− );
202− }
203−
204− #[test]
205− fn test_octal_at_point() {
206− let rope = Rope::from_str("Test text 0o1074312 more text.");
207− let range = Range::point(12);
208− assert_eq!(
209− NumberIncrementor::from_range(rope.slice(..), range),
210− Some(NumberIncrementor {
211− range: Range::new(10, 19),
212− value: 0o1074312,
213− radix: 8,
214− text: rope.slice(..),
215− })
216− );
217− }
218−
219− #[test]
220− fn test_binary_at_point() {
221− let rope = Rope::from_str("Test text 0b10111010010101 more text.");
222− let range = Range::point(12);
223− assert_eq!(
224− NumberIncrementor::from_range(rope.slice(..), range),
225− Some(NumberIncrementor {
226− range: Range::new(10, 26),
227− value: 0b10111010010101,
228− radix: 2,
229− text: rope.slice(..),
230− })
231− );
232− }
233−
234− #[test]
235− fn test_negative_decimal_at_point() {
236− let rope = Rope::from_str("Test text -54321 more text.");
237− let range = Range::point(12);
238− assert_eq!(
239− NumberIncrementor::from_range(rope.slice(..), range),
240− Some(NumberIncrementor {
241− range: Range::new(10, 16),
242− value: -54321,
243− radix: 10,
244− text: rope.slice(..),
245− })
246− );
247− }
248−
249− #[test]
250− fn test_decimal_with_leading_zeroes_at_point() {
251− let rope = Rope::from_str("Test text 000045326 more text.");
252− let range = Range::point(12);
253− assert_eq!(
254− NumberIncrementor::from_range(rope.slice(..), range),
255− Some(NumberIncrementor {
256− range: Range::new(10, 19),
257− value: 45326,
258− radix: 10,
259− text: rope.slice(..),
260− })
261− );
262− }
263−
264− #[test]
265− fn test_negative_decimal_cursor_on_minus_sign() {
266− let rope = Rope::from_str("Test text -54321 more text.");
267− let range = Range::point(10);
268− assert_eq!(
269− NumberIncrementor::from_range(rope.slice(..), range),
270− Some(NumberIncrementor {
271− range: Range::new(10, 16),
272− value: -54321,
273− radix: 10,
274− text: rope.slice(..),
275− })
276− );
277− }
278−
279− #[test]
280− fn test_number_under_range_start_of_rope() {
281− let rope = Rope::from_str("100");
282− let range = Range::point(0);
283− assert_eq!(
284− NumberIncrementor::from_range(rope.slice(..), range),
285− Some(NumberIncrementor {
286− range: Range::new(0, 3),
287− value: 100,
288− radix: 10,
289− text: rope.slice(..),
290− })
291− );
292− }
293−
294− #[test]
295− fn test_number_under_range_end_of_rope() {
296− let rope = Rope::from_str("100");
297− let range = Range::point(2);
298− assert_eq!(
299− NumberIncrementor::from_range(rope.slice(..), range),
300− Some(NumberIncrementor {
301− range: Range::new(0, 3),
302− value: 100,
303− radix: 10,
304− text: rope.slice(..),
305− })
306− );
307− }
308−
309− #[test]
310− fn test_number_surrounded_by_punctuation() {
311− let rope = Rope::from_str(",100;");
312− let range = Range::point(1);
313− assert_eq!(
314− NumberIncrementor::from_range(rope.slice(..), range),
315− Some(NumberIncrementor {
316− range: Range::new(1, 4),
317− value: 100,
318− radix: 10,
319− text: rope.slice(..),
320− })
321− );
322− }
323−
324− #[test]
325− fn test_not_a_number_point() {
326− let rope = Rope::from_str("Test text 45326 more text.");
327− let range = Range::point(6);
328− assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
329− }
330−
331− #[test]
332− fn test_number_too_large_at_point() {
333− let rope = Rope::from_str("Test text 0xFFFFFFFFFFFFFFFFF more text.");
334− let range = Range::point(12);
335− assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
336− }
337−
338− #[test]
339− fn test_number_cursor_one_right_of_number() {
340− let rope = Rope::from_str("100 ");
341− let range = Range::point(3);
342− assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
343− }
344−
345− #[test]
346− fn test_number_cursor_one_left_of_number() {
347− let rope = Rope::from_str(" 100");
348− let range = Range::point(0);
349− assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None);
350− }
351−
352− #[test]
353− fn test_increment_basic_decimal_numbers() {
354− let tests = [
355− ("100", 1, "101"),
356− ("100", -1, "99"),
357− ("99", 1, "100"),
358− ("100", 1000, "1100"),
359− ("100", -1000, "-900"),
360− ("-1", 1, "0"),
361− ("-1", 2, "1"),
362− ("1", -1, "0"),
363− ("1", -2, "-1"),
364− ];
365−
366− for (original, amount, expected) in tests {
367− let rope = Rope::from_str(original);
368− let range = Range::point(0);
369− assert_eq!(
370− NumberIncrementor::from_range(rope.slice(..), range)
371− .unwrap()
372− .increment(amount)
373− .1,
374− Tendril::from(expected)
375− );
376− }
377− }
378−
379− #[test]
380− fn test_increment_basic_hexadecimal_numbers() {
381− let tests = [
382− ("0x0100", 1, "0x0101"),
383− ("0x0100", -1, "0x00ff"),
384− ("0x0001", -1, "0x0000"),
385− ("0x0000", -1, "0xffffffffffffffff"),
386− ("0xffffffffffffffff", 1, "0x0000000000000000"),
387− ("0xffffffffffffffff", 2, "0x0000000000000001"),
388− ("0xffffffffffffffff", -1, "0xfffffffffffffffe"),
389− ("0xABCDEF1234567890", 1, "0xABCDEF1234567891"),
390− ("0xabcdef1234567890", 1, "0xabcdef1234567891"),
391− ];
392−
393− for (original, amount, expected) in tests {
394− let rope = Rope::from_str(original);
395− let range = Range::point(0);
396− assert_eq!(
397− NumberIncrementor::from_range(rope.slice(..), range)
398− .unwrap()
399− .increment(amount)
400− .1,
401− Tendril::from(expected)
402− );
403− }
404− }
405−
406− #[test]
407− fn test_increment_basic_octal_numbers() {
408− let tests = [
409− ("0o0107", 1, "0o0110"),
410− ("0o0110", -1, "0o0107"),
411− ("0o0001", -1, "0o0000"),
412− ("0o7777", 1, "0o10000"),
413− ("0o1000", -1, "0o0777"),
414− ("0o0107", 10, "0o0121"),
415− ("0o0000", -1, "0o1777777777777777777777"),
416− ("0o1777777777777777777777", 1, "0o0000000000000000000000"),
417− ("0o1777777777777777777777", 2, "0o0000000000000000000001"),
418− ("0o1777777777777777777777", -1, "0o1777777777777777777776"),
419− ];
420−
421− for (original, amount, expected) in tests {
422− let rope = Rope::from_str(original);
423− let range = Range::point(0);
424− assert_eq!(
425− NumberIncrementor::from_range(rope.slice(..), range)
426− .unwrap()
427− .increment(amount)
428− .1,
429− Tendril::from(expected)
430− );
431− }
432− }
433−
434− #[test]
435− fn test_increment_basic_binary_numbers() {
436− let tests = [
437− ("0b00000100", 1, "0b00000101"),
438− ("0b00000100", -1, "0b00000011"),
439− ("0b00000100", 2, "0b00000110"),
440− ("0b00000100", -2, "0b00000010"),
441− ("0b00000001", -1, "0b00000000"),
442− ("0b00111111", 10, "0b01001001"),
443− ("0b11111111", 1, "0b100000000"),
444− ("0b10000000", -1, "0b01111111"),
445− (
446− "0b0000",
447− -1,
448− "0b1111111111111111111111111111111111111111111111111111111111111111",
449− ),
450− (
451− "0b1111111111111111111111111111111111111111111111111111111111111111",
452− 1,
453− "0b0000000000000000000000000000000000000000000000000000000000000000",
454− ),
455− (
456− "0b1111111111111111111111111111111111111111111111111111111111111111",
457− 2,
458− "0b0000000000000000000000000000000000000000000000000000000000000001",
459− ),
460− (
461− "0b1111111111111111111111111111111111111111111111111111111111111111",
462− -1,
463− "0b1111111111111111111111111111111111111111111111111111111111111110",
464− ),
465− ];
466−
467− for (original, amount, expected) in tests {
468− let rope = Rope::from_str(original);
469− let range = Range::point(0);
470− assert_eq!(
471− NumberIncrementor::from_range(rope.slice(..), range)
472− .unwrap()
473− .increment(amount)
474− .1,
475− Tendril::from(expected)
476− );
477− }
478− }
479−
480− #[test]
481− fn test_increment_with_separators() {
482− let tests = [
483− ("999_999", 1, "1_000_000"),
484− ("1_000_000", -1, "999_999"),
485− ("-999_999", -1, "-1_000_000"),
486− ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
487− ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
488− ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"),
489− ("0x0000_0000", -1, "0xffff_ffff_ffff_ffff"),
490− ("0x0000_0000_0000", -1, "0xffff_ffff_ffff_ffff"),
491− ("0b01111111_11111111", 1, "0b10000000_00000000"),
492− ("0b11111111_11111111", 1, "0b1_00000000_00000000"),
493− ];
494−
495− for (original, amount, expected) in tests {
496− let rope = Rope::from_str(original);
497− let range = Range::point(0);
498− assert_eq!(
499− NumberIncrementor::from_range(rope.slice(..), range)
500− .unwrap()
501− .increment(amount)
502− .1,
503− Tendril::from(expected)
504− );
505− }
506− }
507−}
helix-term/src/commands.rs+32−81
@@ -11,9 +11,7 @@ pub use typed::*;
1111use helix_core::{
1212 comment, coords_at_pos, encoding, find_first_non_whitespace_char, find_root, graphemes,
1313 history::UndoKind,
14− increment::date_time::DateTimeIncrementor,
15− increment::{number::NumberIncrementor, Increment},
16− indent,
14+ increment, indent,
1715 indent::IndentStyle,
1816 line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending},
1917 match_brackets,
@@ -5028,57 +5026,25 @@ enum IncrementDirection {
50285026 Increase,
50295027 Decrease,
50305028}
5031−/// Increment object under cursor by count.
5029+
5030+/// Increment objects within selections by count.
50325031fn increment(cx: &mut Context) {
50335032 increment_impl(cx, IncrementDirection::Increase);
50345033}
50355034
5036−/// Decrement object under cursor by count.
5035+/// Decrement objects within selections by count.
50375036fn decrement(cx: &mut Context) {
50385037 increment_impl(cx, IncrementDirection::Decrease);
50395038}
50405039
5041−/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
5042−/// starts searching at the current character, instead of the next.
5043−/// It does not want to start at the next character because this function is used for incrementing
5044−/// number and we don't want to move forward if we're already on a digit.
5045−fn find_next_char_until_newline<M: CharMatcher>(
5046− text: RopeSlice,
5047− char_matcher: M,
5048− pos: usize,
5049− _count: usize,
5050− _inclusive: bool,
5051−) -> Option<usize> {
5052− // Since we send the current line to find_nth_next instead of the whole text, we need to adjust
5053− // the position we send to this function so that it's relative to that line and its returned
5054− // position since it's expected this function returns a global position.
5055− let line_index = text.char_to_line(pos);
5056− let pos_delta = text.line_to_char(line_index);
5057− let pos = pos - pos_delta;
5058− search::find_nth_next(text.line(line_index), char_matcher, pos, 1).map(|pos| pos + pos_delta)
5059−}
5060−
5061−/// Decrement object under cursor by `amount`.
5040+/// Increment objects within selections by `amount`.
5041+/// A negative `amount` will decrement objects within selections.
50625042fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
5063− // TODO: when incrementing or decrementing a number that gets a new digit or lose one, the
5064− // selection is updated improperly.
5065− find_char_impl(
5066− cx.editor,
5067− &find_next_char_until_newline,
5068− true,
5069− true,
5070− char::is_ascii_digit,
5071− 1,
5072− );
5073−
5074− // Increase by 1 if `IncrementDirection` is `Increase`
5075− // Decrease by 1 if `IncrementDirection` is `Decrease`
50765043 let sign = match increment_direction {
50775044 IncrementDirection::Increase => 1,
50785045 IncrementDirection::Decrease => -1,
50795046 };
50805047 let mut amount = sign * cx.count() as i64;
5081−
50825048 // If the register is `#` then increase or decrease the `amount` by 1 per element
50835049 let increase_by = if cx.register == Some('#') { sign } else { 0 };
50845050
@@ -5086,55 +5052,40 @@ fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
50865052 let selection = doc.selection(view.id);
50875053 let text = doc.text().slice(..);
50885054
5089− let changes: Vec<_> = selection
5090− .ranges()
5091− .iter()
5092− .filter_map(|range| {
5093− let incrementor: Box<dyn Increment> =
5094− if let Some(incrementor) = DateTimeIncrementor::from_range(text, *range) {
5095− Box::new(incrementor)
5096− } else if let Some(incrementor) = NumberIncrementor::from_range(text, *range) {
5097− Box::new(incrementor)
5098− } else {
5099− return None;
5100− };
5055+ let mut new_selection_ranges = SmallVec::new();
5056+ let mut cumulative_length_diff: i128 = 0;
5057+ let mut changes = vec![];
51015058
5102− let (range, new_text) = incrementor.increment(amount);
5059+ for range in selection {
5060+ let selected_text: Cow<str> = range.fragment(text);
5061+ let new_from = ((range.from() as i128) + cumulative_length_diff) as usize;
5062+ let incremented = [increment::integer, increment::date_time]
5063+ .iter()
5064+ .find_map(|incrementor| incrementor(selected_text.as_ref(), amount));
51035065
5104− amount += increase_by;
5066+ amount += increase_by;
51055067
5106− Some((range.from(), range.to(), Some(new_text)))
5107− })
5108− .collect();
5109−
5110− // Overlapping changes in a transaction will panic, so we need to find and remove them.
5111− // For example, if there are cursors on each of the year, month, and day of `2021-11-29`,
5112− // incrementing will give overlapping changes, with each change incrementing a different part of
5113− // the date. Since these conflict with each other we remove these changes from the transaction
5114− // so nothing happens.
5115− let mut overlapping_indexes = HashSet::new();
5116− for (i, changes) in changes.windows(2).enumerate() {
5117− if changes[0].1 > changes[1].0 {
5118− overlapping_indexes.insert(i);
5119− overlapping_indexes.insert(i + 1);
5068+ match incremented {
5069+ None => {
5070+ let new_range = Range::new(
5071+ new_from,
5072+ (range.to() as i128 + cumulative_length_diff) as usize,
5073+ );
5074+ new_selection_ranges.push(new_range);
5075+ }
5076+ Some(new_text) => {
5077+ let new_range = Range::new(new_from, new_from + new_text.len());
5078+ cumulative_length_diff += new_text.len() as i128 - selected_text.len() as i128;
5079+ new_selection_ranges.push(new_range);
5080+ changes.push((range.from(), range.to(), Some(new_text.into())));
5081+ }
51205082 }
51215083 }
5122− let changes: Vec<_> = changes
5123− .into_iter()
5124− .enumerate()
5125− .filter_map(|(i, change)| {
5126− if overlapping_indexes.contains(&i) {
5127− None
5128− } else {
5129− Some(change)
5130− }
5131− })
5132− .collect();
51335084
51345085 if !changes.is_empty() {
5086+ let new_selection = Selection::new(new_selection_ranges, selection.primary_index());
51355087 let transaction = Transaction::change(doc.text(), changes.into_iter());
5136− let transaction = transaction.with_selection(selection.clone());
5137−
5088+ let transaction = transaction.with_selection(new_selection);
51385089 apply_transaction(&transaction, doc, view);
51395090 }
51405091}