Skip to content

Commit

Permalink
Merge pull request #18902 from Veykril/push-znlsxykqrkoo
Browse files Browse the repository at this point in the history
internal: Add Definition::Crate
  • Loading branch information
Veykril authored Jan 10, 2025
2 parents 89b3767 + e6270bd commit 6f04f37
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 133 deletions.
10 changes: 10 additions & 0 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ impl HasAttrs for AssocItem {
}
}

impl HasAttrs for crate::Crate {
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
let def = AttrDefId::ModuleId(self.root_module().id);
AttrsWithOwner::new(db.upcast(), def)
}
fn attr_id(self) -> AttrDefId {
AttrDefId::ModuleId(self.root_module().id)
}
}

/// Resolves the item `link` points to in the scope of `def`.
pub fn resolve_doc_path_on(
db: &dyn HirDatabase,
Expand Down
33 changes: 23 additions & 10 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use hir_ty::{
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, TraitRef, TupleField, TyBuilder, Type, TypeAlias,
TypeOrConstParam, TypeParam, Union, Variant,
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Crate, Enum,
ExternCrateDecl, Field, Function, GenericParam, HasCrate, HasVisibility, Impl, LifetimeParam,
Macro, Module, SelfParam, Static, Struct, Trait, TraitAlias, TraitRef, TupleField, TyBuilder,
Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
};

impl HirDisplay for Function {
Expand Down Expand Up @@ -846,14 +846,27 @@ impl HirDisplay for TypeAlias {

impl HirDisplay for Module {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
// FIXME: Module doesn't have visibility saved in data.
match self.parent(f.db) {
Some(m) => write_visibility(m.id, self.visibility(f.db), f)?,
None => {
return match self.krate(f.db).display_name(f.db) {
Some(name) => write!(f, "extern crate {name}"),
None => f.write_str("extern crate {unknown}"),
}
}
}
match self.name(f.db) {
Some(name) => write!(f, "mod {}", name.display(f.db.upcast(), f.edition())),
None if self.is_crate_root() => match self.krate(f.db).display_name(f.db) {
Some(name) => write!(f, "extern crate {name}"),
None => f.write_str("extern crate {unknown}"),
},
None => f.write_str("mod {unnamed}"),
None => f.write_str("mod {unknown}"),
}
}
}

impl HirDisplay for Crate {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
match self.display_name(f.db) {
Some(name) => write!(f, "extern crate {name}"),
None => f.write_str("extern crate {unknown}"),
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Definition {
Field(Field),
TupleField(TupleField),
Module(Module),
Crate(Crate),
Function(Function),
Adt(Adt),
Variant(Variant),
Expand Down Expand Up @@ -62,14 +63,19 @@ impl Definition {
pub fn krate(&self, db: &RootDatabase) -> Option<Crate> {
Some(match self {
Definition::Module(m) => m.krate(),
&Definition::Crate(it) => it,
_ => self.module(db)?.krate(),
})
}

/// Returns the module this definition resides in.
///
/// As such, for modules themselves this will return the parent module.
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
let module = match self {
Definition::Macro(it) => it.module(db),
Definition::Module(it) => it.parent(db)?,
Definition::Crate(_) => return None,
Definition::Field(it) => it.parent_def(db).module(db),
Definition::Function(it) => it.module(db),
Definition::Adt(it) => it.module(db),
Expand Down Expand Up @@ -108,6 +114,7 @@ impl Definition {
match self {
Definition::Macro(it) => Some(it.module(db).into()),
Definition::Module(it) => it.parent(db).map(Definition::Module),
Definition::Crate(_) => None,
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()),
Expand Down Expand Up @@ -137,6 +144,7 @@ impl Definition {
let vis = match self {
Definition::Field(sf) => sf.visibility(db),
Definition::Module(it) => it.visibility(db),
Definition::Crate(_) => return None,
Definition::Function(it) => it.visibility(db),
Definition::Adt(it) => it.visibility(db),
Definition::Const(it) => it.visibility(db),
Expand All @@ -146,8 +154,8 @@ impl Definition {
Definition::TypeAlias(it) => it.visibility(db),
Definition::Variant(it) => it.visibility(db),
Definition::ExternCrateDecl(it) => it.visibility(db),
Definition::Macro(it) => it.visibility(db),
Definition::BuiltinType(_) | Definition::TupleField(_) => Visibility::Public,
Definition::Macro(_) => return None,
Definition::BuiltinAttr(_)
| Definition::BuiltinLifetime(_)
| Definition::ToolModule(_)
Expand All @@ -167,6 +175,9 @@ impl Definition {
Definition::Macro(it) => it.name(db),
Definition::Field(it) => it.name(db),
Definition::Module(it) => it.name(db)?,
Definition::Crate(it) => {
Name::new_symbol_root(it.display_name(db)?.crate_name().symbol().clone())
}
Definition::Function(it) => it.name(db),
Definition::Adt(it) => it.name(db),
Definition::Variant(it) => it.name(db),
Expand Down Expand Up @@ -202,6 +213,7 @@ impl Definition {
Definition::Macro(it) => it.docs(db),
Definition::Field(it) => it.docs(db),
Definition::Module(it) => it.docs(db),
Definition::Crate(it) => it.docs(db),
Definition::Function(it) => it.docs(db),
Definition::Adt(it) => it.docs(db),
Definition::Variant(it) => it.docs(db),
Expand Down Expand Up @@ -282,6 +294,7 @@ impl Definition {
Definition::Field(it) => it.display(db, edition).to_string(),
Definition::TupleField(it) => it.display(db, edition).to_string(),
Definition::Module(it) => it.display(db, edition).to_string(),
Definition::Crate(it) => it.display(db, edition).to_string(),
Definition::Function(it) => it.display(db, edition).to_string(),
Definition::Adt(it) => it.display(db, edition).to_string(),
Definition::Variant(it) => it.display(db, edition).to_string(),
Expand Down Expand Up @@ -415,7 +428,7 @@ impl IdentClass {
}
IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand { decl, krate }) => {
res.push((Definition::ExternCrateDecl(decl), None));
res.push((Definition::Module(krate.root_module()), None));
res.push((Definition::Crate(krate), None));
}
IdentClass::Operator(
OperatorClass::Await(func)
Expand Down Expand Up @@ -456,7 +469,7 @@ impl IdentClass {
}
IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand { decl, krate }) => {
res.push(Definition::ExternCrateDecl(decl));
res.push(Definition::Module(krate.root_module()));
res.push(Definition::Crate(krate));
}
IdentClass::Operator(_) => (),
}
Expand Down Expand Up @@ -800,7 +813,7 @@ impl NameRefClass {
let extern_crate = sema.to_def(&extern_crate_ast)?;
let krate = extern_crate.resolved_crate(sema.db)?;
Some(if extern_crate_ast.rename().is_some() {
NameRefClass::Definition(Definition::Module(krate.root_module()), None)
NameRefClass::Definition(Definition::Crate(krate), None)
} else {
NameRefClass::ExternCrateShorthand { krate, decl: extern_crate }
})
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ macro_rules! impl_has_docs {

impl_has_docs![
Variant, Field, Static, Const, Trait, TraitAlias, TypeAlias, Macro, Function, Adt, Module,
Impl,
Impl, Crate,
];

macro_rules! impl_has_docs_enum {
Expand Down
1 change: 1 addition & 0 deletions crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl Definition {
FieldSource::Pos(_) => None,
}
}
Definition::Crate(_) => None,
Definition::Module(module) => {
let src = module.declaration_source(sema.db)?;
let name = src.value.name()?;
Expand Down
2 changes: 2 additions & 0 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub(crate) fn resolve_doc_path_for_def(
) -> Option<Definition> {
match def {
Definition::Module(it) => it.resolve_doc_path(db, link, ns),
Definition::Crate(it) => it.resolve_doc_path(db, link, ns),
Definition::Function(it) => it.resolve_doc_path(db, link, ns),
Definition::Adt(it) => it.resolve_doc_path(db, link, ns),
Definition::Variant(it) => it.resolve_doc_path(db, link, ns),
Expand Down Expand Up @@ -594,6 +595,7 @@ fn filename_and_frag_for_def(
Adt::Enum(e) => format!("enum.{}.html", e.name(db).unescaped().display(db.upcast())),
Adt::Union(u) => format!("union.{}.html", u.name(db).unescaped().display(db.upcast())),
},
Definition::Crate(_) => String::from("index.html"),
Definition::Module(m) => match m.name(db) {
// `#[doc(keyword = "...")]` is internal used only by rust compiler
Some(name) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6112,7 +6112,7 @@ use foo::bar::{self$0};
```
```rust
mod bar
pub mod bar
```
---
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati
MacroKind::ProcMacro => Macro,
},
Definition::Field(..) | Definition::TupleField(..) => Field,
Definition::Module(..) => Module,
Definition::Module(..) | Definition::Crate(..) => Module,
Definition::Function(it) => {
if it.as_assoc_item(db).is_some() {
if it.has_self_param(db) {
Expand Down
7 changes: 7 additions & 0 deletions crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl TryToNav for Definition {
Definition::Local(it) => Some(it.to_nav(db)),
Definition::Label(it) => it.try_to_nav(db),
Definition::Module(it) => Some(it.to_nav(db)),
Definition::Crate(it) => Some(it.to_nav(db)),
Definition::Macro(it) => it.try_to_nav(db),
Definition::Field(it) => it.try_to_nav(db),
Definition::SelfType(it) => it.try_to_nav(db),
Expand Down Expand Up @@ -398,6 +399,12 @@ impl ToNav for hir::Module {
}
}

impl ToNav for hir::Crate {
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
self.root_module().to_nav(db)
}
}

impl TryToNav for hir::Impl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let InFile { file_id, value } = self.source(db)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/ide/src/syntax_highlighting/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ pub(super) fn highlight_def(
Definition::Field(_) | Definition::TupleField(_) => {
Highlight::new(HlTag::Symbol(SymbolKind::Field))
}
Definition::Crate(_) => {
Highlight::new(HlTag::Symbol(SymbolKind::Module)) | HlMod::CrateRoot
}
Definition::Module(module) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));
if module.is_crate_root() {
Expand Down
4 changes: 3 additions & 1 deletion crates/ide/src/syntax_highlighting/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::Stri

fn module_def_to_hl_tag(def: Definition) -> HlTag {
let symbol = match def {
Definition::Module(_) | Definition::ExternCrateDecl(_) => SymbolKind::Module,
Definition::Module(_) | Definition::Crate(_) | Definition::ExternCrateDecl(_) => {
SymbolKind::Module
}
Definition::Function(_) => SymbolKind::Function,
Definition::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
Definition::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">foo</span> <span class="brace">{</span>
<pre><code><span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration public">foo</span> <span class="brace">{</span>
<span class="parenthesis">(</span><span class="punctuation">$</span>foo<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="operator">=</span><span class="operator">&gt;</span> <span class="brace">{</span>
<span class="keyword">mod</span> y <span class="brace">{</span>
<span class="keyword">pub</span> <span class="keyword">struct</span> <span class="punctuation">$</span>foo<span class="semicolon">;</span>
<span class="brace">}</span>
<span class="brace">}</span><span class="semicolon">;</span>
<span class="brace">}</span>
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
<span class="macro">foo</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="struct declaration macro public">Foo</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro public">foo</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="struct declaration macro public">Foo</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="keyword">mod</span> <span class="module declaration">module</span> <span class="brace">{</span>
<span class="macro">foo</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="struct declaration macro public">Bar</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro public">foo</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="struct declaration macro public">Bar</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="keyword">fn</span> <span class="function declaration">func</span><span class="parenthesis">(</span><span class="punctuation">_</span><span class="colon">:</span> <span class="module">y</span><span class="operator">::</span><span class="struct public">Bar</span><span class="parenthesis">)</span> <span class="brace">{</span>
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span>
<span class="keyword">struct</span> <span class="struct declaration">Innerest</span><span class="angle">&lt;</span><span class="keyword">const</span> <span class="const_param const declaration">C</span><span class="colon">:</span> <span class="unresolved_reference">usize</span><span class="angle">&gt;</span> <span class="brace">{</span> <span class="field declaration">field</span><span class="colon">:</span> <span class="bracket">[</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span> <span class="brace">{</span><span class="const_param const">C</span><span class="brace">}</span><span class="bracket">]</span> <span class="brace">}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">id</span> <span class="brace">{</span>
<pre><code><span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration public">id</span> <span class="brace">{</span>
<span class="parenthesis">(</span><span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="colon">:</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="operator">&gt;</span> <span class="brace">{</span>
<span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span>
<span class="brace">}</span><span class="semicolon">;</span>
Expand All @@ -57,7 +57,7 @@
<span class="keyword const">const</span> <span class="brace">{</span>
<span class="keyword">const</span> <span class="punctuation">|</span><span class="punctuation">|</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>
<span class="macro">id</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span>
<span class="macro public">id</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span>
<span class="constant const macro">CONST_ITEM</span><span class="semicolon macro">;</span>
<span class="const_param const macro">CONST_PARAM</span><span class="semicolon macro">;</span>
<span class="keyword const macro">const</span> <span class="brace macro">{</span>
Expand All @@ -78,7 +78,7 @@
<span class="keyword const">const</span> <span class="keyword">fn</span> <span class="function associated const declaration static trait">assoc_const_fn</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
<span class="brace">}</span>

<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">unsafe_deref</span> <span class="brace">{</span>
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration public">unsafe_deref</span> <span class="brace">{</span>
<span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="operator">&gt;</span> <span class="brace">{</span>
<span class="punctuation">*</span><span class="parenthesis">(</span><span class="punctuation">&</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="keyword">as</span> <span class="punctuation">*</span><span class="keyword">const</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="parenthesis">)</span>
<span class="brace">}</span><span class="semicolon">;</span>
Expand Down
Loading

0 comments on commit 6f04f37

Please sign in to comment.