2020-06-12 23:21:48 +00:00
|
|
|
//! Patterns telling us certain facts about current syntax element, they are used in completion context
|
|
|
|
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::{
|
2020-06-11 12:16:35 +00:00
|
|
|
algo::non_trivia_sibling,
|
|
|
|
ast::{self, LoopBodyOwner},
|
|
|
|
match_ast, AstNode, Direction, NodeOrToken, SyntaxElement,
|
2021-05-27 01:01:46 +00:00
|
|
|
SyntaxKind::{self, *},
|
2021-01-10 15:40:52 +00:00
|
|
|
SyntaxNode, SyntaxToken, T,
|
2020-06-11 12:16:35 +00:00
|
|
|
};
|
|
|
|
|
2020-06-13 11:47:30 +00:00
|
|
|
#[cfg(test)]
|
2020-10-18 10:09:00 +00:00
|
|
|
use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable};
|
2020-06-13 11:47:30 +00:00
|
|
|
|
2020-06-12 22:55:21 +00:00
|
|
|
pub(crate) fn has_trait_parent(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element)
|
2020-07-30 09:42:51 +00:00
|
|
|
.filter(|it| it.kind() == ASSOC_ITEM_LIST)
|
2020-06-12 22:55:21 +00:00
|
|
|
.and_then(|it| it.parent())
|
2020-07-30 16:17:28 +00:00
|
|
|
.filter(|it| it.kind() == TRAIT)
|
2020-06-12 22:55:21 +00:00
|
|
|
.is_some()
|
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_trait_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"trait A { f$0 }", has_trait_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-12 22:55:21 +00:00
|
|
|
|
|
|
|
pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element)
|
2020-07-30 09:42:51 +00:00
|
|
|
.filter(|it| it.kind() == ASSOC_ITEM_LIST)
|
2020-06-12 22:55:21 +00:00
|
|
|
.and_then(|it| it.parent())
|
2020-07-30 16:28:28 +00:00
|
|
|
.filter(|it| it.kind() == IMPL)
|
2020-06-12 22:55:21 +00:00
|
|
|
.is_some()
|
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_impl_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"impl A { f$0 }", has_impl_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-10-17 08:03:07 +00:00
|
|
|
|
2020-10-17 08:17:58 +00:00
|
|
|
pub(crate) fn inside_impl_trait_block(element: SyntaxElement) -> bool {
|
|
|
|
// Here we search `impl` keyword up through the all ancestors, unlike in `has_impl_parent`,
|
|
|
|
// where we only check the first parent with different text range.
|
|
|
|
element
|
|
|
|
.ancestors()
|
|
|
|
.find(|it| it.kind() == IMPL)
|
2020-10-17 08:03:07 +00:00
|
|
|
.map(|it| ast::Impl::cast(it).unwrap())
|
|
|
|
.map(|it| it.trait_().is_some())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
#[test]
|
2020-10-17 08:17:58 +00:00
|
|
|
fn test_inside_impl_trait_block() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"impl Foo for Bar { f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_applicable(r"impl Foo for Bar { fn f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_not_applicable(r"impl A { f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_not_applicable(r"impl A { fn f$0 }", inside_impl_trait_block);
|
2020-10-17 08:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:20:29 +00:00
|
|
|
pub(crate) fn has_field_list_parent(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element).filter(|it| it.kind() == RECORD_FIELD_LIST).is_some()
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_has_field_list_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"struct Foo { f$0 }", has_field_list_parent);
|
|
|
|
check_pattern_is_applicable(r"struct Foo { f$0 pub f: i32}", has_field_list_parent);
|
2020-08-25 15:20:29 +00:00
|
|
|
}
|
2020-06-12 22:55:21 +00:00
|
|
|
|
|
|
|
pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some()
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_block_expr_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"fn my_fn() { let a = 2; f$0 }", has_block_expr_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-11 12:16:35 +00:00
|
|
|
|
|
|
|
pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool {
|
2021-03-21 12:13:34 +00:00
|
|
|
element.ancestors().any(|it| it.kind() == IDENT_PAT)
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
2021-05-27 01:01:46 +00:00
|
|
|
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_bind_pat_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"fn my_fn(m$0) {}", has_bind_pat_parent);
|
|
|
|
check_pattern_is_applicable(r"fn my_fn() { let m$0 }", has_bind_pat_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-11 12:16:35 +00:00
|
|
|
|
2020-06-12 22:55:21 +00:00
|
|
|
pub(crate) fn has_ref_parent(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element)
|
|
|
|
.filter(|it| it.kind() == REF_PAT || it.kind() == REF_EXPR)
|
|
|
|
.is_some()
|
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_ref_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"fn my_fn(&m$0) {}", has_ref_parent);
|
|
|
|
check_pattern_is_applicable(r"fn my() { let &m$0 }", has_ref_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-12 22:55:21 +00:00
|
|
|
|
2020-06-13 08:43:39 +00:00
|
|
|
pub(crate) fn has_item_list_or_source_file_parent(element: SyntaxElement) -> bool {
|
2021-05-27 02:34:21 +00:00
|
|
|
let it = element
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.text_range() == element.text_range())
|
|
|
|
.last()
|
|
|
|
.map(|it| (it.kind(), it.parent()));
|
|
|
|
match it {
|
|
|
|
Some((_, Some(it))) => it.kind() == SOURCE_FILE || it.kind() == ITEM_LIST,
|
|
|
|
Some((MACRO_ITEMS, None) | (SOURCE_FILE, None)) => true,
|
|
|
|
_ => false,
|
2020-06-13 08:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_item_list_or_source_file_parent() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"i$0", has_item_list_or_source_file_parent);
|
|
|
|
check_pattern_is_applicable(r"mod foo { f$0 }", has_item_list_or_source_file_parent);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-13 08:43:39 +00:00
|
|
|
|
2020-06-12 22:55:21 +00:00
|
|
|
pub(crate) fn is_match_arm(element: SyntaxElement) -> bool {
|
|
|
|
not_same_range_ancestor(element.clone()).filter(|it| it.kind() == MATCH_ARM).is_some()
|
|
|
|
&& previous_sibling_or_ancestor_sibling(element)
|
|
|
|
.and_then(|it| it.into_token())
|
|
|
|
.filter(|it| it.kind() == FAT_ARROW)
|
|
|
|
.is_some()
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_is_match_arm() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"fn my_fn() { match () { () => m$0 } }", is_match_arm);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-11 12:16:35 +00:00
|
|
|
|
2021-05-26 19:09:27 +00:00
|
|
|
pub(crate) fn previous_token(element: SyntaxElement) -> Option<SyntaxToken> {
|
|
|
|
element.into_token().and_then(|it| previous_non_trivia_token(it))
|
2020-10-17 07:56:00 +00:00
|
|
|
}
|
2020-10-12 07:59:15 +00:00
|
|
|
|
|
|
|
/// Check if the token previous to the previous one is `for`.
|
2021-01-06 20:15:48 +00:00
|
|
|
/// For example, `for _ i$0` => true.
|
2020-10-12 07:59:15 +00:00
|
|
|
pub(crate) fn for_is_prev2(element: SyntaxElement) -> bool {
|
|
|
|
element
|
|
|
|
.into_token()
|
|
|
|
.and_then(|it| previous_non_trivia_token(it))
|
|
|
|
.and_then(|it| previous_non_trivia_token(it))
|
2021-01-10 15:40:52 +00:00
|
|
|
.filter(|it| it.kind() == T![for])
|
2020-10-12 07:59:15 +00:00
|
|
|
.is_some()
|
|
|
|
}
|
2020-10-17 07:56:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_for_is_prev2() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"for i i$0", for_is_prev2);
|
2020-10-17 07:56:00 +00:00
|
|
|
}
|
2020-10-12 07:59:15 +00:00
|
|
|
|
2021-05-27 01:01:46 +00:00
|
|
|
pub(crate) fn has_prev_sibling(element: SyntaxElement, kind: SyntaxKind) -> bool {
|
|
|
|
previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == kind).is_some()
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_has_impl_as_prev_sibling() {
|
2021-05-27 01:01:46 +00:00
|
|
|
check_pattern_is_applicable(r"impl A w$0 {}", |it| has_prev_sibling(it, IMPL));
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-11 12:16:35 +00:00
|
|
|
|
|
|
|
pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
|
2021-05-27 00:54:49 +00:00
|
|
|
element
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.kind() != FN && it.kind() != CLOSURE_EXPR)
|
|
|
|
.find_map(|it| {
|
|
|
|
let loop_body = match_ast! {
|
|
|
|
match it {
|
|
|
|
ast::ForExpr(it) => it.loop_body(),
|
|
|
|
ast::WhileExpr(it) => it.loop_body(),
|
|
|
|
ast::LoopExpr(it) => it.loop_body(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
loop_body.filter(|it| it.syntax().text_range().contains_range(element.text_range()))
|
|
|
|
})
|
|
|
|
.is_some()
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:25:58 +00:00
|
|
|
fn not_same_range_ancestor(element: SyntaxElement) -> Option<SyntaxNode> {
|
|
|
|
element
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.text_range() == element.text_range())
|
|
|
|
.last()
|
|
|
|
.and_then(|it| it.parent())
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:25:58 +00:00
|
|
|
fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
|
|
|
|
let mut token = token.prev_token();
|
|
|
|
while let Some(inner) = token.clone() {
|
|
|
|
if !inner.kind().is_trivia() {
|
|
|
|
return Some(inner);
|
|
|
|
} else {
|
|
|
|
token = inner.prev_token();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<SyntaxElement> {
|
2020-06-11 12:16:35 +00:00
|
|
|
let token_sibling = non_trivia_sibling(element.clone(), Direction::Prev);
|
2020-06-11 21:25:58 +00:00
|
|
|
if let Some(sibling) = token_sibling {
|
|
|
|
Some(sibling)
|
2020-06-11 12:16:35 +00:00
|
|
|
} else {
|
|
|
|
// if not trying to find first ancestor which has such a sibling
|
2021-01-30 15:19:21 +00:00
|
|
|
let range = element.text_range();
|
|
|
|
let top_node = element.ancestors().take_while(|it| it.text_range() == range).last()?;
|
2020-06-11 12:16:35 +00:00
|
|
|
let prev_sibling_node = top_node.ancestors().find(|it| {
|
|
|
|
non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some()
|
|
|
|
})?;
|
2020-06-11 21:25:58 +00:00
|
|
|
non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev)
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
|
|
|
}
|