Skip to content

Commit f6d6fda

Browse files
committed
Refine functional update completion some more
1 parent e1dcec0 commit f6d6fda

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

crates/ide_completion/src/completions/record.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! Complete fields in record literals and patterns.
22
use ide_db::{helpers::FamousDefs, SymbolKind};
3-
use syntax::ast::Expr;
3+
use syntax::{ast::Expr, T};
44

55
use crate::{
66
item::CompletionKind, patterns::ImmediateLocation, CompletionContext, CompletionItem,
7-
Completions,
7+
CompletionItemKind, Completions,
88
};
99

1010
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
1111
let missing_fields = match &ctx.completion_location {
12-
Some(ImmediateLocation::RecordExpr(record_expr)) => {
12+
Some(
13+
ImmediateLocation::RecordExpr(record_expr)
14+
| ImmediateLocation::RecordExprUpdate(record_expr),
15+
) => {
1316
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
1417
let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
1518
let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
@@ -29,7 +32,13 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
2932
item.insert_text(completion_text).kind(SymbolKind::Field);
3033
item.add_to(acc);
3134
}
32-
35+
if ctx.previous_token_is(T![.]) {
36+
let mut item =
37+
CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), "..");
38+
item.insert_text(".").kind(CompletionItemKind::Snippet);
39+
item.add_to(acc);
40+
return None;
41+
}
3342
missing_fields
3443
}
3544
Some(ImmediateLocation::RecordPat(record_pat)) => {

crates/ide_completion/src/patterns.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ pub(crate) enum ImmediateLocation {
6363
/// The record expr of the field name we are completing
6464
RecordExpr(ast::RecordExpr),
6565
// Original file ast node
66+
/// The record expr of the functional update syntax we are completing
67+
RecordExprUpdate(ast::RecordExpr),
68+
// Original file ast node
6669
/// The record pat of the field name we are completing
6770
RecordPat(ast::RecordPat),
6871
}
@@ -209,7 +212,7 @@ pub(crate) fn determine_location(
209212
},
210213
ast::RecordExprFieldList(_it) => sema
211214
.find_node_at_offset_with_macros(original_file, offset)
212-
.map(ImmediateLocation::RecordExpr)?,
215+
.map(ImmediateLocation::RecordExprUpdate)?,
213216
ast::TupleField(_it) => ImmediateLocation::TupleField,
214217
ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
215218
ast::TypeBound(_it) => ImmediateLocation::TypeBound,

crates/ide_completion/src/tests/record.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ fn main() {
136136
"#,
137137
expect![[r#"
138138
fd ..Default::default()
139-
fd foo1 u32
140-
fd foo2 u32
139+
sn ..
141140
"#]],
142141
);
143142
check(
@@ -155,6 +154,28 @@ fn main() {
155154
}
156155
"#,
157156
expect![[r#"
157+
kw unsafe
158+
kw match
159+
kw while
160+
kw while let
161+
kw loop
162+
kw if
163+
kw if let
164+
kw for
165+
kw true
166+
kw false
167+
kw return
168+
kw self
169+
kw super
170+
kw crate
171+
lc foo Foo
172+
lc thing i32
173+
st Foo
174+
fn main() fn()
175+
md core
176+
bt u32
177+
tt Sized
178+
tt Default
158179
fd ..Default::default()
159180
fd foo1 u32
160181
fd foo2 u32

0 commit comments

Comments
 (0)