Various keyword completion fixes

This commit is contained in:
Lukas Wirth 2021-06-21 15:14:28 +02:00
parent b9d85f55b7
commit 0729913525
4 changed files with 40 additions and 34 deletions

View File

@ -92,7 +92,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
} }
if !ctx.has_visibility_prev_sibling() if !ctx.has_visibility_prev_sibling()
&& (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field()) && (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_field())
{ {
add_keyword("pub(crate)", "pub(crate) "); add_keyword("pub(crate)", "pub(crate) ");
add_keyword("pub", "pub "); add_keyword("pub", "pub ");
@ -122,6 +122,10 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
add_keyword("union", "union $1 {\n $0\n}"); add_keyword("union", "union $1 {\n $0\n}");
} }
if ctx.expects_type() {
return;
}
if ctx.expects_expression() { if ctx.expects_expression() {
if !has_block_expr_parent { if !has_block_expr_parent {
add_keyword("unsafe", "unsafe {\n $0\n}"); add_keyword("unsafe", "unsafe {\n $0\n}");

View File

@ -286,8 +286,11 @@ pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool {
) )
} }
pub(crate) fn expect_record_field(&self) -> bool { pub(crate) fn expect_field(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::RecordField)) matches!(
self.completion_location,
Some(ImmediateLocation::RecordField | ImmediateLocation::TupleField)
)
} }
pub(crate) fn in_use_tree(&self) -> bool { pub(crate) fn in_use_tree(&self) -> bool {

View File

@ -31,6 +31,7 @@ pub(crate) enum ImmediateLocation {
Impl, Impl,
Trait, Trait,
RecordField, RecordField,
TupleField,
RefExpr, RefExpr,
IdentPat, IdentPat,
BlockExpr, BlockExpr,
@ -187,7 +188,13 @@ pub(crate) fn determine_location(
ast::SourceFile(_it) => ImmediateLocation::ItemList, ast::SourceFile(_it) => ImmediateLocation::ItemList,
ast::ItemList(_it) => ImmediateLocation::ItemList, ast::ItemList(_it) => ImmediateLocation::ItemList,
ast::RefExpr(_it) => ImmediateLocation::RefExpr, ast::RefExpr(_it) => ImmediateLocation::RefExpr,
ast::RecordField(_it) => ImmediateLocation::RecordField, ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) {
return None;
} else {
ImmediateLocation::RecordField
},
ast::TupleField(_it) => ImmediateLocation::TupleField,
ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) { ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
Some(IMPL) => ImmediateLocation::Impl, Some(IMPL) => ImmediateLocation::Impl,
Some(TRAIT) => ImmediateLocation::Trait, Some(TRAIT) => ImmediateLocation::Trait,

View File

@ -23,12 +23,33 @@ macro_rules! makro {}
#[test] #[test]
fn record_field_ty() { fn record_field_ty() {
// FIXME: pub shouldnt show up here
check_with( check_with(
r#" r#"
struct Foo<'lt, T, const C: usize> { struct Foo<'lt, T, const C: usize> {
f: $0 f: $0
} }
"#,
expect![[r#"
sp Self
tp T
tt Trait
en Enum
st Record
st Tuple
md module
st Foo<>
st Unit
ma makro!() macro_rules! makro
bt u32
"#]],
)
}
#[test]
fn tuple_struct_field() {
check_with(
r#"
struct Foo<'lt, T, const C: usize>(f$0);
"#, "#,
expect![[r#" expect![[r#"
kw pub(crate) kw pub(crate)
@ -48,38 +69,13 @@ struct Foo<'lt, T, const C: usize> {
) )
} }
#[test]
fn tuple_struct_field() {
// FIXME: pub should show up here
check_with(
r#"
struct Foo<'lt, T, const C: usize>(f$0);
"#,
expect![[r#"
sp Self
tp T
tt Trait
en Enum
st Record
st Tuple
md module
st Foo<>
st Unit
ma makro!() macro_rules! makro
bt u32
"#]],
)
}
#[test] #[test]
fn fn_return_type() { fn fn_return_type() {
// FIXME: return shouldnt show up here
check_with( check_with(
r#" r#"
fn x<'lt, T, const C: usize>() -> $0 fn x<'lt, T, const C: usize>() -> $0
"#, "#,
expect![[r#" expect![[r#"
kw return
tp T tp T
tt Trait tt Trait
en Enum en Enum
@ -95,7 +91,6 @@ fn x<'lt, T, const C: usize>() -> $0
#[test] #[test]
fn body_type_pos() { fn body_type_pos() {
// FIXME: return shouldnt show up here
check_with( check_with(
r#" r#"
fn foo<'lt, T, const C: usize>() { fn foo<'lt, T, const C: usize>() {
@ -104,7 +99,6 @@ fn foo<'lt, T, const C: usize>() {
} }
"#, "#,
expect![[r#" expect![[r#"
kw return
tp T tp T
tt Trait tt Trait
en Enum en Enum
@ -136,7 +130,6 @@ fn foo<'lt, T, const C: usize>() {
#[test] #[test]
fn completes_types_and_const_in_arg_list() { fn completes_types_and_const_in_arg_list() {
// FIXME: return shouldnt show up here
// FIXME: we should complete the lifetime here for now // FIXME: we should complete the lifetime here for now
check_with( check_with(
r#" r#"
@ -147,7 +140,6 @@ trait Trait2 {
fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
"#, "#,
expect![[r#" expect![[r#"
kw return
ta Foo = type Foo; ta Foo = type Foo;
tp T tp T
cp CONST_PARAM cp CONST_PARAM