mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Refactor
This commit is contained in:
parent
a4966c9282
commit
a0a7860141
2 changed files with 78 additions and 75 deletions
|
@ -289,80 +289,84 @@ impl DefCollector<'_> {
|
||||||
let module_id = self.def_map.root;
|
let module_id = self.def_map.root;
|
||||||
|
|
||||||
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
|
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
|
||||||
if attrs.cfg().map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) {
|
if let Some(cfg) = attrs.cfg() {
|
||||||
self.inject_prelude(&attrs);
|
if self.cfg_options.check(&cfg) == Some(false) {
|
||||||
|
return;
|
||||||
// Process other crate-level attributes.
|
|
||||||
for attr in &*attrs {
|
|
||||||
let attr_name = match attr.path.as_ident() {
|
|
||||||
Some(name) => name,
|
|
||||||
None => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
if *attr_name == hir_expand::name![recursion_limit] {
|
|
||||||
if let Some(limit) = attr.string_value() {
|
|
||||||
if let Ok(limit) = limit.parse() {
|
|
||||||
self.def_map.recursion_limit = Some(limit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if *attr_name == hir_expand::name![crate_type] {
|
|
||||||
if let Some("proc-macro") = attr.string_value().map(SmolStr::as_str) {
|
|
||||||
self.is_proc_macro = true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
|
|
||||||
self.def_map.rustc_coherence_is_core = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if *attr_name == hir_expand::name![feature] {
|
|
||||||
let hygiene = &Hygiene::new_unhygienic();
|
|
||||||
let features = attr
|
|
||||||
.parse_path_comma_token_tree(self.db.upcast(), hygiene)
|
|
||||||
.into_iter()
|
|
||||||
.flatten()
|
|
||||||
.filter_map(|feat| match feat.segments() {
|
|
||||||
[name] => Some(name.to_smol_str()),
|
|
||||||
_ => None,
|
|
||||||
});
|
|
||||||
self.def_map.unstable_features.extend(features);
|
|
||||||
}
|
|
||||||
|
|
||||||
let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
|
|
||||||
|| *attr_name == hir_expand::name![register_tool];
|
|
||||||
if !attr_is_register_like {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let registered_name = match attr.single_ident_value() {
|
|
||||||
Some(ident) => ident.as_name(),
|
|
||||||
_ => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
if *attr_name == hir_expand::name![register_attr] {
|
|
||||||
self.def_map.registered_attrs.push(registered_name.to_smol_str());
|
|
||||||
cov_mark::hit!(register_attr);
|
|
||||||
} else {
|
|
||||||
self.def_map.registered_tools.push(registered_name.to_smol_str());
|
|
||||||
cov_mark::hit!(register_tool);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ModCollector {
|
|
||||||
def_collector: self,
|
|
||||||
macro_depth: 0,
|
|
||||||
module_id,
|
|
||||||
tree_id: TreeId::new(file_id.into(), None),
|
|
||||||
item_tree: &item_tree,
|
|
||||||
mod_dir: ModDir::root(),
|
|
||||||
}
|
|
||||||
.collect_in_top_module(item_tree.top_level_items());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.inject_prelude(&attrs);
|
||||||
|
|
||||||
|
// Process other crate-level attributes.
|
||||||
|
for attr in &*attrs {
|
||||||
|
let attr_name = match attr.path.as_ident() {
|
||||||
|
Some(name) => name,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
if *attr_name == hir_expand::name![recursion_limit] {
|
||||||
|
if let Some(limit) = attr.string_value() {
|
||||||
|
if let Ok(limit) = limit.parse() {
|
||||||
|
self.def_map.recursion_limit = Some(limit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if *attr_name == hir_expand::name![crate_type] {
|
||||||
|
if let Some("proc-macro") = attr.string_value().map(SmolStr::as_str) {
|
||||||
|
self.is_proc_macro = true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
|
||||||
|
self.def_map.rustc_coherence_is_core = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if *attr_name == hir_expand::name![feature] {
|
||||||
|
let hygiene = &Hygiene::new_unhygienic();
|
||||||
|
let features = attr
|
||||||
|
.parse_path_comma_token_tree(self.db.upcast(), hygiene)
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.filter_map(|feat| match feat.segments() {
|
||||||
|
[name] => Some(name.to_smol_str()),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
self.def_map.unstable_features.extend(features);
|
||||||
|
}
|
||||||
|
|
||||||
|
let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
|
||||||
|
|| *attr_name == hir_expand::name![register_tool];
|
||||||
|
if !attr_is_register_like {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let registered_name = match attr.single_ident_value() {
|
||||||
|
Some(ident) => ident.as_name(),
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
if *attr_name == hir_expand::name![register_attr] {
|
||||||
|
self.def_map.registered_attrs.push(registered_name.to_smol_str());
|
||||||
|
cov_mark::hit!(register_attr);
|
||||||
|
} else {
|
||||||
|
self.def_map.registered_tools.push(registered_name.to_smol_str());
|
||||||
|
cov_mark::hit!(register_tool);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ModCollector {
|
||||||
|
def_collector: self,
|
||||||
|
macro_depth: 0,
|
||||||
|
module_id,
|
||||||
|
tree_id: TreeId::new(file_id.into(), None),
|
||||||
|
item_tree: &item_tree,
|
||||||
|
mod_dir: ModDir::root(),
|
||||||
|
}
|
||||||
|
.collect_in_top_module(item_tree.top_level_items());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn seed_with_inner(&mut self, tree_id: TreeId) {
|
fn seed_with_inner(&mut self, tree_id: TreeId) {
|
||||||
|
|
|
@ -4,7 +4,6 @@ use hir_def::{
|
||||||
attr::{AttrsWithOwner, Documentation},
|
attr::{AttrsWithOwner, Documentation},
|
||||||
item_scope::ItemInNs,
|
item_scope::ItemInNs,
|
||||||
path::ModPath,
|
path::ModPath,
|
||||||
per_ns::PerNs,
|
|
||||||
resolver::HasResolver,
|
resolver::HasResolver,
|
||||||
AttrDefId, GenericParamId, ModuleDefId,
|
AttrDefId, GenericParamId, ModuleDefId,
|
||||||
};
|
};
|
||||||
|
@ -155,14 +154,14 @@ fn resolve_doc_path(
|
||||||
.syntax_node()
|
.syntax_node()
|
||||||
.descendants()
|
.descendants()
|
||||||
.find_map(ast::Path::cast)?;
|
.find_map(ast::Path::cast)?;
|
||||||
if ast_path.to_string() != link {
|
if ast_path.syntax().text() != link {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
ModPath::from_src(db.upcast(), ast_path, &Hygiene::new_unhygienic())?
|
ModPath::from_src(db.upcast(), ast_path, &Hygiene::new_unhygienic())?
|
||||||
};
|
};
|
||||||
|
|
||||||
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
|
||||||
let resolved = if resolved == PerNs::none() {
|
let resolved = if resolved.is_none() {
|
||||||
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
|
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
|
||||||
} else {
|
} else {
|
||||||
resolved
|
resolved
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue