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 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