Skip to content

Commit ef6a6d7

Browse files
bors[bot]matklad
andauthored
Merge #5201
5201: Add function to test completion edit r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 57576ac + 503de8e commit ef6a6d7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

crates/ra_ide/src/completion/complete_keyword.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ fn complete_return(
176176
mod tests {
177177
use expect::{expect, Expect};
178178

179-
use crate::completion::{test_utils::completion_list, CompletionKind};
179+
use crate::completion::{
180+
test_utils::{check_edit, completion_list},
181+
CompletionKind,
182+
};
180183

181184
fn check(ra_fixture: &str, expect: Expect) {
182185
let actual = completion_list(ra_fixture, CompletionKind::Keyword);
@@ -312,6 +315,11 @@ mod tests {
312315
kw while
313316
"#]],
314317
);
318+
check_edit(
319+
"else",
320+
r#"fn quux() { if true { () } <|> }"#,
321+
r#"fn quux() { if true { () } else {$0} }"#,
322+
);
315323
}
316324

317325
#[test]

crates/ra_ide/src/completion/test_utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Runs completion for testing purposes.
22
33
use hir::Semantics;
4+
use itertools::Itertools;
45
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
56
use stdx::format_to;
7+
use test_utils::assert_eq_text;
68

79
use crate::{
810
completion::{completion_item::CompletionKind, CompletionConfig},
@@ -54,6 +56,17 @@ pub(crate) fn completion_list_with_options(
5456
.collect()
5557
}
5658

59+
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
60+
let (analysis, position) = analysis_and_position(ra_fixture_before);
61+
let completions: Vec<CompletionItem> =
62+
analysis.completions(&CompletionConfig::default(), position).unwrap().unwrap().into();
63+
let (completion,) =
64+
completions.into_iter().filter(|it| it.label() == what).collect_tuple().unwrap();
65+
let mut actual = analysis.file_text(position.file_id).unwrap().to_string();
66+
completion.text_edit().apply(&mut actual);
67+
assert_eq_text!(ra_fixture_after, &actual)
68+
}
69+
5770
pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
5871
let (analysis, pos) = analysis_and_position(code);
5972
analysis

0 commit comments

Comments
 (0)