aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-10 21:09:12 +0000
committerBernardo <[email protected]>2018-12-10 21:09:12 +0000
commit7344d28768c43d8955bf23c183d606be08f27c64 (patch)
treef0773b4db640dac303f1f683115c780ac3eb0b9c /crates/ra_editor/src
parentf655f993fe6d9faa81b0e776b9b24308d2ea1c68 (diff)
extract AtomEdit and Edit into new ra_text_edit crate
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r--crates/ra_editor/src/edit.rs83
-rw-r--r--crates/ra_editor/src/lib.rs4
-rw-r--r--crates/ra_editor/src/typing.rs3
3 files changed, 3 insertions, 87 deletions
diff --git a/crates/ra_editor/src/edit.rs b/crates/ra_editor/src/edit.rs
deleted file mode 100644
index 372b8d14c..000000000
--- a/crates/ra_editor/src/edit.rs
+++ /dev/null
@@ -1,83 +0,0 @@
1use crate::{TextRange, TextUnit};
2use ra_syntax::{text_utils::contains_offset_nonstrict, AtomEdit};
3
4#[derive(Debug, Clone)]
5pub struct Edit {
6 atoms: Vec<AtomEdit>,
7}
8
9#[derive(Debug)]
10pub struct EditBuilder {
11 atoms: Vec<AtomEdit>,
12}
13
14impl EditBuilder {
15 pub fn new() -> EditBuilder {
16 EditBuilder { atoms: Vec::new() }
17 }
18 pub fn replace(&mut self, range: TextRange, replace_with: String) {
19 self.atoms.push(AtomEdit::replace(range, replace_with))
20 }
21 pub fn delete(&mut self, range: TextRange) {
22 self.atoms.push(AtomEdit::delete(range))
23 }
24 pub fn insert(&mut self, offset: TextUnit, text: String) {
25 self.atoms.push(AtomEdit::insert(offset, text))
26 }
27 pub fn finish(self) -> Edit {
28 let mut atoms = self.atoms;
29 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
30 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
31 assert!(a1.delete.end() <= a2.delete.start())
32 }
33 Edit { atoms }
34 }
35 pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
36 self.atoms
37 .iter()
38 .any(|atom| contains_offset_nonstrict(atom.delete, offset))
39 }
40}
41
42impl Edit {
43 pub fn into_atoms(self) -> Vec<AtomEdit> {
44 self.atoms
45 }
46
47 pub fn apply(&self, text: &str) -> String {
48 let mut total_len = text.len();
49 for atom in self.atoms.iter() {
50 total_len += atom.insert.len();
51 total_len -= u32::from(atom.delete.end() - atom.delete.start()) as usize;
52 }
53 let mut buf = String::with_capacity(total_len);
54 let mut prev = 0;
55 for atom in self.atoms.iter() {
56 let start = u32::from(atom.delete.start()) as usize;
57 let end = u32::from(atom.delete.end()) as usize;
58 if start > prev {
59 buf.push_str(&text[prev..start]);
60 }
61 buf.push_str(&atom.insert);
62 prev = end;
63 }
64 buf.push_str(&text[prev..text.len()]);
65 assert_eq!(buf.len(), total_len);
66 buf
67 }
68
69 pub fn apply_to_offset(&self, offset: TextUnit) -> Option<TextUnit> {
70 let mut res = offset;
71 for atom in self.atoms.iter() {
72 if atom.delete.start() >= offset {
73 break;
74 }
75 if offset < atom.delete.end() {
76 return None;
77 }
78 res += TextUnit::of_str(&atom.insert);
79 res -= atom.delete.len();
80 }
81 Some(res)
82 }
83}
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index ce080ee97..ddc44c778 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -1,5 +1,4 @@
1mod code_actions; 1mod code_actions;
2mod edit;
3mod extend_selection; 2mod extend_selection;
4mod folding_ranges; 3mod folding_ranges;
5mod line_index; 4mod line_index;
@@ -10,14 +9,13 @@ mod typing;
10 9
11pub use self::{ 10pub use self::{
12 code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit}, 11 code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit},
13 edit::{Edit, EditBuilder},
14 extend_selection::extend_selection, 12 extend_selection::extend_selection,
15 folding_ranges::{folding_ranges, Fold, FoldKind}, 13 folding_ranges::{folding_ranges, Fold, FoldKind},
16 line_index::{LineCol, LineIndex}, 14 line_index::{LineCol, LineIndex},
17 symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, 15 symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
18 typing::{join_lines, on_enter, on_eq_typed}, 16 typing::{join_lines, on_enter, on_eq_typed},
19}; 17};
20pub use ra_syntax::AtomEdit; 18use ra_text_edit::{Edit, EditBuilder};
21use ra_syntax::{ 19use ra_syntax::{
22 algo::find_leaf_at_offset, 20 algo::find_leaf_at_offset,
23 ast::{self, AstNode, NameOwner}, 21 ast::{self, AstNode, NameOwner},
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs
index 01acdda7c..cf9af001b 100644
--- a/crates/ra_editor/src/typing.rs
+++ b/crates/ra_editor/src/typing.rs
@@ -3,11 +3,12 @@ use std::mem;
3use ra_syntax::{ 3use ra_syntax::{
4 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, 4 algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
5 ast, 5 ast,
6 text_utils::{contains_offset_nonstrict, intersect}, 6 text_utils::intersect,
7 AstNode, SourceFileNode, SyntaxKind, 7 AstNode, SourceFileNode, SyntaxKind,
8 SyntaxKind::*, 8 SyntaxKind::*,
9 SyntaxNodeRef, TextRange, TextUnit, 9 SyntaxNodeRef, TextRange, TextUnit,
10}; 10};
11use ra_text_edit::text_utils::contains_offset_nonstrict;
11 12
12use crate::{find_node_at_offset, EditBuilder, LocalEdit}; 13use crate::{find_node_at_offset, EditBuilder, LocalEdit};
13 14