Fix completion of trait items
Trait items should be public by default.
This commit is contained in:
parent
bf0c3ec67e
commit
0a9ea782eb
3 changed files with 80 additions and 11 deletions
|
@ -34,7 +34,8 @@ pub struct FunctionData {
|
|||
|
||||
impl FunctionData {
|
||||
pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> {
|
||||
let src = func.lookup(db).source(db);
|
||||
let loc = func.lookup(db);
|
||||
let src = loc.source(db);
|
||||
let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
|
||||
let mut params = Vec::new();
|
||||
let mut has_self_param = false;
|
||||
|
@ -76,7 +77,9 @@ impl FunctionData {
|
|||
ret_type
|
||||
};
|
||||
|
||||
let visibility = RawVisibility::from_ast(db, src.map(|s| s.visibility()));
|
||||
let vis_default = RawVisibility::default_for_container(loc.container);
|
||||
let visibility =
|
||||
RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility()));
|
||||
|
||||
let sig = FunctionData { name, params, ret_type, has_self_param, visibility };
|
||||
Arc::new(sig)
|
||||
|
@ -105,10 +108,13 @@ impl TypeAliasData {
|
|||
db: &impl DefDatabase,
|
||||
typ: TypeAliasId,
|
||||
) -> Arc<TypeAliasData> {
|
||||
let node = typ.lookup(db).source(db);
|
||||
let loc = typ.lookup(db);
|
||||
let node = loc.source(db);
|
||||
let name = node.value.name().map_or_else(Name::missing, |n| n.as_name());
|
||||
let type_ref = node.value.type_ref().map(TypeRef::from_ast);
|
||||
let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility()));
|
||||
let vis_default = RawVisibility::default_for_container(loc.container);
|
||||
let visibility =
|
||||
RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility()));
|
||||
Arc::new(TypeAliasData { name, type_ref, visibility })
|
||||
}
|
||||
}
|
||||
|
@ -230,22 +236,26 @@ pub struct ConstData {
|
|||
|
||||
impl ConstData {
|
||||
pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc<ConstData> {
|
||||
let node = konst.lookup(db).source(db);
|
||||
Arc::new(ConstData::new(db, node))
|
||||
let loc = konst.lookup(db);
|
||||
let node = loc.source(db);
|
||||
let vis_default = RawVisibility::default_for_container(loc.container);
|
||||
Arc::new(ConstData::new(db, vis_default, node))
|
||||
}
|
||||
|
||||
pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> {
|
||||
let node = konst.lookup(db).source(db);
|
||||
Arc::new(ConstData::new(db, node))
|
||||
Arc::new(ConstData::new(db, RawVisibility::private(), node))
|
||||
}
|
||||
|
||||
fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>(
|
||||
db: &impl DefDatabase,
|
||||
vis_default: RawVisibility,
|
||||
node: InFile<N>,
|
||||
) -> ConstData {
|
||||
let name = node.value.name().map(|n| n.as_name());
|
||||
let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type());
|
||||
let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility()));
|
||||
let visibility =
|
||||
RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility()));
|
||||
ConstData { name, type_ref, visibility }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use ra_syntax::ast;
|
|||
use crate::{
|
||||
db::DefDatabase,
|
||||
path::{ModPath, PathKind},
|
||||
ModuleId,
|
||||
AssocContainerId, ModuleId,
|
||||
};
|
||||
|
||||
/// Visibility of an item, not yet resolved.
|
||||
|
@ -20,11 +20,30 @@ pub enum RawVisibility {
|
|||
}
|
||||
|
||||
impl RawVisibility {
|
||||
const fn private() -> RawVisibility {
|
||||
pub(crate) const fn private() -> RawVisibility {
|
||||
let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() };
|
||||
RawVisibility::Module(path)
|
||||
}
|
||||
|
||||
pub(crate) fn default_for_container(container_id: AssocContainerId) -> Self {
|
||||
match container_id {
|
||||
AssocContainerId::TraitId(_) => RawVisibility::Public,
|
||||
_ => RawVisibility::private(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_ast_with_default(
|
||||
db: &impl DefDatabase,
|
||||
default: RawVisibility,
|
||||
node: InFile<Option<ast::Visibility>>,
|
||||
) -> RawVisibility {
|
||||
Self::from_ast_with_hygiene_and_default(
|
||||
node.value,
|
||||
default,
|
||||
&Hygiene::new(db, node.file_id),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn from_ast(
|
||||
db: &impl DefDatabase,
|
||||
node: InFile<Option<ast::Visibility>>,
|
||||
|
@ -35,9 +54,17 @@ impl RawVisibility {
|
|||
pub(crate) fn from_ast_with_hygiene(
|
||||
node: Option<ast::Visibility>,
|
||||
hygiene: &Hygiene,
|
||||
) -> RawVisibility {
|
||||
Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene)
|
||||
}
|
||||
|
||||
pub(crate) fn from_ast_with_hygiene_and_default(
|
||||
node: Option<ast::Visibility>,
|
||||
default: RawVisibility,
|
||||
hygiene: &Hygiene,
|
||||
) -> RawVisibility {
|
||||
let node = match node {
|
||||
None => return RawVisibility::private(),
|
||||
None => return default,
|
||||
Some(node) => node,
|
||||
};
|
||||
match node.kind() {
|
||||
|
|
|
@ -401,6 +401,38 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_trait_method_from_other_module() {
|
||||
assert_debug_snapshot!(
|
||||
do_ref_completion(
|
||||
r"
|
||||
struct A {}
|
||||
mod m {
|
||||
pub trait Trait { fn the_method(&self); }
|
||||
}
|
||||
use m::Trait;
|
||||
impl Trait for A {}
|
||||
fn foo(a: A) {
|
||||
a.<|>
|
||||
}
|
||||
",
|
||||
),
|
||||
@r###"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "the_method()",
|
||||
source_range: [219; 219),
|
||||
delete: [219; 219),
|
||||
insert: "the_method()$0",
|
||||
kind: Method,
|
||||
lookup: "the_method",
|
||||
detail: "fn the_method(&self)",
|
||||
},
|
||||
]
|
||||
"###
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_non_self_method() {
|
||||
assert_debug_snapshot!(
|
||||
|
|
Loading…
Add table
Reference in a new issue