extend selection works with macros

This commit is contained in:
Aleksey Kladov 2018-12-28 20:33:39 +03:00
parent 10e687f281
commit 5299a35e3d
3 changed files with 68 additions and 4 deletions

View File

@ -1,4 +1,8 @@
use ra_db::SyntaxDatabase;
use ra_syntax::{
SyntaxNodeRef, AstNode,
ast, algo::find_covering_node,
};
use crate::{
TextRange, FileRange,
@ -6,6 +10,42 @@ use crate::{
};
pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange {
let file = db.source_file(frange.file_id);
ra_editor::extend_selection(&file, frange.range).unwrap_or(frange.range)
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;
}
}
}
}
}
ra_editor::extend_selection(&source_file, frange.range).unwrap_or(frange.range)
}
fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> {
find_covering_node(node, range)
.ancestors()
.find_map(ast::MacroCall::cast)
}
#[cfg(test)]
mod tests {
use crate::mock_analysis::single_file_with_range;
use test_utils::assert_eq_dbg;
#[test]
fn extend_selection_inside_macros() {
let (analysis, frange) = single_file_with_range(
"
fn main() {
ctry!(foo(|x| <|>x<|>));
}
",
);
let r = analysis.extend_selection(frange);
assert_eq_dbg("[51; 56)", &r);
}
}

View File

@ -61,4 +61,15 @@ impl MacroExpansion {
}
None
}
pub(crate) fn map_range_forward(&self, src_range: TextRange) -> Option<TextRange> {
for (s_range, t_range) in self.ranges_map.iter() {
if src_range.is_subrange(&s_range) {
let src_at_zero_range = src_range - src_range.start();
let src_range_offset = src_range.start() - s_range.start();
let src_range = src_at_zero_range + src_range_offset + t_range.start();
return Some(src_range);
}
}
None
}
}

View File

@ -1,10 +1,10 @@
use std::sync::Arc;
use relative_path::RelativePathBuf;
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
use ra_db::mock::FileMap;
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FilePosition, SourceRootId};
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FilePosition, FileRange, SourceRootId};
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
/// from a set of in-memory files.
@ -66,6 +66,12 @@ impl MockAnalysis {
self.files.push((path.to_string(), text.to_string()));
FilePosition { file_id, offset }
}
pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
let (range, text) = extract_range(text);
let file_id = FileId((self.files.len() + 1) as u32);
self.files.push((path.to_string(), text.to_string()));
FileRange { file_id, range }
}
pub fn id_of(&self, path: &str) -> FileId {
let (idx, _) = self
.files
@ -115,3 +121,10 @@ pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) {
let pos = mock.add_file_with_position("/main.rs", code);
(mock.analysis(), pos)
}
/// Creates analysis for a single file, returns range marked with a pair of <|>.
pub fn single_file_with_range(code: &str) -> (Analysis, FileRange) {
let mut mock = MockAnalysis::new();
let pos = mock.add_file_with_range("/main.rs", code);
(mock.analysis(), pos)
}