Skip to content

Commit 5c8f00f

Browse files
committed
Auto merge of rust-lang#13805 - ntBre:master, r=jonas-schievink
Complete enum variants without parens when snippets are disabled This handles the portion of rust-lang#13767 that bothered me, but I can try to work on the other parts we discussed if needed.
2 parents 9dfb9df + 694ae77 commit 5c8f00f

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

crates/ide-completion/src/completions/record.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ fn complete_fields(
124124

125125
#[cfg(test)]
126126
mod tests {
127-
use crate::tests::check_edit;
127+
use ide_db::SnippetCap;
128+
129+
use crate::{
130+
tests::{check_edit, check_edit_with_config, TEST_CONFIG},
131+
CompletionConfig,
132+
};
128133

129134
#[test]
130135
fn literal_struct_completion_edit() {
@@ -151,6 +156,37 @@ fn baz() {
151156
)
152157
}
153158

159+
#[test]
160+
fn enum_variant_no_snippets() {
161+
let conf = CompletionConfig { snippet_cap: SnippetCap::new(false), ..TEST_CONFIG };
162+
check_edit_with_config(
163+
conf,
164+
"Variant()",
165+
r#"
166+
enum Enum {
167+
Variant(usize),
168+
}
169+
170+
impl Enum {
171+
fn new(u: usize) -> Self {
172+
Self::Va$0
173+
}
174+
}
175+
"#,
176+
r#"
177+
enum Enum {
178+
Variant(usize),
179+
}
180+
181+
impl Enum {
182+
fn new(u: usize) -> Self {
183+
Self::Variant
184+
}
185+
}
186+
"#,
187+
)
188+
}
189+
154190
#[test]
155191
fn literal_struct_impl_self_completion() {
156192
check_edit(

crates/ide-completion/src/render/literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn render(
9696
if !should_add_parens {
9797
kind = StructKind::Unit;
9898
}
99-
let label = format_literal_label(&qualified_name, kind);
99+
let label = format_literal_label(&qualified_name, kind, snippet_cap);
100100
let lookup = if qualified {
101101
format_literal_lookup(&short_qualified_name.to_string(), kind)
102102
} else {

crates/ide-completion/src/render/pattern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) fn render_struct_pat(
3333
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
3434
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
3535
let kind = strukt.kind(ctx.db());
36-
let label = format_literal_label(name.as_str(), kind);
36+
let label = format_literal_label(name.as_str(), kind, ctx.snippet_cap());
3737
let lookup = format_literal_lookup(name.as_str(), kind);
3838
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
3939

@@ -67,7 +67,7 @@ pub(crate) fn render_variant_pat(
6767
}
6868
_ => {
6969
let kind = variant.kind(ctx.db());
70-
let label = format_literal_label(name.as_str(), kind);
70+
let label = format_literal_label(name.as_str(), kind, ctx.snippet_cap());
7171
let lookup = format_literal_lookup(name.as_str(), kind);
7272
let pat = render_pat(
7373
&ctx,

crates/ide-completion/src/render/union_literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn render_union_literal(
2424
Some(p) => (p.unescaped().to_string(), p.to_string()),
2525
None => (name.unescaped().to_string(), name.to_string()),
2626
};
27-
let label = format_literal_label(&name.to_smol_str(), StructKind::Record);
27+
let label = format_literal_label(&name.to_smol_str(), StructKind::Record, ctx.snippet_cap());
2828
let lookup = format_literal_lookup(&name.to_smol_str(), StructKind::Record);
2929
let mut item = CompletionItem::new(
3030
CompletionItemKind::SymbolKind(SymbolKind::Union),

crates/ide-completion/src/render/variant.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub(crate) fn render_tuple_lit(
4848
fields: &[hir::Field],
4949
path: &str,
5050
) -> RenderedLiteral {
51+
if snippet_cap.is_none() {
52+
return RenderedLiteral { literal: format!("{}", path), detail: format!("{}", path) };
53+
}
5154
let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
5255
if snippet_cap.is_some() {
5356
f(&format_args!("${{{}:()}}", idx + 1))
@@ -87,7 +90,14 @@ pub(crate) fn visible_fields(
8790
}
8891

8992
/// Format a struct, etc. literal option for display in the completions menu.
90-
pub(crate) fn format_literal_label(name: &str, kind: StructKind) -> SmolStr {
93+
pub(crate) fn format_literal_label(
94+
name: &str,
95+
kind: StructKind,
96+
snippet_cap: Option<SnippetCap>,
97+
) -> SmolStr {
98+
if snippet_cap.is_none() {
99+
return name.into();
100+
}
91101
match kind {
92102
StructKind::Tuple => SmolStr::from_iter([name, "(…)"]),
93103
StructKind::Record => SmolStr::from_iter([name, " {…}"]),

0 commit comments

Comments
 (0)