2020-03-11 09:46:43 +00:00
|
|
|
//! Runs completion for testing purposes.
|
|
|
|
|
|
|
|
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
|
|
|
};
|
2020-06-11 22:17:30 +00:00
|
|
|
use hir::Semantics;
|
2020-06-12 08:12:15 +00:00
|
|
|
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
|
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
|
|
|
}
|
|
|
|
|
2020-06-13 11:57:18 +00:00
|
|
|
pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
|
|
|
|
completion_list_with_options(code, kind, &CompletionConfig::default())
|
2020-06-12 06:49:12 +00:00
|
|
|
}
|
|
|
|
|
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-12 18:30:57 +00:00
|
|
|
fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<CompletionItem> {
|
2020-03-11 09:46:43 +00:00
|
|
|
let (analysis, position) = if code.contains("//-") {
|
|
|
|
analysis_and_position(code)
|
|
|
|
} else {
|
2020-06-24 09:31:30 +00:00
|
|
|
analysis_and_position(code)
|
2020-03-11 09:46:43 +00:00
|
|
|
};
|
2020-06-12 18:30:57 +00:00
|
|
|
analysis.completions(options, position).unwrap().unwrap().into()
|
|
|
|
}
|
|
|
|
|
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-06-13 11:57:18 +00:00
|
|
|
.map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label()))
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
}
|