mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Functions use new id scheme
This commit is contained in:
parent
f1959bbae0
commit
ec7ed054e0
13 changed files with 297 additions and 201 deletions
|
@ -5,9 +5,9 @@
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::ast::{TypeParamList, AstNode, NameOwner};
|
||||
use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner};
|
||||
|
||||
use crate::{db::HirDatabase, DefId, Name, AsName};
|
||||
use crate::{db::HirDatabase, DefId, Name, AsName, Function};
|
||||
|
||||
/// Data about a generic parameter (to a function, struct, impl, ...).
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
|
@ -22,26 +22,62 @@ pub struct GenericParams {
|
|||
pub(crate) params: Vec<GenericParam>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum GenericDef {
|
||||
Function(Function),
|
||||
Def(DefId),
|
||||
}
|
||||
|
||||
impl From<Function> for GenericDef {
|
||||
fn from(func: Function) -> GenericDef {
|
||||
GenericDef::Function(func)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DefId> for GenericDef {
|
||||
fn from(def_id: DefId) -> GenericDef {
|
||||
GenericDef::Def(def_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl GenericParams {
|
||||
pub(crate) fn generic_params_query(db: &impl HirDatabase, def_id: DefId) -> Arc<GenericParams> {
|
||||
let (_file_id, node) = def_id.source(db);
|
||||
pub(crate) fn generic_params_query(
|
||||
db: &impl HirDatabase,
|
||||
def: GenericDef,
|
||||
) -> Arc<GenericParams> {
|
||||
let mut generics = GenericParams::default();
|
||||
if let Some(type_param_list) = node.children().find_map(TypeParamList::cast) {
|
||||
for (idx, type_param) in type_param_list.type_params().enumerate() {
|
||||
let name = type_param
|
||||
.name()
|
||||
.map(AsName::as_name)
|
||||
.unwrap_or_else(Name::missing);
|
||||
let param = GenericParam {
|
||||
idx: idx as u32,
|
||||
name,
|
||||
};
|
||||
generics.params.push(param);
|
||||
match def {
|
||||
GenericDef::Function(func) => {
|
||||
let (_, fn_def) = func.source(db);
|
||||
if let Some(type_param_list) = fn_def.type_param_list() {
|
||||
generics.fill(type_param_list)
|
||||
}
|
||||
}
|
||||
GenericDef::Def(def_id) => {
|
||||
let (_file_id, node) = def_id.source(db);
|
||||
if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) {
|
||||
generics.fill(type_param_list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Arc::new(generics)
|
||||
}
|
||||
|
||||
fn fill(&mut self, params: &ast::TypeParamList) {
|
||||
for (idx, type_param) in params.type_params().enumerate() {
|
||||
let name = type_param
|
||||
.name()
|
||||
.map(AsName::as_name)
|
||||
.unwrap_or_else(Name::missing);
|
||||
let param = GenericParam {
|
||||
idx: idx as u32,
|
||||
name,
|
||||
};
|
||||
self.params.push(param);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn find_by_name(&self, name: &Name) -> Option<&GenericParam> {
|
||||
self.params.iter().find(|p| &p.name == name)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue