use Source for StructField

This commit is contained in:
Aleksey Kladov 2019-06-11 17:43:36 +03:00
parent 8b94b429e5
commit 178d8e96b5
5 changed files with 17 additions and 10 deletions

View file

@ -44,8 +44,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
.into_iter()
.map(|field| {
let name = field.name(db).to_string();
let (_, source) = field.source(db);
match source {
let src = field.source(db);
match src.ast {
FieldSource::Named(_) => name,
FieldSource::Pos(_) => "_".to_string(),
}

View file

@ -346,13 +346,20 @@ pub enum FieldSource {
Pos(TreeArc<ast::PosFieldDef>),
}
impl HasSource for StructField {
type Ast = FieldSource;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
self.source_impl(db).into()
}
}
impl StructField {
pub fn name(&self, db: &impl HirDatabase) -> Name {
self.parent.variant_data(db).fields().unwrap()[self.id].name.clone()
}
pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, FieldSource) {
self.source_impl(db)
pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
self.source_impl(db).into()
}
pub fn ty(&self, db: &impl HirDatabase) -> Ty {

View file

@ -72,7 +72,7 @@ pub(crate) fn documentation_query(
) -> Option<Documentation> {
match def {
DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.1),
DocDef::StructField(it) => match it.source(db).1 {
DocDef::StructField(it) => match it.source(db).ast {
FieldSource::Named(named) => docs_from_ast(&*named),
FieldSource::Pos(..) => return None,
},

View file

@ -168,9 +168,9 @@ impl NavigationTarget {
}
pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
let (file_id, field) = field.source(db);
let file_id = file_id.original_file(db);
match field {
let src = field.source(db);
let file_id = src.file_id.original_file(db);
match src.ast {
FieldSource::Named(it) => {
NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label())
}

View file

@ -102,8 +102,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
res.extend(hover_text(src.ast.doc_comment_text(), None));
}
Some(FieldAccess(it)) => {
let it = it.source(db).1;
if let hir::FieldSource::Named(it) = it {
let src = it.source(db);
if let hir::FieldSource::Named(it) = src.ast {
res.extend(hover_text(it.doc_comment_text(), it.short_label()));
}
}