Skip to content

Commit 3576671

Browse files
committed
Rewrite snapshot checks
1 parent 42a719a commit 3576671

File tree

4 files changed

+100
-245
lines changed

4 files changed

+100
-245
lines changed

crates/ra_ide/src/completion/complete_keyword.rs

Lines changed: 29 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ fn add_keyword(
6161

6262
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
6363
add_keyword(ctx, acc, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
64-
add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
6564
add_keyword(ctx, acc, "use", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
6665
add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item);
6766
add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item);
@@ -111,145 +110,49 @@ fn complete_return(
111110

112111
#[cfg(test)]
113112
mod tests {
114-
use crate::{
115-
completion::{
116-
test_utils::{do_completion, do_completion_with_position},
117-
CompletionItem, CompletionKind,
118-
},
119-
CompletionItemKind,
113+
use crate::completion::{
114+
test_utils::{do_completion, get_completions},
115+
CompletionItem, CompletionKind,
120116
};
121117
use insta::assert_debug_snapshot;
122118

123119
fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {
124120
do_completion(code, CompletionKind::Keyword)
125121
}
126122

127-
fn get_completion_text_and_assert_positions(code: &str) -> Vec<(String, String)> {
128-
let (position, completion_items) =
129-
do_completion_with_position(code, CompletionKind::Keyword);
130-
let mut returned_keywords = Vec::<(String, String)>::new();
131-
132-
for item in completion_items {
133-
assert!(item.text_edit().len() == 1);
134-
assert!(item.kind() == Some(CompletionItemKind::Keyword));
135-
let atom = item.text_edit().iter().next().unwrap().clone();
136-
assert!(atom.delete.start() == position.offset);
137-
assert!(atom.delete.end() == position.offset);
138-
let pair = (item.label().to_string(), atom.insert);
139-
returned_keywords.push(pair);
140-
}
141-
returned_keywords.sort();
142-
returned_keywords
143-
}
144-
145-
#[test]
146-
fn completes_keywords_in_use_stmt_new_approach() {
147-
assert_debug_snapshot!(
148-
get_completion_text_and_assert_positions(r"
149-
use <|>
150-
"),
151-
@r###"
152-
[
153-
(
154-
"crate",
155-
"crate::",
156-
),
157-
(
158-
"self",
159-
"self",
160-
),
161-
(
162-
"super",
163-
"super::",
164-
),
165-
]
166-
"###
167-
);
123+
fn get_keyword_completions(code: &str) -> Vec<String> {
124+
get_completions(code, CompletionKind::Keyword)
168125
}
169126

170127
#[test]
171128
fn completes_keywords_in_use_stmt() {
172129
assert_debug_snapshot!(
173-
do_keyword_completion(
174-
r"
175-
use <|>
176-
",
177-
),
130+
get_keyword_completions(r"use <|>"),
178131
@r###"
179132
[
180-
CompletionItem {
181-
label: "crate",
182-
source_range: 21..21,
183-
delete: 21..21,
184-
insert: "crate::",
185-
kind: Keyword,
186-
},
187-
CompletionItem {
188-
label: "self",
189-
source_range: 21..21,
190-
delete: 21..21,
191-
insert: "self",
192-
kind: Keyword,
193-
},
194-
CompletionItem {
195-
label: "super",
196-
source_range: 21..21,
197-
delete: 21..21,
198-
insert: "super::",
199-
kind: Keyword,
200-
},
133+
"kw crate",
134+
"kw self",
135+
"kw super",
201136
]
202137
"###
203138
);
204139

205140
assert_debug_snapshot!(
206-
do_keyword_completion(
207-
r"
208-
use a::<|>
209-
",
210-
),
141+
get_keyword_completions(r"use a::<|>"),
211142
@r###"
212143
[
213-
CompletionItem {
214-
label: "self",
215-
source_range: 24..24,
216-
delete: 24..24,
217-
insert: "self",
218-
kind: Keyword,
219-
},
220-
CompletionItem {
221-
label: "super",
222-
source_range: 24..24,
223-
delete: 24..24,
224-
insert: "super::",
225-
kind: Keyword,
226-
},
144+
"kw self",
145+
"kw super",
227146
]
228147
"###
229148
);
230149

231150
assert_debug_snapshot!(
232-
do_keyword_completion(
233-
r"
234-
use a::{b, <|>}
235-
",
236-
),
151+
get_keyword_completions(r"use a::{b, <|>}"),
237152
@r###"
238153
[
239-
CompletionItem {
240-
label: "self",
241-
source_range: 28..28,
242-
delete: 28..28,
243-
insert: "self",
244-
kind: Keyword,
245-
},
246-
CompletionItem {
247-
label: "super",
248-
source_range: 28..28,
249-
delete: 28..28,
250-
insert: "super::",
251-
kind: Keyword,
252-
},
154+
"kw self",
155+
"kw super",
253156
]
254157
"###
255158
);
@@ -258,50 +161,22 @@ mod tests {
258161
#[test]
259162
fn completes_various_keywords_in_function() {
260163
assert_debug_snapshot!(
261-
do_keyword_completion(
262-
r"
263-
fn quux() {
264-
<|>
265-
}
266-
",
267-
),
164+
get_keyword_completions(r"fn quux() { <|> }"),
268165
@r###"
269166
[
270-
CompletionItem {
271-
label: "if",
272-
source_range: 49..49,
273-
delete: 49..49,
274-
insert: "if $0 {}",
275-
kind: Keyword,
276-
},
277-
CompletionItem {
278-
label: "loop",
279-
source_range: 49..49,
280-
delete: 49..49,
281-
insert: "loop {$0}",
282-
kind: Keyword,
283-
},
284-
CompletionItem {
285-
label: "match",
286-
source_range: 49..49,
287-
delete: 49..49,
288-
insert: "match $0 {}",
289-
kind: Keyword,
290-
},
291-
CompletionItem {
292-
label: "return",
293-
source_range: 49..49,
294-
delete: 49..49,
295-
insert: "return;",
296-
kind: Keyword,
297-
},
298-
CompletionItem {
299-
label: "while",
300-
source_range: 49..49,
301-
delete: 49..49,
302-
insert: "while $0 {}",
303-
kind: Keyword,
304-
},
167+
"kw const",
168+
"kw extern",
169+
"kw fn",
170+
"kw let",
171+
"kw loop",
172+
"kw match",
173+
"kw mod",
174+
"kw return",
175+
"kw static",
176+
"kw type",
177+
"kw unsafe",
178+
"kw use",
179+
"kw while",
305180
]
306181
"###
307182
);

crates/ra_ide/src/completion/completion_item.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ pub enum CompletionItemKind {
125125
Attribute,
126126
}
127127

128+
impl CompletionItemKind {
129+
pub fn tag(&self) -> String {
130+
let tag = match self {
131+
CompletionItemKind::Snippet => "sn",
132+
CompletionItemKind::Keyword => "kw",
133+
CompletionItemKind::Module => "md",
134+
CompletionItemKind::Function => "fn",
135+
CompletionItemKind::BuiltinType => "bt",
136+
CompletionItemKind::Struct => "st",
137+
CompletionItemKind::Enum => "en",
138+
CompletionItemKind::EnumVariant => "ev",
139+
CompletionItemKind::Binding => "bn",
140+
CompletionItemKind::Field => "fd",
141+
CompletionItemKind::Static => "sc",
142+
CompletionItemKind::Const => "ct",
143+
CompletionItemKind::Trait => "tt",
144+
CompletionItemKind::TypeAlias => "ta",
145+
CompletionItemKind::Method => "me",
146+
CompletionItemKind::TypeParam => "tp",
147+
CompletionItemKind::Macro => "ma",
148+
CompletionItemKind::Attribute => "at",
149+
};
150+
tag.to_owned()
151+
}
152+
}
153+
128154
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
129155
pub(crate) enum CompletionKind {
130156
/// Parser-based keyword completion.

crates/ra_ide/src/completion/patterns.rs

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -121,115 +121,51 @@ mod tests {
121121

122122
#[test]
123123
fn test_unsafe_is_prev() {
124-
check_pattern_is_applicable(
125-
r"
126-
unsafe i<|>
127-
",
128-
unsafe_is_prev,
129-
);
124+
check_pattern_is_applicable(r"unsafe i<|>", unsafe_is_prev);
130125
}
131126

132127
#[test]
133128
fn test_if_is_prev() {
134-
check_pattern_is_applicable(
135-
r"
136-
if l<|>
137-
",
138-
if_is_prev,
139-
);
129+
check_pattern_is_applicable(r"if l<|>", if_is_prev);
140130
}
141131

142132
#[test]
143133
fn test_inside_trait() {
144-
check_pattern_is_applicable(
145-
r"
146-
trait A {
147-
fn<|>
148-
}
149-
",
150-
inside_trait,
151-
);
134+
check_pattern_is_applicable(r"trait A { fn<|> }", inside_trait);
152135
}
153136

154137
#[test]
155138
fn test_has_trait_as_prev_sibling() {
156-
check_pattern_is_applicable(
157-
r"
158-
trait A w<|> {
159-
}
160-
",
161-
has_trait_as_prev_sibling,
162-
);
139+
check_pattern_is_applicable(r"trait A w<|> {}", has_trait_as_prev_sibling);
163140
}
164141

165142
#[test]
166143
fn test_has_impl_as_prev_sibling() {
167-
check_pattern_is_applicable(
168-
r"
169-
impl A w<|> {
170-
}
171-
",
172-
has_impl_as_prev_sibling,
173-
);
144+
check_pattern_is_applicable(r"impl A w<|> {}", has_impl_as_prev_sibling);
174145
}
175146

176147
#[test]
177148
fn test_parent_block_expr() {
178-
check_pattern_is_applicable(
179-
r"
180-
fn my_fn() {
181-
let a = 2;
182-
f<|>
183-
}
184-
",
185-
has_block_expr_parent,
186-
);
149+
check_pattern_is_applicable(r"fn my_fn() { let a = 2; f<|> }", has_block_expr_parent);
187150
}
188151

189152
#[test]
190153
fn test_has_ref_pat_parent_in_func_parameters() {
191-
check_pattern_is_applicable(
192-
r"
193-
fn my_fn(&<|>) {
194-
let a = 2;
195-
}
196-
",
197-
has_ref_pat_parent,
198-
);
154+
check_pattern_is_applicable(r"fn my_fn(&<|>) {}", has_ref_pat_parent);
199155
}
200156

201157
#[test]
202158
fn test_has_ref_pat_parent_in_let_statement() {
203-
check_pattern_is_applicable(
204-
r"
205-
fn my_fn() {
206-
let &<|>
207-
}
208-
",
209-
has_ref_pat_parent,
210-
);
159+
check_pattern_is_applicable(r"fn my_fn() { let &<|> }", has_ref_pat_parent);
211160
}
212161

213162
#[test]
214163
fn test_has_bind_pat_parent_in_func_parameters() {
215-
check_pattern_is_applicable(
216-
r"
217-
fn my_fn(m<|>) {
218-
}
219-
",
220-
has_bind_pat_parent,
221-
);
164+
check_pattern_is_applicable(r"fn my_fn(m<|>) {}", has_bind_pat_parent);
222165
}
223166

224167
#[test]
225168
fn test_has_bind_pat_parent_in_let_statement() {
226-
check_pattern_is_applicable(
227-
r"
228-
fn my_fn() {
229-
let m<|>
230-
}
231-
",
232-
has_bind_pat_parent,
233-
);
169+
check_pattern_is_applicable(r"fn my_fn() { let m<|> }", has_bind_pat_parent);
234170
}
235171
}

0 commit comments

Comments
 (0)