mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
Merge pull request #20390 from A4-Tacks/if-else-comp-in-args-or-let
Add if..else completions in LetStmt and ArgList
This commit is contained in:
commit
b06ce60086
@ -61,6 +61,7 @@ pub(crate) fn complete_expr_path(
|
|||||||
after_if_expr,
|
after_if_expr,
|
||||||
in_condition,
|
in_condition,
|
||||||
incomplete_let,
|
incomplete_let,
|
||||||
|
in_value,
|
||||||
ref ref_expr_parent,
|
ref ref_expr_parent,
|
||||||
after_amp,
|
after_amp,
|
||||||
ref is_func_update,
|
ref is_func_update,
|
||||||
@ -361,10 +362,16 @@ pub(crate) fn complete_expr_path(
|
|||||||
add_keyword("loop", "loop {\n $0\n}");
|
add_keyword("loop", "loop {\n $0\n}");
|
||||||
if in_match_guard {
|
if in_match_guard {
|
||||||
add_keyword("if", "if $0");
|
add_keyword("if", "if $0");
|
||||||
|
} else if in_value {
|
||||||
|
add_keyword("if", "if $1 {\n $2\n} else {\n $0\n}");
|
||||||
} else {
|
} else {
|
||||||
add_keyword("if", "if $1 {\n $0\n}");
|
add_keyword("if", "if $1 {\n $0\n}");
|
||||||
}
|
}
|
||||||
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
|
if in_value {
|
||||||
|
add_keyword("if let", "if let $1 = $2 {\n $3\n} else {\n $0\n}");
|
||||||
|
} else {
|
||||||
|
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
|
||||||
|
}
|
||||||
add_keyword("for", "for $1 in $2 {\n $0\n}");
|
add_keyword("for", "for $1 in $2 {\n $0\n}");
|
||||||
add_keyword("true", "true");
|
add_keyword("true", "true");
|
||||||
add_keyword("false", "false");
|
add_keyword("false", "false");
|
||||||
|
@ -238,6 +238,8 @@ fn main() {
|
|||||||
r#"
|
r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = if $1 {
|
let x = if $1 {
|
||||||
|
$2
|
||||||
|
} else {
|
||||||
$0
|
$0
|
||||||
};
|
};
|
||||||
let y = 92;
|
let y = 92;
|
||||||
@ -335,6 +337,120 @@ fn main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_completion_in_parameter() {
|
||||||
|
check_edit(
|
||||||
|
"if",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo($0)
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(if $1 {
|
||||||
|
$2
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"if",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo($0, 2)
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(if $1 {
|
||||||
|
$2
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
}, 2)
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"if",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(2, $0)
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(2, if $1 {
|
||||||
|
$2
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"if let",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(2, $0)
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
foo(2, if let $1 = $2 {
|
||||||
|
$3
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_completion_in_let_statement() {
|
||||||
|
check_edit(
|
||||||
|
"if",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
let x = $0;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
let x = if $1 {
|
||||||
|
$2
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
check_edit(
|
||||||
|
"if let",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
let x = $0;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
fn main() {
|
||||||
|
let x = if let $1 = $2 {
|
||||||
|
$3
|
||||||
|
} else {
|
||||||
|
$0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn completes_let_in_block() {
|
fn completes_let_in_block() {
|
||||||
check_edit(
|
check_edit(
|
||||||
|
@ -147,6 +147,7 @@ pub(crate) struct PathExprCtx<'db> {
|
|||||||
/// Whether this expression is the direct condition of an if or while expression
|
/// Whether this expression is the direct condition of an if or while expression
|
||||||
pub(crate) in_condition: bool,
|
pub(crate) in_condition: bool,
|
||||||
pub(crate) incomplete_let: bool,
|
pub(crate) incomplete_let: bool,
|
||||||
|
pub(crate) in_value: bool,
|
||||||
pub(crate) ref_expr_parent: Option<ast::RefExpr>,
|
pub(crate) ref_expr_parent: Option<ast::RefExpr>,
|
||||||
pub(crate) after_amp: bool,
|
pub(crate) after_amp: bool,
|
||||||
/// The surrounding RecordExpression we are completing a functional update
|
/// The surrounding RecordExpression we are completing a functional update
|
||||||
|
@ -1248,6 +1248,7 @@ fn classify_name_ref<'db>(
|
|||||||
.parent()
|
.parent()
|
||||||
.and_then(ast::LetStmt::cast)
|
.and_then(ast::LetStmt::cast)
|
||||||
.is_some_and(|it| it.semicolon_token().is_none());
|
.is_some_and(|it| it.semicolon_token().is_none());
|
||||||
|
let in_value = it.parent().and_then(Either::<ast::LetStmt, ast::ArgList>::cast).is_some();
|
||||||
let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
|
let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
|
||||||
|
|
||||||
let in_match_guard = match it.parent().and_then(ast::MatchArm::cast) {
|
let in_match_guard = match it.parent().and_then(ast::MatchArm::cast) {
|
||||||
@ -1268,6 +1269,7 @@ fn classify_name_ref<'db>(
|
|||||||
is_func_update,
|
is_func_update,
|
||||||
innermost_ret_ty,
|
innermost_ret_ty,
|
||||||
self_param,
|
self_param,
|
||||||
|
in_value,
|
||||||
incomplete_let,
|
incomplete_let,
|
||||||
impl_,
|
impl_,
|
||||||
in_match_guard,
|
in_match_guard,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user