Skip to content

Commit c460bae

Browse files
bors[bot]Veykril
andauthored
9079: Don't take the parent kind of trailing attributes in attr completion r=Veykril a=Veykril bors r+ fixes rust-lang#9076 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents e6ec860 + 741b2f5 commit c460bae

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

crates/ide_completion/src/completions/attribute.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use once_cell::sync::Lazy;
77
use rustc_hash::{FxHashMap, FxHashSet};
8-
use syntax::{ast, AstNode, NodeOrToken, SyntaxKind, T};
8+
use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, NodeOrToken, SyntaxKind, T};
99

1010
use crate::{
1111
context::CompletionContext,
@@ -37,15 +37,20 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
3737
}
3838

3939
fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
40-
let attribute_annotated_item_kind = attribute.syntax().parent().map(|it| it.kind());
40+
let is_inner = attribute.kind() == ast::AttrKind::Inner;
41+
let attribute_annotated_item_kind =
42+
attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
43+
is_inner
44+
// If we got nothing coming after the attribute it could be anything so filter it the kind out
45+
|| non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
46+
});
4147
let attributes = attribute_annotated_item_kind.and_then(|kind| {
4248
if ast::Expr::can_cast(kind) {
4349
Some(EXPR_ATTRIBUTES)
4450
} else {
4551
KIND_TO_ATTRIBUTES.get(&kind).copied()
4652
}
4753
});
48-
let is_inner = attribute.kind() == ast::AttrKind::Inner;
4954

5055
let add_completion = |attr_completion: &AttrCompletion| {
5156
let mut item = CompletionItem::new(
@@ -781,4 +786,49 @@ mod tests {
781786
"#]],
782787
);
783788
}
789+
790+
#[test]
791+
fn complete_attribute_in_source_file_end() {
792+
check(
793+
r#"#[$0]"#,
794+
expect![[r#"
795+
at allow(…)
796+
at automatically_derived
797+
at cfg(…)
798+
at cfg_attr(…)
799+
at cold
800+
at deny(…)
801+
at deprecated
802+
at derive(…)
803+
at doc = "…"
804+
at doc(alias = "…")
805+
at doc(hidden)
806+
at export_name = "…"
807+
at forbid(…)
808+
at global_allocator
809+
at ignore = "…"
810+
at inline
811+
at link
812+
at link_name = "…"
813+
at link_section = "…"
814+
at macro_export
815+
at macro_use
816+
at must_use
817+
at no_mangle
818+
at non_exhaustive
819+
at panic_handler
820+
at path = "…"
821+
at proc_macro
822+
at proc_macro_attribute
823+
at proc_macro_derive(…)
824+
at repr(…)
825+
at should_panic
826+
at target_feature = "…"
827+
at test
828+
at track_caller
829+
at used
830+
at warn(…)
831+
"#]],
832+
);
833+
}
784834
}

0 commit comments

Comments
 (0)