mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
nameify structs&enums
This commit is contained in:
parent
11122e29b7
commit
7928995876
5 changed files with 43 additions and 57 deletions
|
@ -1,10 +1,10 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}};
|
||||
use ra_syntax::ast::{self, NameOwner, StructFlavor};
|
||||
|
||||
use crate::{
|
||||
DefId, Cancelable,
|
||||
db::{HirDatabase},
|
||||
DefId, Cancelable, Name, AsName,
|
||||
db::HirDatabase,
|
||||
type_ref::TypeRef,
|
||||
};
|
||||
|
||||
|
@ -29,26 +29,26 @@ impl Struct {
|
|||
Ok(db.struct_data(self.def_id)?)
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.struct_data(self.def_id)?.name.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StructData {
|
||||
name: Option<SmolStr>,
|
||||
name: Option<Name>,
|
||||
variant_data: Arc<VariantData>,
|
||||
}
|
||||
|
||||
impl StructData {
|
||||
pub(crate) fn new(struct_def: ast::StructDef) -> StructData {
|
||||
let name = struct_def.name().map(|n| n.text());
|
||||
let name = struct_def.name().map(|n| n.as_name());
|
||||
let variant_data = VariantData::new(struct_def.flavor());
|
||||
let variant_data = Arc::new(variant_data);
|
||||
StructData { name, variant_data }
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&SmolStr> {
|
||||
pub fn name(&self) -> Option<&Name> {
|
||||
self.name.as_ref()
|
||||
}
|
||||
|
||||
|
@ -70,31 +70,29 @@ impl Enum {
|
|||
self.def_id
|
||||
}
|
||||
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
||||
}
|
||||
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SmolStr, Arc<VariantData>)>> {
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
|
||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EnumData {
|
||||
name: Option<SmolStr>,
|
||||
variants: Vec<(SmolStr, Arc<VariantData>)>,
|
||||
name: Option<Name>,
|
||||
variants: Vec<(Name, Arc<VariantData>)>,
|
||||
}
|
||||
|
||||
impl EnumData {
|
||||
pub(crate) fn new(enum_def: ast::EnumDef) -> Self {
|
||||
let name = enum_def.name().map(|n| n.text());
|
||||
let name = enum_def.name().map(|n| n.as_name());
|
||||
let variants = if let Some(evl) = enum_def.variant_list() {
|
||||
evl.variants()
|
||||
.map(|v| {
|
||||
(
|
||||
v.name()
|
||||
.map(|n| n.text())
|
||||
.unwrap_or_else(|| SmolStr::new("[error]")),
|
||||
v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
|
||||
Arc::new(VariantData::new(v.flavor())),
|
||||
)
|
||||
})
|
||||
|
@ -109,12 +107,12 @@ impl EnumData {
|
|||
/// A single field of an enum variant or struct
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StructField {
|
||||
name: SmolStr,
|
||||
name: Name,
|
||||
type_ref: TypeRef,
|
||||
}
|
||||
|
||||
impl StructField {
|
||||
pub fn name(&self) -> SmolStr {
|
||||
pub fn name(&self) -> Name {
|
||||
self.name.clone()
|
||||
}
|
||||
pub fn type_ref(&self) -> &TypeRef {
|
||||
|
@ -138,7 +136,7 @@ impl VariantData {
|
|||
.fields()
|
||||
.enumerate()
|
||||
.map(|(i, fd)| StructField {
|
||||
name: SmolStr::new(i.to_string()),
|
||||
name: Name::tuple_field_name(i),
|
||||
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
|
||||
})
|
||||
.collect();
|
||||
|
@ -148,10 +146,7 @@ impl VariantData {
|
|||
let fields = fl
|
||||
.fields()
|
||||
.map(|fd| StructField {
|
||||
name: fd
|
||||
.name()
|
||||
.map(|n| n.text())
|
||||
.unwrap_or_else(|| SmolStr::new("[error]")),
|
||||
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
|
||||
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
|
||||
})
|
||||
.collect();
|
||||
|
@ -161,10 +156,10 @@ impl VariantData {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_field_type_ref(&self, field_name: &str) -> Option<&TypeRef> {
|
||||
pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
|
||||
self.fields()
|
||||
.iter()
|
||||
.find(|f| f.name == field_name)
|
||||
.find(|f| f.name == *field_name)
|
||||
.map(|f| &f.type_ref)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::{
|
||||
SmolStr,
|
||||
SyntaxNode,
|
||||
};
|
||||
use ra_syntax::SyntaxNode;
|
||||
use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable};
|
||||
|
||||
use crate::{
|
||||
DefLoc, DefId,
|
||||
DefLoc, DefId, Name,
|
||||
SourceFileItems, SourceItemId,
|
||||
query_definitions,
|
||||
FnScopes,
|
||||
|
@ -47,7 +44,7 @@ pub trait HirDatabase: SyntaxDatabase
|
|||
use fn query_definitions::type_for_def;
|
||||
}
|
||||
|
||||
fn type_for_field(def_id: DefId, field: SmolStr) -> Cancelable<Ty> {
|
||||
fn type_for_field(def_id: DefId, field: Name) -> Cancelable<Ty> {
|
||||
type TypeForFieldQuery;
|
||||
use fn query_definitions::type_for_field;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,18 @@ impl fmt::Debug for Name {
|
|||
}
|
||||
|
||||
impl Name {
|
||||
fn new(text: SmolStr) -> Name {
|
||||
Name { text }
|
||||
}
|
||||
|
||||
pub(crate) fn missing() -> Name {
|
||||
Name::new("[missing name]".into())
|
||||
}
|
||||
|
||||
pub(crate) fn tuple_field_name(idx: usize) -> Name {
|
||||
Name::new(idx.to_string().into())
|
||||
}
|
||||
|
||||
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
|
||||
let name = match self.text.as_str() {
|
||||
"isize" => KnownName::Isize,
|
||||
|
@ -43,10 +55,6 @@ impl Name {
|
|||
};
|
||||
Some(name)
|
||||
}
|
||||
|
||||
fn new(text: SmolStr) -> Name {
|
||||
Name { text }
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait AsName {
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::{
|
|||
|
||||
use rustc_hash::FxHashMap;
|
||||
use ra_syntax::{
|
||||
AstNode, SyntaxNode, SmolStr,
|
||||
AstNode, SyntaxNode,
|
||||
ast::{self, NameOwner, ModuleItemOwner}
|
||||
};
|
||||
use ra_db::{SourceRootId, FileId, Cancelable,};
|
||||
|
@ -39,11 +39,7 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T
|
|||
ty::type_for_def(db, def_id)
|
||||
}
|
||||
|
||||
pub(super) fn type_for_field(
|
||||
db: &impl HirDatabase,
|
||||
def_id: DefId,
|
||||
field: SmolStr,
|
||||
) -> Cancelable<Ty> {
|
||||
pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Cancelable<Ty> {
|
||||
ty::type_for_field(db, def_id, field)
|
||||
}
|
||||
|
||||
|
|
|
@ -10,13 +10,12 @@ use rustc_hash::{FxHashMap};
|
|||
|
||||
use ra_db::{LocalSyntaxPtr, Cancelable};
|
||||
use ra_syntax::{
|
||||
SmolStr,
|
||||
ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp},
|
||||
SyntaxNodeRef
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Def, DefId, FnScopes, Module, Function, Struct, Enum, Path,
|
||||
Def, DefId, FnScopes, Module, Function, Struct, Enum, Path, Name, AsName,
|
||||
db::HirDatabase,
|
||||
adt::VariantData,
|
||||
type_ref::{TypeRef, Mutability},
|
||||
|
@ -45,7 +44,7 @@ pub enum Ty {
|
|||
/// The DefId of the struct/enum.
|
||||
def_id: DefId,
|
||||
/// The name, for displaying.
|
||||
name: SmolStr,
|
||||
name: Name,
|
||||
// later we'll need generic substitutions here
|
||||
},
|
||||
|
||||
|
@ -276,18 +275,14 @@ pub fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
|
|||
pub fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
|
||||
Ok(Ty::Adt {
|
||||
def_id: s.def_id(),
|
||||
name: s
|
||||
.name(db)?
|
||||
.unwrap_or_else(|| SmolStr::new("[unnamed struct]")),
|
||||
name: s.name(db)?.unwrap_or_else(Name::missing),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> {
|
||||
Ok(Ty::Adt {
|
||||
def_id: s.def_id(),
|
||||
name: s
|
||||
.name(db)?
|
||||
.unwrap_or_else(|| SmolStr::new("[unnamed enum]")),
|
||||
name: s.name(db)?.unwrap_or_else(Name::missing),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -308,11 +303,7 @@ pub fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Ty> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn type_for_field(
|
||||
db: &impl HirDatabase,
|
||||
def_id: DefId,
|
||||
field: SmolStr,
|
||||
) -> Cancelable<Ty> {
|
||||
pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Cancelable<Ty> {
|
||||
let def = def_id.resolve(db)?;
|
||||
let variant_data = match def {
|
||||
Def::Struct(s) => {
|
||||
|
@ -559,14 +550,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
ast::Expr::FieldExpr(e) => {
|
||||
let receiver_ty = self.infer_expr_opt(e.expr())?;
|
||||
if let Some(nr) = e.name_ref() {
|
||||
let text = nr.text();
|
||||
match receiver_ty {
|
||||
Ty::Tuple(fields) => {
|
||||
let i = text.parse::<usize>().ok();
|
||||
let i = nr.text().parse::<usize>().ok();
|
||||
i.and_then(|i| fields.get(i).cloned())
|
||||
.unwrap_or(Ty::Unknown)
|
||||
}
|
||||
Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, text)?,
|
||||
Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, nr.as_name())?,
|
||||
_ => Ty::Unknown,
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue