Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/utils.rs')
-rw-r--r--crates/ide-assists/src/utils.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/crates/ide-assists/src/utils.rs b/crates/ide-assists/src/utils.rs
index 1b6c9a579a..2c4fb5f405 100644
--- a/crates/ide-assists/src/utils.rs
+++ b/crates/ide-assists/src/utils.rs
@@ -110,6 +110,26 @@ fn needs_parens_in_call(make: &SyntaxFactory, param: &ast::Expr) -> bool {
param.needs_parens_in_place_of(call.syntax(), callable.syntax())
}
+pub(crate) fn wrap_paren_in_guard_chain(guard: ast::Expr, make: &SyntaxFactory) -> ast::Expr {
+ if needs_parens_in_guard_chain(make, &guard) { make.expr_paren(guard).into() } else { guard }
+}
+
+fn needs_parens_in_guard_chain(make: &SyntaxFactory, guard: &ast::Expr) -> bool {
+ let ast::Expr::BinExpr(if_let_and_guard) = make.expr_bin_op(
+ make.expr_unit(),
+ ast::BinaryOp::LogicOp(ast::LogicOp::And),
+ make.expr_unit(),
+ ) else {
+ stdx::never!("`SyntaxFactory::expr_bin_op` returns a `BinExpr`");
+ return false;
+ };
+ let Some(fake_guard) = if_let_and_guard.rhs() else {
+ stdx::never!("invalid make call");
+ return false;
+ };
+ guard.needs_parens_in_place_of(if_let_and_guard.syntax(), fake_guard.syntax())
+}
+
/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
/// `#[test_case(...)]`, `#[tokio::test]` and similar.
/// Also a regular `#[test]` annotation is supported.
@@ -1177,6 +1197,15 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo
is_const
}
+pub(crate) fn original_range_in(
+ file_id: hir::EditionedFileId,
+ sema: &Semantics<'_, RootDatabase>,
+ value: &SyntaxNode,
+) -> Option<TextRange> {
+ let original = sema.original_range_opt(value)?;
+ (original.file_id == file_id).then_some(original.range)
+}
+
// FIXME: #20460 When hir-ty can analyze the `never` statement at the end of block, remove it
pub(crate) fn is_never_block(
sema: &Semantics<'_, RootDatabase>,