10555: minor: Hide private methods in `generate_delegate_methods` r=lnicola a=lnicola

Fixes #10553

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2021-10-16 10:17:15 +00:00 committed by GitHub
commit c4e88e950f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
use hir::{self, HasCrate, HasSource}; use hir::{self, HasCrate, HasSource, HasVisibility};
use syntax::ast::{self, make, AstNode, HasGenericParams, HasName, HasVisibility}; use syntax::ast::{self, make, AstNode, HasGenericParams, HasName, HasVisibility as _};
use crate::{ use crate::{
utils::{convert_param_list_to_arg_list, find_struct_impl, render_snippet, Cursor}, utils::{convert_param_list_to_arg_list, find_struct_impl, render_snippet, Cursor},
@ -45,6 +45,7 @@ use syntax::ast::edit::AstNodeEdit;
pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let strukt = ctx.find_node_at_offset::<ast::Struct>()?; let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
let strukt_name = strukt.name()?; let strukt_name = strukt.name()?;
let current_module = ctx.sema.scope(strukt.syntax()).module()?;
let (field_name, field_ty) = match ctx.find_node_at_offset::<ast::RecordField>() { let (field_name, field_ty) = match ctx.find_node_at_offset::<ast::RecordField>() {
Some(field) => { Some(field) => {
@ -66,7 +67,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext)
let mut methods = vec![]; let mut methods = vec![];
sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| { sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| {
if let hir::AssocItem::Function(f) = item { if let hir::AssocItem::Function(f) = item {
if f.self_param(ctx.db()).is_some() { if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
methods.push(f) methods.push(f)
} }
} }
@ -170,7 +171,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext)
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::check_assist; use crate::tests::{check_assist, check_assist_not_applicable};
use super::*; use super::*;
@ -311,4 +312,24 @@ impl<T> Person<T> {
}"#, }"#,
); );
} }
#[test]
fn test_generate_delegate_visibility() {
check_assist_not_applicable(
generate_delegate_methods,
r#"
mod m {
pub struct Age(u8);
impl Age {
fn age(&self) -> u8 {
self.0
}
}
}
struct Person {
ag$0e: m::Age,
}"#,
)
}
} }