This commit is contained in:
A4-Tacks 2025-12-22 16:36:17 +01:00 committed by GitHub
commit 3832d031b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 45 additions and 5 deletions

View file

@ -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;",
@ -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;"

View file

@ -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,

View file

@ -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 {

View file

@ -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,
},
};
@ -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]
@ -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]

View file

@ -599,6 +599,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.
@ -1768,6 +1771,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),

View file

@ -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![],
@ -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![],
@ -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![],

View file

@ -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`

View file

@ -1258,6 +1258,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": {