rust/crates/ra_ide/src/expand.rs

93 lines
3.0 KiB
Rust
Raw Normal View History

//! Utilities to work with files, produced by macros.
use std::iter::successors;
use hir::{InFile, Origin};
use ra_db::FileId;
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange};
use crate::{db::RootDatabase, FileRange};
2019-12-15 02:34:16 +08:00
pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
if let Some((range, Origin::Call)) = original_range_and_origin(db, node) {
return range;
}
if let Some(expansion) = node.file_id.expansion_info(db) {
if let Some(call_node) = expansion.call_node() {
return FileRange {
file_id: call_node.file_id.original_file(db),
range: call_node.value.text_range(),
};
}
}
FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
2019-12-15 01:20:07 +08:00
}
2019-12-15 02:34:16 +08:00
fn original_range_and_origin(
2019-12-15 01:20:07 +08:00
db: &RootDatabase,
node: InFile<&SyntaxNode>,
2019-12-15 02:34:16 +08:00
) -> Option<(FileRange, Origin)> {
2019-12-15 01:20:07 +08:00
let expansion = node.file_id.expansion_info(db)?;
// the input node has only one token ?
let single = node.value.first_token()? == node.value.last_token()?;
// FIXME: We should handle recurside macro expansions
2019-12-15 02:34:16 +08:00
let (range, origin) = node.value.descendants().find_map(|it| {
let first = it.first_token()?;
let last = it.last_token()?;
if !single && first == last {
return None;
}
// Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
if first.file_id != last.file_id || first_origin != last_origin {
return None;
}
// FIXME: Add union method in TextRange
Some((
first.with_value(union_range(first.value.text_range(), last.value.text_range())),
first_origin,
))
})?;
return Some((
FileRange { file_id: range.file_id.original_file(db), range: range.value },
origin,
));
2019-12-15 01:20:07 +08:00
fn union_range(a: TextRange, b: TextRange) -> TextRange {
let start = a.start().min(b.start());
let end = a.end().max(b.end());
TextRange::from_to(start, end)
2019-11-18 15:08:39 +03:00
}
}
pub(crate) fn descend_into_macros(
db: &RootDatabase,
file_id: FileId,
token: SyntaxToken,
2019-11-28 12:50:26 +03:00
) -> InFile<SyntaxToken> {
let src = InFile::new(file_id.into(), token);
successors(Some(src), |token| {
2019-11-20 09:40:36 +03:00
let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
let tt = macro_call.token_tree()?;
2019-11-20 09:40:36 +03:00
if !token.value.text_range().is_subrange(&tt.syntax().text_range()) {
return None;
}
let source_analyzer =
2019-11-20 13:09:21 +03:00
hir::SourceAnalyzer::new(db, token.with_value(token.value.parent()).as_ref(), None);
2019-11-20 12:21:31 +08:00
let exp = source_analyzer.expand(db, token.with_value(&macro_call))?;
exp.map_token_down(db, token.as_ref())
})
.last()
.unwrap()
}