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_analysis/src/extend_selection.rs | 29 +++++++++++++++++------------ crates/ra_cli/src/main.rs | 2 +- crates/ra_editor/src/extend_selection.rs | 12 ++++-------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index cde6ee101..805e9059e 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs @@ -1,6 +1,6 @@ use ra_db::SyntaxDatabase; use ra_syntax::{ - SyntaxNodeRef, AstNode, + SyntaxNodeRef, AstNode, SourceFileNode, ast, algo::find_covering_node, }; @@ -11,18 +11,23 @@ use crate::{ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { let source_file = db.source_file(frange.file_id); - if let Some(macro_call) = find_macro_call(source_file.syntax(), frange.range) { - if let Some(exp) = crate::macros::expand(db, frange.file_id, macro_call) { - if let Some(dst_range) = exp.map_range_forward(frange.range) { - if let Some(dst_range) = ra_editor::extend_selection(exp.source_file(), dst_range) { - if let Some(src_range) = exp.map_range_back(dst_range) { - return src_range; - } - } - } - } + if let Some(range) = extend_selection_in_macro(db, &source_file, frange) { + return range; } - ra_editor::extend_selection(&source_file, frange.range).unwrap_or(frange.range) + ra_editor::extend_selection(source_file.syntax(), frange.range).unwrap_or(frange.range) +} + +fn extend_selection_in_macro( + db: &RootDatabase, + source_file: &SourceFileNode, + frange: FileRange, +) -> Option { + let macro_call = find_macro_call(source_file.syntax(), frange.range)?; + let exp = crate::macros::expand(db, frange.file_id, macro_call)?; + let dst_range = exp.map_range_forward(frange.range)?; + let dst_range = ra_editor::extend_selection(exp.source_file().syntax(), dst_range)?; + let src_range = exp.map_range_back(dst_range)?; + Some(src_range) } fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option { diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 939f7fe77..a3b856aa9 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -102,7 +102,7 @@ fn selections(file: &SourceFileNode, start: u32, end: u32) -> String { let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into())); while let Some(r) = cur { ranges.push(r); - cur = extend_selection(&file, r); + cur = extend_selection(file.syntax(), r); } let ranges = ranges .iter() 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