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 21:13:22 +00:00
|
|
|
use stdx::{format_to, trim_indent};
|
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-07-04 07:08:51 +00:00
|
|
|
do_completion_with_config(code, kind, &CompletionConfig::default())
|
2020-03-11 09:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 07:08:51 +00:00
|
|
|
pub(crate) fn do_completion_with_config(
|
2020-03-11 09:46:43 +00:00
|
|
|
code: &str,
|
|
|
|
kind: CompletionKind,
|
2020-07-04 07:08:51 +00:00
|
|
|
config: &CompletionConfig,
|
2020-03-11 09:46:43 +00:00
|
|
|
) -> Vec<CompletionItem> {
|
2020-07-04 07:08:51 +00:00
|
|
|
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, config)
|
2020-06-12 18:30:57 +00:00
|
|
|
.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 {
|
2020-07-04 07:08:51 +00:00
|
|
|
completion_list_with_config(code, kind, &CompletionConfig::default())
|
2020-06-12 18:30:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 07:08:51 +00:00
|
|
|
pub(crate) fn completion_list_with_config(
|
2020-06-12 18:30:57 +00:00
|
|
|
code: &str,
|
|
|
|
kind: CompletionKind,
|
2020-07-04 07:08:51 +00:00
|
|
|
config: &CompletionConfig,
|
2020-06-13 11:57:18 +00:00
|
|
|
) -> String {
|
2020-07-04 07:08:51 +00:00
|
|
|
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, config)
|
2020-06-12 18:30:57 +00:00
|
|
|
.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-07-04 17:03:58 +00:00
|
|
|
let label_width = kind_completions
|
|
|
|
.iter()
|
|
|
|
.map(|it| monospace_width(it.label()))
|
|
|
|
.max()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.min(16);
|
2020-06-12 18:30:57 +00:00
|
|
|
kind_completions
|
|
|
|
.into_iter()
|
2020-07-03 09:48:48 +00:00
|
|
|
.map(|it| {
|
2020-07-04 17:03:58 +00:00
|
|
|
let tag = it.kind().unwrap().tag();
|
|
|
|
let var_name = format!("{} {}", tag, it.label());
|
|
|
|
let mut buf = var_name;
|
2020-07-03 09:48:48 +00:00
|
|
|
if let Some(detail) = it.detail() {
|
2020-07-04 17:03:58 +00:00
|
|
|
let width = label_width.saturating_sub(monospace_width(it.label()));
|
|
|
|
format_to!(buf, "{:width$} {}", "", detail, width = width);
|
2020-07-03 09:48:48 +00:00
|
|
|
}
|
|
|
|
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-04 17:03:58 +00:00
|
|
|
fn monospace_width(s: &str) -> usize {
|
|
|
|
s.chars().count()
|
|
|
|
}
|
|
|
|
|
2020-07-03 11:21:14 +00:00
|
|
|
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2020-07-03 21:46:36 +00:00
|
|
|
check_edit_with_config(what, ra_fixture_before, ra_fixture_after, &CompletionConfig::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn check_edit_with_config(
|
|
|
|
what: &str,
|
|
|
|
ra_fixture_before: &str,
|
|
|
|
ra_fixture_after: &str,
|
|
|
|
config: &CompletionConfig,
|
|
|
|
) {
|
2020-07-03 21:13:22 +00:00
|
|
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
2020-07-03 11:21:14 +00:00
|
|
|
let (analysis, position) = analysis_and_position(ra_fixture_before);
|
|
|
|
let completions: Vec<CompletionItem> =
|
2020-07-03 21:46:36 +00:00
|
|
|
analysis.completions(config, position).unwrap().unwrap().into();
|
2020-07-03 21:13:22 +00:00
|
|
|
let (completion,) = completions
|
|
|
|
.iter()
|
|
|
|
.filter(|it| it.lookup() == what)
|
|
|
|
.collect_tuple()
|
|
|
|
.unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
|
2020-07-03 11:21:14 +00:00
|
|
|
let mut actual = analysis.file_text(position.file_id).unwrap().to_string();
|
|
|
|
completion.text_edit().apply(&mut actual);
|
2020-07-03 21:13:22 +00:00
|
|
|
assert_eq_text!(&ra_fixture_after, &actual)
|
2020-07-03 11:21:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-04 07:53:54 +00:00
|
|
|
pub(crate) fn get_all_completion_items(
|
|
|
|
code: &str,
|
|
|
|
options: &CompletionConfig,
|
|
|
|
) -> Vec<CompletionItem> {
|
2020-06-27 10:07:48 +00:00
|
|
|
let (analysis, position) = analysis_and_position(code);
|
|
|
|
analysis.completions(options, position).unwrap().unwrap().into()
|
|
|
|
}
|