Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,15 @@ pub(crate) fn complete_expr_path(
}

if let Some(loop_ty) = innermost_breakable_ty {
if in_block_expr {
let semicolon = in_block_expr && ctx.config.add_semicolon_to_jumps;
if semicolon {
add_keyword("continue", "continue;");
} else {
add_keyword("continue", "continue");
}
add_keyword(
"break",
match (loop_ty.is_unit(), in_block_expr) {
match (loop_ty.is_unit(), semicolon) {
(true, true) => "break;",
(true, false) => "break",
(false, true) => "break $0;",
Expand All @@ -423,9 +424,10 @@ pub(crate) fn complete_expr_path(
}

if let Some(ret_ty) = innermost_ret_ty {
let semicolon = in_block_expr && ctx.config.add_semicolon_to_jumps;
add_keyword(
"return",
match (ret_ty.is_unit(), in_block_expr) {
match (ret_ty.is_unit(), semicolon) {
(true, true) => {
cov_mark::hit!(return_unit_block);
"return;"
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct CompletionConfig<'a> {
pub term_search_fuel: u64,
pub full_function_signatures: bool,
pub callable: Option<CallableSnippets>,
pub add_semicolon_to_jumps: bool,
pub add_semicolon_to_unit: bool,
pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig,
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig<'_> = CompletionConfig {
term_search_fuel: 200,
full_function_signatures: false,
callable: Some(CallableSnippets::FillArguments),
add_semicolon_to_jumps: true,
add_semicolon_to_unit: true,
snippet_cap: SnippetCap::new(true),
insert_use: InsertUseConfig {
Expand Down
16 changes: 14 additions & 2 deletions crates/ide-completion/src/tests/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
CompletionConfig,
config::AutoImportExclusionType,
tests::{
BASE_ITEMS_FIXTURE, TEST_CONFIG, check, check_edit, check_with_base_items,
completion_list_with_config,
BASE_ITEMS_FIXTURE, TEST_CONFIG, check, check_edit, check_edit_with_config,
check_with_base_items, completion_list_with_config,
},
};

Expand Down Expand Up @@ -1077,6 +1077,12 @@ fn return_value_block() {
r#"fn f() -> i32 { if true { $0 } }"#,
r#"fn f() -> i32 { if true { return $0; } }"#,
);
check_edit_with_config(
CompletionConfig { add_semicolon_to_jumps: false, ..TEST_CONFIG },
"return",
r#"fn f() -> i32 { if true { $0 } }"#,
r#"fn f() -> i32 { if true { return $0 } }"#,
);
}

#[test]
Expand All @@ -1093,6 +1099,12 @@ fn return_value_no_block() {
fn break_unit_block() {
check_edit("break", r#"fn f() { loop { break; $0 } }"#, r#"fn f() { loop { break; break; } }"#);
check_edit("break", r#"fn f() { loop { $0 } }"#, r#"fn f() { loop { break; } }"#);
check_edit_with_config(
CompletionConfig { add_semicolon_to_jumps: false, ..TEST_CONFIG },
"break",
r#"fn f() { loop { $0 } }"#,
r#"fn f() { loop { break } }"#,
);
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ config_data! {
/// Term search fuel in "units of work" for assists (Defaults to 1800).
assist_termSearch_fuel: usize = 1800,

/// Automatically add a semicolon when completing break, continue and return.
completion_addSemicolonToJumps: bool = true,

/// Automatically add a semicolon when completing unit-returning functions.
///
/// In `match` arms it completes a comma instead.
Expand Down Expand Up @@ -1759,6 +1762,7 @@ impl Config {
CallableCompletionDef::AddParentheses => Some(CallableSnippets::AddParentheses),
CallableCompletionDef::None => None,
},
add_semicolon_to_jumps: *self.completion_addSemicolonToJumps(source_root),
add_semicolon_to_unit: *self.completion_addSemicolonToUnit(source_root),
snippet_cap: SnippetCap::new(self.completion_snippet()),
insert_use: self.insert_use_config(source_root),
Expand Down
3 changes: 3 additions & 0 deletions crates/rust-analyzer/src/integrated_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fn integrated_completion_benchmark() {
prefer_absolute: false,
snippets: Vec::new(),
limit: None,
add_semicolon_to_jumps: true,
add_semicolon_to_unit: true,
fields_to_resolve: CompletionFieldsToResolve::empty(),
exclude_flyimport: vec![],
Expand Down Expand Up @@ -235,6 +236,7 @@ fn integrated_completion_benchmark() {
prefer_absolute: false,
snippets: Vec::new(),
limit: None,
add_semicolon_to_jumps: true,
add_semicolon_to_unit: true,
fields_to_resolve: CompletionFieldsToResolve::empty(),
exclude_flyimport: vec![],
Expand Down Expand Up @@ -288,6 +290,7 @@ fn integrated_completion_benchmark() {
prefer_absolute: false,
snippets: Vec::new(),
limit: None,
add_semicolon_to_jumps: true,
add_semicolon_to_unit: true,
fields_to_resolve: CompletionFieldsToResolve::empty(),
exclude_flyimport: vec![],
Expand Down
7 changes: 7 additions & 0 deletions docs/book/src/configuration_generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ If false, `-p <package>` will be passed instead if applicable. In case it is not
check will be performed.


## rust-analyzer.completion.addSemicolonToJumps {#completion.addSemicolonToJumps}

Default: `true`

Automatically add a semicolon when completing break, continue and return.


## rust-analyzer.completion.addSemicolonToUnit {#completion.addSemicolonToUnit}

Default: `true`
Expand Down
10 changes: 10 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,16 @@
}
}
},
{
"title": "Completion",
"properties": {
"rust-analyzer.completion.addSemicolonToJumps": {
"markdownDescription": "Automatically add a semicolon when completing break, continue and return.",
"default": true,
"type": "boolean"
}
}
},
{
"title": "Completion",
"properties": {
Expand Down