diff options
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r-- | crates/libeditor/src/edit.rs | 37 | ||||
-rw-r--r-- | crates/libeditor/src/lib.rs | 3 |
2 files changed, 14 insertions, 26 deletions
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 @@ | |||
1 | use {TextRange, TextUnit}; | 1 | use {TextRange, TextUnit}; |
2 | use libsyntax2::AtomEdit; | ||
2 | 3 | ||
3 | #[derive(Debug, Clone)] | 4 | #[derive(Debug, Clone)] |
4 | pub struct Edit { | 5 | pub struct Edit { |
5 | atoms: Vec<AtomEdit>, | 6 | atoms: Vec<AtomEdit>, |
6 | } | 7 | } |
7 | 8 | ||
8 | #[derive(Debug, Clone)] | ||
9 | pub struct AtomEdit { | ||
10 | pub delete: TextRange, | ||
11 | pub insert: String, | ||
12 | } | ||
13 | |||
14 | #[derive(Debug)] | 9 | #[derive(Debug)] |
15 | pub struct EditBuilder { | 10 | pub struct EditBuilder { |
16 | atoms: Vec<AtomEdit> | 11 | atoms: Vec<AtomEdit> |
@@ -21,23 +16,23 @@ impl EditBuilder { | |||
21 | EditBuilder { atoms: Vec::new() } | 16 | EditBuilder { atoms: Vec::new() } |
22 | } | 17 | } |
23 | 18 | ||
24 | pub fn replace(&mut self, range: TextRange, replacement: String) { | 19 | pub fn replace(&mut self, range: TextRange, replace_with: String) { |
25 | self.atoms.push(AtomEdit { delete: range, insert: replacement }) | 20 | self.atoms.push(AtomEdit::replace(range, replace_with)) |
26 | } | 21 | } |
27 | 22 | ||
28 | pub fn delete(&mut self, range: TextRange) { | 23 | pub fn delete(&mut self, range: TextRange) { |
29 | self.replace(range, String::new()); | 24 | self.atoms.push(AtomEdit::delete(range)) |
30 | } | 25 | } |
31 | 26 | ||
32 | pub fn insert(&mut self, offset: TextUnit, text: String) { | 27 | pub fn insert(&mut self, offset: TextUnit, text: String) { |
33 | self.replace(TextRange::offset_len(offset, 0.into()), text) | 28 | self.atoms.push(AtomEdit::insert(offset, text)) |
34 | } | 29 | } |
35 | 30 | ||
36 | pub fn finish(self) -> Edit { | 31 | pub fn finish(self) -> Edit { |
37 | let mut atoms = self.atoms; | 32 | let mut atoms = self.atoms; |
38 | atoms.sort_by_key(|a| a.delete.start()); | 33 | atoms.sort_by_key(|a| a.delete.start()); |
39 | for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { | 34 | for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { |
40 | assert!(a1.end() <= a2.start()) | 35 | assert!(a1.delete.end() <= a2.delete.start()) |
41 | } | 36 | } |
42 | Edit { atoms } | 37 | Edit { atoms } |
43 | } | 38 | } |
@@ -52,16 +47,18 @@ impl Edit { | |||
52 | let mut total_len = text.len(); | 47 | let mut total_len = text.len(); |
53 | for atom in self.atoms.iter() { | 48 | for atom in self.atoms.iter() { |
54 | total_len += atom.insert.len(); | 49 | total_len += atom.insert.len(); |
55 | total_len -= atom.end() - atom.start(); | 50 | total_len -= u32::from(atom.delete.end() - atom.delete.start()) as usize; |
56 | } | 51 | } |
57 | let mut buf = String::with_capacity(total_len); | 52 | let mut buf = String::with_capacity(total_len); |
58 | let mut prev = 0; | 53 | let mut prev = 0; |
59 | for atom in self.atoms.iter() { | 54 | for atom in self.atoms.iter() { |
60 | if atom.start() > prev { | 55 | let start = u32::from(atom.delete.start()) as usize; |
61 | buf.push_str(&text[prev..atom.start()]); | 56 | let end = u32::from(atom.delete.end()) as usize; |
57 | if start > prev { | ||
58 | buf.push_str(&text[prev..start]); | ||
62 | } | 59 | } |
63 | buf.push_str(&atom.insert); | 60 | buf.push_str(&atom.insert); |
64 | prev = atom.end(); | 61 | prev = end; |
65 | } | 62 | } |
66 | buf.push_str(&text[prev..text.len()]); | 63 | buf.push_str(&text[prev..text.len()]); |
67 | assert_eq!(buf.len(), total_len); | 64 | assert_eq!(buf.len(), total_len); |
@@ -83,13 +80,3 @@ impl Edit { | |||
83 | Some(res) | 80 | Some(res) |
84 | } | 81 | } |
85 | } | 82 | } |
86 | |||
87 | impl AtomEdit { | ||
88 | fn start(&self) -> usize { | ||
89 | u32::from(self.delete.start()) as usize | ||
90 | } | ||
91 | |||
92 | fn end(&self) -> usize { | ||
93 | u32::from(self.delete.end()) as usize | ||
94 | } | ||
95 | } | ||
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::{ | |||
15 | algo::{walk, find_leaf_at_offset}, | 15 | algo::{walk, find_leaf_at_offset}, |
16 | SyntaxKind::{self, *}, | 16 | SyntaxKind::{self, *}, |
17 | }; | 17 | }; |
18 | pub use libsyntax2::AtomEdit; | ||
18 | pub use self::{ | 19 | pub use self::{ |
19 | line_index::{LineIndex, LineCol}, | 20 | line_index::{LineIndex, LineCol}, |
20 | extend_selection::extend_selection, | 21 | extend_selection::extend_selection, |
21 | symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, | 22 | symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, |
22 | edit::{EditBuilder, Edit, AtomEdit}, | 23 | edit::{EditBuilder, Edit}, |
23 | code_actions::{ | 24 | code_actions::{ |
24 | ActionResult, find_node, | 25 | ActionResult, find_node, |
25 | flip_comma, add_derive, add_impl, | 26 | flip_comma, add_derive, add_impl, |