diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-09 08:50:19 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-09 08:50:19 +0000 |
commit | 30062da6284052deac04c759540f81d5b326689c (patch) | |
tree | 12f56d863263283c42d20fd337957d3980e5481b /crates/ra_hir | |
parent | 0363c9495a6a07db276dce4c67fa35fbfc20153c (diff) | |
parent | 05e1c7b1972a87abe6d352b5d0cd8a58e2b7adc7 (diff) |
Merge #3516
3516: Handle visibility in more cases in completion r=matklad a=flodiebold
This means we don't show private items when completing paths or method calls.
We might want to show private items if we can edit their definition and provide a "make public" assist, but I feel like we'd need better sorting of completion items for that, so they can be not shown or sorted to the bottom by default. Until then, they're usually more of a distraction to me.
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 2944926e6..911c809fd 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -204,10 +204,26 @@ impl Module { | |||
204 | } | 204 | } |
205 | 205 | ||
206 | /// Returns a `ModuleScope`: a set of items, visible in this module. | 206 | /// Returns a `ModuleScope`: a set of items, visible in this module. |
207 | pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> { | 207 | pub fn scope( |
208 | self, | ||
209 | db: &impl HirDatabase, | ||
210 | visible_from: Option<Module>, | ||
211 | ) -> Vec<(Name, ScopeDef)> { | ||
208 | db.crate_def_map(self.id.krate)[self.id.local_id] | 212 | db.crate_def_map(self.id.krate)[self.id.local_id] |
209 | .scope | 213 | .scope |
210 | .entries() | 214 | .entries() |
215 | .filter_map(|(name, def)| { | ||
216 | if let Some(m) = visible_from { | ||
217 | let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id)); | ||
218 | if filtered.is_none() && !def.is_none() { | ||
219 | None | ||
220 | } else { | ||
221 | Some((name, filtered)) | ||
222 | } | ||
223 | } else { | ||
224 | Some((name, def)) | ||
225 | } | ||
226 | }) | ||
211 | .map(|(name, def)| (name.clone(), def.into())) | 227 | .map(|(name, def)| (name.clone(), def.into())) |
212 | .collect() | 228 | .collect() |
213 | } | 229 | } |
@@ -571,6 +587,14 @@ impl Function { | |||
571 | } | 587 | } |
572 | } | 588 | } |
573 | 589 | ||
590 | impl HasVisibility for Function { | ||
591 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
592 | let function_data = db.function_data(self.id); | ||
593 | let visibility = &function_data.visibility; | ||
594 | visibility.resolve(db, &self.id.resolver(db)) | ||
595 | } | ||
596 | } | ||
597 | |||
574 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 598 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
575 | pub struct Const { | 599 | pub struct Const { |
576 | pub(crate) id: ConstId, | 600 | pub(crate) id: ConstId, |
@@ -590,6 +614,14 @@ impl Const { | |||
590 | } | 614 | } |
591 | } | 615 | } |
592 | 616 | ||
617 | impl HasVisibility for Const { | ||
618 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
619 | let function_data = db.const_data(self.id); | ||
620 | let visibility = &function_data.visibility; | ||
621 | visibility.resolve(db, &self.id.resolver(db)) | ||
622 | } | ||
623 | } | ||
624 | |||
593 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 625 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
594 | pub struct Static { | 626 | pub struct Static { |
595 | pub(crate) id: StaticId, | 627 | pub(crate) id: StaticId, |
@@ -664,6 +696,14 @@ impl TypeAlias { | |||
664 | } | 696 | } |
665 | } | 697 | } |
666 | 698 | ||
699 | impl HasVisibility for TypeAlias { | ||
700 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
701 | let function_data = db.type_alias_data(self.id); | ||
702 | let visibility = &function_data.visibility; | ||
703 | visibility.resolve(db, &self.id.resolver(db)) | ||
704 | } | ||
705 | } | ||
706 | |||
667 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 707 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
668 | pub struct MacroDef { | 708 | pub struct MacroDef { |
669 | pub(crate) id: MacroDefId, | 709 | pub(crate) id: MacroDefId, |
@@ -751,6 +791,16 @@ impl AssocItem { | |||
751 | } | 791 | } |
752 | } | 792 | } |
753 | 793 | ||
794 | impl HasVisibility for AssocItem { | ||
795 | fn visibility(&self, db: &impl HirDatabase) -> Visibility { | ||
796 | match self { | ||
797 | AssocItem::Function(f) => f.visibility(db), | ||
798 | AssocItem::Const(c) => c.visibility(db), | ||
799 | AssocItem::TypeAlias(t) => t.visibility(db), | ||
800 | } | ||
801 | } | ||
802 | } | ||
803 | |||
754 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] | 804 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] |
755 | pub enum GenericDef { | 805 | pub enum GenericDef { |
756 | Function(Function), | 806 | Function(Function), |