Skip to content

Commit 4fe5f03

Browse files
committed
Rename compound things to variant things
1 parent 46008d4 commit 4fe5f03

File tree

10 files changed

+101
-36
lines changed

10 files changed

+101
-36
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,17 @@ pub(crate) fn inlay_hints(
6969

7070
let mut hints = Vec::new();
7171

72-
if let Some(range_limit) = range_limit {
73-
let range_limit = range_limit.range;
74-
match file.covering_element(range_limit) {
72+
let get_hints = |node| get_hints(&mut hints, &sema, config, node);
73+
match range_limit {
74+
Some(FileRange { range, .. }) => match file.covering_element(range) {
7575
NodeOrToken::Token(_) => return hints,
76-
NodeOrToken::Node(n) => {
77-
for node in n
78-
.descendants()
79-
.filter(|descendant| range_limit.contains_range(descendant.text_range()))
80-
{
81-
get_hints(&mut hints, &sema, config, node);
82-
}
83-
}
84-
}
85-
} else {
86-
for node in file.descendants() {
87-
get_hints(&mut hints, &sema, config, node);
88-
}
89-
}
76+
NodeOrToken::Node(n) => n
77+
.descendants()
78+
.filter(|descendant| range.contains_range(descendant.text_range()))
79+
.for_each(get_hints),
80+
},
81+
None => file.descendants().for_each(get_hints),
82+
};
9083

9184
hints
9285
}

crates/ide_completion/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(super) enum PathKind {
5858

5959
#[derive(Debug)]
6060
pub(crate) struct PathCompletionCtx {
61-
/// If this is a call with () already there
61+
/// If this is a call with () already there (or {} in case of record patterns)
6262
pub(super) has_call_parens: bool,
6363
/// Whether this path stars with a `::`.
6464
pub(super) is_absolute_path: bool,
@@ -890,6 +890,7 @@ impl<'a> CompletionContext<'a> {
890890
Some(PathKind::Pat)
891891
},
892892
ast::RecordPat(it) => {
893+
path_ctx.has_call_parens = true;
893894
pat_ctx = Some(pattern_context_for(original_file, it.into()));
894895
Some(PathKind::Pat)
895896
},

crates/ide_completion/src/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) mod const_;
88
pub(crate) mod pattern;
99
pub(crate) mod type_alias;
1010
pub(crate) mod struct_literal;
11-
pub(crate) mod compound;
11+
pub(crate) mod variant;
1212
pub(crate) mod union_literal;
1313

1414
use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef};

crates/ide_completion/src/render/enum_variant.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use syntax::SmolStr;
77
use crate::{
88
item::{CompletionItem, ImportEdit},
99
render::{
10-
compound::{format_literal_label, render_record, render_tuple, RenderedCompound},
11-
compute_ref_match, compute_type_match, RenderContext,
10+
compute_ref_match, compute_type_match,
11+
variant::{format_literal_label, render_record, render_tuple, RenderedLiteral},
12+
RenderContext,
1213
},
1314
CompletionRelevance,
1415
};
@@ -56,7 +57,7 @@ fn render(
5657
render_record(db, ctx.snippet_cap(), &variant.fields(db), Some(&qualified_name))
5758
}
5859
StructKind::Unit => {
59-
RenderedCompound { literal: qualified_name.clone(), detail: qualified_name.clone() }
60+
RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() }
6061
}
6162
};
6263

crates/ide_completion/src/render/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
192192
Some(PathCompletionCtx { kind: Some(PathKind::Expr), has_call_parens: true, .. }) => {
193193
return false
194194
}
195-
Some(PathCompletionCtx { kind: Some(PathKind::Use), .. }) => {
195+
Some(PathCompletionCtx { kind: Some(PathKind::Use | PathKind::Type), .. }) => {
196196
cov_mark::hit!(no_parens_in_use_item);
197197
return false;
198198
}

crates/ide_completion/src/render/struct_literal.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Renderer for `struct` literal.
22
33
use hir::{HasAttrs, Name, StructKind};
4+
use ide_db::SymbolKind;
45
use syntax::SmolStr;
56

67
use crate::{
7-
render::compound::{
8-
format_literal_label, render_record, render_tuple, visible_fields, RenderedCompound,
8+
render::variant::{
9+
format_literal_label, render_record, render_tuple, visible_fields, RenderedLiteral,
910
},
1011
render::RenderContext,
1112
CompletionItem, CompletionItemKind,
@@ -37,12 +38,12 @@ pub(crate) fn render_struct_literal(
3738
fn build_completion(
3839
ctx: &RenderContext<'_>,
3940
name: SmolStr,
40-
rendered: RenderedCompound,
41+
rendered: RenderedLiteral,
4142
kind: StructKind,
4243
def: impl HasAttrs + Copy,
4344
) -> CompletionItem {
4445
let mut item = CompletionItem::new(
45-
CompletionItemKind::Snippet,
46+
CompletionItemKind::SymbolKind(SymbolKind::Struct),
4647
ctx.source_range(),
4748
format_literal_label(&name, kind),
4849
);
@@ -64,7 +65,7 @@ fn render_literal(
6465
name: &str,
6566
kind: StructKind,
6667
fields: &[hir::Field],
67-
) -> Option<RenderedCompound> {
68+
) -> Option<RenderedLiteral> {
6869
let path_string;
6970

7071
let qualified_name = if let Some(path) = path {

crates/ide_completion/src/render/union_literal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Renderer for `union` literals.
22
33
use hir::{HirDisplay, Name, StructKind};
4+
use ide_db::SymbolKind;
45
use itertools::Itertools;
56

67
use crate::{
78
render::{
8-
compound::{format_literal_label, visible_fields},
9+
variant::{format_literal_label, visible_fields},
910
RenderContext,
1011
},
1112
CompletionItem, CompletionItemKind,
@@ -25,7 +26,7 @@ pub(crate) fn render_union_literal(
2526
};
2627

2728
let mut item = CompletionItem::new(
28-
CompletionItemKind::Snippet,
29+
CompletionItemKind::SymbolKind(SymbolKind::Union),
2930
ctx.source_range(),
3031
format_literal_label(&name, StructKind::Record),
3132
);

crates/ide_completion/src/render/compound.rs renamed to crates/ide_completion/src/render/variant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::SmolStr;
99
/// A rendered struct, union, or enum variant, split into fields for actual
1010
/// auto-completion (`literal`, using `field: ()`) and display in the
1111
/// completions menu (`detail`, using `field: type`).
12-
pub(crate) struct RenderedCompound {
12+
pub(crate) struct RenderedLiteral {
1313
pub(crate) literal: String,
1414
pub(crate) detail: String,
1515
}
@@ -21,7 +21,7 @@ pub(crate) fn render_record(
2121
snippet_cap: Option<SnippetCap>,
2222
fields: &[hir::Field],
2323
name: Option<&str>,
24-
) -> RenderedCompound {
24+
) -> RenderedLiteral {
2525
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
2626
if snippet_cap.is_some() {
2727
f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1))
@@ -34,7 +34,7 @@ pub(crate) fn render_record(
3434
f(&format_args!("{}: {}", field.name(db), field.ty(db).display(db)))
3535
});
3636

37-
RenderedCompound {
37+
RenderedLiteral {
3838
literal: format!("{} {{ {} }}", name.unwrap_or(""), completions),
3939
detail: format!("{} {{ {} }}", name.unwrap_or(""), types),
4040
}
@@ -47,7 +47,7 @@ pub(crate) fn render_tuple(
4747
snippet_cap: Option<SnippetCap>,
4848
fields: &[hir::Field],
4949
name: Option<&str>,
50-
) -> RenderedCompound {
50+
) -> RenderedLiteral {
5151
let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
5252
if snippet_cap.is_some() {
5353
f(&format_args!("${{{}:()}}", idx + 1))
@@ -58,7 +58,7 @@ pub(crate) fn render_tuple(
5858

5959
let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db)));
6060

61-
RenderedCompound {
61+
RenderedLiteral {
6262
literal: format!("{}({})", name.unwrap_or(""), completions),
6363
detail: format!("{}({})", name.unwrap_or(""), types),
6464
}

crates/ide_completion/src/tests/pattern.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,71 @@ fn foo() {
378378
"#]],
379379
)
380380
}
381+
382+
#[test]
383+
fn completes_no_delims_if_existing() {
384+
check_empty(
385+
r#"
386+
struct Bar(u32);
387+
fn foo() {
388+
match Bar(0) {
389+
B$0(b) => {}
390+
}
391+
}
392+
"#,
393+
expect![[r#"
394+
kw self::
395+
kw super::
396+
kw crate::
397+
"#]],
398+
);
399+
check_empty(
400+
r#"
401+
struct Foo { bar: u32 }
402+
fn foo() {
403+
match Foo { bar: 0 } {
404+
F$0 { bar } => {}
405+
}
406+
}
407+
"#,
408+
expect![[r#"
409+
kw return
410+
kw self
411+
kw super
412+
kw crate
413+
st Foo
414+
fn foo() fn()
415+
bt u32
416+
"#]],
417+
);
418+
check_empty(
419+
r#"
420+
enum Enum {
421+
TupleVariant(u32)
422+
}
423+
fn foo() {
424+
match Enum::TupleVariant(0) {
425+
Enum::T$0(b) => {}
426+
}
427+
}
428+
"#,
429+
expect![[r#"
430+
ev TupleVariant(…) TupleVariant(u32)
431+
"#]],
432+
);
433+
check_empty(
434+
r#"
435+
enum Enum {
436+
RecordVariant { field: u32 }
437+
}
438+
fn foo() {
439+
match (Enum::RecordVariant { field: 0 }) {
440+
Enum::RecordV$0 { field } => {}
441+
}
442+
}
443+
"#,
444+
expect![[r#"
445+
ev RecordVariant {…} RecordVariant { field: u32 }
446+
"#]],
447+
);
448+
}

crates/ide_completion/src/tests/record.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn main() {
166166
kw true
167167
kw false
168168
kw return
169-
sn Foo {…} Foo { foo1: u32, foo2: u32 }
169+
st Foo {…} Foo { foo1: u32, foo2: u32 }
170170
fd ..Default::default()
171171
fd foo1 u32
172172
fd foo2 u32

0 commit comments

Comments
 (0)