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/Cargo.toml | 1 + 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 ++- 5 files changed, 6 insertions(+), 30 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 8ad8ed196..8c9a7e238 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -15,6 +15,7 @@ drop_bomb = "0.1.4" parking_lot = "0.6.0" rowan = "0.1.2" text_unit = "0.1.5" +ra_text_edit = { path = "../ra_text_edit" } [dev-dependencies] test_utils = { path = "../test_utils" } 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 ++++----- .../tests/data/parser/fuzz-failures/0000.rs | 38 +++++++++++----------- 3 files changed, 31 insertions(+), 31 deletions(-) (limited to 'crates/ra_syntax') 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(), }; diff --git a/crates/ra_syntax/tests/data/parser/fuzz-failures/0000.rs b/crates/ra_syntax/tests/data/parser/fuzz-failures/0000.rs index 53c93d9e9..59cd11495 100644 --- a/crates/ra_syntax/tests/data/parser/fuzz-failures/0000.rs +++ b/crates/ra_syntax/tests/data/parser/fuzz-failures/0000.rs @@ -10,10 +10,10 @@ ); File::new(green, errors) } - pub fn reparse(&self, edit: &AtomEdit) -> File { + pub fn reparse(&self, edit: &AtomTextEdit) -> File { 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 { let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?; let text = replace_range( node.text().to_string(), @@ -31,7 +31,7 @@ let errors = merge_errors(self.errors(), new_errors, node, edit); Some(File::new(green_root, errors)) } - fn full_reparse(&self, edit: &AtomEdit) -> File { + fn full_reparse(&self, edit: &AtomTextEdit) -> File { let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); File::parse(&text) } @@ -58,22 +58,22 @@ } #[derive(Debug, Clone)] -pub struct AtomEdit { +pub struct AtomTextEdit { pub delete: TextRange, pub insert: String, } -impl AtomEdit { - pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { - AtomEdit { delete: range, insert: replace_with } +impl AtomTextEdit { + pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit { + AtomTextEdit { delete: range, insert: replace_with } } - pub fn delete(range: TextRange) -> AtomEdit { - AtomEdit::replace(range, String::new()) + pub fn delete(range: TextRange) -> AtomTextEdit { + AtomTextEdit::replace(range, String::new()) } - pub fn insert(offset: TextUnit, text: String) -> AtomEdit { - AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) + pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { + AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) } } @@ -114,17 +114,17 @@ fn is_balanced(tokens: &[Token]) -> bool { pub insert: String, } -impl AtomEdit { - pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { - AtomEdit { delete: range, insert: replace_with } +impl AtomTextEdit { + pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit { + AtomTextEdit { delete: range, insert: replace_with } } - pub fn delete(range: TextRange) -> AtomEdit { - AtomEdit::replace(range, String::new()) + pub fn delete(range: TextRange) -> AtomTextEdit { + AtomTextEdit::replace(range, String::new()) } - pub fn insert(offset: TextUnit, text: String) -> AtomEdit { - AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) + pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { + AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) } } @@ -176,7 +176,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 { -- cgit v1.2.3