Skip to content

Commit

Permalink
Merge pull request #18758 from mgsloan/scip-unique-symbols
Browse files Browse the repository at this point in the history
Improve SCIP symbols
  • Loading branch information
Veykril authored Dec 31, 2024
2 parents 6ccbb2d + 5f7425c commit add0963
Show file tree
Hide file tree
Showing 8 changed files with 618 additions and 323 deletions.
45 changes: 18 additions & 27 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ impl HirDisplay for ProjectionTy {

let trait_ref = self.trait_ref(f.db);
write!(f, "<")?;
fmt_trait_ref(f, &trait_ref, true)?;
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, " as ")?;
trait_ref.hir_fmt(f)?;
write!(
f,
">::{}",
Expand Down Expand Up @@ -1775,32 +1777,14 @@ fn write_bounds_like_dyn_trait(
Ok(())
}

fn fmt_trait_ref(
f: &mut HirFormatter<'_>,
tr: &TraitRef,
use_as: bool,
) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{TYPE_HINT_TRUNCATION}");
}

tr.self_type_parameter(Interner).hir_fmt(f)?;
if use_as {
write!(f, " as ")?;
} else {
write!(f, ": ")?;
}
let trait_ = tr.hir_trait_id();
f.start_location_link(trait_.into());
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
f.end_location_link();
let substs = tr.substitution.as_slice(Interner);
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
}

impl HirDisplay for TraitRef {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
fmt_trait_ref(f, self, false)
let trait_ = self.hir_trait_id();
f.start_location_link(trait_.into());
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
f.end_location_link();
let substs = self.substitution.as_slice(Interner);
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
}
}

Expand All @@ -1811,10 +1795,17 @@ impl HirDisplay for WhereClause {
}

match self {
WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
WhereClause::Implemented(trait_ref) => {
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, ": ")?;
trait_ref.hir_fmt(f)?;
}
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
write!(f, "<")?;
fmt_trait_ref(f, &projection_ty.trait_ref(f.db), true)?;
let trait_ref = &projection_ty.trait_ref(f.db);
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, " as ")?;
trait_ref.hir_fmt(f)?;
write!(f, ">::",)?;
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
f.start_location_link(type_alias.into());
Expand Down
8 changes: 7 additions & 1 deletion crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use itertools::Itertools;
use crate::{
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl,
Field, Function, GenericParam, HasCrate, HasVisibility, Impl, LifetimeParam, Macro, Module,
SelfParam, Static, Struct, Trait, TraitAlias, TupleField, TyBuilder, Type, TypeAlias,
SelfParam, Static, Struct, Trait, TraitAlias, TraitRef, TupleField, TyBuilder, Type, TypeAlias,
TypeOrConstParam, TypeParam, Union, Variant,
};

Expand Down Expand Up @@ -743,6 +743,12 @@ impl HirDisplay for Static {
}
}

impl HirDisplay for TraitRef {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
self.trait_ref.hir_fmt(f)
}
}

impl HirDisplay for Trait {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
write_trait_header(self, f)?;
Expand Down
54 changes: 49 additions & 5 deletions crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use either::Either;
use hir::{
Adt, AsAssocItem, AsExternAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType,
Const, Crate, DefWithBody, DeriveHelper, DocLinkDef, ExternAssocItem, ExternCrateDecl, Field,
Function, GenericParam, GenericSubstitution, HasVisibility, HirDisplay, Impl, InlineAsmOperand,
Label, Local, Macro, Module, ModuleDef, Name, PathResolution, Semantics, Static,
StaticLifetime, Struct, ToolModule, Trait, TraitAlias, TupleField, TypeAlias, Variant,
VariantDef, Visibility,
Function, GenericDef, GenericParam, GenericSubstitution, HasContainer, HasVisibility,
HirDisplay, Impl, InlineAsmOperand, ItemContainer, Label, Local, Macro, Module, ModuleDef,
Name, PathResolution, Semantics, Static, StaticLifetime, Struct, ToolModule, Trait, TraitAlias,
TupleField, TypeAlias, Variant, VariantDef, Visibility,
};
use span::Edition;
use stdx::{format_to, impl_from};
Expand Down Expand Up @@ -97,9 +97,39 @@ impl Definition {
}

pub fn enclosing_definition(&self, db: &RootDatabase) -> Option<Definition> {
fn container_to_definition(container: ItemContainer) -> Option<Definition> {
match container {
ItemContainer::Trait(it) => Some(it.into()),
ItemContainer::Impl(it) => Some(it.into()),
ItemContainer::Module(it) => Some(it.into()),
ItemContainer::ExternBlock() | ItemContainer::Crate(_) => None,
}
}
match self {
Definition::Macro(it) => Some(it.module(db).into()),
Definition::Module(it) => it.parent(db).map(Definition::Module),
Definition::Field(it) => Some(it.parent_def(db).into()),
Definition::Function(it) => container_to_definition(it.container(db)),
Definition::Adt(it) => Some(it.module(db).into()),
Definition::Const(it) => container_to_definition(it.container(db)),
Definition::Static(it) => container_to_definition(it.container(db)),
Definition::Trait(it) => container_to_definition(it.container(db)),
Definition::TraitAlias(it) => container_to_definition(it.container(db)),
Definition::TypeAlias(it) => container_to_definition(it.container(db)),
Definition::Variant(it) => Some(Adt::Enum(it.parent_enum(db)).into()),
Definition::SelfType(it) => Some(it.module(db).into()),
Definition::Local(it) => it.parent(db).try_into().ok(),
_ => None,
Definition::GenericParam(it) => Some(it.parent().into()),
Definition::Label(it) => it.parent(db).try_into().ok(),
Definition::ExternCrateDecl(it) => container_to_definition(it.container(db)),
Definition::DeriveHelper(it) => Some(it.derive().module(db).into()),
Definition::InlineAsmOperand(it) => it.parent(db).try_into().ok(),
Definition::BuiltinAttr(_)
| Definition::BuiltinType(_)
| Definition::BuiltinLifetime(_)
| Definition::TupleField(_)
| Definition::ToolModule(_)
| Definition::InlineAsmRegOrRegClass(_) => None,
}
}

Expand Down Expand Up @@ -932,3 +962,17 @@ impl TryFrom<DefWithBody> for Definition {
}
}
}

impl From<GenericDef> for Definition {
fn from(def: GenericDef) -> Self {
match def {
GenericDef::Function(it) => it.into(),
GenericDef::Adt(it) => it.into(),
GenericDef::Trait(it) => it.into(),
GenericDef::TraitAlias(it) => it.into(),
GenericDef::TypeAlias(it) => it.into(),
GenericDef::Impl(it) => it.into(),
GenericDef::Const(it) => it.into(),
}
}
}
4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub use crate::{
join_lines::JoinLinesConfig,
markup::Markup,
moniker::{
MonikerDescriptorKind, MonikerKind, MonikerResult, PackageInformation,
SymbolInformationKind,
Moniker, MonikerDescriptorKind, MonikerIdentifier, MonikerKind, MonikerResult,
PackageInformation, SymbolInformationKind,
},
move_item::Direction,
navigation_target::{NavigationTarget, TryToNav, UpmappingResult},
Expand Down
Loading

0 comments on commit add0963

Please sign in to comment.