diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/reparsing.rs | 13 | ||||
-rw-r--r-- | crates/ra_syntax/src/text_utils.rs | 18 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_text.rs | 14 |
5 files changed, 12 insertions, 39 deletions
diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index eea0e251a..7c8e5b696 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml | |||
@@ -14,7 +14,7 @@ itertools = "0.8.0" | |||
14 | drop_bomb = "0.1.4" | 14 | drop_bomb = "0.1.4" |
15 | parking_lot = "0.7.0" | 15 | parking_lot = "0.7.0" |
16 | rowan = "0.2.0" | 16 | rowan = "0.2.0" |
17 | text_unit = "0.1.5" | 17 | text_unit = "0.1.6" |
18 | ra_text_edit = { path = "../ra_text_edit" } | 18 | ra_text_edit = { path = "../ra_text_edit" } |
19 | 19 | ||
20 | [dev-dependencies] | 20 | [dev-dependencies] |
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index a75e641ea..1dbfca91b 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -31,7 +31,6 @@ mod parser_impl; | |||
31 | mod reparsing; | 31 | mod reparsing; |
32 | mod string_lexing; | 32 | mod string_lexing; |
33 | mod syntax_kinds; | 33 | mod syntax_kinds; |
34 | pub mod text_utils; | ||
35 | /// Utilities for simple uses of the parser. | 34 | /// Utilities for simple uses of the parser. |
36 | pub mod utils; | 35 | pub mod utils; |
37 | mod validation; | 36 | mod validation; |
@@ -75,8 +74,7 @@ impl SourceFile { | |||
75 | .map(|(green_node, errors)| SourceFile::new(green_node, errors)) | 74 | .map(|(green_node, errors)| SourceFile::new(green_node, errors)) |
76 | } | 75 | } |
77 | fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> { | 76 | fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> { |
78 | let text = | 77 | let text = edit.apply(self.syntax().text().to_string()); |
79 | text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); | ||
80 | SourceFile::parse(&text) | 78 | SourceFile::parse(&text) |
81 | } | 79 | } |
82 | pub fn errors(&self) -> Vec<SyntaxError> { | 80 | pub fn errors(&self) -> Vec<SyntaxError> { |
diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index d5d72e1f8..2f1de6b02 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs | |||
@@ -3,7 +3,6 @@ use crate::grammar; | |||
3 | use crate::lexer::{tokenize, Token}; | 3 | use crate::lexer::{tokenize, Token}; |
4 | use crate::parser_api::Parser; | 4 | use crate::parser_api::Parser; |
5 | use crate::parser_impl; | 5 | use crate::parser_impl; |
6 | use crate::text_utils::replace_range; | ||
7 | use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; | 6 | use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; |
8 | use crate::{SyntaxKind::*, TextRange, TextUnit}; | 7 | use crate::{SyntaxKind::*, TextRange, TextUnit}; |
9 | use ra_text_edit::AtomTextEdit; | 8 | use ra_text_edit::AtomTextEdit; |
@@ -62,11 +61,8 @@ fn reparse_block<'node>( | |||
62 | } | 61 | } |
63 | 62 | ||
64 | fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { | 63 | fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { |
65 | replace_range( | 64 | let edit = AtomTextEdit::replace(edit.delete - node.range().start(), edit.insert.clone()); |
66 | node.text().to_string(), | 65 | edit.apply(node.text().to_string()) |
67 | edit.delete - node.range().start(), | ||
68 | &edit.insert, | ||
69 | ) | ||
70 | } | 66 | } |
71 | 67 | ||
72 | fn is_contextual_kw(text: &str) -> bool { | 68 | fn is_contextual_kw(text: &str) -> bool { |
@@ -156,7 +152,7 @@ fn merge_errors( | |||
156 | mod tests { | 152 | mod tests { |
157 | use test_utils::{extract_range, assert_eq_text}; | 153 | use test_utils::{extract_range, assert_eq_text}; |
158 | 154 | ||
159 | use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree}; | 155 | use crate::{SourceFile, AstNode, utils::dump_tree}; |
160 | use super::*; | 156 | use super::*; |
161 | 157 | ||
162 | fn do_check<F>(before: &str, replace_with: &str, reparser: F) | 158 | fn do_check<F>(before: &str, replace_with: &str, reparser: F) |
@@ -167,7 +163,8 @@ mod tests { | |||
167 | ) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>, | 163 | ) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>, |
168 | { | 164 | { |
169 | let (range, before) = extract_range(before); | 165 | let (range, before) = extract_range(before); |
170 | let after = replace_range(before.clone(), range, replace_with); | 166 | let edit = AtomTextEdit::replace(range, replace_with.to_owned()); |
167 | let after = edit.apply(before.clone()); | ||
171 | 168 | ||
172 | let fully_reparsed = SourceFile::parse(&after); | 169 | let fully_reparsed = SourceFile::parse(&after); |
173 | let incrementally_reparsed = { | 170 | let incrementally_reparsed = { |
diff --git a/crates/ra_syntax/src/text_utils.rs b/crates/ra_syntax/src/text_utils.rs deleted file mode 100644 index 417d43e1b..000000000 --- a/crates/ra_syntax/src/text_utils.rs +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | use crate::TextRange; | ||
2 | |||
3 | pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { | ||
4 | let start = r1.start().max(r2.start()); | ||
5 | let end = r1.end().min(r2.end()); | ||
6 | if start <= end { | ||
7 | Some(TextRange::from_to(start, end)) | ||
8 | } else { | ||
9 | None | ||
10 | } | ||
11 | } | ||
12 | |||
13 | pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { | ||
14 | let start = u32::from(range.start()) as usize; | ||
15 | let end = u32::from(range.end()) as usize; | ||
16 | text.replace_range(start..end, replace_with); | ||
17 | text | ||
18 | } | ||
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index 31db0fdab..08dbe57a2 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs | |||
@@ -1,10 +1,6 @@ | |||
1 | use std::{fmt, ops}; | 1 | use std::{fmt, ops}; |
2 | 2 | ||
3 | use ra_text_edit::text_utils::contains_offset_nonstrict; | 3 | use crate::{SyntaxNode, TextRange, TextUnit}; |
4 | use crate::{ | ||
5 | text_utils::intersect, | ||
6 | SyntaxNode, TextRange, TextUnit, | ||
7 | }; | ||
8 | 4 | ||
9 | #[derive(Clone)] | 5 | #[derive(Clone)] |
10 | pub struct SyntaxText<'a> { | 6 | pub struct SyntaxText<'a> { |
@@ -23,7 +19,7 @@ impl<'a> SyntaxText<'a> { | |||
23 | let range = self.range; | 19 | let range = self.range; |
24 | self.node.descendants().filter_map(move |node| { | 20 | self.node.descendants().filter_map(move |node| { |
25 | let text = node.leaf_text()?; | 21 | let text = node.leaf_text()?; |
26 | let range = intersect(range, node.range())?; | 22 | let range = range.intersection(&node.range())?; |
27 | let range = range - node.range().start(); | 23 | let range = range - node.range().start(); |
28 | Some(&text[range]) | 24 | Some(&text[range]) |
29 | }) | 25 | }) |
@@ -92,13 +88,13 @@ pub trait SyntaxTextSlice: fmt::Debug { | |||
92 | 88 | ||
93 | impl SyntaxTextSlice for TextRange { | 89 | impl SyntaxTextSlice for TextRange { |
94 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | 90 | fn restrict(&self, range: TextRange) -> Option<TextRange> { |
95 | intersect(*self, range) | 91 | self.intersection(&range) |
96 | } | 92 | } |
97 | } | 93 | } |
98 | 94 | ||
99 | impl SyntaxTextSlice for ops::RangeTo<TextUnit> { | 95 | impl SyntaxTextSlice for ops::RangeTo<TextUnit> { |
100 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | 96 | fn restrict(&self, range: TextRange) -> Option<TextRange> { |
101 | if !contains_offset_nonstrict(range, self.end) { | 97 | if !range.contains_inclusive(self.end) { |
102 | return None; | 98 | return None; |
103 | } | 99 | } |
104 | Some(TextRange::from_to(range.start(), self.end)) | 100 | Some(TextRange::from_to(range.start(), self.end)) |
@@ -107,7 +103,7 @@ impl SyntaxTextSlice for ops::RangeTo<TextUnit> { | |||
107 | 103 | ||
108 | impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { | 104 | impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { |
109 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | 105 | fn restrict(&self, range: TextRange) -> Option<TextRange> { |
110 | if !contains_offset_nonstrict(range, self.start) { | 106 | if !range.contains_inclusive(self.start) { |
111 | return None; | 107 | return None; |
112 | } | 108 | } |
113 | Some(TextRange::from_to(self.start, range.end())) | 109 | Some(TextRange::from_to(self.start, range.end())) |