match on segments of path and some small cleanup

This commit is contained in:
Ryan Mehri 2025-06-04 08:30:27 -04:00
parent 59fe3995c8
commit c5355a19bd
2 changed files with 14 additions and 23 deletions

View file

@ -42,31 +42,21 @@ pub(crate) fn complete_known_attribute_input(
) -> Option<()> {
let attribute = fake_attribute_under_caret;
let path = attribute.path()?;
let name_ref = path.segment()?.name_ref();
let (name_ref, tt) = name_ref.zip(attribute.token_tree())?;
tt.l_paren_token()?;
let segments = path.segments().map(|s| s.name_ref()).collect::<Option<Vec<_>>>()?;
let segments = segments.iter().map(|n| n.text()).collect::<Vec<_>>();
let segments = segments.iter().map(|t| t.as_str()).collect::<Vec<_>>();
let tt = attribute.token_tree()?;
if let Some(qualifier) = path.qualifier() {
let qualifier_name_ref = qualifier.as_single_name_ref()?;
match (qualifier_name_ref.text().as_str(), name_ref.text().as_str()) {
("diagnostic", "on_unimplemented") => {
diagnostic::complete_on_unimplemented(acc, ctx, tt)
}
_ => (),
}
return Some(());
}
match name_ref.text().as_str() {
"repr" => repr::complete_repr(acc, ctx, tt),
"feature" => lint::complete_lint(
match segments.as_slice() {
["repr"] => repr::complete_repr(acc, ctx, tt),
["feature"] => lint::complete_lint(
acc,
ctx,
colon_prefix,
&parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
FEATURES,
),
"allow" | "expect" | "deny" | "forbid" | "warn" => {
["allow"] | ["expect"] | ["deny"] | ["forbid"] | ["warn"] => {
let existing_lints = parse_tt_as_comma_sep_paths(tt, ctx.edition)?;
let lints: Vec<Lint> = CLIPPY_LINT_GROUPS
@ -80,13 +70,14 @@ pub(crate) fn complete_known_attribute_input(
lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
}
"cfg" => cfg::complete_cfg(acc, ctx),
"macro_use" => macro_use::complete_macro_use(
["cfg"] => cfg::complete_cfg(acc, ctx),
["macro_use"] => macro_use::complete_macro_use(
acc,
ctx,
extern_crate,
&parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
),
["diagnostic", "on_unimplemented"] => diagnostic::complete_on_unimplemented(acc, ctx, tt),
_ => (),
}
Some(())

View file

@ -1,7 +1,7 @@
//! Completion for diagnostic attributes.
use ide_db::SymbolKind;
use syntax::ast::{self};
use syntax::ast;
use crate::{CompletionItem, Completions, context::CompletionContext};
@ -13,7 +13,7 @@ pub(super) fn complete_on_unimplemented(
input: ast::TokenTree,
) {
if let Some(existing_keys) = super::parse_comma_sep_expr(input) {
for attr in ATTRIBUTES {
for attr in ATTRIBUTE_ARGS {
let already_annotated = existing_keys
.iter()
.filter_map(|expr| match expr {
@ -53,7 +53,7 @@ pub(super) fn complete_on_unimplemented(
}
}
const ATTRIBUTES: &[AttrCompletion] = &[
const ATTRIBUTE_ARGS: &[AttrCompletion] = &[
super::attr(r#"label = "…""#, Some("label"), Some(r#"label = "${0:label}""#)),
super::attr(r#"message = "…""#, Some("message"), Some(r#"message = "${0:message}""#)),
super::attr(r#"note = "…""#, Some("note"), Some(r#"note = "${0:note}""#)),