Skip to content

Commit f6f9a0b

Browse files
bors[bot]matklad
andcommitted
Merge #182
182: Module source r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents d685a9b + f2b654f commit f6f9a0b

File tree

5 files changed

+114
-38
lines changed

5 files changed

+114
-38
lines changed

crates/ra_analysis/src/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn complete_module_items(
148148
this_item: Option<ast::NameRef>,
149149
acc: &mut Vec<CompletionItem>,
150150
) {
151-
let scope = ModuleScope::from_items(items);
151+
let scope = ModuleScope::new(items); // FIXME
152152
acc.extend(
153153
scope
154154
.entries()

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22

33
use ra_syntax::{
4-
ast::{self, NameOwner},
4+
ast::{self, ModuleItemOwner, NameOwner},
55
SmolStr,
66
};
77
use relative_path::RelativePathBuf;
@@ -14,7 +14,10 @@ use crate::{
1414
Cancelable, FileId, FileResolverImp,
1515
};
1616

17-
use super::{LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleTree, Problem};
17+
use super::{
18+
LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleSourceNode,
19+
ModuleTree, Problem,
20+
};
1821

1922
pub(crate) fn submodules(
2023
db: &impl DescriptorDatabase,
@@ -43,9 +46,14 @@ pub(crate) fn module_scope(
4346
module_id: ModuleId,
4447
) -> Cancelable<Arc<ModuleScope>> {
4548
let tree = db.module_tree(source_root_id)?;
46-
let file_id = module_id.file_id(&tree);
47-
let syntax = db.file_syntax(file_id);
48-
let res = ModuleScope::new(&syntax);
49+
let source = module_id.source(&tree).resolve(db);
50+
let res = match source {
51+
ModuleSourceNode::Root(root) => ModuleScope::new(root.ast().items()),
52+
ModuleSourceNode::Inline(inline) => match inline.ast().item_list() {
53+
Some(items) => ModuleScope::new(items.items()),
54+
None => ModuleScope::new(std::iter::empty()),
55+
},
56+
};
4957
Ok(Arc::new(res))
5058
}
5159

@@ -106,7 +114,7 @@ fn build_subtree(
106114
) -> Cancelable<ModuleId> {
107115
visited.insert(file_id);
108116
let id = tree.push_mod(ModuleData {
109-
file_id,
117+
source: ModuleSource::File(file_id),
110118
parent,
111119
children: Vec::new(),
112120
});

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

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ use ra_syntax::{
77
};
88
use relative_path::RelativePathBuf;
99

10-
use crate::FileId;
10+
use crate::{db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId};
1111

1212
pub(crate) use self::scope::ModuleScope;
1313

14+
/// Phisically, rust source is organized as a set of files, but logically it is
15+
/// organized as a tree of modules. Usually, a single file corresponds to a
16+
/// single module, but it is not nessary the case.
17+
///
18+
/// Module encapsulate the logic of transitioning from the fuzzy world of files
19+
/// (which can have multiple parents) to the precise world of modules (which
20+
/// always have one parent).
1421
#[derive(Debug, PartialEq, Eq, Hash)]
1522
pub(crate) struct ModuleTree {
1623
mods: Vec<ModuleData>,
@@ -22,7 +29,7 @@ impl ModuleTree {
2229
self.mods
2330
.iter()
2431
.enumerate()
25-
.filter(|(_idx, it)| it.file_id == file_id)
32+
.filter(|(_idx, it)| it.source.is_file(file_id))
2633
.map(|(idx, _)| ModuleId(idx as u32))
2734
.collect()
2835
}
@@ -32,6 +39,23 @@ impl ModuleTree {
3239
}
3340
}
3441

42+
/// `ModuleSource` is the syntax tree element that produced this module:
43+
/// either a file, or an inlinde module.
44+
/// TODO: we don't produce Inline modules yet
45+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
46+
pub(crate) enum ModuleSource {
47+
File(FileId),
48+
#[allow(dead_code)]
49+
Inline(SyntaxPtr),
50+
}
51+
52+
/// An owned syntax node for a module. Unlike `ModuleSource`,
53+
/// this holds onto the AST for the whole file.
54+
enum ModuleSourceNode {
55+
Root(ast::RootNode),
56+
Inline(ast::ModuleNode),
57+
}
58+
3559
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
3660
pub(crate) struct ModuleId(u32);
3761

@@ -50,8 +74,8 @@ pub enum Problem {
5074
}
5175

5276
impl ModuleId {
53-
pub(crate) fn file_id(self, tree: &ModuleTree) -> FileId {
54-
tree.module(self).file_id
77+
pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource {
78+
tree.module(self).source
5579
}
5680
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
5781
tree.module(self).parent
@@ -82,14 +106,18 @@ impl ModuleId {
82106
.find(|it| it.name == name)?;
83107
Some(*link.points_to.first()?)
84108
}
85-
pub(crate) fn problems(self, tree: &ModuleTree, root: ast::Root) -> Vec<(SyntaxNode, Problem)> {
109+
pub(crate) fn problems(
110+
self,
111+
tree: &ModuleTree,
112+
db: &impl SyntaxDatabase,
113+
) -> Vec<(SyntaxNode, Problem)> {
86114
tree.module(self)
87115
.children
88116
.iter()
89117
.filter_map(|&it| {
90118
let p = tree.link(it).problem.clone()?;
91-
let s = it.bind_source(tree, root);
92-
let s = s.name().unwrap().syntax().owned();
119+
let s = it.bind_source(tree, db);
120+
let s = s.ast().name().unwrap().syntax().owned();
93121
Some((s, p))
94122
})
95123
.collect()
@@ -100,21 +128,62 @@ impl LinkId {
100128
pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
101129
tree.link(self).owner
102130
}
103-
pub(crate) fn bind_source<'a>(self, tree: &ModuleTree, root: ast::Root<'a>) -> ast::Module<'a> {
104-
imp::modules(root)
105-
.find(|(name, _)| name == &tree.link(self).name)
106-
.unwrap()
107-
.1
131+
pub(crate) fn bind_source<'a>(
132+
self,
133+
tree: &ModuleTree,
134+
db: &impl SyntaxDatabase,
135+
) -> ast::ModuleNode {
136+
let owner = self.owner(tree);
137+
match owner.source(tree).resolve(db) {
138+
ModuleSourceNode::Root(root) => {
139+
let ast = imp::modules(root.ast())
140+
.find(|(name, _)| name == &tree.link(self).name)
141+
.unwrap()
142+
.1;
143+
ast.into()
144+
}
145+
ModuleSourceNode::Inline(..) => {
146+
unimplemented!("https://github.com/rust-analyzer/rust-analyzer/issues/181")
147+
}
148+
}
108149
}
109150
}
110151

111152
#[derive(Debug, PartialEq, Eq, Hash)]
112153
struct ModuleData {
113-
file_id: FileId,
154+
source: ModuleSource,
114155
parent: Option<LinkId>,
115156
children: Vec<LinkId>,
116157
}
117158

159+
impl ModuleSource {
160+
pub(crate) fn as_file(self) -> Option<FileId> {
161+
match self {
162+
ModuleSource::File(f) => Some(f),
163+
ModuleSource::Inline(..) => None,
164+
}
165+
}
166+
167+
fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode {
168+
match self {
169+
ModuleSource::File(file_id) => {
170+
let syntax = db.file_syntax(file_id);
171+
ModuleSourceNode::Root(syntax.ast().into())
172+
}
173+
ModuleSource::Inline(ptr) => {
174+
let syntax = db.resolve_syntax_ptr(ptr);
175+
let syntax = syntax.borrowed();
176+
let module = ast::Module::cast(syntax).unwrap();
177+
ModuleSourceNode::Inline(module.into())
178+
}
179+
}
180+
}
181+
182+
fn is_file(self, file_id: FileId) -> bool {
183+
self.as_file() == Some(file_id)
184+
}
185+
}
186+
118187
#[derive(Hash, Debug, PartialEq, Eq)]
119188
struct LinkData {
120189
owner: ModuleId,

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Backend for module-level scope resolution & completion
22
3-
use ra_syntax::{
4-
ast::{self, ModuleItemOwner},
5-
AstNode, File, SmolStr,
6-
};
3+
use ra_syntax::{ast, AstNode, SmolStr};
74

85
use crate::syntax_ptr::LocalSyntaxPtr;
96

@@ -28,11 +25,7 @@ enum EntryKind {
2825
}
2926

3027
impl ModuleScope {
31-
pub fn new(file: &File) -> ModuleScope {
32-
ModuleScope::from_items(file.ast().items())
33-
}
34-
35-
pub fn from_items<'a>(items: impl Iterator<Item = ast::ModuleItem<'a>>) -> ModuleScope {
28+
pub(crate) fn new<'a>(items: impl Iterator<Item = ast::ModuleItem<'a>>) -> ModuleScope {
3629
let mut entries = Vec::new();
3730
for item in items {
3831
let entry = match item {
@@ -102,11 +95,11 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
10295
#[cfg(test)]
10396
mod tests {
10497
use super::*;
105-
use ra_syntax::File;
98+
use ra_syntax::{ast::ModuleItemOwner, File};
10699

107100
fn do_check(code: &str, expected: &[&str]) {
108101
let file = File::parse(&code);
109-
let scope = ModuleScope::new(&file);
102+
let scope = ModuleScope::new(file.ast().items());
110103
let actual = scope.entries.iter().map(|it| it.name()).collect::<Vec<_>>();
111104
assert_eq!(expected, actual.as_slice());
112105
}

crates/ra_analysis/src/imp.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
db::{self, FileSyntaxQuery, SyntaxDatabase},
2121
descriptors::{
2222
function::{FnDescriptor, FnId},
23-
module::{ModuleTree, Problem},
23+
module::{ModuleSource, ModuleTree, Problem},
2424
DeclarationDescriptor, DescriptorDatabase,
2525
},
2626
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
@@ -222,9 +222,15 @@ impl AnalysisImpl {
222222
.into_iter()
223223
.filter_map(|module_id| {
224224
let link = module_id.parent_link(&module_tree)?;
225-
let file_id = link.owner(&module_tree).file_id(&module_tree);
226-
let syntax = self.db.file_syntax(file_id);
227-
let decl = link.bind_source(&module_tree, syntax.ast());
225+
let file_id = match link.owner(&module_tree).source(&module_tree) {
226+
ModuleSource::File(file_id) => file_id,
227+
ModuleSource::Inline(..) => {
228+
//TODO: https://github.com/rust-analyzer/rust-analyzer/issues/181
229+
return None;
230+
}
231+
};
232+
let decl = link.bind_source(&module_tree, &self.db);
233+
let decl = decl.ast();
228234

229235
let sym = FileSymbol {
230236
name: decl.name().unwrap().text(),
@@ -243,7 +249,7 @@ impl AnalysisImpl {
243249
.modules_for_file(file_id)
244250
.into_iter()
245251
.map(|it| it.root(&module_tree))
246-
.map(|it| it.file_id(&module_tree))
252+
.filter_map(|it| it.source(&module_tree).as_file())
247253
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
248254
.collect();
249255

@@ -365,7 +371,7 @@ impl AnalysisImpl {
365371
})
366372
.collect::<Vec<_>>();
367373
if let Some(m) = module_tree.any_module_for_file(file_id) {
368-
for (name_node, problem) in m.problems(&module_tree, syntax.ast()) {
374+
for (name_node, problem) in m.problems(&module_tree, &self.db) {
369375
let diag = match problem {
370376
Problem::UnresolvedModule { candidate } => {
371377
let create_file = FileSystemEdit::CreateFile {
@@ -533,7 +539,7 @@ impl AnalysisImpl {
533539
};
534540
module_id
535541
.child(module_tree, name.as_str())
536-
.map(|it| it.file_id(module_tree))
542+
.and_then(|it| it.source(&module_tree).as_file())
537543
.into_iter()
538544
.collect()
539545
}

0 commit comments

Comments
 (0)