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 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'crates/ra_analysis/src') 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 { -- 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_analysis/src/syntax_highlighting.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_analysis/src') diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 38219da71..7e9139a74 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -9,14 +9,14 @@ use crate::{ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable> { let source_file = db.source_file(file_id); - let mut res = ra_editor::highlight(&source_file); + let mut res = ra_editor::highlight(source_file.syntax()); for macro_call in source_file .syntax() .descendants() .filter_map(ast::MacroCall::cast) { if let Some(exp) = crate::macros::expand(db, file_id, macro_call) { - let mapped_ranges = ra_editor::highlight(exp.source_file()) + let mapped_ranges = ra_editor::highlight(exp.source_file().syntax()) .into_iter() .filter_map(|r| { let mapped_range = exp.map_range_back(r.range)?; -- cgit v1.2.3