From 7344d28768c43d8955bf23c183d606be08f27c64 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 10 Dec 2018 22:09:12 +0100 Subject: extract AtomEdit and Edit into new ra_text_edit crate --- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/reparsing.rs | 24 +----------------------- crates/ra_syntax/src/text_utils.rs | 6 +----- crates/ra_syntax/src/yellow/syntax_text.rs | 3 ++- 4 files changed, 5 insertions(+), 30 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 0e5c9baad..7295fb237 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -41,13 +41,13 @@ pub use rowan::{SmolStr, TextRange, TextUnit}; pub use crate::{ ast::AstNode, lexer::{tokenize, Token}, - reparsing::AtomEdit, syntax_kinds::SyntaxKind, yellow::{ Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location, }, }; +use ra_text_edit::AtomEdit; use crate::yellow::GreenNode; /// `SourceFileNode` represents a parse tree for a single Rust file. diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index 732fb0e4a..873809a5a 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs @@ -6,29 +6,7 @@ use crate::parser_impl; use crate::text_utils::replace_range; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; use crate::{SyntaxKind::*, TextRange, TextUnit}; - -#[derive(Debug, Clone)] -pub struct AtomEdit { - pub delete: TextRange, - pub insert: String, -} - -impl AtomEdit { - pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { - AtomEdit { - delete: range, - insert: replace_with, - } - } - - pub fn delete(range: TextRange) -> AtomEdit { - AtomEdit::replace(range, String::new()) - } - - pub fn insert(offset: TextUnit, text: String) -> AtomEdit { - AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) - } -} +use ra_text_edit::AtomEdit; pub(crate) fn incremental_reparse( node: SyntaxNodeRef, diff --git a/crates/ra_syntax/src/text_utils.rs b/crates/ra_syntax/src/text_utils.rs index a90f8a083..417d43e1b 100644 --- a/crates/ra_syntax/src/text_utils.rs +++ b/crates/ra_syntax/src/text_utils.rs @@ -1,8 +1,4 @@ -use crate::{TextRange, TextUnit}; - -pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { - range.start() <= offset && offset <= range.end() -} +use crate::TextRange; pub fn intersect(r1: TextRange, r2: TextRange) -> Option { let start = r1.start().max(r2.start()); diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index 5395ca90b..46bde9a08 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs @@ -1,7 +1,8 @@ use std::{fmt, ops}; +use ra_text_edit::text_utils::contains_offset_nonstrict; use crate::{ - text_utils::{contains_offset_nonstrict, intersect}, + text_utils::intersect, SyntaxNodeRef, TextRange, TextUnit, }; -- cgit v1.2.3 From 0527e3b283af0153cf13fa64fe73862a5b7655c8 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 11 Dec 2018 19:07:17 +0100 Subject: rename Edit to TextEdit and AtomEdit to AtomTextEdit --- crates/ra_syntax/src/lib.rs | 8 ++++---- crates/ra_syntax/src/reparsing.rs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 7295fb237..34a3aabef 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -47,7 +47,7 @@ pub use crate::{ }, }; -use ra_text_edit::AtomEdit; +use ra_text_edit::AtomTextEdit; use crate::yellow::GreenNode; /// `SourceFileNode` represents a parse tree for a single Rust file. @@ -68,15 +68,15 @@ impl SourceFileNode { parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); SourceFileNode::new(green, errors) } - pub fn reparse(&self, edit: &AtomEdit) -> SourceFileNode { + pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { self.incremental_reparse(edit) .unwrap_or_else(|| self.full_reparse(edit)) } - pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option { + pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option { reparsing::incremental_reparse(self.syntax(), edit, self.errors()) .map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) } - fn full_reparse(&self, edit: &AtomEdit) -> SourceFileNode { + fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { let text = text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); SourceFileNode::parse(&text) diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index 873809a5a..208cae5c8 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs @@ -6,11 +6,11 @@ use crate::parser_impl; use crate::text_utils::replace_range; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; use crate::{SyntaxKind::*, TextRange, TextUnit}; -use ra_text_edit::AtomEdit; +use ra_text_edit::AtomTextEdit; pub(crate) fn incremental_reparse( node: SyntaxNodeRef, - edit: &AtomEdit, + edit: &AtomTextEdit, errors: Vec, ) -> Option<(GreenNode, Vec)> { let (node, green, new_errors) = @@ -22,7 +22,7 @@ pub(crate) fn incremental_reparse( fn reparse_leaf<'node>( node: SyntaxNodeRef<'node>, - edit: &AtomEdit, + edit: &AtomTextEdit, ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec)> { let node = algo::find_covering_node(node, edit.delete); match node.kind() { @@ -48,7 +48,7 @@ fn reparse_leaf<'node>( fn reparse_block<'node>( node: SyntaxNodeRef<'node>, - edit: &AtomEdit, + edit: &AtomTextEdit, ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec)> { let (node, reparser) = find_reparsable_node(node, edit.delete)?; let text = get_text_after_edit(node, &edit); @@ -61,7 +61,7 @@ fn reparse_block<'node>( Some((node, green, new_errors)) } -fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomEdit) -> String { +fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String { replace_range( node.text().to_string(), edit.delete - node.range().start(), @@ -139,7 +139,7 @@ fn merge_errors( old_errors: Vec, new_errors: Vec, old_node: SyntaxNodeRef, - edit: &AtomEdit, + edit: &AtomTextEdit, ) -> Vec { let mut res = Vec::new(); for e in old_errors { @@ -166,7 +166,7 @@ mod tests { where for<'a> F: Fn( SyntaxNodeRef<'a>, - &AtomEdit, + &AtomTextEdit, ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec)>, { let (range, before) = extract_range(before); @@ -175,7 +175,7 @@ mod tests { let fully_reparsed = SourceFileNode::parse(&after); let incrementally_reparsed = { let f = SourceFileNode::parse(&before); - let edit = AtomEdit { + let edit = AtomTextEdit { delete: range, insert: replace_with.to_string(), }; -- cgit v1.2.3