mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
introduce Source struct
This commit is contained in:
parent
e505fe9d7b
commit
91c120ccea
6 changed files with 36 additions and 26 deletions
|
@ -59,8 +59,8 @@ impl StructData {
|
||||||
db: &(impl DefDatabase + AstDatabase),
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
struct_: Struct,
|
struct_: Struct,
|
||||||
) -> Arc<StructData> {
|
) -> Arc<StructData> {
|
||||||
let (_, struct_def) = struct_.source(db);
|
let src = struct_.source(db);
|
||||||
Arc::new(StructData::new(&*struct_def))
|
Arc::new(StructData::new(&*src.ast))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,9 +211,8 @@ impl StructField {
|
||||||
let es;
|
let es;
|
||||||
let (file_id, struct_kind) = match self.parent {
|
let (file_id, struct_kind) = match self.parent {
|
||||||
VariantDef::Struct(s) => {
|
VariantDef::Struct(s) => {
|
||||||
let (file_id, source) = s.source(db);
|
ss = s.source(db);
|
||||||
ss = source;
|
(ss.file_id, ss.ast.kind())
|
||||||
(file_id, ss.kind())
|
|
||||||
}
|
}
|
||||||
VariantDef::EnumVariant(e) => {
|
VariantDef::EnumVariant(e) => {
|
||||||
let (file_id, source) = e.source(db);
|
let (file_id, source) = e.source(db);
|
||||||
|
|
|
@ -20,6 +20,17 @@ use crate::{
|
||||||
type_ref::Mutability,
|
type_ref::Mutability,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub struct Source<T> {
|
||||||
|
pub file_id: HirFileId,
|
||||||
|
pub ast: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<(HirFileId, T)> for Source<T> {
|
||||||
|
fn from((file_id, ast): (HirFileId, T)) -> Self {
|
||||||
|
Source { file_id, ast }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main interface with which
|
/// hir::Crate describes a single crate. It's the main interface with which
|
||||||
/// a crate's dependencies interact. Mostly, it should be just a proxy for the
|
/// a crate's dependencies interact. Mostly, it should be just a proxy for the
|
||||||
/// root module.
|
/// root module.
|
||||||
|
@ -354,11 +365,8 @@ pub struct Struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Struct {
|
impl Struct {
|
||||||
pub fn source(
|
pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
|
||||||
self,
|
self.id.source(db).into()
|
||||||
db: &(impl DefDatabase + AstDatabase),
|
|
||||||
) -> (HirFileId, TreeArc<ast::StructDef>) {
|
|
||||||
self.id.source(db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn module(self, db: &impl HirDatabase) -> Module {
|
pub fn module(self, db: &impl HirDatabase) -> Module {
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub(crate) fn documentation_query(
|
||||||
FieldSource::Named(named) => docs_from_ast(&*named),
|
FieldSource::Named(named) => docs_from_ast(&*named),
|
||||||
FieldSource::Pos(..) => return None,
|
FieldSource::Pos(..) => return None,
|
||||||
},
|
},
|
||||||
DocDef::Struct(it) => docs_from_ast(&*it.source(db).1),
|
DocDef::Struct(it) => docs_from_ast(&*it.source(db).ast),
|
||||||
DocDef::Enum(it) => docs_from_ast(&*it.source(db).1),
|
DocDef::Enum(it) => docs_from_ast(&*it.source(db).1),
|
||||||
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1),
|
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1),
|
||||||
DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
|
DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
|
||||||
|
|
|
@ -69,7 +69,7 @@ impl GenericParams {
|
||||||
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
|
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
|
||||||
match def {
|
match def {
|
||||||
GenericDef::Function(it) => generics.fill(&*it.source(db).1, start),
|
GenericDef::Function(it) => generics.fill(&*it.source(db).1, start),
|
||||||
GenericDef::Struct(it) => generics.fill(&*it.source(db).1, start),
|
GenericDef::Struct(it) => generics.fill(&*it.source(db).ast, start),
|
||||||
GenericDef::Union(it) => generics.fill(&*it.source(db).1, start),
|
GenericDef::Union(it) => generics.fill(&*it.source(db).1, start),
|
||||||
GenericDef::Enum(it) => generics.fill(&*it.source(db).1, start),
|
GenericDef::Enum(it) => generics.fill(&*it.source(db).1, start),
|
||||||
GenericDef::Trait(it) => {
|
GenericDef::Trait(it) => {
|
||||||
|
|
|
@ -189,12 +189,12 @@ impl NavigationTarget {
|
||||||
pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget {
|
pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget {
|
||||||
match adt_def {
|
match adt_def {
|
||||||
hir::AdtDef::Struct(s) => {
|
hir::AdtDef::Struct(s) => {
|
||||||
let (file_id, node) = s.source(db);
|
let src = s.source(db);
|
||||||
NavigationTarget::from_named(
|
NavigationTarget::from_named(
|
||||||
file_id.original_file(db),
|
src.file_id.original_file(db),
|
||||||
&*node,
|
&*src.ast,
|
||||||
node.doc_comment_text(),
|
src.ast.doc_comment_text(),
|
||||||
node.short_label(),
|
src.ast.short_label(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
hir::AdtDef::Union(s) => {
|
hir::AdtDef::Union(s) => {
|
||||||
|
@ -226,12 +226,12 @@ impl NavigationTarget {
|
||||||
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
|
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
|
||||||
hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
|
hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
|
||||||
hir::ModuleDef::Struct(s) => {
|
hir::ModuleDef::Struct(s) => {
|
||||||
let (file_id, node) = s.source(db);
|
let src = s.source(db);
|
||||||
NavigationTarget::from_named(
|
NavigationTarget::from_named(
|
||||||
file_id.original_file(db),
|
src.file_id.original_file(db),
|
||||||
&*node,
|
&*src.ast,
|
||||||
node.doc_comment_text(),
|
src.ast.doc_comment_text(),
|
||||||
node.short_label(),
|
src.ast.short_label(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
hir::ModuleDef::Union(s) => {
|
hir::ModuleDef::Union(s) => {
|
||||||
|
|
|
@ -136,8 +136,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
|
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
|
||||||
}
|
}
|
||||||
hir::ModuleDef::Struct(it) => {
|
hir::ModuleDef::Struct(it) => {
|
||||||
let it = it.source(db).1;
|
let src = it.source(db);
|
||||||
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
|
res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
|
||||||
}
|
}
|
||||||
hir::ModuleDef::Union(it) => {
|
hir::ModuleDef::Union(it) => {
|
||||||
let it = it.source(db).1;
|
let it = it.source(db).1;
|
||||||
|
@ -176,8 +176,11 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
if let Some((adt_def, _)) = ty.as_adt() {
|
if let Some((adt_def, _)) = ty.as_adt() {
|
||||||
match adt_def {
|
match adt_def {
|
||||||
hir::AdtDef::Struct(it) => {
|
hir::AdtDef::Struct(it) => {
|
||||||
let it = it.source(db).1;
|
let src = it.source(db);
|
||||||
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
|
res.extend(hover_text(
|
||||||
|
src.ast.doc_comment_text(),
|
||||||
|
src.ast.short_label(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
hir::AdtDef::Union(it) => {
|
hir::AdtDef::Union(it) => {
|
||||||
let it = it.source(db).1;
|
let it = it.source(db).1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue