mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Insert ;
when completing keywords in let
This commit is contained in:
parent
41ea260201
commit
f2ba2048d1
2 changed files with 69 additions and 7 deletions
|
@ -161,7 +161,17 @@ fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet
|
||||||
let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
|
let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
|
||||||
.kind(CompletionItemKind::Keyword);
|
.kind(CompletionItemKind::Keyword);
|
||||||
let builder = match ctx.config.snippet_cap {
|
let builder = match ctx.config.snippet_cap {
|
||||||
Some(cap) => builder.insert_snippet(cap, snippet),
|
Some(cap) => {
|
||||||
|
let tmp;
|
||||||
|
let snippet = if snippet.ends_with('}') && ctx.incomplete_let {
|
||||||
|
mark::hit!(let_semi);
|
||||||
|
tmp = format!("{};", snippet);
|
||||||
|
&tmp
|
||||||
|
} else {
|
||||||
|
snippet
|
||||||
|
};
|
||||||
|
builder.insert_snippet(cap, snippet)
|
||||||
|
}
|
||||||
None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
|
None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
|
||||||
};
|
};
|
||||||
acc.add(builder.build());
|
acc.add(builder.build());
|
||||||
|
@ -601,4 +611,50 @@ fn foo() {
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn let_semi() {
|
||||||
|
mark::check!(let_semi);
|
||||||
|
check_edit(
|
||||||
|
"match",
|
||||||
|
r#"
|
||||||
|
fn main() { let x = $0 }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() { let x = match $0 {}; }
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"if",
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = $0
|
||||||
|
let y = 92;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = if $0 {};
|
||||||
|
let y = 92;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"loop",
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = $0
|
||||||
|
bar();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = loop {$0};
|
||||||
|
bar();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,7 @@ pub(crate) struct CompletionContext<'a> {
|
||||||
pub(super) has_item_list_or_source_file_parent: bool,
|
pub(super) has_item_list_or_source_file_parent: bool,
|
||||||
pub(super) for_is_prev2: bool,
|
pub(super) for_is_prev2: bool,
|
||||||
pub(super) fn_is_prev: bool,
|
pub(super) fn_is_prev: bool,
|
||||||
|
pub(super) incomplete_let: bool,
|
||||||
pub(super) locals: Vec<(String, Local)>,
|
pub(super) locals: Vec<(String, Local)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,9 +133,9 @@ impl<'a> CompletionContext<'a> {
|
||||||
scope,
|
scope,
|
||||||
db,
|
db,
|
||||||
config,
|
config,
|
||||||
|
position,
|
||||||
original_token,
|
original_token,
|
||||||
token,
|
token,
|
||||||
position,
|
|
||||||
krate,
|
krate,
|
||||||
expected_type: None,
|
expected_type: None,
|
||||||
name_ref_syntax: None,
|
name_ref_syntax: None,
|
||||||
|
@ -155,30 +156,31 @@ impl<'a> CompletionContext<'a> {
|
||||||
is_expr: false,
|
is_expr: false,
|
||||||
is_new_item: false,
|
is_new_item: false,
|
||||||
dot_receiver: None,
|
dot_receiver: None,
|
||||||
|
dot_receiver_is_ambiguous_float_literal: false,
|
||||||
is_call: false,
|
is_call: false,
|
||||||
is_pattern_call: false,
|
is_pattern_call: false,
|
||||||
is_macro_call: false,
|
is_macro_call: false,
|
||||||
is_path_type: false,
|
is_path_type: false,
|
||||||
has_type_args: false,
|
has_type_args: false,
|
||||||
dot_receiver_is_ambiguous_float_literal: false,
|
|
||||||
attribute_under_caret: None,
|
attribute_under_caret: None,
|
||||||
mod_declaration_under_caret: None,
|
mod_declaration_under_caret: None,
|
||||||
unsafe_is_prev: false,
|
unsafe_is_prev: false,
|
||||||
in_loop_body: false,
|
if_is_prev: false,
|
||||||
ref_pat_parent: false,
|
|
||||||
bind_pat_parent: false,
|
|
||||||
block_expr_parent: false,
|
block_expr_parent: false,
|
||||||
|
bind_pat_parent: false,
|
||||||
|
ref_pat_parent: false,
|
||||||
|
in_loop_body: false,
|
||||||
has_trait_parent: false,
|
has_trait_parent: false,
|
||||||
has_impl_parent: false,
|
has_impl_parent: false,
|
||||||
inside_impl_trait_block: false,
|
inside_impl_trait_block: false,
|
||||||
has_field_list_parent: false,
|
has_field_list_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,
|
|
||||||
is_match_arm: false,
|
is_match_arm: false,
|
||||||
has_item_list_or_source_file_parent: false,
|
has_item_list_or_source_file_parent: false,
|
||||||
for_is_prev2: false,
|
for_is_prev2: false,
|
||||||
fn_is_prev: false,
|
fn_is_prev: false,
|
||||||
|
incomplete_let: false,
|
||||||
locals,
|
locals,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -270,6 +272,10 @@ impl<'a> CompletionContext<'a> {
|
||||||
.filter(|module| module.item_list().is_none());
|
.filter(|module| module.item_list().is_none());
|
||||||
self.for_is_prev2 = for_is_prev2(syntax_element.clone());
|
self.for_is_prev2 = for_is_prev2(syntax_element.clone());
|
||||||
self.fn_is_prev = fn_is_prev(syntax_element.clone());
|
self.fn_is_prev = fn_is_prev(syntax_element.clone());
|
||||||
|
self.incomplete_let =
|
||||||
|
syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
|
||||||
|
it.syntax().text_range().end() == syntax_element.text_range().end()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill(
|
fn fill(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue