Skip to content

Commit e6cff7a

Browse files
committed
Add config completion.addSemicolonToJumps
Example --- ```rust fn f() -> i32 { if true { $0 } } ``` **addSemicolonToJumps: true (default)** ```rust fn f() -> i32 { if true { return $0; } } ``` **addSemicolonToJumps: false** ```rust fn f() -> i32 { if true { return $0 } } ```
1 parent 2a40241 commit e6cff7a

File tree

8 files changed

+45
-5
lines changed

8 files changed

+45
-5
lines changed

crates/ide-completion/src/completions/expr.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,15 @@ pub(crate) fn complete_expr_path(
406406
}
407407

408408
if let Some(loop_ty) = innermost_breakable_ty {
409-
if in_block_expr {
409+
let semicolon = in_block_expr && ctx.config.add_semicolon_to_jumps;
410+
if semicolon {
410411
add_keyword("continue", "continue;");
411412
} else {
412413
add_keyword("continue", "continue");
413414
}
414415
add_keyword(
415416
"break",
416-
match (loop_ty.is_unit(), in_block_expr) {
417+
match (loop_ty.is_unit(), semicolon) {
417418
(true, true) => "break;",
418419
(true, false) => "break",
419420
(false, true) => "break $0;",
@@ -423,9 +424,10 @@ pub(crate) fn complete_expr_path(
423424
}
424425

425426
if let Some(ret_ty) = innermost_ret_ty {
427+
let semicolon = in_block_expr && ctx.config.add_semicolon_to_jumps;
426428
add_keyword(
427429
"return",
428-
match (ret_ty.is_unit(), in_block_expr) {
430+
match (ret_ty.is_unit(), semicolon) {
429431
(true, true) => {
430432
cov_mark::hit!(return_unit_block);
431433
"return;"

crates/ide-completion/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct CompletionConfig<'a> {
2424
pub term_search_fuel: u64,
2525
pub full_function_signatures: bool,
2626
pub callable: Option<CallableSnippets>,
27+
pub add_semicolon_to_jumps: bool,
2728
pub add_semicolon_to_unit: bool,
2829
pub snippet_cap: Option<SnippetCap>,
2930
pub insert_use: InsertUseConfig,

crates/ide-completion/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig<'_> = CompletionConfig {
7171
term_search_fuel: 200,
7272
full_function_signatures: false,
7373
callable: Some(CallableSnippets::FillArguments),
74+
add_semicolon_to_jumps: true,
7475
add_semicolon_to_unit: true,
7576
snippet_cap: SnippetCap::new(true),
7677
insert_use: InsertUseConfig {

crates/ide-completion/src/tests/expression.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::{
55
CompletionConfig,
66
config::AutoImportExclusionType,
77
tests::{
8-
BASE_ITEMS_FIXTURE, TEST_CONFIG, check, check_edit, check_with_base_items,
9-
completion_list_with_config,
8+
BASE_ITEMS_FIXTURE, TEST_CONFIG, check, check_edit, check_edit_with_config,
9+
check_with_base_items, completion_list_with_config,
1010
},
1111
};
1212

@@ -1077,6 +1077,12 @@ fn return_value_block() {
10771077
r#"fn f() -> i32 { if true { $0 } }"#,
10781078
r#"fn f() -> i32 { if true { return $0; } }"#,
10791079
);
1080+
check_edit_with_config(
1081+
CompletionConfig { add_semicolon_to_jumps: false, ..TEST_CONFIG },
1082+
"return",
1083+
r#"fn f() -> i32 { if true { $0 } }"#,
1084+
r#"fn f() -> i32 { if true { return $0 } }"#,
1085+
);
10801086
}
10811087

10821088
#[test]
@@ -1093,6 +1099,12 @@ fn return_value_no_block() {
10931099
fn break_unit_block() {
10941100
check_edit("break", r#"fn f() { loop { break; $0 } }"#, r#"fn f() { loop { break; break; } }"#);
10951101
check_edit("break", r#"fn f() { loop { $0 } }"#, r#"fn f() { loop { break; } }"#);
1102+
check_edit_with_config(
1103+
CompletionConfig { add_semicolon_to_jumps: false, ..TEST_CONFIG },
1104+
"break",
1105+
r#"fn f() { loop { $0 } }"#,
1106+
r#"fn f() { loop { break } }"#,
1107+
);
10961108
}
10971109

10981110
#[test]

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ config_data! {
592592
/// Term search fuel in "units of work" for assists (Defaults to 1800).
593593
assist_termSearch_fuel: usize = 1800,
594594

595+
/// Automatically add a semicolon when completing break, continue and return.
596+
completion_addSemicolonToJumps: bool = true,
597+
595598
/// Automatically add a semicolon when completing unit-returning functions.
596599
///
597600
/// In `match` arms it completes a comma instead.
@@ -1759,6 +1762,7 @@ impl Config {
17591762
CallableCompletionDef::AddParentheses => Some(CallableSnippets::AddParentheses),
17601763
CallableCompletionDef::None => None,
17611764
},
1765+
add_semicolon_to_jumps: *self.completion_addSemicolonToJumps(source_root),
17621766
add_semicolon_to_unit: *self.completion_addSemicolonToUnit(source_root),
17631767
snippet_cap: SnippetCap::new(self.completion_snippet()),
17641768
insert_use: self.insert_use_config(source_root),

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ fn integrated_completion_benchmark() {
180180
prefer_absolute: false,
181181
snippets: Vec::new(),
182182
limit: None,
183+
add_semicolon_to_jumps: true,
183184
add_semicolon_to_unit: true,
184185
fields_to_resolve: CompletionFieldsToResolve::empty(),
185186
exclude_flyimport: vec![],
@@ -235,6 +236,7 @@ fn integrated_completion_benchmark() {
235236
prefer_absolute: false,
236237
snippets: Vec::new(),
237238
limit: None,
239+
add_semicolon_to_jumps: true,
238240
add_semicolon_to_unit: true,
239241
fields_to_resolve: CompletionFieldsToResolve::empty(),
240242
exclude_flyimport: vec![],
@@ -288,6 +290,7 @@ fn integrated_completion_benchmark() {
288290
prefer_absolute: false,
289291
snippets: Vec::new(),
290292
limit: None,
293+
add_semicolon_to_jumps: true,
291294
add_semicolon_to_unit: true,
292295
fields_to_resolve: CompletionFieldsToResolve::empty(),
293296
exclude_flyimport: vec![],

docs/book/src/configuration_generated.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ If false, `-p <package>` will be passed instead if applicable. In case it is not
359359
check will be performed.
360360

361361

362+
## rust-analyzer.completion.addSemicolonToJumps {#completion.addSemicolonToJumps}
363+
364+
Default: `true`
365+
366+
Automatically add a semicolon when completing break, continue and return.
367+
368+
362369
## rust-analyzer.completion.addSemicolonToUnit {#completion.addSemicolonToUnit}
363370

364371
Default: `true`

editors/code/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,16 @@
12531253
}
12541254
}
12551255
},
1256+
{
1257+
"title": "Completion",
1258+
"properties": {
1259+
"rust-analyzer.completion.addSemicolonToJumps": {
1260+
"markdownDescription": "Automatically add a semicolon when completing break, continue and return.",
1261+
"default": true,
1262+
"type": "boolean"
1263+
}
1264+
}
1265+
},
12561266
{
12571267
"title": "Completion",
12581268
"properties": {

0 commit comments

Comments
 (0)