Skip to content

Commit 0729913

Browse files
committed
Various keyword completion fixes
1 parent b9d85f5 commit 0729913

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

crates/ide_completion/src/completions/keyword.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
9292
}
9393

9494
if !ctx.has_visibility_prev_sibling()
95-
&& (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field())
95+
&& (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_field())
9696
{
9797
add_keyword("pub(crate)", "pub(crate) ");
9898
add_keyword("pub", "pub ");
@@ -122,6 +122,10 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
122122
add_keyword("union", "union $1 {\n $0\n}");
123123
}
124124

125+
if ctx.expects_type() {
126+
return;
127+
}
128+
125129
if ctx.expects_expression() {
126130
if !has_block_expr_parent {
127131
add_keyword("unsafe", "unsafe {\n $0\n}");

crates/ide_completion/src/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,11 @@ impl<'a> CompletionContext<'a> {
286286
)
287287
}
288288

289-
pub(crate) fn expect_record_field(&self) -> bool {
290-
matches!(self.completion_location, Some(ImmediateLocation::RecordField))
289+
pub(crate) fn expect_field(&self) -> bool {
290+
matches!(
291+
self.completion_location,
292+
Some(ImmediateLocation::RecordField | ImmediateLocation::TupleField)
293+
)
291294
}
292295

293296
pub(crate) fn in_use_tree(&self) -> bool {

crates/ide_completion/src/patterns.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) enum ImmediateLocation {
3131
Impl,
3232
Trait,
3333
RecordField,
34+
TupleField,
3435
RefExpr,
3536
IdentPat,
3637
BlockExpr,
@@ -187,7 +188,13 @@ pub(crate) fn determine_location(
187188
ast::SourceFile(_it) => ImmediateLocation::ItemList,
188189
ast::ItemList(_it) => ImmediateLocation::ItemList,
189190
ast::RefExpr(_it) => ImmediateLocation::RefExpr,
190-
ast::RecordField(_it) => ImmediateLocation::RecordField,
191+
ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) {
192+
return None;
193+
} else {
194+
ImmediateLocation::RecordField
195+
},
196+
ast::TupleField(_it) => ImmediateLocation::TupleField,
197+
ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
191198
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
192199
Some(IMPL) => ImmediateLocation::Impl,
193200
Some(TRAIT) => ImmediateLocation::Trait,

crates/ide_completion/src/tests/type_pos.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ macro_rules! makro {}
2323

2424
#[test]
2525
fn record_field_ty() {
26-
// FIXME: pub shouldnt show up here
2726
check_with(
2827
r#"
2928
struct Foo<'lt, T, const C: usize> {
3029
f: $0
3130
}
3231
"#,
3332
expect![[r#"
34-
kw pub(crate)
35-
kw pub
3633
sp Self
3734
tp T
3835
tt Trait
@@ -42,20 +39,21 @@ struct Foo<'lt, T, const C: usize> {
4239
md module
4340
st Foo<…>
4441
st Unit
45-
ma makro!(…) macro_rules! makro
42+
ma makro!(…) macro_rules! makro
4643
bt u32
4744
"#]],
4845
)
4946
}
5047

5148
#[test]
5249
fn tuple_struct_field() {
53-
// FIXME: pub should show up here
5450
check_with(
5551
r#"
5652
struct Foo<'lt, T, const C: usize>(f$0);
5753
"#,
5854
expect![[r#"
55+
kw pub(crate)
56+
kw pub
5957
sp Self
6058
tp T
6159
tt Trait
@@ -65,21 +63,19 @@ struct Foo<'lt, T, const C: usize>(f$0);
6563
md module
6664
st Foo<…>
6765
st Unit
68-
ma makro!(…) macro_rules! makro
66+
ma makro!(…) macro_rules! makro
6967
bt u32
7068
"#]],
7169
)
7270
}
7371

7472
#[test]
7573
fn fn_return_type() {
76-
// FIXME: return shouldnt show up here
7774
check_with(
7875
r#"
7976
fn x<'lt, T, const C: usize>() -> $0
8077
"#,
8178
expect![[r#"
82-
kw return
8379
tp T
8480
tt Trait
8581
en Enum
@@ -95,7 +91,6 @@ fn x<'lt, T, const C: usize>() -> $0
9591

9692
#[test]
9793
fn body_type_pos() {
98-
// FIXME: return shouldnt show up here
9994
check_with(
10095
r#"
10196
fn foo<'lt, T, const C: usize>() {
@@ -104,7 +99,6 @@ fn foo<'lt, T, const C: usize>() {
10499
}
105100
"#,
106101
expect![[r#"
107-
kw return
108102
tp T
109103
tt Trait
110104
en Enum
@@ -136,7 +130,6 @@ fn foo<'lt, T, const C: usize>() {
136130

137131
#[test]
138132
fn completes_types_and_const_in_arg_list() {
139-
// FIXME: return shouldnt show up here
140133
// FIXME: we should complete the lifetime here for now
141134
check_with(
142135
r#"
@@ -147,7 +140,6 @@ trait Trait2 {
147140
fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
148141
"#,
149142
expect![[r#"
150-
kw return
151143
ta Foo = type Foo;
152144
tp T
153145
cp CONST_PARAM

0 commit comments

Comments
 (0)