Skip to content

Commit c5355a1

Browse files
committed
match on segments of path and some small cleanup
1 parent 59fe399 commit c5355a1

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,21 @@ pub(crate) fn complete_known_attribute_input(
4242
) -> Option<()> {
4343
let attribute = fake_attribute_under_caret;
4444
let path = attribute.path()?;
45-
let name_ref = path.segment()?.name_ref();
46-
let (name_ref, tt) = name_ref.zip(attribute.token_tree())?;
47-
tt.l_paren_token()?;
45+
let segments = path.segments().map(|s| s.name_ref()).collect::<Option<Vec<_>>>()?;
46+
let segments = segments.iter().map(|n| n.text()).collect::<Vec<_>>();
47+
let segments = segments.iter().map(|t| t.as_str()).collect::<Vec<_>>();
48+
let tt = attribute.token_tree()?;
4849

49-
if let Some(qualifier) = path.qualifier() {
50-
let qualifier_name_ref = qualifier.as_single_name_ref()?;
51-
match (qualifier_name_ref.text().as_str(), name_ref.text().as_str()) {
52-
("diagnostic", "on_unimplemented") => {
53-
diagnostic::complete_on_unimplemented(acc, ctx, tt)
54-
}
55-
_ => (),
56-
}
57-
return Some(());
58-
}
59-
60-
match name_ref.text().as_str() {
61-
"repr" => repr::complete_repr(acc, ctx, tt),
62-
"feature" => lint::complete_lint(
50+
match segments.as_slice() {
51+
["repr"] => repr::complete_repr(acc, ctx, tt),
52+
["feature"] => lint::complete_lint(
6353
acc,
6454
ctx,
6555
colon_prefix,
6656
&parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
6757
FEATURES,
6858
),
69-
"allow" | "expect" | "deny" | "forbid" | "warn" => {
59+
["allow"] | ["expect"] | ["deny"] | ["forbid"] | ["warn"] => {
7060
let existing_lints = parse_tt_as_comma_sep_paths(tt, ctx.edition)?;
7161

7262
let lints: Vec<Lint> = CLIPPY_LINT_GROUPS
@@ -80,13 +70,14 @@ pub(crate) fn complete_known_attribute_input(
8070

8171
lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
8272
}
83-
"cfg" => cfg::complete_cfg(acc, ctx),
84-
"macro_use" => macro_use::complete_macro_use(
73+
["cfg"] => cfg::complete_cfg(acc, ctx),
74+
["macro_use"] => macro_use::complete_macro_use(
8575
acc,
8676
ctx,
8777
extern_crate,
8878
&parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
8979
),
80+
["diagnostic", "on_unimplemented"] => diagnostic::complete_on_unimplemented(acc, ctx, tt),
9081
_ => (),
9182
}
9283
Some(())

src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/diagnostic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion for diagnostic attributes.
22
33
use ide_db::SymbolKind;
4-
use syntax::ast::{self};
4+
use syntax::ast;
55

66
use crate::{CompletionItem, Completions, context::CompletionContext};
77

@@ -13,7 +13,7 @@ pub(super) fn complete_on_unimplemented(
1313
input: ast::TokenTree,
1414
) {
1515
if let Some(existing_keys) = super::parse_comma_sep_expr(input) {
16-
for attr in ATTRIBUTES {
16+
for attr in ATTRIBUTE_ARGS {
1717
let already_annotated = existing_keys
1818
.iter()
1919
.filter_map(|expr| match expr {
@@ -53,7 +53,7 @@ pub(super) fn complete_on_unimplemented(
5353
}
5454
}
5555

56-
const ATTRIBUTES: &[AttrCompletion] = &[
56+
const ATTRIBUTE_ARGS: &[AttrCompletion] = &[
5757
super::attr(r#"label = "…""#, Some("label"), Some(r#"label = "${0:label}""#)),
5858
super::attr(r#"message = "…""#, Some("message"), Some(r#"message = "${0:message}""#)),
5959
super::attr(r#"note = "…""#, Some("note"), Some(r#"note = "${0:note}""#)),

0 commit comments

Comments
 (0)