Skip to content

Commit 4f4f793

Browse files
bors[bot]matklad
andcommitted
469: kill text utils r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents c9e42fc + 921689b commit 4f4f793

File tree

12 files changed

+33
-63
lines changed

12 files changed

+33
-63
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_editor/src/typing.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use itertools::Itertools;
44
use ra_syntax::{
55
algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset},
66
ast,
7-
text_utils::intersect,
87
AstNode, Direction, SourceFile, SyntaxKind,
98
SyntaxKind::*,
109
SyntaxNode, TextRange, TextUnit,
1110
};
12-
use ra_text_edit::text_utils::contains_offset_nonstrict;
1311

1412
use crate::{LocalEdit, TextEditBuilder};
1513

@@ -39,7 +37,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
3937
Some(text) => text,
4038
None => continue,
4139
};
42-
let range = match intersect(range, node.range()) {
40+
let range = match range.intersection(&node.range()) {
4341
Some(range) => range,
4442
None => continue,
4543
} - node.range().start();
@@ -112,7 +110,7 @@ pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
112110
}
113111
if let Some(expr) = let_stmt.initializer() {
114112
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() {
116114
return None;
117115
}
118116
if file

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use languageserver_types::{
1111
use ra_analysis::{
1212
FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange,
1313
};
14-
use ra_syntax::{text_utils::intersect, TextUnit, AstNode};
15-
use ra_text_edit::text_utils::contains_offset_nonstrict;
14+
use ra_syntax::{TextUnit, AstNode};
1615
use rustc_hash::FxHashMap;
1716
use serde_json::to_value;
1817
use std::io::Write;
@@ -248,7 +247,7 @@ pub fn handle_runnables(
248247
let mut res = Vec::new();
249248
for runnable in world.analysis().runnables(file_id)? {
250249
if let Some(offset) = offset {
251-
if !contains_offset_nonstrict(runnable.range, offset) {
250+
if !runnable.range.contains_inclusive(offset) {
252251
continue;
253252
}
254253
}
@@ -650,7 +649,7 @@ pub fn handle_code_action(
650649
.diagnostics(file_id)?
651650
.into_iter()
652651
.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())
654653
.map(|(_range, fix)| fix);
655654

656655
let mut res = Vec::new();

crates/ra_syntax/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ itertools = "0.8.0"
1414
drop_bomb = "0.1.4"
1515
parking_lot = "0.7.0"
1616
rowan = "0.2.0"
17-
text_unit = "0.1.5"
17+
text_unit = "0.1.6"
1818
ra_text_edit = { path = "../ra_text_edit" }
1919

2020
[dev-dependencies]

crates/ra_syntax/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ mod parser_impl;
3131
mod reparsing;
3232
mod string_lexing;
3333
mod syntax_kinds;
34-
pub mod text_utils;
3534
/// Utilities for simple uses of the parser.
3635
pub mod utils;
3736
mod validation;
@@ -75,8 +74,7 @@ impl SourceFile {
7574
.map(|(green_node, errors)| SourceFile::new(green_node, errors))
7675
}
7776
fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
78-
let text =
79-
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
77+
let text = edit.apply(self.syntax().text().to_string());
8078
SourceFile::parse(&text)
8179
}
8280
pub fn errors(&self) -> Vec<SyntaxError> {

crates/ra_syntax/src/reparsing.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::grammar;
33
use crate::lexer::{tokenize, Token};
44
use crate::parser_api::Parser;
55
use crate::parser_impl;
6-
use crate::text_utils::replace_range;
76
use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode};
87
use crate::{SyntaxKind::*, TextRange, TextUnit};
98
use ra_text_edit::AtomTextEdit;
@@ -62,11 +61,8 @@ fn reparse_block<'node>(
6261
}
6362

6463
fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String {
65-
replace_range(
66-
node.text().to_string(),
67-
edit.delete - node.range().start(),
68-
&edit.insert,
69-
)
64+
let edit = AtomTextEdit::replace(edit.delete - node.range().start(), edit.insert.clone());
65+
edit.apply(node.text().to_string())
7066
}
7167

7268
fn is_contextual_kw(text: &str) -> bool {
@@ -156,7 +152,7 @@ fn merge_errors(
156152
mod tests {
157153
use test_utils::{extract_range, assert_eq_text};
158154

159-
use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree};
155+
use crate::{SourceFile, AstNode, utils::dump_tree};
160156
use super::*;
161157

162158
fn do_check<F>(before: &str, replace_with: &str, reparser: F)
@@ -167,7 +163,8 @@ mod tests {
167163
) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>,
168164
{
169165
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());
171168

172169
let fully_reparsed = SourceFile::parse(&after);
173170
let incrementally_reparsed = {

crates/ra_syntax/src/text_utils.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

crates/ra_syntax/src/yellow/syntax_text.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::{fmt, ops};
22

3-
use ra_text_edit::text_utils::contains_offset_nonstrict;
4-
use crate::{
5-
text_utils::intersect,
6-
SyntaxNode, TextRange, TextUnit,
7-
};
3+
use crate::{SyntaxNode, TextRange, TextUnit};
84

95
#[derive(Clone)]
106
pub struct SyntaxText<'a> {
@@ -23,7 +19,7 @@ impl<'a> SyntaxText<'a> {
2319
let range = self.range;
2420
self.node.descendants().filter_map(move |node| {
2521
let text = node.leaf_text()?;
26-
let range = intersect(range, node.range())?;
22+
let range = range.intersection(&node.range())?;
2723
let range = range - node.range().start();
2824
Some(&text[range])
2925
})
@@ -92,13 +88,13 @@ pub trait SyntaxTextSlice: fmt::Debug {
9288

9389
impl SyntaxTextSlice for TextRange {
9490
fn restrict(&self, range: TextRange) -> Option<TextRange> {
95-
intersect(*self, range)
91+
self.intersection(&range)
9692
}
9793
}
9894

9995
impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
10096
fn restrict(&self, range: TextRange) -> Option<TextRange> {
101-
if !contains_offset_nonstrict(range, self.end) {
97+
if !range.contains_inclusive(self.end) {
10298
return None;
10399
}
104100
Some(TextRange::from_to(range.start(), self.end))
@@ -107,7 +103,7 @@ impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
107103

108104
impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
109105
fn restrict(&self, range: TextRange) -> Option<TextRange> {
110-
if !contains_offset_nonstrict(range, self.start) {
106+
if !range.contains_inclusive(self.start) {
111107
return None;
112108
}
113109
Some(TextRange::from_to(self.start, range.end()))

crates/ra_text_edit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Aleksey Kladov <[email protected]>"]
66
publish = false
77

88
[dependencies]
9-
text_unit = "0.1.5"
9+
text_unit = "0.1.6"
1010
proptest = "0.8.7"
1111

1212
[dev-dependencies]

crates/ra_text_edit/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod text_edit;
2-
pub mod text_utils;
32
pub mod test_utils;
43

54
pub use crate::text_edit::{TextEdit, TextEditBuilder};
@@ -29,4 +28,11 @@ impl AtomTextEdit {
2928
pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
3029
AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
3130
}
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+
}
3238
}

0 commit comments

Comments
 (0)