Resolve invisible defs in fix_visibility assist

This commit is contained in:
Ryo Yoshida 2022-11-11 19:56:47 +09:00
parent dea49d0826
commit e75afebeb2
No known key found for this signature in database
GPG key ID: E25698A930586171
2 changed files with 14 additions and 10 deletions

View file

@ -1,4 +1,4 @@
use hir::{db::HirDatabase, HasSource, HasVisibility, PathResolution};
use hir::{db::HirDatabase, HasSource, HasVisibility, ModuleDef, PathResolution, ScopeDef};
use ide_db::base_db::FileId;
use syntax::{
ast::{self, HasVisibility as _},
@ -18,7 +18,7 @@ use crate::{utils::vis_offset, AssistContext, AssistId, AssistKind, Assists};
// fn frobnicate() {}
// }
// fn main() {
// m::frobnicate$0() {}
// m::frobnicate$0();
// }
// ```
// ->
@ -27,7 +27,7 @@ use crate::{utils::vis_offset, AssistContext, AssistId, AssistKind, Assists};
// $0pub(crate) fn frobnicate() {}
// }
// fn main() {
// m::frobnicate() {}
// m::frobnicate();
// }
// ```
pub(crate) fn fix_visibility(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
@ -37,11 +37,15 @@ pub(crate) fn fix_visibility(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let path: ast::Path = ctx.find_node_at_offset()?;
let path_res = ctx.sema.resolve_path(&path)?;
let def = match path_res {
PathResolution::Def(def) => def,
_ => return None,
};
let qualifier = path.qualifier()?;
let name_ref = path.segment()?.name_ref()?;
let qualifier_res = ctx.sema.resolve_path(&qualifier)?;
let PathResolution::Def(ModuleDef::Module(module)) = qualifier_res else { return None; };
let (_, def) = module
.scope(ctx.db(), None)
.into_iter()
.find(|(name, _)| name.to_smol_str() == name_ref.text().as_str())?;
let ScopeDef::ModuleDef(def) = def else { return None; };
let current_module = ctx.sema.scope(path.syntax())?.module();
let target_module = def.module(ctx.db())?;

View file

@ -741,7 +741,7 @@ mod m {
fn frobnicate() {}
}
fn main() {
m::frobnicate$0() {}
m::frobnicate$0();
}
"#####,
r#####"
@ -749,7 +749,7 @@ mod m {
$0pub(crate) fn frobnicate() {}
}
fn main() {
m::frobnicate() {}
m::frobnicate();
}
"#####,
)