diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 451b227a6e..2e52a1f5c0 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -58,6 +58,6 @@ pub use hir_def::{ type_ref::Mutability, }; pub use hir_expand::{ - name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, + name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin, }; pub use hir_ty::{display::HirDisplay, CallableDef}; diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 94e1e466a5..cb4e1950bf 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -214,7 +214,13 @@ pub struct ExpansionInfo { exp_map: Arc, } +pub use mbe::Origin; + impl ExpansionInfo { + pub fn call_node(&self) -> Option> { + Some(self.arg.with_value(self.arg.value.parent()?)) + } + pub fn map_token_down(&self, token: InFile<&SyntaxToken>) -> Option> { assert_eq!(token.file_id, self.arg.file_id); let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?; @@ -228,7 +234,10 @@ impl ExpansionInfo { Some(self.expanded.with_value(token)) } - pub fn map_token_up(&self, token: InFile<&SyntaxToken>) -> Option> { + pub fn map_token_up( + &self, + token: InFile<&SyntaxToken>, + ) -> Option<(InFile, Origin)> { let token_id = self.exp_map.token_by_range(token.value.text_range())?; let (token_id, origin) = self.macro_def.0.map_id_up(token_id); @@ -242,7 +251,7 @@ impl ExpansionInfo { let range = token_map.range_by_token(token_id)?; let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) .into_token()?; - Some(tt.with_value(token)) + Some((tt.with_value(token), origin)) } } diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 661628ae46..7a22bb0a4e 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -1,56 +1,66 @@ //! Utilities to work with files, produced by macros. use std::iter::successors; -use hir::InFile; +use hir::{InFile, Origin}; use ra_db::FileId; use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; use crate::{db::RootDatabase, FileRange}; pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { - let expansion = match node.file_id.expansion_info(db) { - None => { + 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: node.file_id.original_file(db), - range: node.value.text_range(), - } + file_id: call_node.file_id.original_file(db), + range: call_node.value.text_range(), + }; } - Some(it) => it, - }; + } + + FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } +} + +fn original_range_and_origin( + db: &RootDatabase, + node: InFile<&SyntaxNode>, +) -> Option<(FileRange, Origin)> { + 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 + let (range, origin) = node.value.descendants().find_map(|it| { + let first = it.first_token()?; + let last = it.last_token()?; - let range = node.value.descendants_with_tokens().find_map(|it| { - match it.as_token() { - // FIXME: Remove this branch after all `tt::TokenTree`s have a proper `TokenId`, - // and return the range of the overall macro expansions if mapping first and last tokens fails. - Some(token) => { - let token = expansion.map_token_up(node.with_value(&token))?; - Some(token.with_value(token.value.text_range())) - } - None => { - // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens - let n = it.into_node()?; - let first = expansion.map_token_up(node.with_value(&n.first_token()?))?; - let last = expansion.map_token_up(node.with_value(&n.last_token()?))?; - - // FIXME: Is is possible ? - if first.file_id != last.file_id { - return None; - } - - // FIXME: Add union method in TextRange - let range = union_range(first.value.text_range(), last.value.text_range()); - Some(first.with_value(range)) - } + if !single && first == last { + return None; } - }); - return match range { - Some(it) => FileRange { file_id: it.file_id.original_file(db), range: it.value }, - None => { - FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } + // 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, + )); fn union_range(a: TextRange, b: TextRange) -> TextRange { let start = a.start().min(b.start()); diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 27052d72b6..bee8e9df28 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -221,7 +221,7 @@ fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option (fn $name() {}) } - define_fn!( - foo - ) + define_fn!(foo); fn bar() { <|>foo(); } ", - "foo FN_DEF FileId(1) [80; 83) [80; 83)", + "foo FN_DEF FileId(1) [64; 80) [75; 78)", + "define_fn!(foo);|foo", ); } #[test] fn goto_definition_works_for_macro_defined_fn_no_arg() { - check_goto( + check_goto_with_range_content( " //- /lib.rs macro_rules! define_fn { @@ -397,7 +414,8 @@ mod tests { <|>foo(); } ", - "foo FN_DEF FileId(1) [39; 42) [39; 42)", + "foo FN_DEF FileId(1) [51; 64) [51; 64)", + "define_fn!();|define_fn!();", ); } diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 0d2d43bef4..ce2deadf6e 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -104,6 +104,7 @@ impl Shift { } } +#[derive(Debug, Eq, PartialEq)] pub enum Origin { Def, Call,