More Source

This commit is contained in:
Aleksey Kladov 2019-11-18 14:07:13 +03:00
parent 614c034ffc
commit 6d8ca870b3

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},
@ -79,7 +79,8 @@ 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 (file_id, text_range) =
find_range_from_node(db, src.as_ref().map(|it| it.syntax()));
return NavigationTarget::from_syntax(
file_id,
name,
@ -147,8 +148,9 @@ 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| 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()));
NavigationTarget::from_syntax(
file_id,
@ -230,9 +232,9 @@ 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 (file_id, text_range) = find_range_from_node(db, src.with_ast(node.syntax()));
NavigationTarget::from_syntax(
file_id,
@ -245,7 +247,7 @@ impl ToNav for hir::Module {
)
}
ModuleSource::Module(node) => {
let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
let (file_id, text_range) = find_range_from_node(db, src.with_ast(node.syntax()));
NavigationTarget::from_syntax(
file_id,
@ -264,7 +266,7 @@ 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 (file_id, text_range) = find_range_from_node(db, src.as_ref().map(|it| it.syntax()));
NavigationTarget::from_syntax(
file_id,
@ -282,16 +284,16 @@ 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 (file_id, text_range) = find_range_from_node(db, src.with_ast(it.syntax()));
NavigationTarget::from_syntax(
file_id,
"".into(),
@ -360,16 +362,13 @@ 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
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((src, text_range));
.unwrap_or((node.file_id, text_range));
// FIXME: handle recursive macro generated macro
(file_id.original_file(db), text_range)