aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:03:35 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:03:35 +0000
commit4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (patch)
treea390d74ee5272a4a0070f5d4ea5281a04d4ba56a /crates
parentc9e42fcf245be16958dca6571e4bccc6c29199df (diff)
parent921689b70da39160dd381e9716472827e36b03b8 (diff)
Merge #469
469: kill text utils r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/src/typing.rs6
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs7
-rw-r--r--crates/ra_syntax/Cargo.toml2
-rw-r--r--crates/ra_syntax/src/lib.rs4
-rw-r--r--crates/ra_syntax/src/reparsing.rs13
-rw-r--r--crates/ra_syntax/src/text_utils.rs18
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs14
-rw-r--r--crates/ra_text_edit/Cargo.toml2
-rw-r--r--crates/ra_text_edit/src/lib.rs8
-rw-r--r--crates/ra_text_edit/src/text_edit.rs3
-rw-r--r--crates/ra_text_edit/src/text_utils.rs5
11 files changed, 26 insertions, 56 deletions
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs
index 576caf6be..d8177f245 100644
--- a/crates/ra_editor/src/typing.rs
+++ b/crates/ra_editor/src/typing.rs
@@ -4,12 +4,10 @@ use itertools::Itertools;
4use ra_syntax::{ 4use ra_syntax::{
5 algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset}, 5 algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset},
6 ast, 6 ast,
7 text_utils::intersect,
8 AstNode, Direction, SourceFile, SyntaxKind, 7 AstNode, Direction, SourceFile, SyntaxKind,
9 SyntaxKind::*, 8 SyntaxKind::*,
10 SyntaxNode, TextRange, TextUnit, 9 SyntaxNode, TextRange, TextUnit,
11}; 10};
12use ra_text_edit::text_utils::contains_offset_nonstrict;
13 11
14use crate::{LocalEdit, TextEditBuilder}; 12use crate::{LocalEdit, TextEditBuilder};
15 13
@@ -39,7 +37,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
39 Some(text) => text, 37 Some(text) => text,
40 None => continue, 38 None => continue,
41 }; 39 };
42 let range = match intersect(range, node.range()) { 40 let range = match range.intersection(&node.range()) {
43 Some(range) => range, 41 Some(range) => range,
44 None => continue, 42 None => continue,
45 } - node.range().start(); 43 } - node.range().start();
@@ -112,7 +110,7 @@ pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
112 } 110 }
113 if let Some(expr) = let_stmt.initializer() { 111 if let Some(expr) = let_stmt.initializer() {
114 let expr_range = expr.syntax().range(); 112 let expr_range = expr.syntax().range();
115 if contains_offset_nonstrict(expr_range, offset) && offset != expr_range.start() { 113 if expr_range.contains(offset) && offset != expr_range.start() {
116 return None; 114 return None;
117 } 115 }
118 if file 116 if file
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index b9b42f1b3..b7777bfc3 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -11,8 +11,7 @@ use languageserver_types::{
11use ra_analysis::{ 11use ra_analysis::{
12 FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, 12 FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange,
13}; 13};
14use ra_syntax::{text_utils::intersect, TextUnit, AstNode}; 14use ra_syntax::{TextUnit, AstNode};
15use ra_text_edit::text_utils::contains_offset_nonstrict;
16use rustc_hash::FxHashMap; 15use rustc_hash::FxHashMap;
17use serde_json::to_value; 16use serde_json::to_value;
18use std::io::Write; 17use std::io::Write;
@@ -248,7 +247,7 @@ pub fn handle_runnables(
248 let mut res = Vec::new(); 247 let mut res = Vec::new();
249 for runnable in world.analysis().runnables(file_id)? { 248 for runnable in world.analysis().runnables(file_id)? {
250 if let Some(offset) = offset { 249 if let Some(offset) = offset {
251 if !contains_offset_nonstrict(runnable.range, offset) { 250 if !runnable.range.contains_inclusive(offset) {
252 continue; 251 continue;
253 } 252 }
254 } 253 }
@@ -650,7 +649,7 @@ pub fn handle_code_action(
650 .diagnostics(file_id)? 649 .diagnostics(file_id)?
651 .into_iter() 650 .into_iter()
652 .filter_map(|d| Some((d.range, d.fix?))) 651 .filter_map(|d| Some((d.range, d.fix?)))
653 .filter(|(diag_range, _fix)| intersect(*diag_range, range).is_some()) 652 .filter(|(diag_range, _fix)| diag_range.intersection(&range).is_some())
654 .map(|(_range, fix)| fix); 653 .map(|(_range, fix)| fix);
655 654
656 let mut res = Vec::new(); 655 let mut res = Vec::new();
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"
14drop_bomb = "0.1.4" 14drop_bomb = "0.1.4"
15parking_lot = "0.7.0" 15parking_lot = "0.7.0"
16rowan = "0.2.0" 16rowan = "0.2.0"
17text_unit = "0.1.5" 17text_unit = "0.1.6"
18ra_text_edit = { path = "../ra_text_edit" } 18ra_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;
31mod reparsing; 31mod reparsing;
32mod string_lexing; 32mod string_lexing;
33mod syntax_kinds; 33mod syntax_kinds;
34pub mod text_utils;
35/// Utilities for simple uses of the parser. 34/// Utilities for simple uses of the parser.
36pub mod utils; 35pub mod utils;
37mod validation; 36mod 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;
3use crate::lexer::{tokenize, Token}; 3use crate::lexer::{tokenize, Token};
4use crate::parser_api::Parser; 4use crate::parser_api::Parser;
5use crate::parser_impl; 5use crate::parser_impl;
6use crate::text_utils::replace_range;
7use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; 6use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode};
8use crate::{SyntaxKind::*, TextRange, TextUnit}; 7use crate::{SyntaxKind::*, TextRange, TextUnit};
9use ra_text_edit::AtomTextEdit; 8use ra_text_edit::AtomTextEdit;
@@ -62,11 +61,8 @@ fn reparse_block<'node>(
62} 61}
63 62
64fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { 63fn 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
72fn is_contextual_kw(text: &str) -> bool { 68fn is_contextual_kw(text: &str) -> bool {
@@ -156,7 +152,7 @@ fn merge_errors(
156mod tests { 152mod 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 @@
1use crate::TextRange;
2
3pub 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
13pub 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 @@
1use std::{fmt, ops}; 1use std::{fmt, ops};
2 2
3use ra_text_edit::text_utils::contains_offset_nonstrict; 3use crate::{SyntaxNode, TextRange, TextUnit};
4use crate::{
5 text_utils::intersect,
6 SyntaxNode, TextRange, TextUnit,
7};
8 4
9#[derive(Clone)] 5#[derive(Clone)]
10pub struct SyntaxText<'a> { 6pub 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
93impl SyntaxTextSlice for TextRange { 89impl 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
99impl SyntaxTextSlice for ops::RangeTo<TextUnit> { 95impl 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
108impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { 104impl 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()))
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml
index e0db49688..71f6ce1ad 100644
--- a/crates/ra_text_edit/Cargo.toml
+++ b/crates/ra_text_edit/Cargo.toml
@@ -6,7 +6,7 @@ authors = ["Aleksey Kladov <[email protected]>"]
6publish = false 6publish = false
7 7
8[dependencies] 8[dependencies]
9text_unit = "0.1.5" 9text_unit = "0.1.6"
10proptest = "0.8.7" 10proptest = "0.8.7"
11 11
12[dev-dependencies] 12[dev-dependencies]
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 8acf10448..fb693b3ae 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -1,5 +1,4 @@
1mod text_edit; 1mod text_edit;
2pub mod text_utils;
3pub mod test_utils; 2pub mod test_utils;
4 3
5pub use crate::text_edit::{TextEdit, TextEditBuilder}; 4pub use crate::text_edit::{TextEdit, TextEditBuilder};
@@ -29,4 +28,11 @@ impl AtomTextEdit {
29 pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { 28 pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
30 AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) 29 AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
31 } 30 }
31
32 pub fn apply(&self, mut text: String) -> String {
33 let start = u32::from(self.delete.start()) as usize;
34 let end = u32::from(self.delete.end()) as usize;
35 text.replace_range(start..end, &self.insert);
36 text
37 }
32} 38}
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index a288a990d..363b3d8c0 100644
--- a/crates/ra_text_edit/src/text_edit.rs
+++ b/crates/ra_text_edit/src/text_edit.rs
@@ -1,5 +1,4 @@
1use crate::AtomTextEdit; 1use crate::AtomTextEdit;
2use crate::text_utils::contains_offset_nonstrict;
3use text_unit::{TextRange, TextUnit}; 2use text_unit::{TextRange, TextUnit};
4 3
5#[derive(Debug, Clone)] 4#[derive(Debug, Clone)]
@@ -28,7 +27,7 @@ impl TextEditBuilder {
28 pub fn invalidates_offset(&self, offset: TextUnit) -> bool { 27 pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
29 self.atoms 28 self.atoms
30 .iter() 29 .iter()
31 .any(|atom| contains_offset_nonstrict(atom.delete, offset)) 30 .any(|atom| atom.delete.contains_inclusive(offset))
32 } 31 }
33} 32}
34 33
diff --git a/crates/ra_text_edit/src/text_utils.rs b/crates/ra_text_edit/src/text_utils.rs
deleted file mode 100644
index e3b4dc4fe..000000000
--- a/crates/ra_text_edit/src/text_utils.rs
+++ /dev/null
@@ -1,5 +0,0 @@
1use text_unit::{TextRange, TextUnit};
2
3pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
4 range.start() <= offset && offset <= range.end()
5}