aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/libeditor/src/edit.rs37
-rw-r--r--crates/libeditor/src/lib.rs3
-rw-r--r--crates/libsyntax2/src/lib.rs22
3 files changed, 35 insertions, 27 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 @@
1use {TextRange, TextUnit}; 1use {TextRange, TextUnit};
2use libsyntax2::AtomEdit;
2 3
3#[derive(Debug, Clone)] 4#[derive(Debug, Clone)]
4pub struct Edit { 5pub struct Edit {
5 atoms: Vec<AtomEdit>, 6 atoms: Vec<AtomEdit>,
6} 7}
7 8
8#[derive(Debug, Clone)]
9pub struct AtomEdit {
10 pub delete: TextRange,
11 pub insert: String,
12}
13
14#[derive(Debug)] 9#[derive(Debug)]
15pub struct EditBuilder { 10pub 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
87impl 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};
18pub use libsyntax2::AtomEdit;
18pub use self::{ 19pub 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,
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 {
64 validate_block_structure(root.borrowed()); 64 validate_block_structure(root.borrowed());
65 File { root } 65 File { root }
66 } 66 }
67 pub fn parse(text: &str) -> Self { 67 pub fn parse(text: &str) -> File {
68 let tokens = tokenize(&text); 68 let tokens = tokenize(&text);
69 let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens); 69 let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
70 File::new(root, errors) 70 File::new(root, errors)
@@ -112,3 +112,23 @@ fn validate_block_structure(root: SyntaxNodeRef) {
112 } 112 }
113 } 113 }
114} 114}
115
116#[derive(Debug, Clone)]
117pub struct AtomEdit {
118 pub delete: TextRange,
119 pub insert: String,
120}
121
122impl AtomEdit {
123 pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
124 AtomEdit { delete: range, insert: replace_with }
125 }
126
127 pub fn delete(range: TextRange) -> AtomEdit {
128 AtomEdit::replace(range, String::new())
129 }
130
131 pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
132 AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
133 }
134}