Skip to content

Commit

Permalink
feat: better completions for extern blcoks
Browse files Browse the repository at this point in the history
  • Loading branch information
roife committed Oct 21, 2024
1 parent e2dd95f commit ba3b7c7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext<'_>, kind: Option
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));

let in_extern_block = matches!(kind, Some(ItemListKind::ExternBlock));
let in_extern_block = matches!(kind, Some(ItemListKind::ExternBlock { .. }));
let in_unsafe_extern_block =
matches!(kind, Some(ItemListKind::ExternBlock { is_unsafe: true }));

let in_trait = matches!(kind, Some(ItemListKind::Trait));
let in_trait_impl = matches!(kind, Some(ItemListKind::TraitImpl(_)));
let in_inherent_impl = matches!(kind, Some(ItemListKind::Impl));
Expand All @@ -85,29 +88,39 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext<'_>, kind: Option
let no_vis_qualifiers = ctx.qualifier_ctx.vis_node.is_none();
let has_unsafe_kw = ctx.qualifier_ctx.unsafe_tok.is_some();
let has_async_kw = ctx.qualifier_ctx.async_tok.is_some();
let has_safe_kw = ctx.qualifier_ctx.safe_tok.is_some();

// We handle completions for trait-impls in [`item_list::trait_impl`]
if in_trait_impl {
return;
}

// Some keywords are invalid after non-vis qualifiers, so we handle them first.
if has_unsafe_kw || has_async_kw {
if !has_unsafe_kw {
add_keyword("unsafe", "unsafe $0");
}
if !has_async_kw {
add_keyword("async", "async $0");
}
if has_unsafe_kw || has_async_kw || has_safe_kw {
if in_extern_block {
add_keyword("fn", "fn $1($2);");
add_keyword("static", "static $1: $2;");
} else {
if !has_unsafe_kw {
add_keyword("unsafe", "unsafe $0");
}
if !has_async_kw {
add_keyword("async", "async $0");
}

if in_item_list || in_assoc_non_trait_impl {
add_keyword("fn", "fn $1($2) {\n $0\n}");
}
if in_item_list || in_assoc_non_trait_impl {
add_keyword("fn", "fn $1($2) {\n $0\n}");
}

if has_unsafe_kw && in_item_list {
add_keyword("trait", "trait $1 {\n $0\n}");
if no_vis_qualifiers {
add_keyword("impl", "impl $1 {\n $0\n}");
if has_unsafe_kw && in_item_list {
add_keyword("trait", "trait $1 {\n $0\n}");
if no_vis_qualifiers {
add_keyword("impl", "impl $1 {\n $0\n}");
}
}

if !has_async_kw && no_vis_qualifiers && in_item_list {
add_keyword("extern", "extern $0");
}
}

Expand Down Expand Up @@ -138,6 +151,11 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext<'_>, kind: Option
}

if in_extern_block {
add_keyword("unsafe", "unsafe $0");
if in_unsafe_extern_block {
add_keyword("safe", "safe $0");
}

add_keyword("fn", "fn $1($2);");
add_keyword("static", "static $1: $2;");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod tests {
r"fn my_fn() { unsafe $0 }",
expect![[r#"
kw async
kw extern
kw fn
kw impl
kw trait
Expand Down
8 changes: 6 additions & 2 deletions src/tools/rust-analyzer/crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ pub(crate) struct QualifierCtx {
// TODO: Add try_tok and default_tok
pub(crate) async_tok: Option<SyntaxToken>,
pub(crate) unsafe_tok: Option<SyntaxToken>,
pub(crate) safe_tok: Option<SyntaxToken>,
pub(crate) vis_node: Option<ast::Visibility>,
}

impl QualifierCtx {
pub(crate) fn none(&self) -> bool {
self.async_tok.is_none() && self.unsafe_tok.is_none() && self.vis_node.is_none()
self.async_tok.is_none()
&& self.unsafe_tok.is_none()
&& self.safe_tok.is_none()
&& self.vis_node.is_none()
}
}

Expand Down Expand Up @@ -229,7 +233,7 @@ pub(crate) enum ItemListKind {
Impl,
TraitImpl(Option<ast::Impl>),
Trait,
ExternBlock,
ExternBlock { is_unsafe: bool },
}

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,14 @@ fn classify_name_ref(
},
None => return None,
} },
ast::ExternItemList(_) => PathKind::Item { kind: ItemListKind::ExternBlock },
ast::ExternItemList(it) => {
let exn_blk = it.syntax().parent().and_then(ast::ExternBlock::cast);
PathKind::Item {
kind: ItemListKind::ExternBlock {
is_unsafe: exn_blk.and_then(|it| it.unsafe_token()).is_some(),
}
}
},
ast::SourceFile(_) => PathKind::Item { kind: ItemListKind::SourceFile },
_ => return None,
}
Expand Down Expand Up @@ -1310,6 +1317,7 @@ fn classify_name_ref(
match token.kind() {
SyntaxKind::UNSAFE_KW => qualifier_ctx.unsafe_tok = Some(token),
SyntaxKind::ASYNC_KW => qualifier_ctx.async_tok = Some(token),
SyntaxKind::SAFE_KW => qualifier_ctx.safe_tok = Some(token),
_ => {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn after_unsafe_token() {
r#"unsafe $0"#,
expect![[r#"
kw async
kw extern
kw fn
kw impl
kw trait
Expand Down Expand Up @@ -495,3 +496,57 @@ type O = $0;
",
)
}

#[test]
fn inside_extern_blocks() {
// Should suggest `fn`, `static`, `unsafe`
check(
r#"extern { $0 }"#,
expect![[r#"
ma makro!(…) macro_rules! makro
md module
kw crate::
kw fn
kw pub
kw pub(crate)
kw pub(super)
kw self::
kw static
kw unsafe
"#]],
);

// Should suggest `fn`, `static`, `safe`, `unsafe`
check(
r#"unsafe extern { $0 }"#,
expect![[r#"
ma makro!(…) macro_rules! makro
md module
kw crate::
kw fn
kw pub
kw pub(crate)
kw pub(super)
kw safe
kw self::
kw static
kw unsafe
"#]],
);

check(
r#"unsafe extern { pub safe $0 }"#,
expect![[r#"
kw fn
kw static
"#]],
);

check(
r#"unsafe extern { pub unsafe $0 }"#,
expect![[r#"
kw fn
kw static
"#]],
)
}

0 comments on commit ba3b7c7

Please sign in to comment.