Skip to content

Commit f7746cf

Browse files
Don't complete doc(hidden) enum variants and use trees
Also refactor the check a bit.
1 parent d6bf619 commit f7746cf

File tree

6 files changed

+102
-55
lines changed

6 files changed

+102
-55
lines changed

Diff for: crates/ide-completion/src/completions.rs

+39-49
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ impl Completions {
188188
resolution: hir::ScopeDef,
189189
doc_aliases: Vec<syntax::SmolStr>,
190190
) {
191-
if !ctx.check_stability(resolution.attrs(ctx.db).as_deref()) {
192-
return;
193-
}
194191
let is_private_editable = match ctx.def_is_visible(&resolution) {
195192
Visible::Yes => false,
196193
Visible::Editable => true,
@@ -216,9 +213,6 @@ impl Completions {
216213
local_name: hir::Name,
217214
resolution: hir::ScopeDef,
218215
) {
219-
if !ctx.check_stability(resolution.attrs(ctx.db).as_deref()) {
220-
return;
221-
}
222216
let is_private_editable = match ctx.def_is_visible(&resolution) {
223217
Visible::Yes => false,
224218
Visible::Editable => true,
@@ -241,7 +235,7 @@ impl Completions {
241235
path_ctx: &PathCompletionCtx,
242236
e: hir::Enum,
243237
) {
244-
if !ctx.check_stability(Some(&e.attrs(ctx.db))) {
238+
if !ctx.check_stability_and_hidden(e) {
245239
return;
246240
}
247241
e.variants(ctx.db)
@@ -257,9 +251,6 @@ impl Completions {
257251
local_name: hir::Name,
258252
doc_aliases: Vec<syntax::SmolStr>,
259253
) {
260-
if !ctx.check_stability(Some(&module.attrs(ctx.db))) {
261-
return;
262-
}
263254
self.add_path_resolution(
264255
ctx,
265256
path_ctx,
@@ -276,9 +267,6 @@ impl Completions {
276267
mac: hir::Macro,
277268
local_name: hir::Name,
278269
) {
279-
if !ctx.check_stability(Some(&mac.attrs(ctx.db))) {
280-
return;
281-
}
282270
let is_private_editable = match ctx.is_visible(&mac) {
283271
Visible::Yes => false,
284272
Visible::Editable => true,
@@ -302,9 +290,6 @@ impl Completions {
302290
func: hir::Function,
303291
local_name: Option<hir::Name>,
304292
) {
305-
if !ctx.check_stability(Some(&func.attrs(ctx.db))) {
306-
return;
307-
}
308293
let is_private_editable = match ctx.is_visible(&func) {
309294
Visible::Yes => false,
310295
Visible::Editable => true,
@@ -332,9 +317,6 @@ impl Completions {
332317
receiver: Option<SmolStr>,
333318
local_name: Option<hir::Name>,
334319
) {
335-
if !ctx.check_stability(Some(&func.attrs(ctx.db))) {
336-
return;
337-
}
338320
let is_private_editable = match ctx.is_visible(&func) {
339321
Visible::Yes => false,
340322
Visible::Editable => true,
@@ -362,9 +344,6 @@ impl Completions {
362344
func: hir::Function,
363345
import: LocatedImport,
364346
) {
365-
if !ctx.check_stability(Some(&func.attrs(ctx.db))) {
366-
return;
367-
}
368347
let is_private_editable = match ctx.is_visible(&func) {
369348
Visible::Yes => false,
370349
Visible::Editable => true,
@@ -387,9 +366,6 @@ impl Completions {
387366
}
388367

389368
pub(crate) fn add_const(&mut self, ctx: &CompletionContext<'_>, konst: hir::Const) {
390-
if !ctx.check_stability(Some(&konst.attrs(ctx.db))) {
391-
return;
392-
}
393369
let is_private_editable = match ctx.is_visible(&konst) {
394370
Visible::Yes => false,
395371
Visible::Editable => true,
@@ -406,9 +382,6 @@ impl Completions {
406382
ctx: &CompletionContext<'_>,
407383
type_alias: hir::TypeAlias,
408384
) {
409-
if !ctx.check_stability(Some(&type_alias.attrs(ctx.db))) {
410-
return;
411-
}
412385
let is_private_editable = match ctx.is_visible(&type_alias) {
413386
Visible::Yes => false,
414387
Visible::Editable => true,
@@ -438,7 +411,7 @@ impl Completions {
438411
variant: hir::Variant,
439412
path: hir::ModPath,
440413
) {
441-
if !ctx.check_stability(Some(&variant.attrs(ctx.db))) {
414+
if !ctx.check_stability_and_hidden(variant) {
442415
return;
443416
}
444417
if let Some(builder) =
@@ -455,7 +428,7 @@ impl Completions {
455428
variant: hir::Variant,
456429
local_name: Option<hir::Name>,
457430
) {
458-
if !ctx.check_stability(Some(&variant.attrs(ctx.db))) {
431+
if !ctx.check_stability_and_hidden(variant) {
459432
return;
460433
}
461434
if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
@@ -479,9 +452,6 @@ impl Completions {
479452
field: hir::Field,
480453
ty: &hir::Type,
481454
) {
482-
if !ctx.check_stability(Some(&field.attrs(ctx.db))) {
483-
return;
484-
}
485455
let is_private_editable = match ctx.is_visible(&field) {
486456
Visible::Yes => false,
487457
Visible::Editable => true,
@@ -506,12 +476,18 @@ impl Completions {
506476
path: Option<hir::ModPath>,
507477
local_name: Option<hir::Name>,
508478
) {
509-
if !ctx.check_stability(Some(&strukt.attrs(ctx.db))) {
510-
return;
511-
}
512-
if let Some(builder) =
513-
render_struct_literal(RenderContext::new(ctx), path_ctx, strukt, path, local_name)
514-
{
479+
let is_private_editable = match ctx.is_visible(&strukt) {
480+
Visible::Yes => false,
481+
Visible::Editable => true,
482+
Visible::No => return,
483+
};
484+
if let Some(builder) = render_struct_literal(
485+
RenderContext::new(ctx).private_editable(is_private_editable),
486+
path_ctx,
487+
strukt,
488+
path,
489+
local_name,
490+
) {
515491
self.add(builder.build(ctx.db));
516492
}
517493
}
@@ -523,10 +499,17 @@ impl Completions {
523499
path: Option<hir::ModPath>,
524500
local_name: Option<hir::Name>,
525501
) {
526-
if !ctx.check_stability(Some(&un.attrs(ctx.db))) {
527-
return;
528-
}
529-
let item = render_union_literal(RenderContext::new(ctx), un, path, local_name);
502+
let is_private_editable = match ctx.is_visible(&un) {
503+
Visible::Yes => false,
504+
Visible::Editable => true,
505+
Visible::No => return,
506+
};
507+
let item = render_union_literal(
508+
RenderContext::new(ctx).private_editable(is_private_editable),
509+
un,
510+
path,
511+
local_name,
512+
);
530513
self.add_opt(item);
531514
}
532515

@@ -571,7 +554,7 @@ impl Completions {
571554
variant: hir::Variant,
572555
local_name: Option<hir::Name>,
573556
) {
574-
if !ctx.check_stability(Some(&variant.attrs(ctx.db))) {
557+
if !ctx.check_stability_and_hidden(variant) {
575558
return;
576559
}
577560
self.add_opt(render_variant_pat(
@@ -591,7 +574,7 @@ impl Completions {
591574
variant: hir::Variant,
592575
path: hir::ModPath,
593576
) {
594-
if !ctx.check_stability(Some(&variant.attrs(ctx.db))) {
577+
if !ctx.check_stability_and_hidden(variant) {
595578
return;
596579
}
597580
let path = Some(&path);
@@ -612,10 +595,17 @@ impl Completions {
612595
strukt: hir::Struct,
613596
local_name: Option<hir::Name>,
614597
) {
615-
if !ctx.check_stability(Some(&strukt.attrs(ctx.db))) {
616-
return;
617-
}
618-
self.add_opt(render_struct_pat(RenderContext::new(ctx), pattern_ctx, strukt, local_name));
598+
let is_private_editable = match ctx.is_visible(&strukt) {
599+
Visible::Yes => false,
600+
Visible::Editable => true,
601+
Visible::No => return,
602+
};
603+
self.add_opt(render_struct_pat(
604+
RenderContext::new(ctx).private_editable(is_private_editable),
605+
pattern_ctx,
606+
strukt,
607+
local_name,
608+
));
619609
}
620610

621611
pub(crate) fn suggest_name(&mut self, ctx: &CompletionContext<'_>, name: &str) {

Diff for: crates/ide-completion/src/completions/item_list/trait_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! }
3232
//! ```
3333
34-
use hir::{db::ExpandDatabase, HasAttrs, MacroFileId, Name};
34+
use hir::{db::ExpandDatabase, MacroFileId, Name};
3535
use ide_db::text_edit::TextEdit;
3636
use ide_db::{
3737
documentation::HasDocs, path_transform::PathTransform,
@@ -155,7 +155,7 @@ fn complete_trait_impl(
155155
if let Some(hir_impl) = ctx.sema.to_def(impl_def) {
156156
get_missing_assoc_items(&ctx.sema, impl_def)
157157
.into_iter()
158-
.filter(|item| ctx.check_stability(Some(&item.attrs(ctx.db))))
158+
.filter(|item| ctx.check_stability_and_hidden(*item))
159159
.for_each(|item| {
160160
use self::ImplCompletionKind::*;
161161
match (item, kind) {

Diff for: crates/ide-completion/src/completions/use_.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ pub(crate) fn complete_use_path(
5252
)
5353
};
5454
for (name, def) in module_scope {
55-
if !ctx.check_stability(def.attrs(ctx.db).as_deref()) {
56-
continue;
55+
if let (Some(attrs), Some(defining_crate)) =
56+
(def.attrs(ctx.db), def.krate(ctx.db))
57+
{
58+
if !ctx.check_stability(Some(&attrs))
59+
|| ctx.is_doc_hidden(&attrs, defining_crate)
60+
{
61+
continue;
62+
}
5763
}
5864
let is_name_already_imported =
5965
already_imported_names.contains(name.as_str());

Diff for: crates/ide-completion/src/context.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl CompletionContext<'_> {
534534
}
535535
}
536536

537-
/// Checks if an item is visible and not `doc(hidden)` at the completion site.
537+
/// Checks if an item is visible, not `doc(hidden)` and stable at the completion site.
538538
pub(crate) fn is_visible<I>(&self, item: &I) -> Visible
539539
where
540540
I: hir::HasVisibility + hir::HasAttrs + hir::HasCrate + Copy,
@@ -570,6 +570,15 @@ impl CompletionContext<'_> {
570570
!attrs.is_unstable() || self.is_nightly
571571
}
572572

573+
pub(crate) fn check_stability_and_hidden<I>(&self, item: I) -> bool
574+
where
575+
I: hir::HasAttrs + hir::HasCrate,
576+
{
577+
let defining_crate = item.krate(self.db);
578+
let attrs = item.attrs(self.db);
579+
self.check_stability(Some(&attrs)) && !self.is_doc_hidden(&attrs, defining_crate)
580+
}
581+
573582
/// Whether the given trait is an operator trait or not.
574583
pub(crate) fn is_ops_trait(&self, trait_: hir::Trait) -> bool {
575584
match trait_.attrs(self.db).lang() {
@@ -647,6 +656,10 @@ impl CompletionContext<'_> {
647656
attrs: &hir::Attrs,
648657
defining_crate: hir::Crate,
649658
) -> Visible {
659+
if !self.check_stability(Some(attrs)) {
660+
return Visible::No;
661+
}
662+
650663
if !vis.is_visible_from(self.db, self.module.into()) {
651664
if !self.config.enable_private_editable {
652665
return Visible::No;
@@ -666,7 +679,7 @@ impl CompletionContext<'_> {
666679
}
667680
}
668681

669-
fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool {
682+
pub(crate) fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool {
670683
// `doc(hidden)` items are only completed within the defining crate.
671684
self.krate != defining_crate && attrs.has_doc_hidden()
672685
}

Diff for: crates/ide-completion/src/tests/expression.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1965,3 +1965,24 @@ fn bar() {
19651965
"#]],
19661966
);
19671967
}
1968+
1969+
#[test]
1970+
fn doc_hidden_enum_variant() {
1971+
check(
1972+
r#"
1973+
//- /foo.rs crate:foo
1974+
pub enum Enum {
1975+
#[doc(hidden)] Hidden,
1976+
Visible,
1977+
}
1978+
1979+
//- /lib.rs crate:lib deps:foo
1980+
fn foo() {
1981+
let _ = foo::Enum::$0;
1982+
}
1983+
"#,
1984+
expect![[r#"
1985+
ev Visible Visible
1986+
"#]],
1987+
);
1988+
}

Diff for: crates/ide-completion/src/tests/use_tree.rs

+17
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,20 @@ marco_rules! m { () => {} }
451451
"#]],
452452
);
453453
}
454+
455+
#[test]
456+
fn use_tree_doc_hidden() {
457+
check(
458+
r#"
459+
//- /foo.rs crate:foo
460+
#[doc(hidden)] pub struct Hidden;
461+
pub struct Visible;
462+
463+
//- /lib.rs crate:lib deps:foo
464+
use foo::$0;
465+
"#,
466+
expect![[r#"
467+
st Visible Visible
468+
"#]],
469+
);
470+
}

0 commit comments

Comments
 (0)