2020-03-11 09:46:43 +00:00
|
|
|
//! Runs completion for testing purposes.
|
|
|
|
|
2020-06-27 10:07:48 +00:00
|
|
|
use hir::Semantics;
|
2020-07-03 11:21:14 +00:00
|
|
|
use itertools::Itertools;
|
2020-06-27 10:07:48 +00:00
|
|
|
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
|
2020-07-03 09:48:48 +00:00
|
|
|
use stdx::format_to;
|
2020-07-03 11:21:14 +00:00
|
|
|
use test_utils::assert_eq_text;
|
2020-06-27 10:07:48 +00:00
|
|
|
|
2020-03-11 09:46:43 +00:00
|
|
|
use crate::{
|
2020-03-31 14:02:55 +00:00
|
|
|
completion::{completion_item::CompletionKind, CompletionConfig},
|
2020-06-24 09:31:30 +00:00
|
|
|
mock_analysis::analysis_and_position,
|
2020-06-12 18:30:57 +00:00
|
|
|
CompletionItem,
|
2020-03-11 09:46:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
|
2020-03-31 14:02:55 +00:00
|
|
|
do_completion_with_options(code, kind, &CompletionConfig::default())
|
2020-03-11 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn do_completion_with_options(
|
|
|
|
code: &str,
|
|
|
|
kind: CompletionKind,
|
2020-03-31 14:02:55 +00:00
|
|
|
options: &CompletionConfig,
|
2020-03-11 09:46:43 +00:00
|
|
|
) -> Vec<CompletionItem> {
|
2020-06-12 18:30:57 +00:00
|
|
|
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c.completion_kind == kind)
|
|
|
|
.collect();
|
2020-06-13 11:47:30 +00:00
|
|
|
kind_completions.sort_by(|l, r| l.label().cmp(r.label()));
|
2020-06-12 18:30:57 +00:00
|
|
|
kind_completions
|
2020-06-12 06:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:07:48 +00:00
|
|
|
pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
|
|
|
|
completion_list_with_options(code, kind, &CompletionConfig::default())
|
2020-06-12 18:30:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 11:57:18 +00:00
|
|
|
pub(crate) fn completion_list_with_options(
|
2020-06-12 18:30:57 +00:00
|
|
|
code: &str,
|
|
|
|
kind: CompletionKind,
|
|
|
|
options: &CompletionConfig,
|
2020-06-13 11:57:18 +00:00
|
|
|
) -> String {
|
2020-06-12 18:30:57 +00:00
|
|
|
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c.completion_kind == kind)
|
|
|
|
.collect();
|
2020-03-11 09:46:43 +00:00
|
|
|
kind_completions.sort_by_key(|c| c.label().to_owned());
|
2020-06-12 18:30:57 +00:00
|
|
|
kind_completions
|
|
|
|
.into_iter()
|
2020-07-03 09:48:48 +00:00
|
|
|
.map(|it| {
|
|
|
|
let mut buf = format!("{} {}", it.kind().unwrap().tag(), it.label());
|
|
|
|
if let Some(detail) = it.detail() {
|
|
|
|
format_to!(buf, " {}", detail);
|
|
|
|
}
|
|
|
|
format_to!(buf, "\n");
|
|
|
|
buf
|
|
|
|
})
|
2020-06-12 18:30:57 +00:00
|
|
|
.collect()
|
2020-03-11 09:46:43 +00:00
|
|
|
}
|
2020-06-11 22:17:30 +00:00
|
|
|
|
2020-07-03 11:21:14 +00:00
|
|
|
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
|
|
let (analysis, position) = analysis_and_position(ra_fixture_before);
|
|
|
|
let completions: Vec<CompletionItem> =
|
|
|
|
analysis.completions(&CompletionConfig::default(), position).unwrap().unwrap().into();
|
|
|
|
let (completion,) =
|
|
|
|
completions.into_iter().filter(|it| it.label() == what).collect_tuple().unwrap();
|
|
|
|
let mut actual = analysis.file_text(position.file_id).unwrap().to_string();
|
|
|
|
completion.text_edit().apply(&mut actual);
|
|
|
|
assert_eq_text!(ra_fixture_after, &actual)
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:17:30 +00:00
|
|
|
pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
|
2020-06-24 09:31:30 +00:00
|
|
|
let (analysis, pos) = analysis_and_position(code);
|
2020-06-11 22:17:30 +00:00
|
|
|
analysis
|
|
|
|
.with_db(|db| {
|
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let original_file = sema.parse(pos.file_id);
|
|
|
|
let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
|
|
|
|
assert!(check(NodeOrToken::Token(token)));
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-06-27 10:07:48 +00:00
|
|
|
|
|
|
|
fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<CompletionItem> {
|
|
|
|
let (analysis, position) = analysis_and_position(code);
|
|
|
|
analysis.completions(options, position).unwrap().unwrap().into()
|
|
|
|
}
|