Skip to content

Commit 1dc5608

Browse files
bors[bot]matklad
andcommitted
Merge #176
176: Move completio to ra_analysis r=matklad a=matklad While we should handle completion for isolated file, it's better achieved by using empty Analysis, rather than working only with &File: we need memoization for type inference even inside a single file. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents e60ef62 + c09e14a commit 1dc5608

File tree

20 files changed

+1066
-1066
lines changed

20 files changed

+1066
-1066
lines changed

crates/ra_analysis/src/completion.rs

Lines changed: 619 additions & 4 deletions
Large diffs are not rendered by default.

crates/ra_analysis/src/db.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use salsa;
99
use crate::{
1010
db,
1111
Cancelable, Canceled,
12-
descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase, ModuleScopeQuery},
12+
descriptors::{
13+
DescriptorDatabase, SubmodulesQuery, ModuleTreeQuery, ModuleScopeQuery,
14+
FnSyntaxQuery, FnScopesQuery
15+
},
1316
symbol_index::SymbolIndex,
1417
syntax_ptr::{SyntaxPtrDatabase, ResolveSyntaxPtrQuery},
1518
FileId,
@@ -63,10 +66,12 @@ salsa::database_storage! {
6366
fn file_lines() for FileLinesQuery;
6467
fn file_symbols() for FileSymbolsQuery;
6568
}
66-
impl ModulesDatabase {
69+
impl DescriptorDatabase {
6770
fn module_tree() for ModuleTreeQuery;
6871
fn module_descriptor() for SubmodulesQuery;
6972
fn module_scope() for ModuleScopeQuery;
73+
fn fn_syntax() for FnSyntaxQuery;
74+
fn fn_scopes() for FnScopesQuery;
7075
}
7176
impl SyntaxPtrDatabase {
7277
fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::sync::Arc;
2+
3+
use ra_syntax::{
4+
ast::{AstNode, FnDef, FnDefNode},
5+
};
6+
7+
use crate::{
8+
descriptors::{
9+
DescriptorDatabase,
10+
function::{FnId, FnScopes},
11+
},
12+
};
13+
14+
/// Resolve `FnId` to the corresponding `SyntaxNode`
15+
/// TODO: this should return something more type-safe then `SyntaxNode`
16+
pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
17+
let syntax = db.resolve_syntax_ptr(fn_id.0);
18+
let fn_def = FnDef::cast(syntax.borrowed()).unwrap();
19+
FnDefNode::new(fn_def)
20+
}
21+
22+
pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc<FnScopes> {
23+
let syntax = db.fn_syntax(fn_id);
24+
let res = FnScopes::new(syntax.ast());
25+
Arc::new(res)
26+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
pub(super) mod imp;
2+
mod scope;
3+
4+
use ra_syntax::{
5+
ast::{self, AstNode, NameOwner}
6+
};
7+
8+
use crate::{
9+
FileId,
10+
syntax_ptr::SyntaxPtr
11+
};
12+
13+
pub(crate) use self::scope::{FnScopes, resolve_local_name};
14+
15+
16+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17+
pub(crate) struct FnId(SyntaxPtr);
18+
19+
impl FnId {
20+
pub(crate) fn new(file_id: FileId, fn_def: ast::FnDef) -> FnId {
21+
let ptr = SyntaxPtr::new(file_id, fn_def.syntax());
22+
FnId(ptr)
23+
}
24+
}
25+
26+
27+
#[derive(Debug, Clone)]
28+
pub struct FnDescriptor {
29+
pub name: String,
30+
pub label: String,
31+
pub ret_type: Option<String>,
32+
pub params: Vec<String>,
33+
}
34+
35+
impl FnDescriptor {
36+
pub fn new(node: ast::FnDef) -> Option<Self> {
37+
let name = node.name()?.text().to_string();
38+
39+
// Strip the body out for the label.
40+
let label: String = if let Some(body) = node.body() {
41+
let body_range = body.syntax().range();
42+
let label: String = node
43+
.syntax()
44+
.children()
45+
.filter(|child| !child.range().is_subrange(&body_range))
46+
.map(|node| node.text().to_string())
47+
.collect();
48+
label
49+
} else {
50+
node.syntax().text().to_string()
51+
};
52+
53+
let params = FnDescriptor::param_list(node);
54+
let ret_type = node.ret_type().map(|r| r.syntax().text().to_string());
55+
56+
Some(FnDescriptor {
57+
name,
58+
ret_type,
59+
params,
60+
label,
61+
})
62+
}
63+
64+
fn param_list(node: ast::FnDef) -> Vec<String> {
65+
let mut res = vec![];
66+
if let Some(param_list) = node.param_list() {
67+
if let Some(self_param) = param_list.self_param() {
68+
res.push(self_param.syntax().text().to_string())
69+
}
70+
71+
// Maybe use param.pat here? See if we can just extract the name?
72+
//res.extend(param_list.params().map(|p| p.syntax().text().to_string()));
73+
res.extend(
74+
param_list
75+
.params()
76+
.filter_map(|p| p.pat())
77+
.map(|pat| pat.syntax().text().to_string()),
78+
);
79+
}
80+
res
81+
}
82+
}
83+

crates/ra_editor/src/scope/fn_scope.rs renamed to crates/ra_analysis/src/descriptors/function/scope.rs

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
use std::fmt;
2-
3-
use rustc_hash::FxHashMap;
1+
use rustc_hash::{FxHashMap, FxHashSet};
42

53
use ra_syntax::{
64
algo::generate,
75
ast::{self, ArgListOwner, LoopBodyOwner, NameOwner},
8-
AstNode, SmolStr, SyntaxNode, SyntaxNodeRef,
6+
AstNode, SmolStr, SyntaxNodeRef,
97
};
108

11-
type ScopeId = usize;
9+
use crate::syntax_ptr::LocalSyntaxPtr;
10+
11+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
12+
pub(crate) struct ScopeId(u32);
1213

13-
#[derive(Debug)]
14+
#[derive(Debug, PartialEq, Eq)]
1415
pub struct FnScopes {
15-
pub self_param: Option<SyntaxNode>,
16+
pub(crate) self_param: Option<LocalSyntaxPtr>,
1617
scopes: Vec<ScopeData>,
17-
scope_for: FxHashMap<SyntaxNode, ScopeId>,
18+
scope_for: FxHashMap<LocalSyntaxPtr, ScopeId>,
19+
}
20+
21+
#[derive(Debug, PartialEq, Eq)]
22+
pub struct ScopeEntry {
23+
name: SmolStr,
24+
ptr: LocalSyntaxPtr,
25+
}
26+
27+
#[derive(Debug, PartialEq, Eq)]
28+
struct ScopeData {
29+
parent: Option<ScopeId>,
30+
entries: Vec<ScopeEntry>,
1831
}
1932

2033
impl FnScopes {
21-
pub fn new(fn_def: ast::FnDef) -> FnScopes {
34+
pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes {
2235
let mut scopes = FnScopes {
2336
self_param: fn_def
2437
.param_list()
2538
.and_then(|it| it.self_param())
26-
.map(|it| it.syntax().owned()),
39+
.map(|it| LocalSyntaxPtr::new(it.syntax())),
2740
scopes: Vec::new(),
2841
scope_for: FxHashMap::default(),
2942
};
@@ -34,24 +47,24 @@ impl FnScopes {
3447
}
3548
scopes
3649
}
37-
pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
38-
&self.scopes[scope].entries
50+
pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
51+
&self.get(scope).entries
3952
}
4053
pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a {
4154
generate(self.scope_for(node), move |&scope| {
42-
self.scopes[scope].parent
55+
self.get(scope).parent
4356
})
4457
}
4558
fn root_scope(&mut self) -> ScopeId {
46-
let res = self.scopes.len();
59+
let res = ScopeId(self.scopes.len() as u32);
4760
self.scopes.push(ScopeData {
4861
parent: None,
4962
entries: vec![],
5063
});
5164
res
5265
}
5366
fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
54-
let res = self.scopes.len();
67+
let res = ScopeId(self.scopes.len() as u32);
5568
self.scopes.push(ScopeData {
5669
parent: Some(parent),
5770
entries: vec![],
@@ -64,7 +77,7 @@ impl FnScopes {
6477
.descendants()
6578
.filter_map(ast::BindPat::cast)
6679
.filter_map(ScopeEntry::new);
67-
self.scopes[scope].entries.extend(entries);
80+
self.get_mut(scope).entries.extend(entries);
6881
}
6982
fn add_params_bindings(&mut self, scope: ScopeId, params: Option<ast::ParamList>) {
7083
params
@@ -74,43 +87,36 @@ impl FnScopes {
7487
.for_each(|it| self.add_bindings(scope, it));
7588
}
7689
fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) {
77-
self.scope_for.insert(node.owned(), scope);
90+
self.scope_for.insert(LocalSyntaxPtr::new(node), scope);
7891
}
7992
fn scope_for(&self, node: SyntaxNodeRef) -> Option<ScopeId> {
8093
node.ancestors()
81-
.filter_map(|it| self.scope_for.get(&it.owned()).map(|&scope| scope))
94+
.map(LocalSyntaxPtr::new)
95+
.filter_map(|it| self.scope_for.get(&it).map(|&scope| scope))
8296
.next()
8397
}
84-
}
85-
86-
pub struct ScopeEntry {
87-
syntax: SyntaxNode,
98+
fn get(&self, scope: ScopeId) -> &ScopeData {
99+
&self.scopes[scope.0 as usize]
100+
}
101+
fn get_mut(&mut self, scope: ScopeId) -> &mut ScopeData {
102+
&mut self.scopes[scope.0 as usize]
103+
}
88104
}
89105

90106
impl ScopeEntry {
91107
fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
92-
if pat.name().is_some() {
93-
Some(ScopeEntry {
94-
syntax: pat.syntax().owned(),
95-
})
96-
} else {
97-
None
98-
}
99-
}
100-
pub fn name(&self) -> SmolStr {
101-
self.ast().name().unwrap().text()
108+
let name = pat.name()?;
109+
let res = ScopeEntry {
110+
name: name.text(),
111+
ptr: LocalSyntaxPtr::new(pat.syntax()),
112+
};
113+
Some(res)
102114
}
103-
pub fn ast(&self) -> ast::BindPat {
104-
ast::BindPat::cast(self.syntax.borrowed()).unwrap()
115+
pub(crate) fn name(&self) -> &SmolStr {
116+
&self.name
105117
}
106-
}
107-
108-
impl fmt::Debug for ScopeEntry {
109-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110-
f.debug_struct("ScopeEntry")
111-
.field("name", &self.name())
112-
.field("syntax", &self.syntax)
113-
.finish()
118+
pub(crate) fn ptr(&self) -> LocalSyntaxPtr {
119+
self.ptr
114120
}
115121
}
116122

@@ -251,33 +257,28 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
251257
}
252258
}
253259

254-
#[derive(Debug)]
255-
struct ScopeData {
256-
parent: Option<ScopeId>,
257-
entries: Vec<ScopeEntry>,
258-
}
259-
260260
pub fn resolve_local_name<'a>(
261261
name_ref: ast::NameRef,
262262
scopes: &'a FnScopes,
263263
) -> Option<&'a ScopeEntry> {
264-
use rustc_hash::FxHashSet;
265-
266264
let mut shadowed = FxHashSet::default();
267265
let ret = scopes
268266
.scope_chain(name_ref.syntax())
269267
.flat_map(|scope| scopes.entries(scope).iter())
270268
.filter(|entry| shadowed.insert(entry.name()))
271-
.filter(|entry| entry.name() == name_ref.text())
269+
.filter(|entry| entry.name() == &name_ref.text())
272270
.nth(0);
273271
ret
274272
}
275273

276274
#[cfg(test)]
277275
mod tests {
278-
use super::*;
279-
use crate::{find_node_at_offset, test_utils::extract_offset};
280276
use ra_syntax::File;
277+
use test_utils::extract_offset;
278+
use ra_editor::{find_node_at_offset};
279+
280+
use super::*;
281+
281282

282283
fn do_check(code: &str, expected: &[&str]) {
283284
let (off, code) = extract_offset(code);
@@ -384,14 +385,11 @@ mod tests {
384385

385386
let scopes = FnScopes::new(fn_def);
386387

387-
let local_name = resolve_local_name(name_ref, &scopes)
388-
.unwrap()
389-
.ast()
390-
.name()
391-
.unwrap();
388+
let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap();
389+
let local_name = local_name_entry.ptr().resolve(&file);
392390
let expected_name =
393391
find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();
394-
assert_eq!(local_name.syntax().range(), expected_name.syntax().range());
392+
assert_eq!(local_name.range(), expected_name.syntax().range());
395393
}
396394

397395
#[test]

0 commit comments

Comments
 (0)