From f553837c1ca30a52bf5091689c21d3c3e3362395 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 21:50:04 +0300 Subject: upstream text-utils to text_unit --- crates/ra_editor/src/typing.rs | 6 ++---- crates/ra_lsp_server/src/main_loop/handlers.rs | 7 +++---- crates/ra_syntax/Cargo.toml | 2 +- crates/ra_syntax/src/text_utils.rs | 10 ---------- crates/ra_syntax/src/yellow/syntax_text.rs | 14 +++++--------- crates/ra_text_edit/Cargo.toml | 2 +- crates/ra_text_edit/src/lib.rs | 1 - crates/ra_text_edit/src/text_edit.rs | 3 +-- crates/ra_text_edit/src/text_utils.rs | 5 ----- 9 files changed, 13 insertions(+), 37 deletions(-) delete mode 100644 crates/ra_text_edit/src/text_utils.rs (limited to 'crates') 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; use ra_syntax::{ algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset}, ast, - text_utils::intersect, AstNode, Direction, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxNode, TextRange, TextUnit, }; -use ra_text_edit::text_utils::contains_offset_nonstrict; use crate::{LocalEdit, TextEditBuilder}; @@ -39,7 +37,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { Some(text) => text, None => continue, }; - let range = match intersect(range, node.range()) { + let range = match range.intersection(&node.range()) { Some(range) => range, None => continue, } - node.range().start(); @@ -112,7 +110,7 @@ pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option { } if let Some(expr) = let_stmt.initializer() { let expr_range = expr.syntax().range(); - if contains_offset_nonstrict(expr_range, offset) && offset != expr_range.start() { + if expr_range.contains(offset) && offset != expr_range.start() { return None; } 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::{ use ra_analysis::{ FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, }; -use ra_syntax::{text_utils::intersect, TextUnit, AstNode}; -use ra_text_edit::text_utils::contains_offset_nonstrict; +use ra_syntax::{TextUnit, AstNode}; use rustc_hash::FxHashMap; use serde_json::to_value; use std::io::Write; @@ -248,7 +247,7 @@ pub fn handle_runnables( let mut res = Vec::new(); for runnable in world.analysis().runnables(file_id)? { if let Some(offset) = offset { - if !contains_offset_nonstrict(runnable.range, offset) { + if !runnable.range.contains_inclusive(offset) { continue; } } @@ -650,7 +649,7 @@ pub fn handle_code_action( .diagnostics(file_id)? .into_iter() .filter_map(|d| Some((d.range, d.fix?))) - .filter(|(diag_range, _fix)| intersect(*diag_range, range).is_some()) + .filter(|(diag_range, _fix)| diag_range.intersection(&range).is_some()) .map(|(_range, fix)| fix); 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" drop_bomb = "0.1.4" parking_lot = "0.7.0" rowan = "0.2.0" -text_unit = "0.1.5" +text_unit = "0.1.6" ra_text_edit = { path = "../ra_text_edit" } [dev-dependencies] diff --git a/crates/ra_syntax/src/text_utils.rs b/crates/ra_syntax/src/text_utils.rs index 417d43e1b..7aaf4c223 100644 --- a/crates/ra_syntax/src/text_utils.rs +++ b/crates/ra_syntax/src/text_utils.rs @@ -1,15 +1,5 @@ use crate::TextRange; -pub fn intersect(r1: TextRange, r2: TextRange) -> Option { - let start = r1.start().max(r2.start()); - let end = r1.end().min(r2.end()); - if start <= end { - Some(TextRange::from_to(start, end)) - } else { - None - } -} - pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { let start = u32::from(range.start()) as usize; let end = u32::from(range.end()) as usize; 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 @@ use std::{fmt, ops}; -use ra_text_edit::text_utils::contains_offset_nonstrict; -use crate::{ - text_utils::intersect, - SyntaxNode, TextRange, TextUnit, -}; +use crate::{SyntaxNode, TextRange, TextUnit}; #[derive(Clone)] pub struct SyntaxText<'a> { @@ -23,7 +19,7 @@ impl<'a> SyntaxText<'a> { let range = self.range; self.node.descendants().filter_map(move |node| { let text = node.leaf_text()?; - let range = intersect(range, node.range())?; + let range = range.intersection(&node.range())?; let range = range - node.range().start(); Some(&text[range]) }) @@ -92,13 +88,13 @@ pub trait SyntaxTextSlice: fmt::Debug { impl SyntaxTextSlice for TextRange { fn restrict(&self, range: TextRange) -> Option { - intersect(*self, range) + self.intersection(&range) } } impl SyntaxTextSlice for ops::RangeTo { fn restrict(&self, range: TextRange) -> Option { - if !contains_offset_nonstrict(range, self.end) { + if !range.contains_inclusive(self.end) { return None; } Some(TextRange::from_to(range.start(), self.end)) @@ -107,7 +103,7 @@ impl SyntaxTextSlice for ops::RangeTo { impl SyntaxTextSlice for ops::RangeFrom { fn restrict(&self, range: TextRange) -> Option { - if !contains_offset_nonstrict(range, self.start) { + if !range.contains_inclusive(self.start) { return None; } 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 "] publish = false [dependencies] -text_unit = "0.1.5" +text_unit = "0.1.6" proptest = "0.8.7" [dev-dependencies] diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 8acf10448..22f3fdc0c 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -1,5 +1,4 @@ mod text_edit; -pub mod text_utils; pub mod test_utils; pub use crate::text_edit::{TextEdit, TextEditBuilder}; 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 @@ use crate::AtomTextEdit; -use crate::text_utils::contains_offset_nonstrict; use text_unit::{TextRange, TextUnit}; #[derive(Debug, Clone)] @@ -28,7 +27,7 @@ impl TextEditBuilder { pub fn invalidates_offset(&self, offset: TextUnit) -> bool { self.atoms .iter() - .any(|atom| contains_offset_nonstrict(atom.delete, offset)) + .any(|atom| atom.delete.contains_inclusive(offset)) } } 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 @@ -use text_unit::{TextRange, TextUnit}; - -pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { - range.start() <= offset && offset <= range.end() -} -- cgit v1.2.3 From 921689b70da39160dd381e9716472827e36b03b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 21:59:55 +0300 Subject: kill text utils --- crates/ra_syntax/src/lib.rs | 4 +--- crates/ra_syntax/src/reparsing.rs | 13 +++++-------- crates/ra_syntax/src/text_utils.rs | 8 -------- crates/ra_text_edit/src/lib.rs | 7 +++++++ 4 files changed, 13 insertions(+), 19 deletions(-) delete mode 100644 crates/ra_syntax/src/text_utils.rs (limited to 'crates') 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; mod reparsing; mod string_lexing; mod syntax_kinds; -pub mod text_utils; /// Utilities for simple uses of the parser. pub mod utils; mod validation; @@ -75,8 +74,7 @@ impl SourceFile { .map(|(green_node, errors)| SourceFile::new(green_node, errors)) } fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr { - let text = - text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); + let text = edit.apply(self.syntax().text().to_string()); SourceFile::parse(&text) } pub fn errors(&self) -> Vec { 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; use crate::lexer::{tokenize, Token}; use crate::parser_api::Parser; use crate::parser_impl; -use crate::text_utils::replace_range; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; use crate::{SyntaxKind::*, TextRange, TextUnit}; use ra_text_edit::AtomTextEdit; @@ -62,11 +61,8 @@ fn reparse_block<'node>( } fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { - replace_range( - node.text().to_string(), - edit.delete - node.range().start(), - &edit.insert, - ) + let edit = AtomTextEdit::replace(edit.delete - node.range().start(), edit.insert.clone()); + edit.apply(node.text().to_string()) } fn is_contextual_kw(text: &str) -> bool { @@ -156,7 +152,7 @@ fn merge_errors( mod tests { use test_utils::{extract_range, assert_eq_text}; - use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree}; + use crate::{SourceFile, AstNode, utils::dump_tree}; use super::*; fn do_check(before: &str, replace_with: &str, reparser: F) @@ -167,7 +163,8 @@ mod tests { ) -> Option<(&'a SyntaxNode, GreenNode, Vec)>, { let (range, before) = extract_range(before); - let after = replace_range(before.clone(), range, replace_with); + let edit = AtomTextEdit::replace(range, replace_with.to_owned()); + let after = edit.apply(before.clone()); let fully_reparsed = SourceFile::parse(&after); 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 7aaf4c223..000000000 --- a/crates/ra_syntax/src/text_utils.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::TextRange; - -pub fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String { - let start = u32::from(range.start()) as usize; - let end = u32::from(range.end()) as usize; - text.replace_range(start..end, replace_with); - text -} diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 22f3fdc0c..fb693b3ae 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -28,4 +28,11 @@ impl AtomTextEdit { pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) } + + pub fn apply(&self, mut text: String) -> String { + let start = u32::from(self.delete.start()) as usize; + let end = u32::from(self.delete.end()) as usize; + text.replace_range(start..end, &self.insert); + text + } } -- cgit v1.2.3