Skip to content

Commit 2d0db31

Browse files
bors[bot]jhgg
andauthored
Merge #10872
10872: ide_db: build symbol index from crate def map r=Veykril a=jhgg fixes #4842, #10764 Is this looking correct? 👀 - [x] build the symbol index based upon the CrateDefMap for the given crate in `crate_symbols` - [x] make it multi threaded again, and figure out how to cache each moduleid's symbol index in salsa. - [x] NavigationTarget for names in macros is wrong, need to figure out how to compute a text range in the original file id? - [x] cleanup some duped code - [x] collect macros from `ItemScope.declared_macros()` into symbol index. - [x] store declared macros in `ItemScope` so we can figure out where macros were defined for the index. - [x] do something about `SymbolIndex::for_files` - ideally it should use the new module symbol index stuff. - [x] delete `source_file_to_file_symbols` & co... - [x] figure out what to do about `library_symbols` - [x] maybe... speed up the new `library_symbols` - the new impl is probably much slower, and definitely much less parallel. **deciding to do nothing here, we can optimize later if necerssary.** - [x] fix failing test: `navigation_target::tests::test_nav_for_symbol` - notably the crate def map doesn't seem to find declarations inside function. - [x] now a bunch of other tests are failing around auto_import & qualify_path handlers. :( - [x] need to assoc items in traits and impls Co-authored-by: Jake Heinz <[email protected]>
2 parents e217632 + f4bf750 commit 2d0db31

File tree

14 files changed

+1045
-178
lines changed

14 files changed

+1045
-178
lines changed

crates/base_db/src/fixture.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::{
1919
pub const WORKSPACE: SourceRootId = SourceRootId(0);
2020

2121
pub trait WithFixture: Default + SourceDatabaseExt + 'static {
22-
fn with_single_file(text: &str) -> (Self, FileId) {
23-
let fixture = ChangeFixture::parse(text);
22+
fn with_single_file(ra_fixture: &str) -> (Self, FileId) {
23+
let fixture = ChangeFixture::parse(ra_fixture);
2424
let mut db = Self::default();
2525
fixture.change.apply(&mut db);
2626
assert_eq!(fixture.files.len(), 1);

crates/base_db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHas
102102
let root_file = graph[krate].root_file_id;
103103
db.file_source_root(root_file) == id
104104
})
105-
.collect::<FxHashSet<_>>();
105+
.collect();
106106
Arc::new(res)
107107
}
108108

crates/hir/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ use hir_def::{
4040
adt::{ReprKind, VariantData},
4141
body::{BodyDiagnostic, SyntheticSyntax},
4242
expr::{BindingAnnotation, LabelId, Pat, PatId},
43-
item_tree::ItemTreeNode,
4443
lang_item::LangItemTarget,
4544
nameres,
4645
per_ns::PerNs,
4746
resolver::{HasResolver, Resolver},
48-
src::HasSource as _,
49-
AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId,
50-
DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId,
51-
LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
52-
TypeParamId, UnionId,
47+
AttrDefId, ConstId, ConstParamId, EnumId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
48+
LocalEnumVariantId, LocalFieldId, StaticId, StructId, TypeAliasId, TypeParamId, UnionId,
5349
};
54-
use hir_expand::{name::name, MacroCallKind, MacroDefId, MacroDefKind};
50+
use hir_expand::{name::name, MacroCallKind, MacroDefKind};
5551
use hir_ty::{
5652
autoderef,
5753
consteval::ConstExt,
@@ -109,14 +105,28 @@ pub use {
109105
attr::{Attr, Attrs, AttrsWithOwner, Documentation},
110106
find_path::PrefixKind,
111107
import_map,
112-
nameres::ModuleSource,
108+
item_scope::ItemScope,
109+
item_tree::ItemTreeNode,
110+
nameres::{DefMap, ModuleData, ModuleOrigin, ModuleSource},
113111
path::{ModPath, PathKind},
112+
src::HasSource as DefHasSource, // xx: I don't like this shadowing of HasSource... :(
114113
type_ref::{Mutability, TypeRef},
115114
visibility::Visibility,
115+
AdtId,
116+
AssocContainerId,
117+
AssocItemId,
118+
AssocItemLoc,
119+
DefWithBodyId,
120+
ImplId,
121+
ItemLoc,
122+
Lookup,
123+
ModuleDefId,
124+
ModuleId,
125+
TraitId,
116126
},
117127
hir_expand::{
118128
name::{known, Name},
119-
ExpandResult, HirFileId, InFile, MacroFile, Origin,
129+
ExpandResult, HirFileId, InFile, MacroDefId, MacroFile, Origin,
120130
},
121131
hir_ty::display::HirDisplay,
122132
};

crates/hir/src/semantics.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
143143
self.imp.parse(file_id)
144144
}
145145

146+
pub fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode> {
147+
self.imp.parse_or_expand(file_id)
148+
}
149+
146150
pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
147151
self.imp.expand(macro_call)
148152
}
@@ -416,6 +420,12 @@ impl<'db> SemanticsImpl<'db> {
416420
tree
417421
}
418422

423+
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode> {
424+
let node = self.db.parse_or_expand(file_id)?;
425+
self.cache(node.clone(), file_id);
426+
Some(node)
427+
}
428+
419429
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
420430
let sa = self.analyze(macro_call.syntax());
421431
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;

crates/hir_def/src/item_scope.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub struct ItemScope {
4444
/// The defs declared in this scope. Each def has a single scope where it is
4545
/// declared.
4646
declarations: Vec<ModuleDefId>,
47+
macro_declarations: Vec<MacroDefId>,
48+
4749
impls: Vec<ImplId>,
4850
unnamed_consts: Vec<ConstId>,
4951
/// Traits imported via `use Trait as _;`.
@@ -101,6 +103,10 @@ impl ItemScope {
101103
self.declarations.iter().copied()
102104
}
103105

106+
pub fn macro_declarations(&self) -> impl Iterator<Item = MacroDefId> + '_ {
107+
self.macro_declarations.iter().copied()
108+
}
109+
104110
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
105111
self.impls.iter().copied()
106112
}
@@ -163,6 +169,10 @@ impl ItemScope {
163169
self.declarations.push(def)
164170
}
165171

172+
pub(crate) fn declare_macro(&mut self, def: MacroDefId) {
173+
self.macro_declarations.push(def);
174+
}
175+
166176
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
167177
self.legacy_macros.get(name).copied()
168178
}
@@ -336,7 +346,8 @@ impl ItemScope {
336346
values,
337347
macros,
338348
unresolved,
339-
declarations: defs,
349+
declarations,
350+
macro_declarations,
340351
impls,
341352
unnamed_consts,
342353
unnamed_trait_imports,
@@ -348,7 +359,8 @@ impl ItemScope {
348359
values.shrink_to_fit();
349360
macros.shrink_to_fit();
350361
unresolved.shrink_to_fit();
351-
defs.shrink_to_fit();
362+
declarations.shrink_to_fit();
363+
macro_declarations.shrink_to_fit();
352364
impls.shrink_to_fit();
353365
unnamed_consts.shrink_to_fit();
354366
unnamed_trait_imports.shrink_to_fit();

crates/hir_def/src/nameres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub enum ModuleOrigin {
148148
}
149149

150150
impl ModuleOrigin {
151-
fn declaration(&self) -> Option<AstId<ast::Module>> {
151+
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
152152
match self {
153153
ModuleOrigin::File { declaration: module, .. }
154154
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),

crates/hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ impl DefCollector<'_> {
594594
) {
595595
// Textual scoping
596596
self.define_legacy_macro(module_id, name.clone(), macro_);
597+
self.def_map.modules[module_id].scope.declare_macro(macro_);
597598

598599
// Module scoping
599600
// In Rust, `#[macro_export]` macros are unconditionally visible at the
@@ -632,6 +633,7 @@ impl DefCollector<'_> {
632633
) {
633634
let vis =
634635
self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public);
636+
self.def_map.modules[module_id].scope.declare_macro(macro_);
635637
self.update(module_id, &[(Some(name), PerNs::macros(macro_, vis))], vis, ImportType::Named);
636638
}
637639

@@ -640,6 +642,7 @@ impl DefCollector<'_> {
640642
/// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
641643
/// And unconditionally exported.
642644
fn define_proc_macro(&mut self, name: Name, macro_: MacroDefId) {
645+
self.def_map.modules[self.def_map.root].scope.declare_macro(macro_);
643646
self.update(
644647
self.def_map.root,
645648
&[(Some(name), PerNs::macros(macro_, Visibility::Public))],

crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ impl Analysis {
367367
pub fn symbol_search(&self, query: Query) -> Cancellable<Vec<NavigationTarget>> {
368368
self.with_db(|db| {
369369
symbol_index::world_symbols(db, query)
370-
.into_iter()
371-
.map(|s| s.to_nav(db))
370+
.into_iter() // xx: should we make this a par iter?
371+
.filter_map(|s| s.try_to_nav(db))
372372
.collect::<Vec<_>>()
373373
})
374374
}

crates/ide/src/navigation_target.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ impl NavigationTarget {
167167
}
168168
}
169169

170-
impl ToNav for FileSymbol {
171-
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
172-
NavigationTarget {
173-
file_id: self.file_id,
170+
impl TryToNav for FileSymbol {
171+
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
172+
let full_range = self.loc.original_range(db)?;
173+
let name_range = self.loc.original_name_range(db)?;
174+
175+
Some(NavigationTarget {
176+
file_id: full_range.file_id,
174177
name: self.name.clone(),
175178
kind: Some(match self.kind {
176179
FileSymbolKind::Function => SymbolKind::Function,
@@ -184,12 +187,12 @@ impl ToNav for FileSymbol {
184187
FileSymbolKind::Macro => SymbolKind::Macro,
185188
FileSymbolKind::Union => SymbolKind::Union,
186189
}),
187-
full_range: self.range,
188-
focus_range: self.name_range,
190+
full_range: full_range.range,
191+
focus_range: Some(name_range.range),
189192
container_name: self.container_name.clone(),
190193
description: description_from_symbol(db, self),
191194
docs: None,
192-
}
195+
})
193196
}
194197
}
195198

@@ -517,8 +520,7 @@ impl TryToNav for hir::ConstParam {
517520
/// e.g. `struct Name`, `enum Name`, `fn Name`
518521
pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
519522
let sema = Semantics::new(db);
520-
let parse = sema.parse(symbol.file_id);
521-
let node = symbol.ptr.to_node(parse.syntax());
523+
let node = symbol.loc.syntax(&sema)?;
522524

523525
match_ast! {
524526
match node {

crates/ide/src/status.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use ide_db::{
1111
};
1212
use itertools::Itertools;
1313
use profile::{memory_usage, Bytes};
14-
use rustc_hash::FxHashMap;
1514
use std::env;
1615
use stdx::format_to;
1716
use syntax::{ast, Parse, SyntaxNode};
@@ -149,20 +148,16 @@ impl fmt::Display for LibrarySymbolsStats {
149148
}
150149
}
151150

152-
impl FromIterator<TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>
153-
for LibrarySymbolsStats
154-
{
151+
impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
155152
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
156153
where
157-
T: IntoIterator<Item = TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>,
154+
T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
158155
{
159156
let mut res = LibrarySymbolsStats::default();
160157
for entry in iter {
161-
let value = entry.value.unwrap();
162-
for symbols in value.values() {
163-
res.total += symbols.len();
164-
res.size += symbols.memory_size();
165-
}
158+
let symbols = entry.value.unwrap();
159+
res.total += symbols.len();
160+
res.size += symbols.memory_size();
166161
}
167162
res
168163
}

crates/ide_db/src/apply_change.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl RootDatabase {
137137
hir::db::InternTypeParamIdQuery
138138

139139
// SymbolsDatabase
140-
crate::symbol_index::FileSymbolsQuery
140+
crate::symbol_index::ModuleSymbolsQuery
141141
crate::symbol_index::LibrarySymbolsQuery
142142
crate::symbol_index::LocalRootsQuery
143143
crate::symbol_index::LibraryRootsQuery

crates/ide_db/src/items_locator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ fn get_name_definition(
133133
import_candidate: &FileSymbol,
134134
) -> Option<Definition> {
135135
let _p = profile::span("get_name_definition");
136-
let file_id = import_candidate.file_id;
137136

138-
let candidate_node = import_candidate.ptr.to_node(sema.parse(file_id).syntax());
137+
let candidate_node = import_candidate.loc.syntax(sema)?;
139138
let candidate_name_node = if candidate_node.kind() != NAME {
140139
candidate_node.children().find(|it| it.kind() == NAME)?
141140
} else {

0 commit comments

Comments
 (0)