Skip to content

Commit 713c3ea

Browse files
bors[bot]matklad
andcommitted
Merge #234
234: Global module r=matklad a=matklad This series of commits re-introdces `ModuleDescriptor` as one stop shop for all information about a module. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 902778c + a2fdb41 commit 713c3ea

File tree

9 files changed

+233
-203
lines changed

9 files changed

+233
-203
lines changed

crates/ra_analysis/src/completion/mod.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod reference_completion;
22

33
use ra_editor::find_node_at_offset;
44
use ra_syntax::{
5-
algo::find_leaf_at_offset,
65
algo::visit::{visitor_ctx, VisitorCtx},
76
ast,
87
AstNode, AtomEdit,
@@ -12,8 +11,9 @@ use rustc_hash::{FxHashMap};
1211

1312
use crate::{
1413
db::{self, SyntaxDatabase},
15-
descriptors::{DescriptorDatabase, module::ModuleSource},
16-
input::{FilesDatabase},
14+
descriptors::{
15+
module::{ModuleDescriptor}
16+
},
1717
Cancelable, FilePosition
1818
};
1919

@@ -38,14 +38,7 @@ pub(crate) fn completions(
3838
original_file.reparse(&edit)
3939
};
4040

41-
let leaf = match find_leaf_at_offset(original_file.syntax(), position.offset).left_biased() {
42-
None => return Ok(None),
43-
Some(it) => it,
44-
};
45-
let source_root_id = db.file_source_root(position.file_id);
46-
let module_tree = db.module_tree(source_root_id)?;
47-
let module_source = ModuleSource::for_node(position.file_id, leaf);
48-
let module_id = match module_tree.any_module_for_source(module_source) {
41+
let module = match ModuleDescriptor::guess_from_position(db, position)? {
4942
None => return Ok(None),
5043
Some(it) => it,
5144
};
@@ -55,15 +48,7 @@ pub(crate) fn completions(
5548
// First, let's try to complete a reference to some declaration.
5649
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
5750
has_completions = true;
58-
reference_completion::completions(
59-
&mut res,
60-
db,
61-
source_root_id,
62-
&module_tree,
63-
module_id,
64-
&file,
65-
name_ref,
66-
)?;
51+
reference_completion::completions(&mut res, db, &module, &file, name_ref)?;
6752
// special case, `trait T { fn foo(i_am_a_name_ref) {} }`
6853
if is_node::<ast::Param>(name_ref.syntax()) {
6954
param_completions(name_ref.syntax(), &mut res);

crates/ra_analysis/src/completion/reference_completion.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ use ra_syntax::{
99

1010
use crate::{
1111
db::RootDatabase,
12-
input::{SourceRootId},
1312
completion::CompletionItem,
14-
descriptors::module::{ModuleId, ModuleTree},
13+
descriptors::module::{ModuleDescriptor},
1514
descriptors::function::FnScopes,
16-
descriptors::DescriptorDatabase,
1715
Cancelable
1816
};
1917

2018
pub(super) fn completions(
2119
acc: &mut Vec<CompletionItem>,
2220
db: &RootDatabase,
23-
source_root_id: SourceRootId,
24-
module_tree: &ModuleTree,
25-
module_id: ModuleId,
21+
module: &ModuleDescriptor,
2622
file: &SourceFileNode,
2723
name_ref: ast::NameRef,
2824
) -> Cancelable<()> {
@@ -40,7 +36,7 @@ pub(super) fn completions(
4036
complete_expr_snippets(acc);
4137
}
4238

43-
let module_scope = db.module_scope(source_root_id, module_id)?;
39+
let module_scope = module.scope(db)?;
4440
acc.extend(
4541
module_scope
4642
.entries()
@@ -56,9 +52,7 @@ pub(super) fn completions(
5652
}),
5753
);
5854
}
59-
NameRefKind::CratePath(path) => {
60-
complete_path(acc, db, source_root_id, module_tree, module_id, path)?
61-
}
55+
NameRefKind::CratePath(path) => complete_path(acc, db, module, path)?,
6256
NameRefKind::BareIdentInMod => {
6357
let name_range = name_ref.syntax().range();
6458
let top_node = name_ref
@@ -171,16 +165,14 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<Completi
171165
fn complete_path(
172166
acc: &mut Vec<CompletionItem>,
173167
db: &RootDatabase,
174-
source_root_id: SourceRootId,
175-
module_tree: &ModuleTree,
176-
module_id: ModuleId,
168+
module: &ModuleDescriptor,
177169
crate_path: Vec<ast::NameRef>,
178170
) -> Cancelable<()> {
179-
let target_module_id = match find_target_module(module_tree, module_id, crate_path) {
171+
let target_module = match find_target_module(module, crate_path) {
180172
None => return Ok(()),
181173
Some(it) => it,
182174
};
183-
let module_scope = db.module_scope(source_root_id, target_module_id)?;
175+
let module_scope = target_module.scope(db)?;
184176
let completions = module_scope.entries().iter().map(|entry| CompletionItem {
185177
label: entry.name().to_string(),
186178
lookup: None,
@@ -191,14 +183,13 @@ fn complete_path(
191183
}
192184

193185
fn find_target_module(
194-
module_tree: &ModuleTree,
195-
module_id: ModuleId,
186+
module: &ModuleDescriptor,
196187
mut crate_path: Vec<ast::NameRef>,
197-
) -> Option<ModuleId> {
188+
) -> Option<ModuleDescriptor> {
198189
crate_path.pop();
199-
let mut target_module = module_id.root(&module_tree);
190+
let mut target_module = module.crate_root();
200191
for name in crate_path {
201-
target_module = target_module.child(module_tree, name.text().as_str())?;
192+
target_module = target_module.child(name.text().as_str())?;
202193
}
203194
Some(target_module)
204195
}

crates/ra_analysis/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ salsa::database_storage! {
8585
}
8686
impl DescriptorDatabase {
8787
fn module_tree() for ModuleTreeQuery;
88-
fn module_descriptor() for SubmodulesQuery;
8988
fn module_scope() for ModuleScopeQuery;
90-
fn fn_syntax() for FnSyntaxQuery;
9189
fn fn_scopes() for FnScopesQuery;
90+
fn _fn_syntax() for FnSyntaxQuery;
91+
fn _submodules() for SubmodulesQuery;
9292
}
9393
}
9494
}

crates/ra_analysis/src/descriptors/function/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode
1515
}
1616

1717
pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc<FnScopes> {
18-
let syntax = db.fn_syntax(fn_id);
18+
let syntax = db._fn_syntax(fn_id);
1919
let res = FnScopes::new(syntax.borrowed());
2020
Arc::new(res)
2121
}

crates/ra_analysis/src/descriptors/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,28 @@ use crate::{
2020

2121
salsa::query_group! {
2222
pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase {
23-
fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
23+
fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> {
24+
type FnScopesQuery;
25+
use fn function::imp::fn_scopes;
26+
}
27+
28+
fn _module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
2429
type ModuleTreeQuery;
2530
use fn module::imp::module_tree;
2631
}
27-
fn submodules(source: ModuleSource) -> Cancelable<Arc<Vec<module::imp::Submodule>>> {
28-
type SubmodulesQuery;
29-
use fn module::imp::submodules;
30-
}
31-
fn module_scope(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<ModuleScope>> {
32+
fn _module_scope(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<ModuleScope>> {
3233
type ModuleScopeQuery;
3334
use fn module::imp::module_scope;
3435
}
35-
fn fn_syntax(fn_id: FnId) -> FnDefNode {
36+
fn _fn_syntax(fn_id: FnId) -> FnDefNode {
3637
type FnSyntaxQuery;
3738
// Don't retain syntax trees in memory
3839
storage volatile;
3940
use fn function::imp::fn_syntax;
4041
}
41-
fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> {
42-
type FnScopesQuery;
43-
use fn function::imp::fn_scopes;
42+
fn _submodules(source: ModuleSource) -> Cancelable<Arc<Vec<module::imp::Submodule>>> {
43+
type SubmodulesQuery;
44+
use fn module::imp::submodules;
4445
}
4546
}
4647
}

crates/ra_analysis/src/descriptors/module/imp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn module_scope(
8686
source_root_id: SourceRootId,
8787
module_id: ModuleId,
8888
) -> Cancelable<Arc<ModuleScope>> {
89-
let tree = db.module_tree(source_root_id)?;
89+
let tree = db._module_tree(source_root_id)?;
9090
let source = module_id.source(&tree).resolve(db);
9191
let res = match source {
9292
ModuleSourceNode::SourceFile(it) => ModuleScope::new(it.borrowed().items()),
@@ -155,7 +155,7 @@ fn build_subtree(
155155
parent,
156156
children: Vec::new(),
157157
});
158-
for sub in db.submodules(source)?.iter() {
158+
for sub in db._submodules(source)?.iter() {
159159
let link = tree.push_link(LinkData {
160160
name: sub.name().clone(),
161161
owner: id,

0 commit comments

Comments
 (0)