Skip to content

Commit c9598a4

Browse files
committed
Add some lint completion tests
1 parent 247faf2 commit c9598a4

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

crates/ide_completion/src/completions/attribute.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//! This module uses a bit of static metadata to provide completions
44
//! for built-in attributes.
55
6-
use std::mem;
7-
86
use once_cell::sync::Lazy;
97
use rustc_hash::{FxHashMap, FxHashSet};
108
use syntax::{ast, AstNode, NodeOrToken, SyntaxKind, T};
@@ -272,27 +270,27 @@ const ATTRIBUTES: &[AttrCompletion] = &[
272270
fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
273271
let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
274272
let mut input_derives = FxHashSet::default();
275-
let mut current_derive = String::new();
276-
for token in derive_input
273+
let mut tokens = derive_input
277274
.syntax()
278275
.children_with_tokens()
279276
.filter_map(NodeOrToken::into_token)
280277
.skip_while(|token| token != &l_paren)
281278
.skip(1)
282279
.take_while(|token| token != &r_paren)
283-
{
284-
if token.kind() == T![,] {
285-
if !current_derive.is_empty() {
286-
input_derives.insert(mem::take(&mut current_derive));
287-
}
288-
} else {
289-
current_derive.push_str(token.text().trim());
280+
.peekable();
281+
let mut input = String::new();
282+
while tokens.peek().is_some() {
283+
for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
284+
input.push_str(token.text());
290285
}
291-
}
292286

293-
if !current_derive.is_empty() {
294-
input_derives.insert(current_derive);
287+
if !input.is_empty() {
288+
input_derives.insert(input.trim().to_owned());
289+
}
290+
291+
input.clear();
295292
}
293+
296294
Some(input_derives)
297295
}
298296

crates/ide_completion/src/completions/attribute/derive.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(super) fn complete_derive(
4545
}
4646
}
4747
}
48+
4849
fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
4950
let mut result = FxHashSet::default();
5051
ctx.scope.process_all_names(&mut |name, scope_def| {
@@ -89,12 +90,14 @@ mod tests {
8990
}
9091

9192
#[test]
92-
fn empty_derive_completion() {
93+
fn no_completion_for_incorrect_derive() {
94+
check(r#"#[derive{$0)] struct Test;"#, expect![[]])
95+
}
96+
97+
#[test]
98+
fn empty_derive() {
9399
check(
94-
r#"
95-
#[derive($0)]
96-
struct Test {}
97-
"#,
100+
r#"#[derive($0)] struct Test;"#,
98101
expect![[r#"
99102
at Clone
100103
at Clone, Copy
@@ -110,23 +113,26 @@ struct Test {}
110113
}
111114

112115
#[test]
113-
fn no_completion_for_incorrect_derive() {
116+
fn derive_with_input() {
114117
check(
115-
r#"
116-
#[derive{$0)]
117-
struct Test {}
118-
"#,
119-
expect![[r#""#]],
118+
r#"#[derive(serde::Serialize, PartialEq, $0)] struct Test;"#,
119+
expect![[r#"
120+
at Clone
121+
at Clone, Copy
122+
at Debug
123+
at Default
124+
at Hash
125+
at Eq
126+
at PartialOrd
127+
at Eq, PartialOrd, Ord
128+
"#]],
120129
)
121130
}
122131

123132
#[test]
124-
fn derive_with_input_completion() {
133+
fn derive_with_input2() {
125134
check(
126-
r#"
127-
#[derive(serde::Serialize, PartialEq, $0)]
128-
struct Test {}
129-
"#,
135+
r#"#[derive($0 serde::Serialize, PartialEq)] struct Test;"#,
130136
expect![[r#"
131137
at Clone
132138
at Clone, Copy

crates/ide_completion/src/completions/attribute/lint.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,36 @@ pub(super) const DEFAULT_LINT_COMPLETIONS: &[LintCompletion] = &[
152152
LintCompletion { label: "unconditional_panic", description: r#"operation will cause a panic at runtime"# },
153153
LintCompletion { label: "unknown_crate_types", description: r#"unknown crate type found in `#[crate_type]` directive"# },
154154
];
155+
156+
#[cfg(test)]
157+
mod tests {
158+
159+
use crate::test_utils::check_edit;
160+
161+
#[test]
162+
fn check_empty() {
163+
check_edit(
164+
"deprecated",
165+
r#"#[allow($0)] struct Test;"#,
166+
r#"#[allow(deprecated)] struct Test;"#,
167+
)
168+
}
169+
170+
#[test]
171+
fn check_with_existing() {
172+
check_edit(
173+
"deprecated",
174+
r#"#[allow(keyword_idents, $0)] struct Test;"#,
175+
r#"#[allow(keyword_idents, deprecated)] struct Test;"#,
176+
)
177+
}
178+
179+
#[test]
180+
fn check_qualified() {
181+
check_edit(
182+
"deprecated",
183+
r#"#[allow(keyword_idents, $0)] struct Test;"#,
184+
r#"#[allow(keyword_idents, deprecated)] struct Test;"#,
185+
)
186+
}
187+
}

0 commit comments

Comments
 (0)