Add more patterns and keywords

This commit is contained in:
Mikhail Rakhmanov 2020-06-11 23:25:58 +02:00
parent eb4004fdb8
commit f46bc12199
3 changed files with 59 additions and 55 deletions

View file

@ -62,7 +62,7 @@ fn add_keyword(
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
add_keyword(ctx, acc, "fn", "fn $0() {}", 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, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
add_keyword(ctx, acc, "use", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item);
add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item);
add_keyword(ctx, acc, "enum", "enum $0 {}", ctx.is_new_item && !ctx.after_unsafe);
@ -72,7 +72,6 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
add_keyword(ctx, acc, "loop", "loop {$0}", ctx.block_expr_parent);
add_keyword(ctx, acc, "while", "while $0 {}", ctx.block_expr_parent);
add_keyword(ctx, acc, "let", "let ", ctx.after_if || ctx.block_expr_parent);
add_keyword(ctx, acc, "let", "let ", ctx.after_if || ctx.block_expr_parent);
add_keyword(ctx, acc, "else", "else {$0}", ctx.after_if);
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);
@ -88,6 +87,8 @@ 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, "continue", "continue", ctx.in_loop_body && !ctx.can_be_stmt);
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 {

View file

@ -12,8 +12,8 @@ use ra_syntax::{
use ra_text_edit::Indel;
use super::patterns::{
goes_after_unsafe, has_bind_pat_parent, has_block_expr_parent, has_ref_pat_parent,
is_in_loop_body,
goes_after_unsafe, has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling,
has_ref_pat_parent, has_trait_as_prev_sibling, inside_trait, is_in_loop_body,
};
use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
use test_utils::mark;
@ -69,6 +69,9 @@ pub(crate) struct CompletionContext<'a> {
pub(super) bind_pat_parent: bool,
pub(super) ref_pat_parent: bool,
pub(super) in_loop_body: bool,
pub(super) inside_trait: bool,
pub(super) trait_as_prev_sibling: bool,
pub(super) impl_as_prev_sibling: bool,
}
impl<'a> CompletionContext<'a> {
@ -132,6 +135,9 @@ impl<'a> CompletionContext<'a> {
ref_pat_parent: false,
bind_pat_parent: false,
block_expr_parent: false,
inside_trait: false,
trait_as_prev_sibling: false,
impl_as_prev_sibling: false,
};
let mut original_file = original_file.syntax().clone();
@ -210,6 +216,9 @@ impl<'a> CompletionContext<'a> {
self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone());
self.ref_pat_parent = has_ref_pat_parent(syntax_element.clone());
self.in_loop_body = is_in_loop_body(syntax_element.clone());
self.inside_trait = inside_trait(syntax_element.clone());
self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone());
self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
}
fn fill(

View file

@ -3,48 +3,47 @@ use ra_syntax::{
ast::{self, LoopBodyOwner},
match_ast, AstNode, Direction, NodeOrToken, SyntaxElement,
SyntaxKind::*,
SyntaxNode,
SyntaxNode, SyntaxToken,
};
pub(crate) fn inside_impl(element: SyntaxElement) -> bool {
let node = match element {
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => token.parent(),
};
node.ancestors().find(|it| it.kind() == IMPL_DEF).is_some()
element.ancestors().find(|it| it.kind() == IMPL_DEF).is_some()
}
pub(crate) fn inside_trait(element: SyntaxElement) -> bool {
element.ancestors().find(|it| it.kind() == TRAIT_DEF).is_some()
}
pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool {
let node = match element {
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => token.parent(),
};
node.ancestors().find(|it| it.kind() == BIND_PAT).is_some()
element.ancestors().find(|it| it.kind() == BIND_PAT).is_some()
}
pub(crate) fn has_ref_pat_parent(element: SyntaxElement) -> bool {
let node = match element {
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => token.parent(),
};
node.ancestors().find(|it| it.kind() == REF_PAT).is_some()
element.ancestors().find(|it| it.kind() == REF_PAT).is_some()
}
pub(crate) fn goes_after_unsafe(element: SyntaxElement) -> bool {
if let Some(token) = previous_non_triva_element(element).and_then(|it| it.into_token()) {
if token.kind() == UNSAFE_KW {
return true;
}
}
false
element
.into_token()
.and_then(|it| previous_non_trivia_token(it))
.filter(|it| it.kind() == UNSAFE_KW)
.is_some()
}
pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
not_same_range_parent(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_parent(element).filter(|it| it.kind() == ITEM_LIST).is_some()
not_same_range_ancestor(element).filter(|it| it.kind() == ITEM_LIST).is_some()
}
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()
}
pub(crate) fn has_impl_as_prev_sibling(element: SyntaxElement) -> bool {
previous_sibling_or_ancestor_sibling(element).filter(|it| it.kind() == IMPL_DEF).is_some()
}
pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
@ -73,20 +72,30 @@ pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
false
}
fn not_same_range_parent(element: SyntaxElement) -> Option<SyntaxNode> {
let node = match element {
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => token.parent(),
};
let range = node.text_range();
node.ancestors().take_while(|it| it.text_range() == range).last().and_then(|it| it.parent())
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())
}
fn previous_non_triva_element(element: SyntaxElement) -> Option<SyntaxElement> {
// trying to get first non triva sibling if we have one
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> {
let token_sibling = non_trivia_sibling(element.clone(), Direction::Prev);
let mut wrapped = if let Some(sibling) = token_sibling {
sibling
if let Some(sibling) = token_sibling {
Some(sibling)
} else {
// if not trying to find first ancestor which has such a sibling
let node = match element {
@ -98,21 +107,6 @@ fn previous_non_triva_element(element: SyntaxElement) -> Option<SyntaxElement> {
let prev_sibling_node = top_node.ancestors().find(|it| {
non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some()
})?;
non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev)?
};
// TODO: Check if this can be simplified
// Matklad: I think you can avoid this loop if you use SyntaxToken::prev_token -- unlike prev_sibling_or_token, it works across parents.
// traversing the tree down to get the last token or node, i.e. the closest one
loop {
if let Some(token) = wrapped.as_token() {
return Some(NodeOrToken::Token(token.clone()));
} else {
let new = wrapped.as_node().and_then(|n| n.last_child_or_token());
if new.is_some() {
wrapped = new.unwrap().clone();
} else {
return Some(wrapped);
}
}
non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev)
}
}