2298: Add ra_ide_api::expand r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-18 11:24:35 +00:00 committed by GitHub
commit 91509073c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 61 deletions

View file

@ -1,6 +1,6 @@
//! 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_syntax::{
ast::{self, DocCommentsOwner, NameOwner},
@ -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,12 +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.file_id, src.ast.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(),
@ -147,14 +148,15 @@ impl NavigationTarget {
) -> 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, file_id, it.syntax()).1);
let (file_id, full_range) = find_range_from_node(db, file_id, node.syntax());
let focus_range =
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,
@ -230,28 +232,28 @@ impl ToNav for hir::Module {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.definition_source(db);
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
match src.ast {
match &src.ast {
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(
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.file_id, 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(),
@ -264,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.file_id, src.ast.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,
@ -282,21 +284,21 @@ impl ToNav for hir::StructField {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
match src.ast {
match &src.ast {
FieldSource::Named(it) => NavigationTarget::from_named(
db,
src.file_id,
&it,
it,
it.doc_comment_text(),
it.short_label(),
),
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(
file_id,
frange.file_id,
"".into(),
None,
text_range,
frange.range,
it.syntax(),
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> {
let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(parse.tree().syntax());

View 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, &macro_call)?;
exp.map_token_down(db, token.as_ref())
})
.last()
.unwrap()
}

View file

@ -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<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! {
match (token.ast.parent()) {
@ -39,24 +40,6 @@ pub(crate) fn goto_definition(
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, &macro_call)?;
exp.map_token_down(db, token.as_ref())
})
.last()
}
#[derive(Debug)]
pub(crate) enum ReferenceResult {
Exact(NavigationTarget),

View file

@ -41,6 +41,7 @@ mod matching_brace;
mod display;
mod inlay_hints;
mod wasm_shims;
mod expand;
#[cfg(test)]
mod marks;