diff options
author | Bernardo <[email protected]> | 2018-12-10 21:09:12 +0000 |
---|---|---|
committer | Bernardo <[email protected]> | 2018-12-10 21:09:12 +0000 |
commit | 7344d28768c43d8955bf23c183d606be08f27c64 (patch) | |
tree | f0773b4db640dac303f1f683115c780ac3eb0b9c /crates/ra_editor | |
parent | f655f993fe6d9faa81b0e776b9b24308d2ea1c68 (diff) |
extract AtomEdit and Edit into new ra_text_edit crate
Diffstat (limited to 'crates/ra_editor')
-rw-r--r-- | crates/ra_editor/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_editor/src/edit.rs | 83 | ||||
-rw-r--r-- | crates/ra_editor/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_editor/src/typing.rs | 3 |
4 files changed, 4 insertions, 87 deletions
diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 7791da156..c29be1350 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml | |||
@@ -12,6 +12,7 @@ join_to_string = "0.1.1" | |||
12 | rustc-hash = "1.0" | 12 | rustc-hash = "1.0" |
13 | 13 | ||
14 | ra_syntax = { path = "../ra_syntax" } | 14 | ra_syntax = { path = "../ra_syntax" } |
15 | ra_text_edit = { path = "../ra_text_edit" } | ||
15 | 16 | ||
16 | [dev-dependencies] | 17 | [dev-dependencies] |
17 | test_utils = { path = "../test_utils" } | 18 | test_utils = { path = "../test_utils" } |
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 @@ | |||
1 | use crate::{TextRange, TextUnit}; | ||
2 | use ra_syntax::{text_utils::contains_offset_nonstrict, AtomEdit}; | ||
3 | |||
4 | #[derive(Debug, Clone)] | ||
5 | pub struct Edit { | ||
6 | atoms: Vec<AtomEdit>, | ||
7 | } | ||
8 | |||
9 | #[derive(Debug)] | ||
10 | pub struct EditBuilder { | ||
11 | atoms: Vec<AtomEdit>, | ||
12 | } | ||
13 | |||
14 | impl 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 | |||
42 | impl 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 @@ | |||
1 | mod code_actions; | 1 | mod code_actions; |
2 | mod edit; | ||
3 | mod extend_selection; | 2 | mod extend_selection; |
4 | mod folding_ranges; | 3 | mod folding_ranges; |
5 | mod line_index; | 4 | mod line_index; |
@@ -10,14 +9,13 @@ mod typing; | |||
10 | 9 | ||
11 | pub use self::{ | 10 | pub 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 | }; |
20 | pub use ra_syntax::AtomEdit; | 18 | use ra_text_edit::{Edit, EditBuilder}; |
21 | use ra_syntax::{ | 19 | use 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; | |||
3 | use ra_syntax::{ | 3 | use 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 | }; |
11 | use ra_text_edit::text_utils::contains_offset_nonstrict; | ||
11 | 12 | ||
12 | use crate::{find_node_at_offset, EditBuilder, LocalEdit}; | 13 | use crate::{find_node_at_offset, EditBuilder, LocalEdit}; |
13 | 14 | ||