From 881c29192d39f657bf518baf399c47a5bfdc922f Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 17 Dec 2018 20:13:54 +0100 Subject: initial newline translation working todo: cleanup, simplify handle columns --- crates/ra_editor/src/line_index.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index aab7e4081..7eddfd502 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -1,4 +1,4 @@ -use crate::TextUnit; +use crate::{TextUnit, TextRange}; use rustc_hash::FxHashMap; use superslice::Ext; @@ -120,6 +120,10 @@ impl LineIndex { col } + + pub fn newlines(&self) -> &[TextUnit] { + &self.newlines[1..] + } } #[test] -- cgit v1.2.3 From 8c9df62c1c6a778a8df9ea028d1dce98c91c4d9d Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 18 Dec 2018 18:46:54 +0100 Subject: move translate_offset_with_edit to ra_editor --- crates/ra_editor/src/lib.rs | 2 + crates/ra_editor/src/line_index.rs | 4 +- crates/ra_editor/src/line_index_utils.rs | 260 +++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 crates/ra_editor/src/line_index_utils.rs (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7a689b0f2..619497f0b 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,6 +2,7 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; +mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils; @@ -12,6 +13,7 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, + line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 7eddfd502..7d9b8d79f 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -1,4 +1,4 @@ -use crate::{TextUnit, TextRange}; +use crate::TextUnit; use rustc_hash::FxHashMap; use superslice::Ext; @@ -121,7 +121,7 @@ impl LineIndex { col } - pub fn newlines(&self) -> &[TextUnit] { + pub(crate) fn newlines(&self) -> &[TextUnit] { &self.newlines[1..] } } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs new file mode 100644 index 000000000..e4fad04b2 --- /dev/null +++ b/crates/ra_editor/src/line_index_utils.rs @@ -0,0 +1,260 @@ +use ra_text_edit::{AtomTextEdit}; +use ra_syntax::{TextUnit, TextRange}; +use crate::{LineIndex, LineCol}; + +#[derive(Debug)] +struct OffsetNewlineIter<'a> { + text: &'a str, + offset: TextUnit, +} + +impl<'a> Iterator for OffsetNewlineIter<'a> { + type Item = TextUnit; + fn next(&mut self) -> Option { + let next_idx = self + .text + .char_indices() + .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) + .next()?; + let next = self.offset + TextUnit::from_usize(next_idx); + self.text = &self.text[next_idx..]; + self.offset = next; + Some(next) + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +enum TranslatedPos { + Before, + After, +} + +/// None means it was deleted +type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; + +fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { + if offset <= edit.delete.start() { + Some((TranslatedPos::Before, offset)) + } else if offset <= edit.delete.end() { + None + } else { + let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; + let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); + Some((TranslatedPos::After, after)) + } +} + +trait TranslatedNewlineIterator { + fn translate(&self, offset: TextUnit) -> TextUnit; + fn translate_range(&self, range: TextRange) -> TextRange { + TextRange::from_to(self.translate(range.start()), self.translate(range.end())) + } + fn next_translated(&mut self) -> Option; + fn boxed<'a>(self) -> Box + where + Self: 'a + Sized, + { + Box::new(self) + } +} + +struct TranslatedAtomEdit<'a> { + delete: TextRange, + insert: &'a str, +} + +struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { + inner: T, + next_inner: Option, + edit: TranslatedAtomEdit<'a>, + insert: OffsetNewlineIter<'a>, +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { + fn from(inner: T, edit: &'a AtomTextEdit) -> Self { + let delete = inner.translate_range(edit.delete); + let mut res = TranslatedNewlines { + inner, + next_inner: None, + edit: TranslatedAtomEdit { + delete, + insert: &edit.insert, + }, + insert: OffsetNewlineIter { + offset: delete.start(), + text: &edit.insert, + }, + }; + // prepare next_inner + res.advance_inner(); + res + } + + fn advance_inner(&mut self) { + self.next_inner = self + .inner + .next_translated() + .map(|x| translate_offset(x, &self.edit)); + } +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { + fn translate(&self, offset: TextUnit) -> TextUnit { + let offset = self.inner.translate(offset); + let (_, offset) = + translate_offset(offset, &self.edit).expect("translate_unit returned None"); + offset + } + + fn next_translated(&mut self) -> Option { + match self.next_inner { + None => self.insert.next(), + Some(next) => match next { + None => self.insert.next().or_else(|| { + self.advance_inner(); + self.next_translated() + }), + Some((TranslatedPos::Before, next)) => { + self.advance_inner(); + Some(next) + } + Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { + self.advance_inner(); + Some(next) + }), + }, + } + } +} + +impl<'a> Iterator for Box { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.next_translated() + } +} + +impl TranslatedNewlineIterator for Box { + fn translate(&self, offset: TextUnit) -> TextUnit { + self.as_ref().translate(offset) + } + fn next_translated(&mut self) -> Option { + self.as_mut().next_translated() + } +} + +struct IteratorWrapper>(T); + +impl> TranslatedNewlineIterator for IteratorWrapper { + fn translate(&self, offset: TextUnit) -> TextUnit { + offset + } + fn next_translated(&mut self) -> Option { + self.0.next() + } +} + +impl> Iterator for IteratorWrapper { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.0.next() + } +} + +fn translate_newlines<'a>( + mut newlines: Box, + edits: &'a [AtomTextEdit], +) -> Box { + for edit in edits { + newlines = TranslatedNewlines::from(newlines, edit).boxed(); + } + newlines +} + +pub fn translate_offset_with_edit( + pre_edit_index: &LineIndex, + offset: TextUnit, + edits: &[AtomTextEdit], +) -> LineCol { + let mut newlines: Box = Box::new(IteratorWrapper( + pre_edit_index.newlines().iter().map(|x| *x), + )); + + newlines = translate_newlines(newlines, edits); + + let mut line = 0; + for n in newlines { + if n > offset { + break; + } + line += 1; + } + + LineCol { + line: line, + col_utf16: 0, // TODO not implemented yet + } +} + +#[cfg(test)] +mod test { + use proptest::{prelude::*, proptest, proptest_helper}; + use super::*; + use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + + #[derive(Debug)] + struct ArbTextWithOffsetAndEdits { + text: String, + offset: TextUnit, + edits: Vec, + } + + fn arb_text_with_offset_and_edits() -> BoxedStrategy { + arb_text() + .prop_flat_map(|text| { + (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( + |(offset, edits, text)| ArbTextWithOffsetAndEdits { + text, + offset, + edits, + }, + ) + }) + .boxed() + } + + fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); + + let mut text = pre_edit_text.to_owned(); + + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); + } + + text + } + + fn translate_after_edit( + pre_edit_text: &str, + offset: TextUnit, + edits: Vec, + ) -> LineCol { + let text = edit_text(pre_edit_text, edits); + let line_index = LineIndex::new(&text); + line_index.line_col(offset) + } + + proptest! { + #[test] + fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + assert_eq!(actual.line, expected.line); + } + } +} -- cgit v1.2.3 From 7299df8409097de67647b371b81da7bcf49112e6 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 19 Dec 2018 19:25:42 +0100 Subject: simplified version --- crates/ra_editor/src/line_index_utils.rs | 139 +++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index e4fad04b2..6bd6a397e 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,7 @@ use ra_text_edit::{AtomTextEdit}; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol}; +use superslice::Ext; #[derive(Debug)] struct OffsetNewlineIter<'a> { @@ -58,6 +59,136 @@ trait TranslatedNewlineIterator { } } +#[derive(Debug)] +struct AltEdit<'a> { + insert_newlines: OffsetNewlineIter<'a>, + delete: TextRange, + diff: i64, +} + +fn translate_range_by(range: TextRange, diff: i64) -> TextRange { + if diff == 0 { + range + } else { + let start = translate_by(range.start(), diff); + let end = translate_by(range.end(), diff); + TextRange::from_to(start, end) + } +} + +fn translate_by(x: TextUnit, diff: i64) -> TextUnit { + if diff == 0 { + x + } else { + TextUnit::from((x.to_usize() as i64 + diff) as u32) + } +} + +fn to_alt_edits<'a>(offset: TextUnit, edits: &'a [AtomTextEdit]) -> Vec> { + let mut xs: Vec> = Vec::with_capacity(edits.len()); + // collect and sort edits + for edit in edits { + // TODO discard after translating? + // if edit.delete.start() >= offset { + // continue; + // } + let insert_index = xs.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); + let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; + xs.insert( + insert_index, + AltEdit { + insert_newlines: OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }, + delete: edit.delete, + diff: diff, + }, + ); + } + // translate edits by previous edits + for i in 1..xs.len() { + let (x, prevs) = xs[0..=i].split_last_mut().unwrap(); + for prev in prevs { + x.delete = translate_range_by(x.delete, prev.diff); + x.insert_newlines.offset = translate_by(x.insert_newlines.offset, prev.diff); + } + } + xs +} + +#[derive(Debug)] +enum NextNewline { + Use, + Discard, + Replace(TextUnit), + New(TextUnit), +} + +fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewline { + let mut candidate = match candidate { + None => { + for edit in edits { + if let Some(inserted) = edit.insert_newlines.next() { + return NextNewline::New(inserted); + } + } + return NextNewline::Use; // END + } + Some(x) => x, + }; + + for edit in edits { + if candidate <= edit.delete.start() { + return NextNewline::Replace(candidate); + } else if candidate <= edit.delete.end() { + return match edit.insert_newlines.next() { + Some(x) => NextNewline::Replace(x), + None => NextNewline::Discard, + }; + } else { + if let Some(inserted) = edit.insert_newlines.next() { + return NextNewline::New(inserted); + } + candidate = translate_by(candidate, edit.diff); + } + } + return NextNewline::Replace(candidate); +} + +fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { + let mut edits = to_alt_edits(offset, edits); + let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); + + let mut count = 0; + + loop { + let res = next_newline(orig_newlines.peek().map(|x| *x), &mut edits); + let next = match res { + NextNewline::Use => orig_newlines.next(), + NextNewline::Discard => { + orig_newlines.next(); + continue; + } + NextNewline::Replace(new) => { + orig_newlines.next(); + Some(new) + } + NextNewline::New(new) => Some(new), + }; + match next { + Some(n) if n <= offset => { + count += 1; + } + _ => { + break; + } + } + } + + count +} + struct TranslatedAtomEdit<'a> { delete: TextRange, insert: &'a str, @@ -256,5 +387,13 @@ mod test { let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); assert_eq!(actual.line, expected.line); } + + #[test] + fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } } } -- cgit v1.2.3 From a005d2a614031a18c9a5bf6557789a41f1b25c31 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 18:38:28 +0100 Subject: final iteration, faster a bit simpler the main thing is we iterate over inserted newlines at once for each edit --- crates/ra_editor/src/line_index_utils.rs | 380 +++++++++++++++++-------------- 1 file changed, 209 insertions(+), 171 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 6bd6a397e..5ce2446c1 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,4 +1,4 @@ -use ra_text_edit::{AtomTextEdit}; +use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol}; use superslice::Ext; @@ -24,41 +24,6 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } } -#[derive(Debug, Clone, Copy, PartialEq)] -enum TranslatedPos { - Before, - After, -} - -/// None means it was deleted -type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; - -fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { - if offset <= edit.delete.start() { - Some((TranslatedPos::Before, offset)) - } else if offset <= edit.delete.end() { - None - } else { - let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; - let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); - Some((TranslatedPos::After, after)) - } -} - -trait TranslatedNewlineIterator { - fn translate(&self, offset: TextUnit) -> TextUnit; - fn translate_range(&self, range: TextRange) -> TextRange { - TextRange::from_to(self.translate(range.start()), self.translate(range.end())) - } - fn next_translated(&mut self) -> Option; - fn boxed<'a>(self) -> Box - where - Self: 'a + Sized, - { - Box::new(self) - } -} - #[derive(Debug)] struct AltEdit<'a> { insert_newlines: OffsetNewlineIter<'a>, @@ -156,7 +121,7 @@ fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewli return NextNewline::Replace(candidate); } -fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { let mut edits = to_alt_edits(offset, edits); let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); @@ -189,142 +154,184 @@ fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdi count } -struct TranslatedAtomEdit<'a> { +#[derive(Debug)] +enum NextNewlines<'a> { + Use, + ReplaceMany(OffsetNewlineIter<'a>), + AddMany(OffsetNewlineIter<'a>), +} + +#[derive(Debug)] +struct TranslatedEdit<'a> { delete: TextRange, insert: &'a str, + diff: i64, } -struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { - inner: T, - next_inner: Option, - edit: TranslatedAtomEdit<'a>, - insert: OffsetNewlineIter<'a>, +struct Edits<'a, 'b> { + edits: &'b [&'a AtomTextEdit], + current: Option>, + acc_diff: i64, } -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { - fn from(inner: T, edit: &'a AtomTextEdit) -> Self { - let delete = inner.translate_range(edit.delete); - let mut res = TranslatedNewlines { - inner, - next_inner: None, - edit: TranslatedAtomEdit { - delete, - insert: &edit.insert, - }, - insert: OffsetNewlineIter { - offset: delete.start(), - text: &edit.insert, - }, +impl<'a, 'b> Edits<'a, 'b> { + fn new(sorted_edits: &'b [&'a AtomTextEdit]) -> Edits<'a, 'b> { + let mut x = Edits { + edits: sorted_edits, + current: None, + acc_diff: 0, }; - // prepare next_inner - res.advance_inner(); - res + x.advance_edit(); + x } - - fn advance_inner(&mut self) { - self.next_inner = self - .inner - .next_translated() - .map(|x| translate_offset(x, &self.edit)); + fn advance_edit(&mut self) { + self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); + match self.edits.split_first() { + Some((next, rest)) => { + let delete = translate_range_by(next.delete, self.acc_diff); + let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; + self.current = Some(TranslatedEdit { + delete, + insert: &next.insert, + diff, + }); + self.edits = rest; + } + None => { + self.current = None; + } + } } -} -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { - fn translate(&self, offset: TextUnit) -> TextUnit { - let offset = self.inner.translate(offset); - let (_, offset) = - translate_offset(offset, &self.edit).expect("translate_unit returned None"); - offset + fn next_inserted_newlines(&mut self) -> Option> { + let cur = self.current.as_ref()?; + let res = Some(OffsetNewlineIter { + offset: cur.delete.start(), + text: &cur.insert, + }); + self.advance_edit(); + res } - fn next_translated(&mut self) -> Option { - match self.next_inner { - None => self.insert.next(), - Some(next) => match next { - None => self.insert.next().or_else(|| { - self.advance_inner(); - self.next_translated() - }), - Some((TranslatedPos::Before, next)) => { - self.advance_inner(); - Some(next) + fn next_newlines(&mut self, candidate: TextUnit) -> NextNewlines { + let res = match &mut self.current { + Some(edit) => { + if candidate <= edit.delete.start() { + NextNewlines::Use + } else if candidate <= edit.delete.end() { + let iter = OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }; + // empty slice + edit.insert = &edit.insert[edit.insert.len()..]; + NextNewlines::ReplaceMany(iter) + } else { + let iter = OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }; + // empty slice + edit.insert = &edit.insert[edit.insert.len()..]; + self.advance_edit(); + NextNewlines::AddMany(iter) } - Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { - self.advance_inner(); - Some(next) - }), - }, - } - } -} - -impl<'a> Iterator for Box { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.next_translated() + } + None => NextNewlines::Use, + }; + res } } -impl TranslatedNewlineIterator for Box { - fn translate(&self, offset: TextUnit) -> TextUnit { - self.as_ref().translate(offset) - } - fn next_translated(&mut self) -> Option { - self.as_mut().next_translated() +pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { + let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); + for edit in edits { + let insert_index = + sorted_edits.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); + sorted_edits.insert(insert_index, &edit); } -} - -struct IteratorWrapper>(T); -impl> TranslatedNewlineIterator for IteratorWrapper { - fn translate(&self, offset: TextUnit) -> TextUnit { - offset - } - fn next_translated(&mut self) -> Option { - self.0.next() + let mut state = Edits::new(&sorted_edits); + + let mut lines: u32 = 0; + + for &orig_newline in line_index.newlines() { + loop { + let translated_newline = translate_by(orig_newline, state.acc_diff); + match state.next_newlines(translated_newline) { + NextNewlines::Use => { + if offset < translated_newline { + return lines; + } else { + lines += 1; + } + break; + } + NextNewlines::ReplaceMany(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + break; + } + NextNewlines::AddMany(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + } + } + } } -} -impl> Iterator for IteratorWrapper { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.0.next() + loop { + match state.next_inserted_newlines() { + None => break, + Some(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + } + } } -} -fn translate_newlines<'a>( - mut newlines: Box, - edits: &'a [AtomTextEdit], -) -> Box { - for edit in edits { - newlines = TranslatedNewlines::from(newlines, edit).boxed(); - } - newlines + lines } -pub fn translate_offset_with_edit( - pre_edit_index: &LineIndex, +// for bench +pub fn translate_after_edit( + pre_edit_text: &str, offset: TextUnit, - edits: &[AtomTextEdit], + edits: Vec, ) -> LineCol { - let mut newlines: Box = Box::new(IteratorWrapper( - pre_edit_index.newlines().iter().map(|x| *x), - )); + let text = edit_text(pre_edit_text, edits); + let line_index = LineIndex::new(&text); + line_index.line_col(offset) +} - newlines = translate_newlines(newlines, edits); +fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - let mut line = 0; - for n in newlines { - if n > offset { - break; - } - line += 1; - } + let mut text = pre_edit_text.to_owned(); - LineCol { - line: line, - col_utf16: 0, // TODO not implemented yet + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); } + + text } #[cfg(test)] @@ -354,46 +361,77 @@ mod test { .boxed() } - fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { - // apply edits ordered from last to first - // since they should not overlap we can just use start() - edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - - let mut text = pre_edit_text.to_owned(); - - for edit in &edits { - let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); - text.replace_range(range, &edit.insert); - } - - text - } - - fn translate_after_edit( - pre_edit_text: &str, - offset: TextUnit, - edits: Vec, - ) -> LineCol { - let text = edit_text(pre_edit_text, edits); - let line_index = LineIndex::new(&text); - line_index.line_col(offset) - } - proptest! { #[test] fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); - assert_eq!(actual.line, expected.line); + let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); } #[test] fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); assert_eq!(actual_lines, expected.line); } } + + #[test] + fn test_translate_offset_with_edit_alt_1() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("aA\n"), + offset: 2.into(), + edits: vec![AtomTextEdit::delete(TextRange::from_to(1.into(), 2.into()))], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_2() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("\nqꀸ#"), + offset: 5.into(), + edits: vec![AtomTextEdit::insert(1.into(), "\n".into())], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_3() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("\n\n\n"), + offset: 0.into(), + edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_4() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("☻54翑\"A"), + offset: 5.into(), + edits: vec![ + AtomTextEdit::delete(TextRange::from_to(0.into(), 8.into())), + AtomTextEdit::insert(9.into(), String::from("\n")), + ], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + } -- cgit v1.2.3 From d6312085a1ac97030fa768366585b9cfb6c955cd Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 18:51:31 +0100 Subject: remove slower impl, add benchmarks --- crates/ra_editor/src/lib.rs | 4 +- crates/ra_editor/src/line_index_utils.rs | 218 +++---------------------------- 2 files changed, 23 insertions(+), 199 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 619497f0b..7145c6cfb 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,7 +2,8 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; -mod line_index_utils; +// public for benchmarkig +pub mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils; @@ -13,7 +14,6 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, - line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 5ce2446c1..e62c5089d 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -24,136 +24,6 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } } -#[derive(Debug)] -struct AltEdit<'a> { - insert_newlines: OffsetNewlineIter<'a>, - delete: TextRange, - diff: i64, -} - -fn translate_range_by(range: TextRange, diff: i64) -> TextRange { - if diff == 0 { - range - } else { - let start = translate_by(range.start(), diff); - let end = translate_by(range.end(), diff); - TextRange::from_to(start, end) - } -} - -fn translate_by(x: TextUnit, diff: i64) -> TextUnit { - if diff == 0 { - x - } else { - TextUnit::from((x.to_usize() as i64 + diff) as u32) - } -} - -fn to_alt_edits<'a>(offset: TextUnit, edits: &'a [AtomTextEdit]) -> Vec> { - let mut xs: Vec> = Vec::with_capacity(edits.len()); - // collect and sort edits - for edit in edits { - // TODO discard after translating? - // if edit.delete.start() >= offset { - // continue; - // } - let insert_index = xs.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); - let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; - xs.insert( - insert_index, - AltEdit { - insert_newlines: OffsetNewlineIter { - offset: edit.delete.start(), - text: &edit.insert, - }, - delete: edit.delete, - diff: diff, - }, - ); - } - // translate edits by previous edits - for i in 1..xs.len() { - let (x, prevs) = xs[0..=i].split_last_mut().unwrap(); - for prev in prevs { - x.delete = translate_range_by(x.delete, prev.diff); - x.insert_newlines.offset = translate_by(x.insert_newlines.offset, prev.diff); - } - } - xs -} - -#[derive(Debug)] -enum NextNewline { - Use, - Discard, - Replace(TextUnit), - New(TextUnit), -} - -fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewline { - let mut candidate = match candidate { - None => { - for edit in edits { - if let Some(inserted) = edit.insert_newlines.next() { - return NextNewline::New(inserted); - } - } - return NextNewline::Use; // END - } - Some(x) => x, - }; - - for edit in edits { - if candidate <= edit.delete.start() { - return NextNewline::Replace(candidate); - } else if candidate <= edit.delete.end() { - return match edit.insert_newlines.next() { - Some(x) => NextNewline::Replace(x), - None => NextNewline::Discard, - }; - } else { - if let Some(inserted) = edit.insert_newlines.next() { - return NextNewline::New(inserted); - } - candidate = translate_by(candidate, edit.diff); - } - } - return NextNewline::Replace(candidate); -} - -pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { - let mut edits = to_alt_edits(offset, edits); - let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); - - let mut count = 0; - - loop { - let res = next_newline(orig_newlines.peek().map(|x| *x), &mut edits); - let next = match res { - NextNewline::Use => orig_newlines.next(), - NextNewline::Discard => { - orig_newlines.next(); - continue; - } - NextNewline::Replace(new) => { - orig_newlines.next(); - Some(new) - } - NextNewline::New(new) => Some(new), - }; - match next { - Some(n) if n <= offset => { - count += 1; - } - _ => { - break; - } - } - } - - count -} - #[derive(Debug)] enum NextNewlines<'a> { Use, @@ -188,7 +58,7 @@ impl<'a, 'b> Edits<'a, 'b> { self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); match self.edits.split_first() { Some((next, rest)) => { - let delete = translate_range_by(next.delete, self.acc_diff); + let delete = self.translate_range(next.delete); let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; self.current = Some(TranslatedEdit { delete, @@ -241,9 +111,27 @@ impl<'a, 'b> Edits<'a, 'b> { }; res } + + fn translate_range(&self, range: TextRange) -> TextRange { + if self.acc_diff == 0 { + range + } else { + let start = self.translate(range.start()); + let end = self.translate(range.end()); + TextRange::from_to(start, end) + } + } + + fn translate(&self, x: TextUnit) -> TextUnit { + if self.acc_diff == 0 { + x + } else { + TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) + } + } } -pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); for edit in edits { let insert_index = @@ -257,7 +145,7 @@ pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[Ato for &orig_newline in line_index.newlines() { loop { - let translated_newline = translate_by(orig_newline, state.acc_diff); + let translated_newline = state.translate(orig_newline); match state.next_newlines(translated_newline) { NextNewlines::Use => { if offset < translated_newline { @@ -369,69 +257,5 @@ mod test { let actual_lines = count_newlines(x.offset, &line_index, &x.edits); assert_eq!(actual_lines, expected.line); } - - #[test] - fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - } - - #[test] - fn test_translate_offset_with_edit_alt_1() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("aA\n"), - offset: 2.into(), - edits: vec![AtomTextEdit::delete(TextRange::from_to(1.into(), 2.into()))], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_alt_2() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("\nqꀸ#"), - offset: 5.into(), - edits: vec![AtomTextEdit::insert(1.into(), "\n".into())], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_alt_3() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("\n\n\n"), - offset: 0.into(), - edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); } - - #[test] - fn test_translate_offset_with_edit_alt_4() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("☻54翑\"A"), - offset: 5.into(), - edits: vec![ - AtomTextEdit::delete(TextRange::from_to(0.into(), 8.into())), - AtomTextEdit::insert(9.into(), String::from("\n")), - ], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - } -- cgit v1.2.3 From 1c44ba0f04a0997617d517111d0a08245f0dacac Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 20:11:27 +0100 Subject: simplify newline check with macro --- crates/ra_editor/src/line_index_utils.rs | 34 +++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index e62c5089d..2dd7fd708 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -143,35 +143,33 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut lines: u32 = 0; + macro_rules! test_newline { + ($x:ident) => { + if offset < $x { + return lines; + } else { + lines += 1; + } + }; + } + for &orig_newline in line_index.newlines() { loop { let translated_newline = state.translate(orig_newline); match state.next_newlines(translated_newline) { NextNewlines::Use => { - if offset < translated_newline { - return lines; - } else { - lines += 1; - } + test_newline!(translated_newline); break; } NextNewlines::ReplaceMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } break; } NextNewlines::AddMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } @@ -183,11 +181,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex None => break, Some(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } -- cgit v1.2.3 From 36f2b1f3b9c36ace65852d971f998a090cf9b5d5 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 12:59:48 +0100 Subject: iterate over `Step`s which are either, newlines or multibyte chars --- crates/ra_editor/src/line_index.rs | 14 +++- crates/ra_editor/src/line_index_utils.rs | 133 ++++++++++++++++++++++++++----- 2 files changed, 121 insertions(+), 26 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 7d9b8d79f..c29e2e49a 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -15,9 +15,9 @@ pub struct LineCol { } #[derive(Clone, Debug, Hash, PartialEq, Eq)] -struct Utf16Char { - start: TextUnit, - end: TextUnit, +pub(crate) struct Utf16Char { + pub(crate) start: TextUnit, + pub(crate) end: TextUnit, } impl Utf16Char { @@ -122,7 +122,13 @@ impl LineIndex { } pub(crate) fn newlines(&self) -> &[TextUnit] { - &self.newlines[1..] + &self.newlines[..] + } + + pub(crate) fn utf16_chars(&self, newline_idx: usize) -> Option<&[Utf16Char]> { + self.utf16_lines + .get(&(newline_idx as u32)) + .map(|x| x.as_slice()) } } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 2dd7fd708..9c8d801e9 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,8 +1,58 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol}; +use crate::{LineIndex, LineCol, line_index::Utf16Char}; use superslice::Ext; +#[derive(Debug, Clone)] +enum Step { + Newline(TextUnit), + Utf16Char(TextRange), +} + +struct LineIndexStepIter<'a> { + line_index: &'a LineIndex, + newlines: std::slice::Iter<'a, TextUnit>, + next_newline_idx: usize, + utf16_chars: Option>, +} + +impl<'a> LineIndexStepIter<'a> { + fn from(line_index: &LineIndex) -> LineIndexStepIter { + let mut x = LineIndexStepIter { + line_index, + newlines: line_index.newlines().iter(), + next_newline_idx: 0, + utf16_chars: None, + }; + // skip first newline since it's not real + x.next(); + x + } +} + +impl<'a> Iterator for LineIndexStepIter<'a> { + type Item = Step; + fn next(&mut self) -> Option { + self.utf16_chars + .as_mut() + .and_then(|x| { + None + // TODO Enable + // let x = x.next()?; + // Some(Step::Utf16Char(TextRange::from_to(x.start, x.end))) + }) + .or_else(|| { + self.utf16_chars = self + .line_index + .utf16_chars(self.next_newline_idx) + .map(|x| x.iter()); + self.next_newline_idx += 1; + let x = self.newlines.next()?; + Some(Step::Newline(*x)) + }) + } +} + #[derive(Debug)] struct OffsetNewlineIter<'a> { text: &'a str, @@ -10,16 +60,35 @@ struct OffsetNewlineIter<'a> { } impl<'a> Iterator for OffsetNewlineIter<'a> { - type Item = TextUnit; - fn next(&mut self) -> Option { - let next_idx = self + type Item = Step; + fn next(&mut self) -> Option { + let (next, next_offset) = self .text .char_indices() - .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) + .filter_map(|(i, c)| { + if c == '\n' { + let next_offset = self.offset + TextUnit::from_usize(i + 1); + let next = Step::Newline(next_offset); + Some((next, next_offset)) + } else { + None + // TODO enable + // let char_len = TextUnit::of_char(c); + // if char_len.to_usize() > 1 { + // let start = self.offset + TextUnit::from_usize(i); + // let end = start + char_len; + // let next = Step::Utf16Char(TextRange::from_to(start, end)); + // let next_offset = end; + // Some((next, next_offset)) + // } else { + // None + // } + } + }) .next()?; - let next = self.offset + TextUnit::from_usize(next_idx); + let next_idx = (next_offset - self.offset).to_usize(); self.text = &self.text[next_idx..]; - self.offset = next; + self.offset = next_offset; Some(next) } } @@ -83,12 +152,16 @@ impl<'a, 'b> Edits<'a, 'b> { res } - fn next_newlines(&mut self, candidate: TextUnit) -> NextNewlines { + fn next_step(&mut self, step: &Step) -> NextNewlines { + let step_pos = match step { + &Step::Newline(n) => n, + &Step::Utf16Char(r) => unimplemented!(), + }; let res = match &mut self.current { Some(edit) => { - if candidate <= edit.delete.start() { + if step_pos <= edit.delete.start() { NextNewlines::Use - } else if candidate <= edit.delete.end() { + } else if step_pos <= edit.delete.end() { let iter = OffsetNewlineIter { offset: edit.delete.start(), text: &edit.insert, @@ -129,6 +202,17 @@ impl<'a, 'b> Edits<'a, 'b> { TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) } } + + fn translate_step(&self, x: &Step) -> Step { + if self.acc_diff == 0 { + x.clone() + } else { + match x { + &Step::Newline(n) => Step::Newline(self.translate(n)), + &Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), + } + } + } } pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { @@ -143,33 +227,38 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut lines: u32 = 0; - macro_rules! test_newline { + macro_rules! test_step { ($x:ident) => { - if offset < $x { - return lines; - } else { - lines += 1; + match &$x { + Step::Newline(n) => { + if offset < *n { + return lines; + } else { + lines += 1; + } + } + Step::Utf16Char(x) => unimplemented!(), } }; } - for &orig_newline in line_index.newlines() { + for orig_step in LineIndexStepIter::from(line_index) { loop { - let translated_newline = state.translate(orig_newline); - match state.next_newlines(translated_newline) { + let translated_newline = state.translate_step(&orig_step); + match state.next_step(&translated_newline) { NextNewlines::Use => { - test_newline!(translated_newline); + test_step!(translated_newline); break; } NextNewlines::ReplaceMany(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } break; } NextNewlines::AddMany(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } } } @@ -181,7 +270,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex None => break, Some(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } } } -- cgit v1.2.3 From 5c8525ce4ae8bb969f2ac263bf14adad1c835c03 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 15:44:27 +0100 Subject: column translation implemented but not quite working yet --- crates/ra_editor/src/line_index.rs | 14 +---- crates/ra_editor/src/line_index_utils.rs | 102 ++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 48 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index c29e2e49a..6dbabd97e 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -4,8 +4,8 @@ use superslice::Ext; #[derive(Clone, Debug, PartialEq, Eq)] pub struct LineIndex { - newlines: Vec, - utf16_lines: FxHashMap>, + pub(crate) newlines: Vec, + pub(crate) utf16_lines: FxHashMap>, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -120,16 +120,6 @@ impl LineIndex { col } - - pub(crate) fn newlines(&self) -> &[TextUnit] { - &self.newlines[..] - } - - pub(crate) fn utf16_chars(&self, newline_idx: usize) -> Option<&[Utf16Char]> { - self.utf16_lines - .get(&(newline_idx as u32)) - .map(|x| x.as_slice()) - } } #[test] diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 9c8d801e9..382f3ac72 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -9,18 +9,17 @@ enum Step { Utf16Char(TextRange), } +#[derive(Debug)] struct LineIndexStepIter<'a> { line_index: &'a LineIndex, - newlines: std::slice::Iter<'a, TextUnit>, next_newline_idx: usize, - utf16_chars: Option>, + utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>, } impl<'a> LineIndexStepIter<'a> { fn from(line_index: &LineIndex) -> LineIndexStepIter { let mut x = LineIndexStepIter { line_index, - newlines: line_index.newlines().iter(), next_newline_idx: 0, utf16_chars: None, }; @@ -35,20 +34,22 @@ impl<'a> Iterator for LineIndexStepIter<'a> { fn next(&mut self) -> Option { self.utf16_chars .as_mut() - .and_then(|x| { - None - // TODO Enable - // let x = x.next()?; - // Some(Step::Utf16Char(TextRange::from_to(x.start, x.end))) + .and_then(|(newline, x)| { + let x = x.next()?; + Some(Step::Utf16Char(TextRange::from_to( + *newline + x.start, + *newline + x.end, + ))) }) .or_else(|| { + let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; self.utf16_chars = self .line_index - .utf16_chars(self.next_newline_idx) - .map(|x| x.iter()); + .utf16_lines + .get(&(self.next_newline_idx as u32)) + .map(|x| (next_newline, x.iter())); self.next_newline_idx += 1; - let x = self.newlines.next()?; - Some(Step::Newline(*x)) + Some(Step::Newline(next_newline)) }) } } @@ -71,18 +72,16 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { let next = Step::Newline(next_offset); Some((next, next_offset)) } else { - None - // TODO enable - // let char_len = TextUnit::of_char(c); - // if char_len.to_usize() > 1 { - // let start = self.offset + TextUnit::from_usize(i); - // let end = start + char_len; - // let next = Step::Utf16Char(TextRange::from_to(start, end)); - // let next_offset = end; - // Some((next, next_offset)) - // } else { - // None - // } + let char_len = TextUnit::of_char(c); + if char_len.to_usize() > 1 { + let start = self.offset + TextUnit::from_usize(i); + let end = start + char_len; + let next = Step::Utf16Char(TextRange::from_to(start, end)); + let next_offset = end; + Some((next, next_offset)) + } else { + None + } } }) .next()?; @@ -155,7 +154,7 @@ impl<'a, 'b> Edits<'a, 'b> { fn next_step(&mut self, step: &Step) -> NextNewlines { let step_pos = match step { &Step::Newline(n) => n, - &Step::Utf16Char(r) => unimplemented!(), + &Step::Utf16Char(r) => r.start(), }; let res = match &mut self.current { Some(edit) => { @@ -215,7 +214,11 @@ impl<'a, 'b> Edits<'a, 'b> { } } -pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn translate_offset_with_edit( + line_index: &LineIndex, + offset: TextUnit, + edits: &[AtomTextEdit], +) -> LineCol { let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); for edit in edits { let insert_index = @@ -225,29 +228,55 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut state = Edits::new(&sorted_edits); - let mut lines: u32 = 0; + let mut pos: LineCol = LineCol { + line: 0, + col_utf16: 0, + }; + + let mut last_newline: TextUnit = TextUnit::from(0); + let mut col_adjust: TextUnit = TextUnit::from(0); macro_rules! test_step { ($x:ident) => { match &$x { Step::Newline(n) => { if offset < *n { - return lines; + return_pos!(); + } else if offset == *n { + pos.line += 1; + pos.col_utf16 = 0; + return pos; } else { - lines += 1; + pos.line += 1; + pos.col_utf16 = 0; + last_newline = *n; + col_adjust = TextUnit::from(0); + } + } + Step::Utf16Char(x) => { + if offset < x.end() { + return_pos!(); + } else { + col_adjust += x.len() - TextUnit::from(1); } } - Step::Utf16Char(x) => unimplemented!(), } }; } + macro_rules! return_pos { + () => { + pos.col_utf16 = ((offset - last_newline) - col_adjust).into(); + return pos; + }; + } + for orig_step in LineIndexStepIter::from(line_index) { loop { - let translated_newline = state.translate_step(&orig_step); - match state.next_step(&translated_newline) { + let translated_step = state.translate_step(&orig_step); + match state.next_step(&translated_step) { NextNewlines::Use => { - test_step!(translated_newline); + test_step!(translated_step); break; } NextNewlines::ReplaceMany(ns) => { @@ -276,7 +305,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex } } - lines + return_pos!(); } // for bench @@ -337,8 +366,9 @@ mod test { fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + // assert_eq!(actual, expected); + assert_eq!(actual.line, expected.line); } } } -- cgit v1.2.3 From dc2afae991892719b97b0e4b40d8483b43b08680 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 20:52:43 +0100 Subject: fix arbitrary offset generation, col translation working --- crates/ra_editor/src/line_index.rs | 28 +++++++++ crates/ra_editor/src/line_index_utils.rs | 103 +++++++++++++++++++------------ 2 files changed, 92 insertions(+), 39 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 6dbabd97e..b01760313 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -62,6 +62,12 @@ impl LineIndex { curr_col += char_len; } + + // Save any utf-16 characters seen in the last line + if utf16_chars.len() > 0 { + utf16_lines.insert(line, utf16_chars); + } + LineIndex { newlines, utf16_lines, @@ -122,6 +128,28 @@ impl LineIndex { } } +// for bench and test +pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { + let mut res = LineCol { + line: 0, + col_utf16: 0, + }; + for (i, c) in text.char_indices() { + if i + c.len_utf8() > offset.to_usize() { + // if it's an invalid offset, inside a multibyte char + // return as if it was at the start of the char + break; + } + if c == '\n' { + res.line += 1; + res.col_utf16 = 0; + } else { + res.col_utf16 += 1; + } + } + res +} + #[test] fn test_line_index() { let text = "hello\nworld"; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 382f3ac72..913ca5b15 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::Utf16Char}; +use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; use superslice::Ext; #[derive(Debug, Clone)] @@ -154,7 +154,7 @@ impl<'a, 'b> Edits<'a, 'b> { fn next_step(&mut self, step: &Step) -> NextNewlines { let step_pos = match step { &Step::Newline(n) => n, - &Step::Utf16Char(r) => r.start(), + &Step::Utf16Char(r) => r.end(), }; let res = match &mut self.current { Some(edit) => { @@ -214,6 +214,40 @@ impl<'a, 'b> Edits<'a, 'b> { } } +#[derive(Debug)] +struct RunningLineCol { + line: u32, + last_newline: TextUnit, + col_adjust: TextUnit, +} + +impl RunningLineCol { + fn new() -> RunningLineCol { + RunningLineCol { + line: 0, + last_newline: TextUnit::from(0), + col_adjust: TextUnit::from(0), + } + } + + fn to_line_col(&self, offset: TextUnit) -> LineCol { + LineCol { + line: self.line, + col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), + } + } + + fn add_line(&mut self, newline: TextUnit) { + self.line += 1; + self.last_newline = newline; + self.col_adjust = TextUnit::from(0); + } + + fn adjust_col(&mut self, range: &TextRange) { + self.col_adjust += range.len() - TextUnit::from(1); + } +} + pub fn translate_offset_with_edit( line_index: &LineIndex, offset: TextUnit, @@ -228,49 +262,35 @@ pub fn translate_offset_with_edit( let mut state = Edits::new(&sorted_edits); - let mut pos: LineCol = LineCol { - line: 0, - col_utf16: 0, - }; - - let mut last_newline: TextUnit = TextUnit::from(0); - let mut col_adjust: TextUnit = TextUnit::from(0); + let mut res = RunningLineCol::new(); macro_rules! test_step { ($x:ident) => { match &$x { Step::Newline(n) => { if offset < *n { - return_pos!(); + return res.to_line_col(offset); } else if offset == *n { - pos.line += 1; - pos.col_utf16 = 0; - return pos; + res.add_line(*n); + return res.to_line_col(offset); } else { - pos.line += 1; - pos.col_utf16 = 0; - last_newline = *n; - col_adjust = TextUnit::from(0); + res.add_line(*n); } } Step::Utf16Char(x) => { if offset < x.end() { - return_pos!(); + // if the offset is inside a multibyte char it's invalid + // clamp it to the start of the char + let clamp = offset.min(x.start()); + return res.to_line_col(clamp); } else { - col_adjust += x.len() - TextUnit::from(1); + res.adjust_col(x); } } } }; } - macro_rules! return_pos { - () => { - pos.col_utf16 = ((offset - last_newline) - col_adjust).into(); - return pos; - }; - } - for orig_step in LineIndexStepIter::from(line_index) { loop { let translated_step = state.translate_step(&orig_step); @@ -305,7 +325,7 @@ pub fn translate_offset_with_edit( } } - return_pos!(); + res.to_line_col(offset) } // for bench @@ -315,8 +335,7 @@ pub fn translate_after_edit( edits: Vec, ) -> LineCol { let text = edit_text(pre_edit_text, edits); - let line_index = LineIndex::new(&text); - line_index.line_col(offset) + line_index::to_line_col(&text, offset) } fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { @@ -343,6 +362,7 @@ mod test { #[derive(Debug)] struct ArbTextWithOffsetAndEdits { text: String, + edited_text: String, offset: TextUnit, edits: Vec, } @@ -350,13 +370,18 @@ mod test { fn arb_text_with_offset_and_edits() -> BoxedStrategy { arb_text() .prop_flat_map(|text| { - (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( - |(offset, edits, text)| ArbTextWithOffsetAndEdits { - text, - offset, - edits, - }, - ) + (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { + let edited_text = edit_text(&text, edits.clone()); + let arb_offset = arb_offset(&edited_text); + (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( + |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { + text, + edits, + edited_text, + offset, + }, + ) + }) }) .boxed() } @@ -364,11 +389,11 @@ mod test { proptest! { #[test] fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); - // assert_eq!(actual, expected); - assert_eq!(actual.line, expected.line); + + assert_eq!(actual, expected); } } } -- cgit v1.2.3 From c886b72dab918d6f4d3be025135f769bc429cc79 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 22:11:10 +0100 Subject: make criterion args work, small simplification --- crates/ra_editor/src/line_index_utils.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 913ca5b15..632c962cc 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -270,9 +270,6 @@ pub fn translate_offset_with_edit( Step::Newline(n) => { if offset < *n { return res.to_line_col(offset); - } else if offset == *n { - res.add_line(*n); - return res.to_line_col(offset); } else { res.add_line(*n); } -- cgit v1.2.3 From aff0124b37e34910190498ad519deb1aea0d8451 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 14:01:36 +0100 Subject: add line_index proptest --- crates/ra_editor/src/line_index.rs | 234 +++++++++++++++++-------------- crates/ra_editor/src/line_index_utils.rs | 4 +- 2 files changed, 132 insertions(+), 106 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index b01760313..5304fbcf6 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -128,7 +128,8 @@ impl LineIndex { } } -// for bench and test +/// Simple reference implementation to use in proptests +/// and benchmarks as baseline pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { let mut res = LineCol { line: 0, @@ -150,111 +151,135 @@ pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { res } -#[test] -fn test_line_index() { - let text = "hello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 0, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(5.into()), - LineCol { - line: 0, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(8.into()), - LineCol { - line: 1, - col_utf16: 2 - } - ); - assert_eq!( - index.line_col(10.into()), - LineCol { - line: 1, - col_utf16: 4 - } - ); - assert_eq!( - index.line_col(11.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(12.into()), - LineCol { - line: 1, - col_utf16: 6 - } - ); +#[cfg(test)] +mod test_line_index { + use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; + use ra_text_edit::test_utils::{arb_text, arb_offset}; - let text = "\nhello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(2.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 2, - col_utf16: 0 + #[test] + fn test_line_index() { + let text = "hello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 0, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(5.into()), + LineCol { + line: 0, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(8.into()), + LineCol { + line: 1, + col_utf16: 2 + } + ); + assert_eq!( + index.line_col(10.into()), + LineCol { + line: 1, + col_utf16: 4 + } + ); + assert_eq!( + index.line_col(11.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(12.into()), + LineCol { + line: 1, + col_utf16: 6 + } + ); + + let text = "\nhello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(2.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 2, + col_utf16: 0 + } + ); + } + + fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { + arb_text() + .prop_flat_map(|text| (arb_offset(&text), Just(text))) + .boxed() + } + + proptest! { + #[test] + fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { + let expected = to_line_col(&text, offset); + let line_index = LineIndex::new(&text); + let actual = line_index.line_col(offset); + + assert_eq!(actual, expected); } - ); + } } #[cfg(test)] @@ -349,4 +374,5 @@ const C: char = \"メ メ\"; assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); } + } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 632c962cc..a0bb9a6dd 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -325,7 +325,7 @@ pub fn translate_offset_with_edit( res.to_line_col(offset) } -// for bench +/// Simplest implementation to use as reference in proptest and benchmarks pub fn translate_after_edit( pre_edit_text: &str, offset: TextUnit, @@ -352,8 +352,8 @@ fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { #[cfg(test)] mod test { - use proptest::{prelude::*, proptest, proptest_helper}; use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; #[derive(Debug)] -- cgit v1.2.3 From 6b2da4e5474ec064e44b7cf19523de1bab72ba27 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 15:49:14 +0100 Subject: use new translate_offset_with_edit for TryConvWith doc comments --- crates/ra_editor/src/lib.rs | 1 + crates/ra_editor/src/line_index_utils.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7145c6cfb..2e3635ea0 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -14,6 +14,7 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, + line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index a0bb9a6dd..ba3ac8aeb 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; +use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; use superslice::Ext; #[derive(Debug, Clone)] -- cgit v1.2.3 From 863ed19946d6f707ce09dd77bf26b26be73e097c Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 24 Dec 2018 17:01:25 +0100 Subject: remove benchmark and simplify tests --- crates/ra_editor/src/line_index.rs | 23 +++++++++++- crates/ra_editor/src/line_index_utils.rs | 63 ++++++++++---------------------- 2 files changed, 41 insertions(+), 45 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 5304fbcf6..898fee7e0 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -128,8 +128,8 @@ impl LineIndex { } } +#[cfg(test)] /// Simple reference implementation to use in proptests -/// and benchmarks as baseline pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { let mut res = LineCol { line: 0, @@ -270,6 +270,27 @@ mod test_line_index { .boxed() } + fn to_line_col(text: &str, offset: TextUnit) -> LineCol { + let mut res = LineCol { + line: 0, + col_utf16: 0, + }; + for (i, c) in text.char_indices() { + if i + c.len_utf8() > offset.to_usize() { + // if it's an invalid offset, inside a multibyte char + // return as if it was at the start of the char + break; + } + if c == '\n' { + res.line += 1; + res.col_utf16 = 0; + } else { + res.col_utf16 += 1; + } + } + res + } + proptest! { #[test] fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index ba3ac8aeb..b8b149442 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; +use crate::{LineIndex, LineCol, line_index::Utf16Char}; use superslice::Ext; #[derive(Debug, Clone)] @@ -325,59 +325,34 @@ pub fn translate_offset_with_edit( res.to_line_col(offset) } -/// Simplest implementation to use as reference in proptest and benchmarks -pub fn translate_after_edit( - pre_edit_text: &str, - offset: TextUnit, - edits: Vec, -) -> LineCol { - let text = edit_text(pre_edit_text, edits); - line_index::to_line_col(&text, offset) -} - -fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { - // apply edits ordered from last to first - // since they should not overlap we can just use start() - edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - - let mut text = pre_edit_text.to_owned(); - - for edit in &edits { - let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); - text.replace_range(range, &edit.insert); - } - - text -} - #[cfg(test)] mod test { use super::*; use proptest::{prelude::*, proptest, proptest_helper}; - use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + use crate::line_index; + use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits}; + use ra_text_edit::TextEdit; #[derive(Debug)] struct ArbTextWithOffsetAndEdits { text: String, + edits: TextEdit, edited_text: String, offset: TextUnit, - edits: Vec, } - fn arb_text_with_offset_and_edits() -> BoxedStrategy { - arb_text() - .prop_flat_map(|text| { - (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { - let edited_text = edit_text(&text, edits.clone()); - let arb_offset = arb_offset(&edited_text); - (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( - |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { - text, - edits, - edited_text, - offset, - }, - ) + fn arb_text_with_edits_and_offset() -> BoxedStrategy { + arb_text_with_edits() + .prop_flat_map(|x| { + let edited_text = x.edits.apply(&x.text); + let arb_offset = arb_offset(&edited_text); + (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { + ArbTextWithOffsetAndEdits { + text: x.text, + edits: x.edits, + edited_text, + offset, + } }) }) .boxed() @@ -385,10 +360,10 @@ mod test { proptest! { #[test] - fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) { let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms()); assert_eq!(actual, expected); } -- cgit v1.2.3 From e9c186e48a77b536053c0f75ac9ea5b2fd322cfa Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 25 Dec 2018 20:49:18 +0100 Subject: change to `TextEdit` to avoid allocation and sort rename newline to step where applicable --- crates/ra_editor/src/line_index_utils.rs | 86 +++++++++++++++----------------- 1 file changed, 39 insertions(+), 47 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index b8b149442..ec3269bbb 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,7 +1,6 @@ -use ra_text_edit::AtomTextEdit; +use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol, line_index::Utf16Char}; -use superslice::Ext; #[derive(Debug, Clone)] enum Step { @@ -55,12 +54,12 @@ impl<'a> Iterator for LineIndexStepIter<'a> { } #[derive(Debug)] -struct OffsetNewlineIter<'a> { +struct OffsetStepIter<'a> { text: &'a str, offset: TextUnit, } -impl<'a> Iterator for OffsetNewlineIter<'a> { +impl<'a> Iterator for OffsetStepIter<'a> { type Item = Step; fn next(&mut self) -> Option { let (next, next_offset) = self @@ -93,10 +92,10 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } #[derive(Debug)] -enum NextNewlines<'a> { +enum NextSteps<'a> { Use, - ReplaceMany(OffsetNewlineIter<'a>), - AddMany(OffsetNewlineIter<'a>), + ReplaceMany(OffsetStepIter<'a>), + AddMany(OffsetStepIter<'a>), } #[derive(Debug)] @@ -106,16 +105,16 @@ struct TranslatedEdit<'a> { diff: i64, } -struct Edits<'a, 'b> { - edits: &'b [&'a AtomTextEdit], +struct Edits<'a> { + edits: &'a [AtomTextEdit], current: Option>, acc_diff: i64, } -impl<'a, 'b> Edits<'a, 'b> { - fn new(sorted_edits: &'b [&'a AtomTextEdit]) -> Edits<'a, 'b> { +impl<'a> Edits<'a> { + fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { let mut x = Edits { - edits: sorted_edits, + edits: text_edit.as_atoms(), current: None, acc_diff: 0, }; @@ -141,9 +140,9 @@ impl<'a, 'b> Edits<'a, 'b> { } } - fn next_inserted_newlines(&mut self) -> Option> { + fn next_inserted_steps(&mut self) -> Option> { let cur = self.current.as_ref()?; - let res = Some(OffsetNewlineIter { + let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert, }); @@ -151,7 +150,7 @@ impl<'a, 'b> Edits<'a, 'b> { res } - fn next_step(&mut self, step: &Step) -> NextNewlines { + fn next_steps(&mut self, step: &Step) -> NextSteps { let step_pos = match step { &Step::Newline(n) => n, &Step::Utf16Char(r) => r.end(), @@ -159,27 +158,27 @@ impl<'a, 'b> Edits<'a, 'b> { let res = match &mut self.current { Some(edit) => { if step_pos <= edit.delete.start() { - NextNewlines::Use + NextSteps::Use } else if step_pos <= edit.delete.end() { - let iter = OffsetNewlineIter { + let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert, }; - // empty slice + // empty slice to avoid returning steps again edit.insert = &edit.insert[edit.insert.len()..]; - NextNewlines::ReplaceMany(iter) + NextSteps::ReplaceMany(iter) } else { - let iter = OffsetNewlineIter { + let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert, }; - // empty slice + // empty slice to avoid returning steps again edit.insert = &edit.insert[edit.insert.len()..]; self.advance_edit(); - NextNewlines::AddMany(iter) + NextSteps::AddMany(iter) } } - None => NextNewlines::Use, + None => NextSteps::Use, }; res } @@ -251,16 +250,9 @@ impl RunningLineCol { pub fn translate_offset_with_edit( line_index: &LineIndex, offset: TextUnit, - edits: &[AtomTextEdit], + text_edit: &TextEdit, ) -> LineCol { - let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); - for edit in edits { - let insert_index = - sorted_edits.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); - sorted_edits.insert(insert_index, &edit); - } - - let mut state = Edits::new(&sorted_edits); + let mut state = Edits::from_text_edit(&text_edit); let mut res = RunningLineCol::new(); @@ -291,18 +283,18 @@ pub fn translate_offset_with_edit( for orig_step in LineIndexStepIter::from(line_index) { loop { let translated_step = state.translate_step(&orig_step); - match state.next_step(&translated_step) { - NextNewlines::Use => { + match state.next_steps(&translated_step) { + NextSteps::Use => { test_step!(translated_step); break; } - NextNewlines::ReplaceMany(ns) => { + NextSteps::ReplaceMany(ns) => { for n in ns { test_step!(n); } break; } - NextNewlines::AddMany(ns) => { + NextSteps::AddMany(ns) => { for n in ns { test_step!(n); } @@ -312,7 +304,7 @@ pub fn translate_offset_with_edit( } loop { - match state.next_inserted_newlines() { + match state.next_inserted_steps() { None => break, Some(ns) => { for n in ns { @@ -330,26 +322,26 @@ mod test { use super::*; use proptest::{prelude::*, proptest, proptest_helper}; use crate::line_index; - use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits}; + use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit}; use ra_text_edit::TextEdit; #[derive(Debug)] - struct ArbTextWithOffsetAndEdits { + struct ArbTextWithEditAndOffset { text: String, - edits: TextEdit, + edit: TextEdit, edited_text: String, offset: TextUnit, } - fn arb_text_with_edits_and_offset() -> BoxedStrategy { - arb_text_with_edits() + fn arb_text_with_edit_and_offset() -> BoxedStrategy { + arb_text_with_edit() .prop_flat_map(|x| { - let edited_text = x.edits.apply(&x.text); + let edited_text = x.edit.apply(&x.text); let arb_offset = arb_offset(&edited_text); (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { - ArbTextWithOffsetAndEdits { + ArbTextWithEditAndOffset { text: x.text, - edits: x.edits, + edit: x.edit, edited_text, offset, } @@ -360,10 +352,10 @@ mod test { proptest! { #[test] - fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) { + fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) { let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms()); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit); assert_eq!(actual, expected); } -- cgit v1.2.3 From 1cda43aafd623b400f5916b1d3727b56c136081b Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 25 Dec 2018 21:26:36 +0100 Subject: test code and dependency cleanup --- crates/ra_editor/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 2e3635ea0..619497f0b 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,8 +2,7 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; -// public for benchmarkig -pub mod line_index_utils; +mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils; -- cgit v1.2.3