Add more pattern tests

This commit is contained in:
Mikhail Rakhmanov 2020-06-12 10:12:15 +02:00
parent 396167eadb
commit 4c92f2d190
2 changed files with 50 additions and 11 deletions

View file

@ -6,10 +6,6 @@ use ra_syntax::{
SyntaxNode, SyntaxToken, SyntaxNode, SyntaxToken,
}; };
pub(crate) fn inside_impl(element: SyntaxElement) -> bool {
element.ancestors().find(|it| it.kind() == IMPL_DEF).is_some()
}
pub(crate) fn inside_trait(element: SyntaxElement) -> bool { pub(crate) fn inside_trait(element: SyntaxElement) -> bool {
element.ancestors().find(|it| it.kind() == TRAIT_DEF).is_some() element.ancestors().find(|it| it.kind() == TRAIT_DEF).is_some()
} }
@ -42,10 +38,6 @@ pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some() not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some()
} }
pub(crate) fn has_item_list_parent(element: SyntaxElement) -> bool {
not_same_range_ancestor(element).filter(|it| it.kind() == ITEM_LIST).is_some()
}
pub(crate) fn has_trait_as_prev_sibling(element: SyntaxElement) -> bool { pub(crate) fn has_trait_as_prev_sibling(element: SyntaxElement) -> bool {
previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == TRAIT_DEF).is_some() previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == TRAIT_DEF).is_some()
} }
@ -122,8 +114,8 @@ fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<Syntax
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{ use super::{
has_block_expr_parent, has_impl_as_prev_sibling, has_trait_as_prev_sibling, if_is_prev, has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_ref_pat_parent,
inside_trait, unsafe_is_prev, has_trait_as_prev_sibling, if_is_prev, inside_trait, unsafe_is_prev,
}; };
use crate::completion::test_utils::check_pattern_is_applicable; use crate::completion::test_utils::check_pattern_is_applicable;
@ -193,4 +185,51 @@ mod tests {
has_block_expr_parent, has_block_expr_parent,
); );
} }
#[test]
fn test_has_ref_pat_parent_in_func_parameters() {
check_pattern_is_applicable(
r"
fn my_fn(&<|>) {
let a = 2;
}
",
has_ref_pat_parent,
);
}
#[test]
fn test_has_ref_pat_parent_in_let_statement() {
check_pattern_is_applicable(
r"
fn my_fn() {
let &<|>
}
",
has_ref_pat_parent,
);
}
#[test]
fn test_has_bind_pat_parent_in_func_parameters() {
check_pattern_is_applicable(
r"
fn my_fn(m<|>) {
}
",
has_bind_pat_parent,
);
}
#[test]
fn test_has_bind_pat_parent_in_let_statement() {
check_pattern_is_applicable(
r"
fn my_fn() {
let m<|>
}
",
has_bind_pat_parent,
);
}
} }

View file

@ -6,7 +6,7 @@ use crate::{
CompletionItem, FilePosition, CompletionItem, FilePosition,
}; };
use hir::Semantics; use hir::Semantics;
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement, SyntaxToken}; use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
do_completion_with_options(code, kind, &CompletionConfig::default()) do_completion_with_options(code, kind, &CompletionConfig::default())