diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 4703cfaf72..291b5ee40a 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -9,8 +9,9 @@ use ra_syntax::{ SyntaxNode, TextRange, }; +use crate::{db::RootDatabase, expand::original_range, FileSymbol}; + use super::short_label::ShortLabel; -use crate::{db::RootDatabase, FileSymbol}; /// `NavigationTarget` represents and element in the editor's UI which you can /// click on to navigate to a particular piece of code. @@ -79,13 +80,12 @@ impl NavigationTarget { pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some(src) = module.declaration_source(db) { - let (file_id, text_range) = - find_range_from_node(db, src.as_ref().map(|it| it.syntax())); + let frange = original_range(db, src.as_ref().map(|it| it.syntax())); return NavigationTarget::from_syntax( - file_id, + frange.file_id, name, None, - text_range, + frange.range, src.ast.syntax(), src.ast.doc_comment_text(), src.ast.short_label(), @@ -149,14 +149,14 @@ impl NavigationTarget { //FIXME: use `_` instead of empty string let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); let focus_range = - node.name().map(|it| find_range_from_node(db, Source::new(file_id, it.syntax())).1); - let (file_id, full_range) = find_range_from_node(db, Source::new(file_id, node.syntax())); + node.name().map(|it| original_range(db, Source::new(file_id, it.syntax())).range); + let frange = original_range(db, Source::new(file_id, node.syntax())); NavigationTarget::from_syntax( - file_id, + frange.file_id, name, focus_range, - full_range, + frange.range, node.syntax(), docs, description, @@ -234,26 +234,26 @@ impl ToNav for hir::Module { let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); match &src.ast { ModuleSource::SourceFile(node) => { - let (file_id, text_range) = find_range_from_node(db, src.with_ast(node.syntax())); + let frange = original_range(db, src.with_ast(node.syntax())); NavigationTarget::from_syntax( - file_id, + frange.file_id, name, None, - text_range, + frange.range, node.syntax(), None, None, ) } ModuleSource::Module(node) => { - let (file_id, text_range) = find_range_from_node(db, src.with_ast(node.syntax())); + let frange = original_range(db, src.with_ast(node.syntax())); NavigationTarget::from_syntax( - file_id, + frange.file_id, name, None, - text_range, + frange.range, node.syntax(), node.doc_comment_text(), node.short_label(), @@ -266,13 +266,13 @@ impl ToNav for hir::Module { impl ToNav for hir::ImplBlock { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.source(db); - let (file_id, text_range) = find_range_from_node(db, src.as_ref().map(|it| it.syntax())); + let frange = original_range(db, src.as_ref().map(|it| it.syntax())); NavigationTarget::from_syntax( - file_id, + frange.file_id, "impl".into(), None, - text_range, + frange.range, src.ast.syntax(), None, None, @@ -293,12 +293,12 @@ impl ToNav for hir::StructField { it.short_label(), ), FieldSource::Pos(it) => { - let (file_id, text_range) = find_range_from_node(db, src.with_ast(it.syntax())); + let frange = original_range(db, src.with_ast(it.syntax())); NavigationTarget::from_syntax( - file_id, + frange.file_id, "".into(), None, - text_range, + frange.range, it.syntax(), None, None, @@ -362,18 +362,6 @@ impl ToNav for hir::Local { } } -fn find_range_from_node(db: &RootDatabase, node: Source<&SyntaxNode>) -> (FileId, TextRange) { - let text_range = node.ast.text_range(); - let (file_id, text_range) = node - .file_id - .expansion_info(db) - .and_then(|expansion_info| expansion_info.find_range(text_range)) - .unwrap_or((node.file_id, text_range)); - - // FIXME: handle recursive macro generated macro - (file_id.original_file(db), text_range) -} - pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); diff --git a/crates/ra_ide_api/src/expand.rs b/crates/ra_ide_api/src/expand.rs new file mode 100644 index 0000000000..5f1fb9a126 --- /dev/null +++ b/crates/ra_ide_api/src/expand.rs @@ -0,0 +1,42 @@ +//! Utilities to work with files, produced by macros. +use std::iter::successors; + +use hir::Source; +use ra_db::FileId; +use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken}; + +use crate::{db::RootDatabase, FileRange}; + +pub(crate) fn original_range(db: &RootDatabase, node: Source<&SyntaxNode>) -> FileRange { + let text_range = node.ast.text_range(); + let (file_id, range) = node + .file_id + .expansion_info(db) + .and_then(|expansion_info| expansion_info.find_range(text_range)) + .unwrap_or((node.file_id, text_range)); + + // FIXME: handle recursive macro generated macro + FileRange { file_id: file_id.original_file(db), range } +} + +pub(crate) fn descend_into_macros( + db: &RootDatabase, + file_id: FileId, + token: SyntaxToken, +) -> Source { + let src = Source::new(file_id.into(), token); + + successors(Some(src), |token| { + let macro_call = token.ast.ancestors().find_map(ast::MacroCall::cast)?; + let tt = macro_call.token_tree()?; + if !token.ast.text_range().is_subrange(&tt.syntax().text_range()) { + return None; + } + let source_analyzer = + hir::SourceAnalyzer::new(db, token.with_ast(token.ast.parent()).as_ref(), None); + let exp = source_analyzer.expand(db, ¯o_call)?; + exp.map_token_down(db, token.as_ref()) + }) + .last() + .unwrap() +} diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index b693a4c313..1a8db0ea07 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,16 +1,15 @@ //! FIXME: write short doc here -use std::iter::successors; - use hir::{db::AstDatabase, Source}; use ra_syntax::{ ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxNode, SyntaxToken, + match_ast, AstNode, SyntaxNode, }; use crate::{ db::RootDatabase, display::{ShortLabel, ToNav}, + expand::descend_into_macros, references::{classify_name_ref, NameKind::*}, FilePosition, NavigationTarget, RangeInfo, }; @@ -19,7 +18,9 @@ pub(crate) fn goto_definition( db: &RootDatabase, position: FilePosition, ) -> Option>> { - let token = descend_into_macros(db, position)?; + let file = db.parse_or_expand(position.file_id.into())?; + let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; + let token = descend_into_macros(db, position.file_id, token); let res = match_ast! { match (token.ast.parent()) { @@ -39,24 +40,6 @@ pub(crate) fn goto_definition( Some(res) } -fn descend_into_macros(db: &RootDatabase, position: FilePosition) -> Option> { - let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; - - successors(Some(Source::new(position.file_id.into(), token)), |token| { - let macro_call = token.ast.ancestors().find_map(ast::MacroCall::cast)?; - let tt = macro_call.token_tree()?; - if !token.ast.text_range().is_subrange(&tt.syntax().text_range()) { - return None; - } - let source_analyzer = - hir::SourceAnalyzer::new(db, token.with_ast(token.ast.parent()).as_ref(), None); - let exp = source_analyzer.expand(db, ¯o_call)?; - exp.map_token_down(db, token.as_ref()) - }) - .last() -} - #[derive(Debug)] pub(crate) enum ReferenceResult { Exact(NavigationTarget), diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 484fbcc823..110ddcd626 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -41,6 +41,7 @@ mod matching_brace; mod display; mod inlay_hints; mod wasm_shims; +mod expand; #[cfg(test)] mod marks;