7206: Use hir::GenericParam in ide_db::Definition instead of relisting all 3 r=Veykril a=Veykril

Basically just this:
```diff
 pub enum Definition {
     Macro(MacroDef),
     Field(Field),
     ModuleDef(ModuleDef),
     SelfType(Impl),
     Local(Local),
-    TypeParam(TypeParam),
-    LifetimeParam(LifetimeParam),
-    ConstParam(ConstParam),
+    GenericParam(GenericParam),
     Label(Label),
 }
```

7231: Cleaner API r=matklad a=matklad

bors r+
🤖

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-01-10 11:45:59 +00:00 committed by GitHub
commit dcbb77cb6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 75 additions and 51 deletions

View file

@ -1263,6 +1263,24 @@ pub enum GenericParam {
} }
impl_from!(TypeParam, LifetimeParam, ConstParam for GenericParam); impl_from!(TypeParam, LifetimeParam, ConstParam for GenericParam);
impl GenericParam {
pub fn module(self, db: &dyn HirDatabase) -> Module {
match self {
GenericParam::TypeParam(it) => it.module(db),
GenericParam::LifetimeParam(it) => it.module(db),
GenericParam::ConstParam(it) => it.module(db),
}
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
match self {
GenericParam::TypeParam(it) => it.name(db),
GenericParam::LifetimeParam(it) => it.name(db),
GenericParam::ConstParam(it) => it.name(db),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TypeParam { pub struct TypeParam {
pub(crate) id: TypeParamId, pub(crate) id: TypeParamId,

View file

@ -215,10 +215,8 @@ impl TryToNav for Definition {
Definition::ModuleDef(it) => it.try_to_nav(db), Definition::ModuleDef(it) => it.try_to_nav(db),
Definition::SelfType(it) => it.try_to_nav(db), Definition::SelfType(it) => it.try_to_nav(db),
Definition::Local(it) => Some(it.to_nav(db)), Definition::Local(it) => Some(it.to_nav(db)),
Definition::TypeParam(it) => it.try_to_nav(db), Definition::GenericParam(it) => it.try_to_nav(db),
Definition::LifetimeParam(it) => it.try_to_nav(db),
Definition::Label(it) => Some(it.to_nav(db)), Definition::Label(it) => Some(it.to_nav(db)),
Definition::ConstParam(it) => it.try_to_nav(db),
} }
} }
} }
@ -389,6 +387,16 @@ impl TryToNav for hir::AssocItem {
} }
} }
impl TryToNav for hir::GenericParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self {
hir::GenericParam::TypeParam(it) => it.try_to_nav(db),
hir::GenericParam::ConstParam(it) => it.try_to_nav(db),
hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db),
}
}
}
impl ToNav for hir::Local { impl ToNav for hir::Local {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db); let src = self.source(db);

View file

@ -216,9 +216,7 @@ fn rewrite_intra_doc_link(
Definition::Field(it) => it.resolve_doc_path(db, link, ns), Definition::Field(it) => it.resolve_doc_path(db, link, ns),
Definition::SelfType(_) Definition::SelfType(_)
| Definition::Local(_) | Definition::Local(_)
| Definition::TypeParam(_) | Definition::GenericParam(_)
| Definition::ConstParam(_)
| Definition::LifetimeParam(_)
| Definition::Label(_) => return None, | Definition::Label(_) => return None,
}?; }?;
let krate = resolved.module(db)?.krate(); let krate = resolved.module(db)?.krate();

View file

@ -111,9 +111,7 @@ fn def_for_doc_comment(
Definition::Field(it) => it.resolve_doc_path(db, link, ns), Definition::Field(it) => it.resolve_doc_path(db, link, ns),
Definition::SelfType(_) Definition::SelfType(_)
| Definition::Local(_) | Definition::Local(_)
| Definition::TypeParam(_) | Definition::GenericParam(_)
| Definition::LifetimeParam(_)
| Definition::ConstParam(_)
| Definition::Label(_) => return None, | Definition::Label(_) => return None,
} }
} }

View file

@ -1,6 +1,6 @@
use hir::{ use hir::{
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasAttrs, HasSource, HirDisplay, Module, Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
ModuleDef, ModuleSource, Semantics, HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
}; };
use ide_db::base_db::SourceDatabase; use ide_db::base_db::SourceDatabase;
use ide_db::{ use ide_db::{
@ -220,12 +220,12 @@ fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
} }
}; };
if let Definition::TypeParam(it) = def { if let Definition::GenericParam(GenericParam::TypeParam(it)) = def {
it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into())); it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
} else { } else {
let ty = match def { let ty = match def {
Definition::Local(it) => it.ty(db), Definition::Local(it) => it.ty(db),
Definition::ConstParam(it) => it.ty(db), Definition::GenericParam(GenericParam::ConstParam(it)) => it.ty(db),
_ => return None, _ => return None,
}; };
@ -357,9 +357,11 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
}) })
} }
Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
Definition::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), Definition::GenericParam(it) => match it {
Definition::TypeParam(type_param) => Some(Markup::fenced_block(&type_param.display(db))), GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))),
Definition::ConstParam(it) => from_def_source(db, it, None), GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
GenericParam::ConstParam(it) => from_def_source(db, it, None),
},
}; };
fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>

View file

@ -130,7 +130,10 @@ pub(crate) fn find_all_refs(
kind = ReferenceKind::FieldShorthandForLocal; kind = ReferenceKind::FieldShorthandForLocal;
} }
} }
} else if matches!(def, Definition::LifetimeParam(_) | Definition::Label(_)) { } else if matches!(
def,
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
) {
kind = ReferenceKind::Lifetime; kind = ReferenceKind::Lifetime;
}; };

View file

@ -328,8 +328,11 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
} }
}, },
Definition::SelfType(_) => HlTag::Symbol(SymbolKind::Impl), Definition::SelfType(_) => HlTag::Symbol(SymbolKind::Impl),
Definition::TypeParam(_) => HlTag::Symbol(SymbolKind::TypeParam), Definition::GenericParam(it) => match it {
Definition::ConstParam(_) => HlTag::Symbol(SymbolKind::ConstParam), hir::GenericParam::TypeParam(_) => HlTag::Symbol(SymbolKind::TypeParam),
hir::GenericParam::ConstParam(_) => HlTag::Symbol(SymbolKind::ConstParam),
hir::GenericParam::LifetimeParam(_) => HlTag::Symbol(SymbolKind::LifetimeParam),
},
Definition::Local(local) => { Definition::Local(local) => {
let tag = if local.is_param(db) { let tag = if local.is_param(db) {
HlTag::Symbol(SymbolKind::ValueParam) HlTag::Symbol(SymbolKind::ValueParam)
@ -345,7 +348,6 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
} }
return h; return h;
} }
Definition::LifetimeParam(_) => HlTag::Symbol(SymbolKind::LifetimeParam),
Definition::Label(_) => HlTag::Symbol(SymbolKind::Label), Definition::Label(_) => HlTag::Symbol(SymbolKind::Label),
} }
.into() .into()

View file

@ -51,18 +51,20 @@ impl Node {
} }
} }
let (start, len) = let overlapping =
equal_range_by(&self.nested, |n| ordering(n.hl_range.range, hl_range.range)); equal_range_by(&self.nested, |n| ordering(n.hl_range.range, hl_range.range));
if len == 1 && self.nested[start].hl_range.range.contains_range(hl_range.range) { if overlapping.len() == 1
return self.nested[start].add(hl_range); && self.nested[overlapping.start].hl_range.range.contains_range(hl_range.range)
{
return self.nested[overlapping.start].add(hl_range);
} }
let nested = self let nested = self
.nested .nested
.splice(start..start + len, iter::once(Node::new(hl_range))) .splice(overlapping.clone(), iter::once(Node::new(hl_range)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
self.nested[start].nested = nested; self.nested[overlapping.start].nested = nested;
} }
fn flatten(&self, acc: &mut Vec<HlRange>) { fn flatten(&self, acc: &mut Vec<HlRange>) {

View file

@ -33,8 +33,7 @@ impl Injector {
&self.buf &self.buf
} }
pub(super) fn map_range_up(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { pub(super) fn map_range_up(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
let (start, len) = equal_range_by(&self.ranges, |&(r, _)| ordering(r, range)); equal_range_by(&self.ranges, |&(r, _)| ordering(r, range)).filter_map(move |i| {
(start..start + len).filter_map(move |i| {
let (target_range, delta) = self.ranges[i]; let (target_range, delta) = self.ranges[i];
let intersection = target_range.intersect(range).unwrap(); let intersection = target_range.intersect(range).unwrap();
Some(intersection + delta?) Some(intersection + delta?)

View file

@ -6,8 +6,8 @@
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
use hir::{ use hir::{
db::HirDatabase, ConstParam, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, db::HirDatabase, Crate, Field, GenericParam, HasVisibility, Impl, Label, Local, MacroDef,
MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, Module, ModuleDef, Name, PathResolution, Semantics, Visibility,
}; };
use syntax::{ use syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
@ -24,9 +24,7 @@ pub enum Definition {
ModuleDef(ModuleDef), ModuleDef(ModuleDef),
SelfType(Impl), SelfType(Impl),
Local(Local), Local(Local),
TypeParam(TypeParam), GenericParam(GenericParam),
LifetimeParam(LifetimeParam),
ConstParam(ConstParam),
Label(Label), Label(Label),
} }
@ -38,9 +36,7 @@ impl Definition {
Definition::ModuleDef(it) => it.module(db), Definition::ModuleDef(it) => it.module(db),
Definition::SelfType(it) => Some(it.module(db)), Definition::SelfType(it) => Some(it.module(db)),
Definition::Local(it) => Some(it.module(db)), Definition::Local(it) => Some(it.module(db)),
Definition::TypeParam(it) => Some(it.module(db)), Definition::GenericParam(it) => Some(it.module(db)),
Definition::LifetimeParam(it) => Some(it.module(db)),
Definition::ConstParam(it) => Some(it.module(db)),
Definition::Label(it) => Some(it.module(db)), Definition::Label(it) => Some(it.module(db)),
} }
} }
@ -52,9 +48,7 @@ impl Definition {
Definition::ModuleDef(def) => def.definition_visibility(db), Definition::ModuleDef(def) => def.definition_visibility(db),
Definition::SelfType(_) => None, Definition::SelfType(_) => None,
Definition::Local(_) => None, Definition::Local(_) => None,
Definition::TypeParam(_) => None, Definition::GenericParam(_) => None,
Definition::LifetimeParam(_) => None,
Definition::ConstParam(_) => None,
Definition::Label(_) => None, Definition::Label(_) => None,
} }
} }
@ -80,9 +74,7 @@ impl Definition {
}, },
Definition::SelfType(_) => return None, Definition::SelfType(_) => return None,
Definition::Local(it) => it.name(db)?, Definition::Local(it) => it.name(db)?,
Definition::TypeParam(it) => it.name(db), Definition::GenericParam(it) => it.name(db),
Definition::LifetimeParam(it) => it.name(db),
Definition::ConstParam(it) => it.name(db),
Definition::Label(it) => it.name(db), Definition::Label(it) => it.name(db),
}; };
Some(name) Some(name)
@ -235,11 +227,11 @@ impl NameClass {
}, },
ast::TypeParam(it) => { ast::TypeParam(it) => {
let def = sema.to_def(&it)?; let def = sema.to_def(&it)?;
Some(NameClass::Definition(Definition::TypeParam(def))) Some(NameClass::Definition(Definition::GenericParam(def.into())))
}, },
ast::ConstParam(it) => { ast::ConstParam(it) => {
let def = sema.to_def(&it)?; let def = sema.to_def(&it)?;
Some(NameClass::Definition(Definition::ConstParam(def))) Some(NameClass::Definition(Definition::GenericParam(def.into())))
}, },
_ => None, _ => None,
} }
@ -257,7 +249,7 @@ impl NameClass {
match parent { match parent {
ast::LifetimeParam(it) => { ast::LifetimeParam(it) => {
let def = sema.to_def(&it)?; let def = sema.to_def(&it)?;
Some(NameClass::Definition(Definition::LifetimeParam(def))) Some(NameClass::Definition(Definition::GenericParam(def.into())))
}, },
ast::Label(it) => { ast::Label(it) => {
let def = sema.to_def(&it)?; let def = sema.to_def(&it)?;
@ -393,7 +385,8 @@ impl NameRefClass {
| SyntaxKind::WHERE_PRED | SyntaxKind::WHERE_PRED
| SyntaxKind::REF_TYPE => sema | SyntaxKind::REF_TYPE => sema
.resolve_lifetime_param(lifetime) .resolve_lifetime_param(lifetime)
.map(Definition::LifetimeParam) .map(GenericParam::LifetimeParam)
.map(Definition::GenericParam)
.map(NameRefClass::Definition), .map(NameRefClass::Definition),
// lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check // lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check
// if our lifetime is in a LifetimeParam without being the constrained lifetime // if our lifetime is in a LifetimeParam without being the constrained lifetime
@ -401,7 +394,8 @@ impl NameRefClass {
!= Some(lifetime) => != Some(lifetime) =>
{ {
sema.resolve_lifetime_param(lifetime) sema.resolve_lifetime_param(lifetime)
.map(Definition::LifetimeParam) .map(GenericParam::LifetimeParam)
.map(Definition::GenericParam)
.map(NameRefClass::Definition) .map(NameRefClass::Definition)
} }
_ => None, _ => None,
@ -422,10 +416,10 @@ impl From<PathResolution> for Definition {
Definition::ModuleDef(def) Definition::ModuleDef(def)
} }
PathResolution::Local(local) => Definition::Local(local), PathResolution::Local(local) => Definition::Local(local),
PathResolution::TypeParam(par) => Definition::TypeParam(par), PathResolution::TypeParam(par) => Definition::GenericParam(par.into()),
PathResolution::Macro(def) => Definition::Macro(def), PathResolution::Macro(def) => Definition::Macro(def),
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
PathResolution::ConstParam(par) => Definition::ConstParam(par), PathResolution::ConstParam(par) => Definition::GenericParam(par.into()),
} }
} }
} }

View file

@ -136,7 +136,7 @@ impl Definition {
return SearchScope::new(res); return SearchScope::new(res);
} }
if let Definition::LifetimeParam(param) = self { if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self {
let range = match param.parent(db) { let range = match param.parent(db) {
hir::GenericDef::Function(it) => { hir::GenericDef::Function(it) => {
it.source(db).and_then(|src| Some(src.value.syntax().text_range())) it.source(db).and_then(|src| Some(src.value.syntax().text_range()))

View file

@ -152,13 +152,13 @@ where
left left
} }
pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> (usize, usize) pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
where where
F: FnMut(&T) -> Ordering, F: FnMut(&T) -> Ordering,
{ {
let start = partition_point(slice, |it| key(it) == Ordering::Less); let start = partition_point(slice, |it| key(it) == Ordering::Less);
let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal); let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal);
(start, len) start..start + len
} }
pub struct JodChild(pub process::Child); pub struct JodChild(pub process::Child);