mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Refactor
- don't take `&self` as receiver for `Copy` types - simplify `hir::Module::nearest_non_block_module()` - style changes for consistency
This commit is contained in:
parent
a02846343f
commit
56dd5368f5
5 changed files with 22 additions and 26 deletions
|
@ -3,12 +3,12 @@ mod block;
|
||||||
use base_db::{fixture::WithFixture, SourceDatabase};
|
use base_db::{fixture::WithFixture, SourceDatabase};
|
||||||
use expect_test::Expect;
|
use expect_test::Expect;
|
||||||
|
|
||||||
use crate::ModuleDefId;
|
use crate::{test_db::TestDB, ModuleDefId};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn lower(ra_fixture: &str) -> Arc<Body> {
|
fn lower(ra_fixture: &str) -> Arc<Body> {
|
||||||
let db = crate::test_db::TestDB::with_files(ra_fixture);
|
let db = TestDB::with_files(ra_fixture);
|
||||||
|
|
||||||
let krate = db.crate_graph().iter().next().unwrap();
|
let krate = db.crate_graph().iter().next().unwrap();
|
||||||
let def_map = db.crate_def_map(krate);
|
let def_map = db.crate_def_map(krate);
|
||||||
|
@ -25,15 +25,15 @@ fn lower(ra_fixture: &str) -> Arc<Body> {
|
||||||
db.body(fn_def.unwrap().into())
|
db.body(fn_def.unwrap().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block_def_map_at(ra_fixture: &str) -> String {
|
fn def_map_at(ra_fixture: &str) -> String {
|
||||||
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
|
let (db, position) = TestDB::with_position(ra_fixture);
|
||||||
|
|
||||||
let module = db.module_at_position(position);
|
let module = db.module_at_position(position);
|
||||||
module.def_map(&db).dump(&db)
|
module.def_map(&db).dump(&db)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
|
fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
|
||||||
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
|
let (db, position) = TestDB::with_position(ra_fixture);
|
||||||
|
|
||||||
let module = db.module_at_position(position);
|
let module = db.module_at_position(position);
|
||||||
let actual = module.def_map(&db).dump_block_scopes(&db);
|
let actual = module.def_map(&db).dump_block_scopes(&db);
|
||||||
|
@ -41,7 +41,7 @@ fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_at(ra_fixture: &str, expect: Expect) {
|
fn check_at(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = block_def_map_at(ra_fixture);
|
let actual = def_map_at(ra_fixture);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,24 +145,28 @@ pub struct ModuleId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleId {
|
impl ModuleId {
|
||||||
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
|
pub fn def_map(self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
|
||||||
match self.block {
|
match self.block {
|
||||||
Some(block) => db.block_def_map(block),
|
Some(block) => db.block_def_map(block),
|
||||||
None => db.crate_def_map(self.krate),
|
None => db.crate_def_map(self.krate),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn krate(&self) -> CrateId {
|
pub fn krate(self) -> CrateId {
|
||||||
self.krate
|
self.krate
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
|
pub fn containing_module(self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
|
||||||
self.def_map(db).containing_module(self.local_id)
|
self.def_map(db).containing_module(self.local_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn containing_block(&self) -> Option<BlockId> {
|
pub fn containing_block(self) -> Option<BlockId> {
|
||||||
self.block
|
self.block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_block_module(self) -> bool {
|
||||||
|
self.block.is_some() && self.local_id == DefMap::ROOT
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An ID of a module, **local** to a `DefMap`.
|
/// An ID of a module, **local** to a `DefMap`.
|
||||||
|
|
|
@ -808,11 +808,8 @@ impl DefCollector<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether all namespace is resolved
|
// Check whether all namespaces are resolved.
|
||||||
if def.take_types().is_some()
|
if def.is_full() {
|
||||||
&& def.take_values().is_some()
|
|
||||||
&& def.take_macros().is_some()
|
|
||||||
{
|
|
||||||
PartialResolvedImport::Resolved(def)
|
PartialResolvedImport::Resolved(def)
|
||||||
} else {
|
} else {
|
||||||
PartialResolvedImport::Indeterminate(def)
|
PartialResolvedImport::Indeterminate(def)
|
||||||
|
@ -821,7 +818,7 @@ impl DefCollector<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_extern_crate(&self, name: &Name) -> Option<CrateRootModuleId> {
|
fn resolve_extern_crate(&self, name: &Name) -> Option<CrateRootModuleId> {
|
||||||
if *name == name!(self) {
|
if *name == name![self] {
|
||||||
cov_mark::hit!(extern_crate_self_as);
|
cov_mark::hit!(extern_crate_self_as);
|
||||||
Some(self.def_map.crate_root())
|
Some(self.def_map.crate_root())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -192,7 +192,7 @@ impl DefMap {
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut segments = path.segments().iter().enumerate();
|
let mut segments = path.segments().iter().enumerate();
|
||||||
let mut curr_per_ns: PerNs = match path.kind {
|
let mut curr_per_ns = match path.kind {
|
||||||
PathKind::DollarCrate(krate) => {
|
PathKind::DollarCrate(krate) => {
|
||||||
if krate == self.krate {
|
if krate == self.krate {
|
||||||
cov_mark::hit!(macro_dollar_crate_self);
|
cov_mark::hit!(macro_dollar_crate_self);
|
||||||
|
|
|
@ -47,7 +47,7 @@ use hir_def::{
|
||||||
lang_item::LangItemTarget,
|
lang_item::LangItemTarget,
|
||||||
layout::{self, ReprOptions, TargetDataLayout},
|
layout::{self, ReprOptions, TargetDataLayout},
|
||||||
macro_id_to_def_id,
|
macro_id_to_def_id,
|
||||||
nameres::{self, diagnostics::DefDiagnostic, ModuleOrigin},
|
nameres::{self, diagnostics::DefDiagnostic},
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
resolver::{HasResolver, Resolver},
|
resolver::{HasResolver, Resolver},
|
||||||
src::HasSource as _,
|
src::HasSource as _,
|
||||||
|
@ -505,15 +505,10 @@ impl Module {
|
||||||
/// Finds nearest non-block ancestor `Module` (`self` included).
|
/// Finds nearest non-block ancestor `Module` (`self` included).
|
||||||
pub fn nearest_non_block_module(self, db: &dyn HirDatabase) -> Module {
|
pub fn nearest_non_block_module(self, db: &dyn HirDatabase) -> Module {
|
||||||
let mut id = self.id;
|
let mut id = self.id;
|
||||||
loop {
|
while id.is_block_module() {
|
||||||
let def_map = id.def_map(db.upcast());
|
id = id.containing_module(db.upcast()).expect("block without parent module");
|
||||||
let origin = def_map[id.local_id].origin;
|
|
||||||
if matches!(origin, ModuleOrigin::BlockExpr { .. }) {
|
|
||||||
id = id.containing_module(db.upcast()).expect("block without parent module")
|
|
||||||
} else {
|
|
||||||
return Module { id };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Module { id }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
|
pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue