-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add DeclarationDescriptor and ReferenceDescriptor #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
crates/ra_analysis/src/imp.rs
Outdated
|
||
return ret; | ||
} | ||
|
||
// Find the symbol we are looking for | ||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point I would like to use the DeclarationDescriptor
machinery and was thinking of a way to work backwards to an ast::BindingPat
. Something like:
let maybe_binding = find_node_at_offset::<ast::BindPat>(syntax, offset)
.or_else(||
find_node_at_offset::<ast::NameRef>(syntax, offset)
.and_then(|name_ref| resolve_local_name(name_ref))
.and_then(|resolved| {
eprintln!("** resolved: {:?}", resolved);
let n = find_node_at_offset::<ast::BindPat>(syntax, resolved.1.start());
eprintln!("**** n: {:?}", n);
n
})
);
if maybe_binding.is_none() {
eprintln!("** maybe_binding is none");
return ret;
}
let binding = maybe_binding.unwrap();
let decl = DeclarationDescriptor::new(binding);
Which would simplify this function quite a bit. However find_node_at_offset::<ast::BindPat>(syntax, resolved.1.start());
always returns None
. I'm sure that I am missing something obvious. Dumping the syntax for my test shows that I should be resolving an ast::BindPat
there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay actually... if I change the offset to: resolved.1.end()
it seems to work().
} | ||
|
||
#[derive(Debug)] | ||
pub struct DeclarationDescriptor<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Descriptors should be owned values and, ideally, should not directly reference syntax trees.
The idea is that, when processing code, we convert from syntax trees to a descriptor tree (which is more compact) and then throw away syntax to save memory.
We should have a method to map between descriptors and syntax on as needed basis. For example, we can store some sort of syntax node id in the descriptor, and use it to retrieve the node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrestled with this but found that it was really the only way to implement find_all_refs
without passing in the syntax
. If we did that then we need to make sure that the syntax is the right one for the stored TextRange
(maybe by storing the FileId
as well?).
Can you sketch out a little what this would look like? I'm assuming that we do not have the concept of a syntax node id. If we had one where would it look for the corresponding syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you sketch out a little what this would look like?
I won't have time until next week to give significant advice unfortunately, but I want to document/refactor/flesh-out the descriptor infrastructure more in the coming weeks.
The motivation for descriptors is that there isn't a 1-to-1 correspondance between rust source code and rust semantic model. If you have
#[path="a.rs"]
mod foo;
#[path="a.rs"]
mod bar;
Then the source of a.rs
is present in the semantic model as two unrelated modules.
So, while SyntaxTrees correspond to source files, descriptors should correspond to the semantic model, and most of the analysis should work solely with descriptors.
Practically speaking, I think we should give ids to all syntax nodes and descriptors, and then have a free-form mapping between these ids.
I think a good star might be just using a triple of (FIleId, TextRange, SyntaxKind)
as a syntax node ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll have to think about how this should work when converting from id to node inside find_all_refs
. It looks like we'll need to access (pass in?) the db
somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #169 for the syntax ptr stuff. I'll try to move some of the desciptors to this.
bors delegate+ |
✌️ kjeremy can now approve this pull request |
Ive moved scopes from editor to analysis, that's probably the cause of the merge conflict :( |
@kjeremy I'll rebase this myself, to merge the rustfmt PR |
merged! |
Fixes #142
Fixes #146