From f1e8ebfbeb5f84405fb609e3841df02e270037c4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 31 Dec 2018 19:01:51 +0300 Subject: generalize extend selection to work with nodes --- crates/ra_editor/src/extend_selection.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index 4665a336a..bf0727dde 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs @@ -1,16 +1,11 @@ use ra_syntax::{ algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, - Direction, SourceFileNode, + Direction, SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, }; -pub fn extend_selection(file: &SourceFileNode, range: TextRange) -> Option { - let syntax = file.syntax(); - extend(syntax.borrowed(), range) -} - -pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option { +pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option { if range.is_empty() { let offset = range.start(); let mut leaves = find_leaf_at_offset(root, offset); @@ -126,6 +121,7 @@ fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { #[cfg(test)] mod tests { use super::*; + use ra_syntax::SourceFileNode; use test_utils::extract_offset; fn do_check(before: &str, afters: &[&str]) { @@ -133,7 +129,7 @@ mod tests { let file = SourceFileNode::parse(&before); let mut range = TextRange::offset_len(cursor, 0.into()); for &after in afters { - range = extend_selection(&file, range).unwrap(); + range = extend_selection(file.syntax(), range).unwrap(); let actual = &before[range]; assert_eq!(after, actual); } -- cgit v1.2.3 From 862c99d0d5ef7c791a9319fa76c436762d88460c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 31 Dec 2018 19:06:00 +0300 Subject: generalize highlighting to work with nodes --- crates/ra_editor/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index a65637d52..a293fec34 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -79,11 +79,11 @@ pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option Vec { +pub fn highlight(root: SyntaxNodeRef) -> Vec { // Visited nodes to handle highlighting priorities let mut highlighted = FxHashSet::default(); let mut res = Vec::new(); - for node in file.syntax().descendants() { + for node in root.descendants() { if highlighted.contains(&node) { continue; } @@ -178,7 +178,7 @@ fn main() {} println!("Hello, {}!", 92); "#, ); - let hls = highlight(&file); + let hls = highlight(file.syntax()); assert_eq_dbg( r#"[HighlightedRange { range: [1; 11), tag: "comment" }, HighlightedRange { range: [12; 14), tag: "keyword" }, -- cgit v1.2.3