From 396167eadbea168e0d9858b5f45b7db860873f8b Mon Sep 17 00:00:00 2001 From: Mikhail Rakhmanov Date: Fri, 12 Jun 2020 08:49:12 +0200 Subject: [PATCH] New testing approach for keywords --- .../ra_ide/src/completion/complete_keyword.rs | 43 +++++++++++++++++-- crates/ra_ide/src/completion/test_utils.rs | 19 +++++++- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs index 675991154b..7eddf76b97 100644 --- a/crates/ra_ide/src/completion/complete_keyword.rs +++ b/crates/ra_ide/src/completion/complete_keyword.rs @@ -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 { 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!( diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs index e9e89104e4..277d2904d2 100644 --- a/crates/ra_ide/src/completion/test_utils.rs +++ b/crates/ra_ide/src/completion/test_utils.rs @@ -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 (FilePosition, Vec) { + do_completion_with_options_and_position(code, kind, &CompletionConfig::default()) +} + pub(crate) fn do_completion_with_options( code: &str, kind: CompletionKind, options: &CompletionConfig, ) -> Vec { + 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) { 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 = 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) {