2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{line_index::Utf16Char, LineCol, LineIndex};
|
|
|
|
use ra_syntax::{TextRange, TextUnit};
|
2018-12-25 13:49:18 -06:00
|
|
|
use ra_text_edit::{AtomTextEdit, TextEdit};
|
2018-12-18 11:46:54 -06:00
|
|
|
|
2018-12-22 05:59:48 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Step {
|
|
|
|
Newline(TextUnit),
|
|
|
|
Utf16Char(TextRange),
|
|
|
|
}
|
|
|
|
|
2018-12-22 08:44:27 -06:00
|
|
|
#[derive(Debug)]
|
2018-12-22 05:59:48 -06:00
|
|
|
struct LineIndexStepIter<'a> {
|
|
|
|
line_index: &'a LineIndex,
|
|
|
|
next_newline_idx: usize,
|
2018-12-22 08:44:27 -06:00
|
|
|
utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>,
|
2018-12-22 05:59:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LineIndexStepIter<'a> {
|
|
|
|
fn from(line_index: &LineIndex) -> LineIndexStepIter {
|
2019-02-08 05:49:43 -06:00
|
|
|
let mut x = LineIndexStepIter { line_index, next_newline_idx: 0, utf16_chars: None };
|
2018-12-22 05:59:48 -06:00
|
|
|
// 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<Step> {
|
|
|
|
self.utf16_chars
|
|
|
|
.as_mut()
|
2018-12-22 08:44:27 -06:00
|
|
|
.and_then(|(newline, x)| {
|
|
|
|
let x = x.next()?;
|
2019-02-08 05:49:43 -06:00
|
|
|
Some(Step::Utf16Char(TextRange::from_to(*newline + x.start, *newline + x.end)))
|
2018-12-22 05:59:48 -06:00
|
|
|
})
|
|
|
|
.or_else(|| {
|
2018-12-22 08:44:27 -06:00
|
|
|
let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
|
2018-12-22 05:59:48 -06:00
|
|
|
self.utf16_chars = self
|
|
|
|
.line_index
|
2018-12-22 08:44:27 -06:00
|
|
|
.utf16_lines
|
|
|
|
.get(&(self.next_newline_idx as u32))
|
|
|
|
.map(|x| (next_newline, x.iter()));
|
2018-12-22 05:59:48 -06:00
|
|
|
self.next_newline_idx += 1;
|
2018-12-22 08:44:27 -06:00
|
|
|
Some(Step::Newline(next_newline))
|
2018-12-22 05:59:48 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:46:54 -06:00
|
|
|
#[derive(Debug)]
|
2018-12-25 13:49:18 -06:00
|
|
|
struct OffsetStepIter<'a> {
|
2018-12-18 11:46:54 -06:00
|
|
|
text: &'a str,
|
|
|
|
offset: TextUnit,
|
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
impl<'a> Iterator for OffsetStepIter<'a> {
|
2018-12-22 05:59:48 -06:00
|
|
|
type Item = Step;
|
|
|
|
fn next(&mut self) -> Option<Step> {
|
|
|
|
let (next, next_offset) = self
|
2018-12-18 11:46:54 -06:00
|
|
|
.text
|
|
|
|
.char_indices()
|
2018-12-22 05:59:48 -06:00
|
|
|
.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 {
|
2018-12-22 08:44:27 -06:00
|
|
|
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
|
|
|
|
}
|
2018-12-22 05:59:48 -06:00
|
|
|
}
|
|
|
|
})
|
2018-12-18 11:46:54 -06:00
|
|
|
.next()?;
|
2018-12-22 05:59:48 -06:00
|
|
|
let next_idx = (next_offset - self.offset).to_usize();
|
2018-12-18 11:46:54 -06:00
|
|
|
self.text = &self.text[next_idx..];
|
2018-12-22 05:59:48 -06:00
|
|
|
self.offset = next_offset;
|
2018-12-18 11:46:54 -06:00
|
|
|
Some(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 11:38:28 -06:00
|
|
|
#[derive(Debug)]
|
2018-12-25 13:49:18 -06:00
|
|
|
enum NextSteps<'a> {
|
2018-12-21 11:38:28 -06:00
|
|
|
Use,
|
2018-12-25 13:49:18 -06:00
|
|
|
ReplaceMany(OffsetStepIter<'a>),
|
|
|
|
AddMany(OffsetStepIter<'a>),
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct TranslatedEdit<'a> {
|
2018-12-18 11:46:54 -06:00
|
|
|
delete: TextRange,
|
|
|
|
insert: &'a str,
|
2018-12-21 11:38:28 -06:00
|
|
|
diff: i64,
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
struct Edits<'a> {
|
|
|
|
edits: &'a [AtomTextEdit],
|
2018-12-21 11:38:28 -06:00
|
|
|
current: Option<TranslatedEdit<'a>>,
|
|
|
|
acc_diff: i64,
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
impl<'a> Edits<'a> {
|
|
|
|
fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> {
|
2019-02-08 05:49:43 -06:00
|
|
|
let mut x = Edits { edits: text_edit.as_atoms(), current: None, acc_diff: 0 };
|
2018-12-21 11:38:28 -06:00
|
|
|
x.advance_edit();
|
|
|
|
x
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
2018-12-21 11:38:28 -06:00
|
|
|
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)) => {
|
2018-12-21 11:51:31 -06:00
|
|
|
let delete = self.translate_range(next.delete);
|
2018-12-21 11:38:28 -06:00
|
|
|
let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64;
|
2019-02-08 05:49:43 -06:00
|
|
|
self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff });
|
2018-12-21 11:38:28 -06:00
|
|
|
self.edits = rest;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.current = None;
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> {
|
2018-12-21 11:38:28 -06:00
|
|
|
let cur = self.current.as_ref()?;
|
2019-02-08 05:49:43 -06:00
|
|
|
let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert });
|
2018-12-21 11:38:28 -06:00
|
|
|
self.advance_edit();
|
|
|
|
res
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
fn next_steps(&mut self, step: &Step) -> NextSteps {
|
2019-06-03 09:07:00 -05:00
|
|
|
let step_pos = match *step {
|
|
|
|
Step::Newline(n) => n,
|
|
|
|
Step::Utf16Char(r) => r.end(),
|
2018-12-22 05:59:48 -06:00
|
|
|
};
|
2019-07-04 12:26:44 -05:00
|
|
|
match &mut self.current {
|
2018-12-21 11:38:28 -06:00
|
|
|
Some(edit) => {
|
2018-12-22 05:59:48 -06:00
|
|
|
if step_pos <= edit.delete.start() {
|
2018-12-25 13:49:18 -06:00
|
|
|
NextSteps::Use
|
2018-12-22 05:59:48 -06:00
|
|
|
} else if step_pos <= edit.delete.end() {
|
2019-02-08 05:49:43 -06:00
|
|
|
let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert };
|
2018-12-25 13:49:18 -06:00
|
|
|
// empty slice to avoid returning steps again
|
2018-12-21 11:38:28 -06:00
|
|
|
edit.insert = &edit.insert[edit.insert.len()..];
|
2018-12-25 13:49:18 -06:00
|
|
|
NextSteps::ReplaceMany(iter)
|
2018-12-21 11:38:28 -06:00
|
|
|
} else {
|
2019-02-08 05:49:43 -06:00
|
|
|
let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert };
|
2018-12-25 13:49:18 -06:00
|
|
|
// empty slice to avoid returning steps again
|
2018-12-21 11:38:28 -06:00
|
|
|
edit.insert = &edit.insert[edit.insert.len()..];
|
|
|
|
self.advance_edit();
|
2018-12-25 13:49:18 -06:00
|
|
|
NextSteps::AddMany(iter)
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
2018-12-25 13:49:18 -06:00
|
|
|
None => NextSteps::Use,
|
2019-07-04 12:26:44 -05:00
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
2018-12-21 11:51:31 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-12-22 05:59:48 -06:00
|
|
|
|
|
|
|
fn translate_step(&self, x: &Step) -> Step {
|
|
|
|
if self.acc_diff == 0 {
|
|
|
|
x.clone()
|
|
|
|
} else {
|
2019-06-03 09:07:00 -05:00
|
|
|
match *x {
|
|
|
|
Step::Newline(n) => Step::Newline(self.translate(n)),
|
|
|
|
Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)),
|
2018-12-22 05:59:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-22 13:52:43 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct RunningLineCol {
|
|
|
|
line: u32,
|
|
|
|
last_newline: TextUnit,
|
|
|
|
col_adjust: TextUnit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RunningLineCol {
|
|
|
|
fn new() -> RunningLineCol {
|
2019-02-08 05:49:43 -06:00
|
|
|
RunningLineCol { line: 0, last_newline: TextUnit::from(0), col_adjust: TextUnit::from(0) }
|
2018-12-22 13:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-05 11:02:32 -05:00
|
|
|
fn adjust_col(&mut self, range: TextRange) {
|
2018-12-22 13:52:43 -06:00
|
|
|
self.col_adjust += range.len() - TextUnit::from(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 08:44:27 -06:00
|
|
|
pub fn translate_offset_with_edit(
|
|
|
|
line_index: &LineIndex,
|
|
|
|
offset: TextUnit,
|
2018-12-25 13:49:18 -06:00
|
|
|
text_edit: &TextEdit,
|
2018-12-22 08:44:27 -06:00
|
|
|
) -> LineCol {
|
2018-12-25 13:49:18 -06:00
|
|
|
let mut state = Edits::from_text_edit(&text_edit);
|
2018-12-21 11:38:28 -06:00
|
|
|
|
2018-12-22 13:52:43 -06:00
|
|
|
let mut res = RunningLineCol::new();
|
2018-12-21 11:38:28 -06:00
|
|
|
|
2018-12-22 05:59:48 -06:00
|
|
|
macro_rules! test_step {
|
2018-12-21 13:11:27 -06:00
|
|
|
($x:ident) => {
|
2018-12-22 05:59:48 -06:00
|
|
|
match &$x {
|
|
|
|
Step::Newline(n) => {
|
|
|
|
if offset < *n {
|
2018-12-22 13:52:43 -06:00
|
|
|
return res.to_line_col(offset);
|
2018-12-22 05:59:48 -06:00
|
|
|
} else {
|
2018-12-22 13:52:43 -06:00
|
|
|
res.add_line(*n);
|
2018-12-22 08:44:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Step::Utf16Char(x) => {
|
|
|
|
if offset < x.end() {
|
2018-12-22 13:52:43 -06:00
|
|
|
// 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);
|
2018-12-22 08:44:27 -06:00
|
|
|
} else {
|
2019-07-05 11:02:32 -05:00
|
|
|
res.adjust_col(*x);
|
2018-12-22 05:59:48 -06:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 13:11:27 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-22 05:59:48 -06:00
|
|
|
for orig_step in LineIndexStepIter::from(line_index) {
|
2018-12-21 11:38:28 -06:00
|
|
|
loop {
|
2018-12-22 08:44:27 -06:00
|
|
|
let translated_step = state.translate_step(&orig_step);
|
2018-12-25 13:49:18 -06:00
|
|
|
match state.next_steps(&translated_step) {
|
|
|
|
NextSteps::Use => {
|
2018-12-22 08:44:27 -06:00
|
|
|
test_step!(translated_step);
|
2018-12-21 11:38:28 -06:00
|
|
|
break;
|
|
|
|
}
|
2018-12-25 13:49:18 -06:00
|
|
|
NextSteps::ReplaceMany(ns) => {
|
2018-12-21 11:38:28 -06:00
|
|
|
for n in ns {
|
2018-12-22 05:59:48 -06:00
|
|
|
test_step!(n);
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-12-25 13:49:18 -06:00
|
|
|
NextSteps::AddMany(ns) => {
|
2018-12-21 11:38:28 -06:00
|
|
|
for n in ns {
|
2018-12-22 05:59:48 -06:00
|
|
|
test_step!(n);
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-21 11:38:28 -06:00
|
|
|
loop {
|
2018-12-25 13:49:18 -06:00
|
|
|
match state.next_inserted_steps() {
|
2018-12-21 11:38:28 -06:00
|
|
|
None => break,
|
|
|
|
Some(ns) => {
|
|
|
|
for n in ns {
|
2018-12-22 05:59:48 -06:00
|
|
|
test_step!(n);
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
2018-12-22 13:52:43 -06:00
|
|
|
res.to_line_col(offset)
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2018-12-24 10:01:25 -06:00
|
|
|
use crate::line_index;
|
2019-07-04 15:05:17 -05:00
|
|
|
use proptest::{prelude::*, proptest};
|
2018-12-25 13:49:18 -06:00
|
|
|
use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit};
|
2018-12-24 10:01:25 -06:00
|
|
|
use ra_text_edit::TextEdit;
|
2018-12-18 11:46:54 -06:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-12-25 13:49:18 -06:00
|
|
|
struct ArbTextWithEditAndOffset {
|
2018-12-18 11:46:54 -06:00
|
|
|
text: String,
|
2018-12-25 13:49:18 -06:00
|
|
|
edit: TextEdit,
|
2018-12-22 13:52:43 -06:00
|
|
|
edited_text: String,
|
2018-12-18 11:46:54 -06:00
|
|
|
offset: TextUnit,
|
|
|
|
}
|
|
|
|
|
2018-12-25 13:49:18 -06:00
|
|
|
fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> {
|
|
|
|
arb_text_with_edit()
|
2018-12-24 10:01:25 -06:00
|
|
|
.prop_flat_map(|x| {
|
2018-12-25 13:49:18 -06:00
|
|
|
let edited_text = x.edit.apply(&x.text);
|
2018-12-24 10:01:25 -06:00
|
|
|
let arb_offset = arb_offset(&edited_text);
|
|
|
|
(Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
|
2019-02-08 05:49:43 -06:00
|
|
|
ArbTextWithEditAndOffset { text: x.text, edit: x.edit, edited_text, offset }
|
2018-12-22 13:52:43 -06:00
|
|
|
})
|
2018-12-18 11:46:54 -06:00
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
proptest! {
|
|
|
|
#[test]
|
2018-12-25 13:49:18 -06:00
|
|
|
fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) {
|
2018-12-22 13:52:43 -06:00
|
|
|
let expected = line_index::to_line_col(&x.edited_text, x.offset);
|
2018-12-18 11:46:54 -06:00
|
|
|
let line_index = LineIndex::new(&x.text);
|
2018-12-25 13:49:18 -06:00
|
|
|
let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit);
|
2018-12-22 13:52:43 -06:00
|
|
|
|
|
|
|
assert_eq!(actual, expected);
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|
2018-12-21 11:38:28 -06:00
|
|
|
}
|
2018-12-18 11:46:54 -06:00
|
|
|
}
|