mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
New testing approach for keywords
This commit is contained in:
parent
a2b4385f16
commit
396167eadb
2 changed files with 56 additions and 6 deletions
|
@ -76,8 +76,6 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||
add_keyword(ctx, acc, "else if", "else if $0 {}", ctx.after_if);
|
||||
add_keyword(ctx, acc, "mod", "mod $0 {}", ctx.is_new_item || ctx.block_expr_parent);
|
||||
add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent);
|
||||
add_keyword(ctx, acc, "true", "true", !ctx.is_new_item); // this should be defined properly
|
||||
add_keyword(ctx, acc, "false", "false", !ctx.is_new_item); // this should be defined properly
|
||||
add_keyword(ctx, acc, "const", "const ", ctx.is_new_item || ctx.block_expr_parent);
|
||||
add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
|
||||
add_keyword(ctx, acc, "static", "static ", ctx.is_new_item || ctx.block_expr_parent);
|
||||
|
@ -89,7 +87,6 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||
add_keyword(ctx, acc, "break", "break", ctx.in_loop_body && !ctx.can_be_stmt);
|
||||
add_keyword(ctx, acc, "pub", "pub ", ctx.is_new_item && !ctx.inside_trait);
|
||||
add_keyword(ctx, acc, "where", "where ", ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling);
|
||||
complete_use_tree_keyword(acc, ctx);
|
||||
|
||||
let fn_def = match &ctx.function_syntax {
|
||||
Some(it) => it,
|
||||
|
@ -114,13 +111,51 @@ fn complete_return(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
||||
use crate::{
|
||||
completion::{
|
||||
test_utils::{do_completion, do_completion_with_position},
|
||||
CompletionItem, CompletionKind,
|
||||
},
|
||||
CompletionItemKind,
|
||||
};
|
||||
use insta::assert_debug_snapshot;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {
|
||||
do_completion(code, CompletionKind::Keyword)
|
||||
}
|
||||
|
||||
fn assert_completion_keyword(code: &str, keywords: &[(&str, &str)]) {
|
||||
let (position, completion_items) =
|
||||
do_completion_with_position(code, CompletionKind::Keyword);
|
||||
let mut set = FxHashSet::<(String, String)>::default();
|
||||
for (key, value) in keywords {
|
||||
set.insert(((*key).to_string(), (*value).to_string()));
|
||||
}
|
||||
|
||||
for item in completion_items {
|
||||
assert!(item.text_edit().len() == 1);
|
||||
assert!(item.kind() == Some(CompletionItemKind::Keyword));
|
||||
let atom = item.text_edit().iter().next().unwrap().clone();
|
||||
assert!(atom.delete.start() == position.offset);
|
||||
assert!(atom.delete.end() == position.offset);
|
||||
let pair = (item.label().to_string(), atom.insert);
|
||||
assert!(set.contains(&pair));
|
||||
set.remove(&pair);
|
||||
}
|
||||
assert!(set.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_keywords_in_use_stmt_new_approach() {
|
||||
assert_completion_keyword(
|
||||
r"
|
||||
use <|>
|
||||
",
|
||||
&[("crate", "crate::"), ("self", "self"), ("super", "super::")],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_keywords_in_use_stmt() {
|
||||
assert_debug_snapshot!(
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use crate::{
|
||||
completion::{completion_item::CompletionKind, CompletionConfig},
|
||||
mock_analysis::{analysis_and_position, single_file_with_position},
|
||||
CompletionItem,
|
||||
CompletionItem, FilePosition,
|
||||
};
|
||||
use hir::Semantics;
|
||||
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement, SyntaxToken};
|
||||
|
@ -12,11 +12,26 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionI
|
|||
do_completion_with_options(code, kind, &CompletionConfig::default())
|
||||
}
|
||||
|
||||
pub(crate) fn do_completion_with_position(
|
||||
code: &str,
|
||||
kind: CompletionKind,
|
||||
) -> (FilePosition, Vec<CompletionItem>) {
|
||||
do_completion_with_options_and_position(code, kind, &CompletionConfig::default())
|
||||
}
|
||||
|
||||
pub(crate) fn do_completion_with_options(
|
||||
code: &str,
|
||||
kind: CompletionKind,
|
||||
options: &CompletionConfig,
|
||||
) -> Vec<CompletionItem> {
|
||||
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>) {
|
||||
let (analysis, position) = if code.contains("//-") {
|
||||
analysis_and_position(code)
|
||||
} else {
|
||||
|
@ -27,7 +42,7 @@ pub(crate) fn do_completion_with_options(
|
|||
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());
|
||||
kind_completions
|
||||
(position, kind_completions)
|
||||
}
|
||||
|
||||
pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
|
||||
|
|
Loading…
Reference in a new issue