From 32c8ea93074286f3111317fe3077698c0afe929f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Aug 2018 12:44:26 +0300 Subject: Move atom edit to libsyntax2 --- crates/libeditor/src/edit.rs | 37 ++++++++++++------------------------- crates/libeditor/src/lib.rs | 3 ++- crates/libsyntax2/src/lib.rs | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/libeditor/src/edit.rs b/crates/libeditor/src/edit.rs index 3edd0809d..dcf1ee81e 100644 --- a/crates/libeditor/src/edit.rs +++ b/crates/libeditor/src/edit.rs @@ -1,16 +1,11 @@ use {TextRange, TextUnit}; +use libsyntax2::AtomEdit; #[derive(Debug, Clone)] pub struct Edit { atoms: Vec, } -#[derive(Debug, Clone)] -pub struct AtomEdit { - pub delete: TextRange, - pub insert: String, -} - #[derive(Debug)] pub struct EditBuilder { atoms: Vec @@ -21,23 +16,23 @@ impl EditBuilder { EditBuilder { atoms: Vec::new() } } - pub fn replace(&mut self, range: TextRange, replacement: String) { - self.atoms.push(AtomEdit { delete: range, insert: replacement }) + pub fn replace(&mut self, range: TextRange, replace_with: String) { + self.atoms.push(AtomEdit::replace(range, replace_with)) } pub fn delete(&mut self, range: TextRange) { - self.replace(range, String::new()); + self.atoms.push(AtomEdit::delete(range)) } pub fn insert(&mut self, offset: TextUnit, text: String) { - self.replace(TextRange::offset_len(offset, 0.into()), text) + self.atoms.push(AtomEdit::insert(offset, text)) } pub fn finish(self) -> Edit { let mut atoms = self.atoms; atoms.sort_by_key(|a| a.delete.start()); for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { - assert!(a1.end() <= a2.start()) + assert!(a1.delete.end() <= a2.delete.start()) } Edit { atoms } } @@ -52,16 +47,18 @@ impl Edit { let mut total_len = text.len(); for atom in self.atoms.iter() { total_len += atom.insert.len(); - total_len -= atom.end() - atom.start(); + total_len -= u32::from(atom.delete.end() - atom.delete.start()) as usize; } let mut buf = String::with_capacity(total_len); let mut prev = 0; for atom in self.atoms.iter() { - if atom.start() > prev { - buf.push_str(&text[prev..atom.start()]); + let start = u32::from(atom.delete.start()) as usize; + let end = u32::from(atom.delete.end()) as usize; + if start > prev { + buf.push_str(&text[prev..start]); } buf.push_str(&atom.insert); - prev = atom.end(); + prev = end; } buf.push_str(&text[prev..text.len()]); assert_eq!(buf.len(), total_len); @@ -83,13 +80,3 @@ impl Edit { Some(res) } } - -impl AtomEdit { - fn start(&self) -> usize { - u32::from(self.delete.start()) as usize - } - - fn end(&self) -> usize { - u32::from(self.delete.end()) as usize - } -} diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index a604d1951..55302265f 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs @@ -15,11 +15,12 @@ use libsyntax2::{ algo::{walk, find_leaf_at_offset}, SyntaxKind::{self, *}, }; +pub use libsyntax2::AtomEdit; pub use self::{ line_index::{LineIndex, LineCol}, extend_selection::extend_selection, symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, - edit::{EditBuilder, Edit, AtomEdit}, + edit::{EditBuilder, Edit}, code_actions::{ ActionResult, find_node, flip_comma, add_derive, add_impl, diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index 01e155855..86fdbd23f 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -64,7 +64,7 @@ impl File { validate_block_structure(root.borrowed()); File { root } } - pub fn parse(text: &str) -> Self { + pub fn parse(text: &str) -> File { let tokens = tokenize(&text); let (root, errors) = parser_impl::parse::(text, &tokens); File::new(root, errors) @@ -112,3 +112,23 @@ fn validate_block_structure(root: SyntaxNodeRef) { } } } + +#[derive(Debug, Clone)] +pub struct AtomEdit { + pub delete: TextRange, + pub insert: String, +} + +impl AtomEdit { + pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { + AtomEdit { delete: range, insert: replace_with } + } + + pub fn delete(range: TextRange) -> AtomEdit { + AtomEdit::replace(range, String::new()) + } + + pub fn insert(offset: TextUnit, text: String) -> AtomEdit { + AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) + } +} -- cgit v1.2.3