rust-analyzer/crates/ra_ide/src/completion/test_utils.rs

59 lines
2 KiB
Rust
Raw Normal View History

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-03-11 09:46:43 +00:00
mock_analysis::{analysis_and_position, single_file_with_position},
2020-06-12 06:49:12 +00:00
CompletionItem, FilePosition,
2020-03-11 09:46:43 +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-12 06:49:12 +00:00
pub(crate) fn do_completion_with_position(
code: &str,
kind: CompletionKind,
) -> (FilePosition, Vec<CompletionItem>) {
do_completion_with_options_and_position(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 06:49:12 +00:00
do_completion_with_options_and_position(code, kind, options).1
}
pub(crate) fn do_completion_with_options_and_position(
code: &str,
kind: CompletionKind,
options: &CompletionConfig,
) -> (FilePosition, Vec<CompletionItem>) {
2020-03-11 09:46:43 +00:00
let (analysis, position) = if code.contains("//-") {
analysis_and_position(code)
} else {
single_file_with_position(code)
};
2020-05-17 10:09:53 +00:00
let completions = analysis.completions(options, position).unwrap().unwrap();
2020-03-11 09:46:43 +00:00
let completion_items: Vec<CompletionItem> = completions.into();
let mut kind_completions: Vec<CompletionItem> =
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
kind_completions.sort_by_key(|c| c.label().to_owned());
2020-06-12 06:49:12 +00:00
(position, kind_completions)
2020-03-11 09:46:43 +00:00
}
pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
let (analysis, pos) = single_file_with_position(code);
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();
}