-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
241 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/// If the cursor is on an custom attribute, this struct will try to resolve its | ||
/// underlying function and return a ReferenceId to it. | ||
/// This is needed in hover and go-to-definition because when an annotation generates | ||
/// code, that code ends up residing in the attribute definition (it ends up having the | ||
/// attribute's span) so using the usual graph to locate what points to that location | ||
/// will give not only the attribute function but also any type generated by it. | ||
use std::collections::BTreeMap; | ||
|
||
use chumsky::Parser; | ||
use fm::FileId; | ||
use noirc_errors::Span; | ||
use noirc_frontend::{ | ||
ast::{AttributeTarget, Visitor}, | ||
graph::CrateId, | ||
hir::{ | ||
def_map::{CrateDefMap, LocalModuleId, ModuleId}, | ||
resolution::path_resolver::{PathResolver, StandardPathResolver}, | ||
}, | ||
lexer::Lexer, | ||
node_interner::ReferenceId, | ||
parser::{path_no_turbofish, ParsedSubModule}, | ||
token::CustomAttribute, | ||
usage_tracker::UsageTracker, | ||
ParsedModule, | ||
}; | ||
|
||
use crate::modules::module_def_id_to_reference_id; | ||
|
||
pub(crate) struct AttributeReferenceFinder<'a> { | ||
byte_index: usize, | ||
/// The module ID in scope. This might change as we traverse the AST | ||
/// if we are analyzing something inside an inline module declaration. | ||
module_id: ModuleId, | ||
def_maps: &'a BTreeMap<CrateId, CrateDefMap>, | ||
reference_id: Option<ReferenceId>, | ||
} | ||
|
||
impl<'a> AttributeReferenceFinder<'a> { | ||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) fn new( | ||
file: FileId, | ||
byte_index: usize, | ||
krate: CrateId, | ||
def_maps: &'a BTreeMap<CrateId, CrateDefMap>, | ||
) -> Self { | ||
// Find the module the current file belongs to | ||
let def_map = &def_maps[&krate]; | ||
let local_id = if let Some((module_index, _)) = | ||
def_map.modules().iter().find(|(_, module_data)| module_data.location.file == file) | ||
{ | ||
LocalModuleId(module_index) | ||
} else { | ||
def_map.root() | ||
}; | ||
let module_id = ModuleId { krate, local_id }; | ||
Self { byte_index, module_id, def_maps, reference_id: None } | ||
} | ||
|
||
pub(crate) fn find(&mut self, parsed_module: &ParsedModule) -> Option<ReferenceId> { | ||
parsed_module.accept(self); | ||
|
||
self.reference_id | ||
} | ||
|
||
fn includes_span(&self, span: Span) -> bool { | ||
span.start() as usize <= self.byte_index && self.byte_index <= span.end() as usize | ||
} | ||
} | ||
|
||
impl<'a> Visitor for AttributeReferenceFinder<'a> { | ||
fn visit_parsed_submodule(&mut self, parsed_sub_module: &ParsedSubModule, _span: Span) -> bool { | ||
// Switch `self.module_id` to the submodule | ||
let previous_module_id = self.module_id; | ||
|
||
let def_map = &self.def_maps[&self.module_id.krate]; | ||
if let Some(module_data) = def_map.modules().get(self.module_id.local_id.0) { | ||
if let Some(child_module) = module_data.children.get(&parsed_sub_module.name) { | ||
self.module_id = ModuleId { krate: self.module_id.krate, local_id: *child_module }; | ||
} | ||
} | ||
|
||
parsed_sub_module.accept_children(self); | ||
|
||
// Restore the old module before continuing | ||
self.module_id = previous_module_id; | ||
|
||
false | ||
} | ||
|
||
fn visit_custom_attribute(&mut self, attribute: &CustomAttribute, _target: AttributeTarget) { | ||
if !self.includes_span(attribute.contents_span) { | ||
return; | ||
} | ||
|
||
let name = match attribute.contents.split_once('(') { | ||
Some((left, _right)) => left.to_string(), | ||
None => attribute.contents.to_string(), | ||
}; | ||
let (tokens, _) = Lexer::lex(&name); | ||
|
||
let parser = path_no_turbofish(); | ||
let Ok(path) = parser.parse(tokens) else { | ||
return; | ||
}; | ||
|
||
let resolver = StandardPathResolver::new(self.module_id); | ||
let mut usage_tracker = UsageTracker::default(); | ||
let Ok(result) = resolver.resolve(self.def_maps, path, &mut usage_tracker, &mut None) | ||
else { | ||
return; | ||
}; | ||
|
||
self.reference_id = Some(module_def_id_to_reference_id(result.module_def_id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters