mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
Merge #2298
2298: Add ra_ide_api::expand r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
91509073c5
4 changed files with 74 additions and 61 deletions
|
@ -1,6 +1,6 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir::{AssocItem, Either, FieldSource, HasSource, ModuleSource};
|
use hir::{AssocItem, Either, FieldSource, HasSource, ModuleSource, Source};
|
||||||
use ra_db::{FileId, SourceDatabase};
|
use ra_db::{FileId, SourceDatabase};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, DocCommentsOwner, NameOwner},
|
ast::{self, DocCommentsOwner, NameOwner},
|
||||||
|
@ -9,8 +9,9 @@ use ra_syntax::{
|
||||||
SyntaxNode, TextRange,
|
SyntaxNode, TextRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::{db::RootDatabase, expand::original_range, FileSymbol};
|
||||||
|
|
||||||
use super::short_label::ShortLabel;
|
use super::short_label::ShortLabel;
|
||||||
use crate::{db::RootDatabase, FileSymbol};
|
|
||||||
|
|
||||||
/// `NavigationTarget` represents and element in the editor's UI which you can
|
/// `NavigationTarget` represents and element in the editor's UI which you can
|
||||||
/// click on to navigate to a particular piece of code.
|
/// click on to navigate to a particular piece of code.
|
||||||
|
@ -79,12 +80,12 @@ impl NavigationTarget {
|
||||||
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> 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();
|
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||||
if let Some(src) = module.declaration_source(db) {
|
if let Some(src) = module.declaration_source(db) {
|
||||||
let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
|
let frange = original_range(db, src.as_ref().map(|it| it.syntax()));
|
||||||
return NavigationTarget::from_syntax(
|
return NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
name,
|
name,
|
||||||
None,
|
None,
|
||||||
text_range,
|
frange.range,
|
||||||
src.ast.syntax(),
|
src.ast.syntax(),
|
||||||
src.ast.doc_comment_text(),
|
src.ast.doc_comment_text(),
|
||||||
src.ast.short_label(),
|
src.ast.short_label(),
|
||||||
|
@ -147,14 +148,15 @@ impl NavigationTarget {
|
||||||
) -> NavigationTarget {
|
) -> NavigationTarget {
|
||||||
//FIXME: use `_` instead of empty string
|
//FIXME: use `_` instead of empty string
|
||||||
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
|
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
|
||||||
let focus_range = node.name().map(|it| find_range_from_node(db, file_id, it.syntax()).1);
|
let focus_range =
|
||||||
let (file_id, full_range) = find_range_from_node(db, 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(
|
NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
name,
|
name,
|
||||||
focus_range,
|
focus_range,
|
||||||
full_range,
|
frange.range,
|
||||||
node.syntax(),
|
node.syntax(),
|
||||||
docs,
|
docs,
|
||||||
description,
|
description,
|
||||||
|
@ -230,28 +232,28 @@ impl ToNav for hir::Module {
|
||||||
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
||||||
let src = self.definition_source(db);
|
let src = self.definition_source(db);
|
||||||
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||||
match src.ast {
|
match &src.ast {
|
||||||
ModuleSource::SourceFile(node) => {
|
ModuleSource::SourceFile(node) => {
|
||||||
let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
|
let frange = original_range(db, src.with_ast(node.syntax()));
|
||||||
|
|
||||||
NavigationTarget::from_syntax(
|
NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
name,
|
name,
|
||||||
None,
|
None,
|
||||||
text_range,
|
frange.range,
|
||||||
node.syntax(),
|
node.syntax(),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ModuleSource::Module(node) => {
|
ModuleSource::Module(node) => {
|
||||||
let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
|
let frange = original_range(db, src.with_ast(node.syntax()));
|
||||||
|
|
||||||
NavigationTarget::from_syntax(
|
NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
name,
|
name,
|
||||||
None,
|
None,
|
||||||
text_range,
|
frange.range,
|
||||||
node.syntax(),
|
node.syntax(),
|
||||||
node.doc_comment_text(),
|
node.doc_comment_text(),
|
||||||
node.short_label(),
|
node.short_label(),
|
||||||
|
@ -264,13 +266,13 @@ impl ToNav for hir::Module {
|
||||||
impl ToNav for hir::ImplBlock {
|
impl ToNav for hir::ImplBlock {
|
||||||
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
||||||
let src = self.source(db);
|
let src = self.source(db);
|
||||||
let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
|
let frange = original_range(db, src.as_ref().map(|it| it.syntax()));
|
||||||
|
|
||||||
NavigationTarget::from_syntax(
|
NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
"impl".into(),
|
"impl".into(),
|
||||||
None,
|
None,
|
||||||
text_range,
|
frange.range,
|
||||||
src.ast.syntax(),
|
src.ast.syntax(),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
|
@ -282,21 +284,21 @@ impl ToNav for hir::StructField {
|
||||||
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
||||||
let src = self.source(db);
|
let src = self.source(db);
|
||||||
|
|
||||||
match src.ast {
|
match &src.ast {
|
||||||
FieldSource::Named(it) => NavigationTarget::from_named(
|
FieldSource::Named(it) => NavigationTarget::from_named(
|
||||||
db,
|
db,
|
||||||
src.file_id,
|
src.file_id,
|
||||||
&it,
|
it,
|
||||||
it.doc_comment_text(),
|
it.doc_comment_text(),
|
||||||
it.short_label(),
|
it.short_label(),
|
||||||
),
|
),
|
||||||
FieldSource::Pos(it) => {
|
FieldSource::Pos(it) => {
|
||||||
let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax());
|
let frange = original_range(db, src.with_ast(it.syntax()));
|
||||||
NavigationTarget::from_syntax(
|
NavigationTarget::from_syntax(
|
||||||
file_id,
|
frange.file_id,
|
||||||
"".into(),
|
"".into(),
|
||||||
None,
|
None,
|
||||||
text_range,
|
frange.range,
|
||||||
it.syntax(),
|
it.syntax(),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
|
@ -360,21 +362,6 @@ impl ToNav for hir::Local {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_range_from_node(
|
|
||||||
db: &RootDatabase,
|
|
||||||
src: hir::HirFileId,
|
|
||||||
node: &SyntaxNode,
|
|
||||||
) -> (FileId, TextRange) {
|
|
||||||
let text_range = node.text_range();
|
|
||||||
let (file_id, text_range) = src
|
|
||||||
.expansion_info(db)
|
|
||||||
.and_then(|expansion_info| expansion_info.find_range(text_range))
|
|
||||||
.unwrap_or((src, 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<String> {
|
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
|
||||||
let parse = db.parse(symbol.file_id);
|
let parse = db.parse(symbol.file_id);
|
||||||
let node = symbol.ptr.to_node(parse.tree().syntax());
|
let node = symbol.ptr.to_node(parse.tree().syntax());
|
||||||
|
|
42
crates/ra_ide_api/src/expand.rs
Normal file
42
crates/ra_ide_api/src/expand.rs
Normal file
|
@ -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<SyntaxToken> {
|
||||||
|
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()
|
||||||
|
}
|
|
@ -1,16 +1,15 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use std::iter::successors;
|
|
||||||
|
|
||||||
use hir::{db::AstDatabase, Source};
|
use hir::{db::AstDatabase, Source};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, DocCommentsOwner},
|
ast::{self, DocCommentsOwner},
|
||||||
match_ast, AstNode, SyntaxNode, SyntaxToken,
|
match_ast, AstNode, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
display::{ShortLabel, ToNav},
|
display::{ShortLabel, ToNav},
|
||||||
|
expand::descend_into_macros,
|
||||||
references::{classify_name_ref, NameKind::*},
|
references::{classify_name_ref, NameKind::*},
|
||||||
FilePosition, NavigationTarget, RangeInfo,
|
FilePosition, NavigationTarget, RangeInfo,
|
||||||
};
|
};
|
||||||
|
@ -19,7 +18,9 @@ pub(crate) fn goto_definition(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||||
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! {
|
let res = match_ast! {
|
||||||
match (token.ast.parent()) {
|
match (token.ast.parent()) {
|
||||||
|
@ -39,24 +40,6 @@ pub(crate) fn goto_definition(
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn descend_into_macros(db: &RootDatabase, position: FilePosition) -> Option<Source<SyntaxToken>> {
|
|
||||||
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)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum ReferenceResult {
|
pub(crate) enum ReferenceResult {
|
||||||
Exact(NavigationTarget),
|
Exact(NavigationTarget),
|
||||||
|
|
|
@ -41,6 +41,7 @@ mod matching_brace;
|
||||||
mod display;
|
mod display;
|
||||||
mod inlay_hints;
|
mod inlay_hints;
|
||||||
mod wasm_shims;
|
mod wasm_shims;
|
||||||
|
mod expand;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod marks;
|
mod marks;
|
||||||
|
|
Loading…
Reference in a new issue