Add more patterns, tests and fix keywords

This commit is contained in:
Mikhail Rakhmanov 2020-06-13 00:55:21 +02:00
parent 3576671043
commit 6feb52c12a
3 changed files with 322 additions and 498 deletions

View file

@ -60,32 +60,104 @@ fn add_keyword(
} }
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
let has_trait_or_impl_parent = ctx.has_impl_parent || ctx.has_trait_parent;
if ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling {
add_keyword(ctx, acc, "where", "where ", true);
return;
}
if ctx.unsafe_is_prev {
add_keyword(ctx, acc, "fn", "fn $0() {}", ctx.is_new_item || ctx.block_expr_parent);
add_keyword(
ctx,
acc,
"trait",
"trait $0 {}",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(
ctx,
acc,
"impl",
"impl $0 {}",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
return;
}
add_keyword(ctx, acc, "fn", "fn $0() {}", 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(
add_keyword(ctx, acc, "impl", "impl $0 {}", ctx.is_new_item); ctx,
add_keyword(ctx, acc, "trait", "impl $0 {}", ctx.is_new_item); acc,
add_keyword(ctx, acc, "enum", "enum $0 {}", ctx.is_new_item && !ctx.unsafe_is_prev); "use",
add_keyword(ctx, acc, "struct", "struct $0 {}", ctx.is_new_item && !ctx.unsafe_is_prev); "use ",
add_keyword(ctx, acc, "union", "union $0 {}", ctx.is_new_item && !ctx.unsafe_is_prev); (ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
add_keyword(ctx, acc, "match", "match $0 {}", ctx.block_expr_parent); );
add_keyword(ctx, acc, "loop", "loop {$0}", ctx.block_expr_parent); add_keyword(
ctx,
acc,
"impl",
"impl $0 {}",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(
ctx,
acc,
"trait",
"trait $0 {}",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(ctx, acc, "enum", "enum $0 {}", ctx.is_new_item && !has_trait_or_impl_parent);
add_keyword(ctx, acc, "struct", "struct $0 {}", ctx.is_new_item && !has_trait_or_impl_parent);
add_keyword(ctx, acc, "union", "union $0 {}", ctx.is_new_item && !has_trait_or_impl_parent);
add_keyword(ctx, acc, "match", "match $0 {}", ctx.block_expr_parent || ctx.is_match_arm);
add_keyword(ctx, acc, "loop", "loop {$0}", ctx.block_expr_parent || ctx.is_match_arm);
add_keyword(ctx, acc, "while", "while $0 {}", ctx.block_expr_parent); add_keyword(ctx, acc, "while", "while $0 {}", ctx.block_expr_parent);
add_keyword(ctx, acc, "let", "let ", ctx.if_is_prev || ctx.block_expr_parent); add_keyword(ctx, acc, "let", "let ", ctx.if_is_prev || ctx.block_expr_parent);
add_keyword(ctx, acc, "if", "if ", ctx.if_is_prev || ctx.block_expr_parent || ctx.is_match_arm);
add_keyword(
ctx,
acc,
"if let",
"if let ",
ctx.if_is_prev || ctx.block_expr_parent || ctx.is_match_arm,
);
add_keyword(ctx, acc, "else", "else {$0}", ctx.after_if); 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, "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,
"mod",
"mod $0 {}",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent); add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent);
add_keyword(ctx, acc, "const", "const ", ctx.is_new_item || ctx.block_expr_parent); 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, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
add_keyword(ctx, acc, "static", "static ", ctx.is_new_item || ctx.block_expr_parent); add_keyword(
add_keyword(ctx, acc, "extern", "extern ", ctx.is_new_item || ctx.block_expr_parent); ctx,
add_keyword(ctx, acc, "unsafe", "unsafe ", ctx.is_new_item || ctx.block_expr_parent); acc,
"static",
"static ",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(
ctx,
acc,
"extern",
"extern ",
(ctx.is_new_item && !has_trait_or_impl_parent) || ctx.block_expr_parent,
);
add_keyword(
ctx,
acc,
"unsafe",
"unsafe ",
ctx.is_new_item || ctx.block_expr_parent || ctx.is_match_arm,
);
add_keyword(ctx, acc, "continue", "continue;", 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, "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, "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, "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, "pub", "pub ", ctx.is_new_item && !ctx.has_trait_parent);
add_keyword(ctx, acc, "where", "where ", ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling);
let fn_def = match &ctx.function_syntax { let fn_def = match &ctx.function_syntax {
Some(it) => it, Some(it) => it,
@ -111,21 +183,17 @@ fn complete_return(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::completion::{ use crate::completion::{
test_utils::{do_completion, get_completions}, test_utils::get_completions,
CompletionItem, CompletionKind, CompletionKind,
}; };
use insta::assert_debug_snapshot; use insta::assert_debug_snapshot;
fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Keyword)
}
fn get_keyword_completions(code: &str) -> Vec<String> { fn get_keyword_completions(code: &str) -> Vec<String> {
get_completions(code, CompletionKind::Keyword) get_completions(code, CompletionKind::Keyword)
} }
#[test] #[test]
fn completes_keywords_in_use_stmt() { fn test_keywords_in_use_stmt() {
assert_debug_snapshot!( assert_debug_snapshot!(
get_keyword_completions(r"use <|>"), get_keyword_completions(r"use <|>"),
@r###" @r###"
@ -159,7 +227,7 @@ mod tests {
} }
#[test] #[test]
fn completes_various_keywords_in_function() { fn test_keywords_in_function() {
assert_debug_snapshot!( assert_debug_snapshot!(
get_keyword_completions(r"fn quux() { <|> }"), get_keyword_completions(r"fn quux() { <|> }"),
@r###" @r###"
@ -167,12 +235,16 @@ mod tests {
"kw const", "kw const",
"kw extern", "kw extern",
"kw fn", "kw fn",
"kw if",
"kw if let",
"kw impl",
"kw let", "kw let",
"kw loop", "kw loop",
"kw match", "kw match",
"kw mod", "kw mod",
"kw return", "kw return",
"kw static", "kw static",
"kw trait",
"kw type", "kw type",
"kw unsafe", "kw unsafe",
"kw use", "kw use",
@ -183,9 +255,37 @@ mod tests {
} }
#[test] #[test]
fn completes_else_after_if() { fn test_keywords_inside_block() {
assert_debug_snapshot!( assert_debug_snapshot!(
do_keyword_completion( get_keyword_completions(r"fn quux() { if true { <|> } }"),
@r###"
[
"kw const",
"kw extern",
"kw fn",
"kw if",
"kw if let",
"kw impl",
"kw let",
"kw loop",
"kw match",
"kw mod",
"kw return",
"kw static",
"kw trait",
"kw type",
"kw unsafe",
"kw use",
"kw while",
]
"###
);
}
#[test]
fn test_keywords_after_if() {
assert_debug_snapshot!(
get_keyword_completions(
r" r"
fn quux() { fn quux() {
if true { if true {
@ -196,166 +296,34 @@ mod tests {
), ),
@r###" @r###"
[ [
CompletionItem { "kw const",
label: "else", "kw else",
source_range: 108..108, "kw else if",
delete: 108..108, "kw extern",
insert: "else {$0}", "kw fn",
kind: Keyword, "kw if",
}, "kw if let",
CompletionItem { "kw impl",
label: "else if", "kw let",
source_range: 108..108, "kw loop",
delete: 108..108, "kw match",
insert: "else if $0 {}", "kw mod",
kind: Keyword, "kw return",
}, "kw static",
CompletionItem { "kw trait",
label: "if", "kw type",
source_range: 108..108, "kw unsafe",
delete: 108..108, "kw use",
insert: "if $0 {}", "kw while",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 108..108,
delete: 108..108,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 108..108,
delete: 108..108,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 108..108,
delete: 108..108,
insert: "return;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 108..108,
delete: 108..108,
insert: "while $0 {}",
kind: Keyword,
},
] ]
"### "###
); );
} }
#[test] #[test]
fn test_completion_return_value() { fn test_keywords_in_match_arm() {
assert_debug_snapshot!( assert_debug_snapshot!(
do_keyword_completion( get_keyword_completions(
r"
fn quux() -> i32 {
<|>
92
}
",
),
@r###"
[
CompletionItem {
label: "if",
source_range: 56..56,
delete: 56..56,
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 56..56,
delete: 56..56,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 56..56,
delete: 56..56,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 56..56,
delete: 56..56,
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 56..56,
delete: 56..56,
insert: "while $0 {}",
kind: Keyword,
},
]
"###
);
assert_debug_snapshot!(
do_keyword_completion(
r"
fn quux() {
<|>
92
}
",
),
@r###"
[
CompletionItem {
label: "if",
source_range: 49..49,
delete: 49..49,
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 49..49,
delete: 49..49,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 49..49,
delete: 49..49,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 49..49,
delete: 49..49,
insert: "return;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 49..49,
delete: 49..49,
insert: "while $0 {}",
kind: Keyword,
},
]
"###
);
}
#[test]
fn dont_add_semi_after_return_if_not_a_statement() {
assert_debug_snapshot!(
do_keyword_completion(
r" r"
fn quux() -> i32 { fn quux() -> i32 {
match () { match () {
@ -366,335 +334,151 @@ mod tests {
), ),
@r###" @r###"
[ [
CompletionItem { "kw if",
label: "if", "kw if let",
source_range: 97..97, "kw loop",
delete: 97..97, "kw match",
insert: "if $0 {}", "kw return",
kind: Keyword, "kw unsafe",
},
CompletionItem {
label: "loop",
source_range: 97..97,
delete: 97..97,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 97..97,
delete: 97..97,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 97..97,
delete: 97..97,
insert: "return $0",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 97..97,
delete: 97..97,
insert: "while $0 {}",
kind: Keyword,
},
] ]
"### "###
); );
} }
#[test] #[test]
fn last_return_in_block_has_semi() { fn test_keywords_in_trait_def() {
assert_debug_snapshot!( assert_debug_snapshot!(
do_keyword_completion( get_keyword_completions(r"trait My { <|> }"),
r"
fn quux() -> i32 {
if condition {
<|>
}
}
",
),
@r###" @r###"
[ [
CompletionItem { "kw const",
label: "if", "kw fn",
source_range: 95..95, "kw type",
delete: 95..95, "kw unsafe",
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 95..95,
delete: 95..95,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 95..95,
delete: 95..95,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 95..95,
delete: 95..95,
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 95..95,
delete: 95..95,
insert: "while $0 {}",
kind: Keyword,
},
]
"###
);
assert_debug_snapshot!(
do_keyword_completion(
r"
fn quux() -> i32 {
if condition {
<|>
}
let x = 92;
x
}
",
),
@r###"
[
CompletionItem {
label: "if",
source_range: 95..95,
delete: 95..95,
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 95..95,
delete: 95..95,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 95..95,
delete: 95..95,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 95..95,
delete: 95..95,
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 95..95,
delete: 95..95,
insert: "while $0 {}",
kind: Keyword,
},
] ]
"### "###
); );
} }
#[test] #[test]
fn completes_break_and_continue_in_loops() { fn test_keywords_in_impl_def() {
assert_debug_snapshot!( assert_debug_snapshot!(
do_keyword_completion( get_keyword_completions(r"impl My { <|> }"),
r"
fn quux() -> i32 {
loop { <|> }
}
",
),
@r###" @r###"
[ [
CompletionItem { "kw const",
label: "break", "kw fn",
source_range: 63..63, "kw pub",
delete: 63..63, "kw type",
insert: "break;", "kw unsafe",
kind: Keyword,
},
CompletionItem {
label: "continue",
source_range: 63..63,
delete: 63..63,
insert: "continue;",
kind: Keyword,
},
CompletionItem {
label: "if",
source_range: 63..63,
delete: 63..63,
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 63..63,
delete: 63..63,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 63..63,
delete: 63..63,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 63..63,
delete: 63..63,
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 63..63,
delete: 63..63,
insert: "while $0 {}",
kind: Keyword,
},
]
"###
);
// No completion: lambda isolates control flow
assert_debug_snapshot!(
do_keyword_completion(
r"
fn quux() -> i32 {
loop { || { <|> } }
}
",
),
@r###"
[
CompletionItem {
label: "if",
source_range: 68..68,
delete: 68..68,
insert: "if $0 {}",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 68..68,
delete: 68..68,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 68..68,
delete: 68..68,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 68..68,
delete: 68..68,
insert: "return $0;",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 68..68,
delete: 68..68,
insert: "while $0 {}",
kind: Keyword,
},
] ]
"### "###
); );
} }
#[test] #[test]
fn no_semi_after_break_continue_in_expr() { fn test_keywords_in_loop() {
assert_debug_snapshot!( assert_debug_snapshot!(
do_keyword_completion( get_keyword_completions(r"fn my() { loop { <|> } }"),
r"
fn f() {
loop {
match () {
() => br<|>
}
}
}
",
),
@r###" @r###"
[ [
CompletionItem { "kw break",
label: "break", "kw const",
source_range: 122..124, "kw continue",
delete: 122..124, "kw extern",
insert: "break", "kw fn",
kind: Keyword, "kw if",
}, "kw if let",
CompletionItem { "kw impl",
label: "continue", "kw let",
source_range: 122..124, "kw loop",
delete: 122..124, "kw match",
insert: "continue", "kw mod",
kind: Keyword, "kw return",
}, "kw static",
CompletionItem { "kw trait",
label: "if", "kw type",
source_range: 122..124, "kw unsafe",
delete: 122..124, "kw use",
insert: "if $0 {}", "kw while",
kind: Keyword,
},
CompletionItem {
label: "loop",
source_range: 122..124,
delete: 122..124,
insert: "loop {$0}",
kind: Keyword,
},
CompletionItem {
label: "match",
source_range: 122..124,
delete: 122..124,
insert: "match $0 {}",
kind: Keyword,
},
CompletionItem {
label: "return",
source_range: 122..124,
delete: 122..124,
insert: "return",
kind: Keyword,
},
CompletionItem {
label: "while",
source_range: 122..124,
delete: 122..124,
insert: "while $0 {}",
kind: Keyword,
},
] ]
"### "###
) );
}
#[test]
fn test_keywords_after_unsafe_in_item_list() {
assert_debug_snapshot!(
get_keyword_completions(r"unsafe <|>"),
@r###"
[
"kw fn",
"kw impl",
"kw trait",
]
"###
);
}
#[test]
fn test_keywords_after_unsafe_in_block_expr() {
assert_debug_snapshot!(
get_keyword_completions(r"fn my_fn() { unsafe <|> }"),
@r###"
[
"kw fn",
"kw impl",
"kw trait",
]
"###
);
}
#[test]
fn test_mut_in_ref_and_in_fn_parameters_list() {
assert_debug_snapshot!(
get_keyword_completions(r"fn my_fn(&<|>) {}"),
@r###"
[
"kw mut",
]
"###
);
assert_debug_snapshot!(
get_keyword_completions(r"fn my_fn(<|>) {}"),
@r###"
[
"kw mut",
]
"###
);
assert_debug_snapshot!(
get_keyword_completions(r"fn my_fn() { let &<|> }"),
@r###"
[
"kw mut",
]
"###
);
}
#[test]
fn test_where_keyword() {
assert_debug_snapshot!(
get_keyword_completions(r"trait A <|>"),
@r###"
[
"kw where",
]
"###
);
assert_debug_snapshot!(
get_keyword_completions(r"impl A <|>"),
@r###"
[
"kw where",
]
"###
);
} }
} }

View file

@ -12,8 +12,9 @@ use ra_syntax::{
use ra_text_edit::Indel; use ra_text_edit::Indel;
use super::patterns::{ use super::patterns::{
has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_ref_pat_parent, has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_impl_parent,
has_trait_as_prev_sibling, if_is_prev, inside_trait, is_in_loop_body, unsafe_is_prev, has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev, is_in_loop_body,
is_match_arm, unsafe_is_prev,
}; };
use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition}; use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
use test_utils::mark; use test_utils::mark;
@ -70,9 +71,11 @@ pub(crate) struct CompletionContext<'a> {
pub(super) bind_pat_parent: bool, pub(super) bind_pat_parent: bool,
pub(super) ref_pat_parent: bool, pub(super) ref_pat_parent: bool,
pub(super) in_loop_body: bool, pub(super) in_loop_body: bool,
pub(super) inside_trait: bool, pub(super) has_trait_parent: bool,
pub(super) has_impl_parent: bool,
pub(super) trait_as_prev_sibling: bool, pub(super) trait_as_prev_sibling: bool,
pub(super) impl_as_prev_sibling: bool, pub(super) impl_as_prev_sibling: bool,
pub(super) is_match_arm: bool,
} }
impl<'a> CompletionContext<'a> { impl<'a> CompletionContext<'a> {
@ -136,10 +139,12 @@ impl<'a> CompletionContext<'a> {
ref_pat_parent: false, ref_pat_parent: false,
bind_pat_parent: false, bind_pat_parent: false,
block_expr_parent: false, block_expr_parent: false,
inside_trait: false, has_trait_parent: false,
has_impl_parent: false,
trait_as_prev_sibling: false, trait_as_prev_sibling: false,
impl_as_prev_sibling: false, impl_as_prev_sibling: false,
if_is_prev: false, if_is_prev: false,
is_match_arm: false,
}; };
let mut original_file = original_file.syntax().clone(); let mut original_file = original_file.syntax().clone();
@ -217,11 +222,13 @@ impl<'a> CompletionContext<'a> {
self.unsafe_is_prev = unsafe_is_prev(syntax_element.clone()); self.unsafe_is_prev = unsafe_is_prev(syntax_element.clone());
self.if_is_prev = if_is_prev(syntax_element.clone()); self.if_is_prev = if_is_prev(syntax_element.clone());
self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone()); self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone());
self.ref_pat_parent = has_ref_pat_parent(syntax_element.clone()); self.ref_pat_parent = has_ref_parent(syntax_element.clone());
self.in_loop_body = is_in_loop_body(syntax_element.clone()); self.in_loop_body = is_in_loop_body(syntax_element.clone());
self.inside_trait = inside_trait(syntax_element.clone()); self.has_trait_parent = has_trait_parent(syntax_element.clone());
self.has_impl_parent = has_impl_parent(syntax_element.clone());
self.impl_as_prev_sibling = has_impl_as_prev_sibling(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()); self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
self.is_match_arm = is_match_arm(syntax_element.clone());
} }
fn fill( fn fill(

View file

@ -6,16 +6,42 @@ use ra_syntax::{
SyntaxNode, SyntaxToken, SyntaxNode, SyntaxToken,
}; };
pub(crate) fn inside_trait(element: SyntaxElement) -> bool { pub(crate) fn has_trait_parent(element: SyntaxElement) -> bool {
element.ancestors().find(|it| it.kind() == TRAIT_DEF).is_some() not_same_range_ancestor(element)
.filter(|it| it.kind() == ITEM_LIST)
.and_then(|it| it.parent())
.filter(|it| it.kind() == TRAIT_DEF)
.is_some()
}
pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool {
not_same_range_ancestor(element)
.filter(|it| it.kind() == ITEM_LIST)
.and_then(|it| it.parent())
.filter(|it| it.kind() == IMPL_DEF)
.is_some()
}
pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some()
} }
pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool { pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool {
element.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 { pub(crate) fn has_ref_parent(element: SyntaxElement) -> bool {
element.ancestors().find(|it| it.kind() == REF_PAT).is_some() not_same_range_ancestor(element)
.filter(|it| it.kind() == REF_PAT || it.kind() == REF_EXPR)
.is_some()
}
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()
} }
pub(crate) fn unsafe_is_prev(element: SyntaxElement) -> bool { pub(crate) fn unsafe_is_prev(element: SyntaxElement) -> bool {
@ -34,10 +60,6 @@ pub(crate) fn if_is_prev(element: SyntaxElement) -> bool {
.is_some() .is_some()
} }
pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).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()
} }
@ -114,8 +136,9 @@ fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<Syntax
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{ use super::{
has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_ref_pat_parent, has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_impl_parent,
has_trait_as_prev_sibling, if_is_prev, inside_trait, unsafe_is_prev, has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev, is_match_arm,
unsafe_is_prev,
}; };
use crate::completion::test_utils::check_pattern_is_applicable; use crate::completion::test_utils::check_pattern_is_applicable;
@ -130,8 +153,13 @@ mod tests {
} }
#[test] #[test]
fn test_inside_trait() { fn test_has_trait_parent() {
check_pattern_is_applicable(r"trait A { fn<|> }", inside_trait); check_pattern_is_applicable(r"trait A { f<|> }", has_trait_parent);
}
#[test]
fn test_has_impl_parent() {
check_pattern_is_applicable(r"impl A { f<|> }", has_impl_parent);
} }
#[test] #[test]
@ -151,12 +179,12 @@ mod tests {
#[test] #[test]
fn test_has_ref_pat_parent_in_func_parameters() { fn test_has_ref_pat_parent_in_func_parameters() {
check_pattern_is_applicable(r"fn my_fn(&<|>) {}", has_ref_pat_parent); check_pattern_is_applicable(r"fn my_fn(&m<|>) {}", has_ref_parent);
} }
#[test] #[test]
fn test_has_ref_pat_parent_in_let_statement() { fn test_has_ref_pat_parent_in_let_statement() {
check_pattern_is_applicable(r"fn my_fn() { let &<|> }", has_ref_pat_parent); check_pattern_is_applicable(r"fn my() { let &m<|> }", has_ref_parent);
} }
#[test] #[test]
@ -168,4 +196,9 @@ mod tests {
fn test_has_bind_pat_parent_in_let_statement() { fn test_has_bind_pat_parent_in_let_statement() {
check_pattern_is_applicable(r"fn my_fn() { let m<|> }", has_bind_pat_parent); check_pattern_is_applicable(r"fn my_fn() { let m<|> }", has_bind_pat_parent);
} }
#[test]
fn test_is_match_arm() {
check_pattern_is_applicable(r"fn my_fn() { match () { () => m<|> } }", is_match_arm);
}
} }