Unnamed repository; edit this file 'description' to name the repository.
Move util to prec.rs and add make ty_paren()
| -rw-r--r-- | crates/ide-db/src/path_transform.rs | 18 | ||||
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 3 | ||||
| -rw-r--r-- | crates/syntax/src/ast/prec.rs | 12 | ||||
| -rw-r--r-- | crates/syntax/src/ast/syntax_factory/constructors.rs | 12 |
4 files changed, 30 insertions, 15 deletions
diff --git a/crates/ide-db/src/path_transform.rs b/crates/ide-db/src/path_transform.rs index 1f4d5c4020..101046cf54 100644 --- a/crates/ide-db/src/path_transform.rs +++ b/crates/ide-db/src/path_transform.rs @@ -415,12 +415,9 @@ impl Ctx<'_> { editor.replace(path.syntax(), qualified.clone().syntax()); } else if let Some(path_ty) = ast::PathType::cast(parent) { let old = path_ty.syntax(); - let needs_paren = type_needs_parens(old.parent(), subst); - let subst = if needs_paren { - make.ty(&format!("({subst})")) - } else { - subst.clone() - }; + let needs_paren = old.parent().is_some_and(|it| subst.needs_parens_in(&it)); + let subst = + if needs_paren { make.ty_paren(subst.clone()) } else { subst.clone() }; if old.parent().is_some() { editor.replace(old, subst.syntax()); @@ -603,15 +600,6 @@ impl Ctx<'_> { } } -fn type_needs_parens(parent: Option<SyntaxNode>, new: &ast::Type) -> bool { - if !matches!(new, ast::Type::DynTraitType(_)) { - return false; - } - let Some(parent) = parent else { return false }; - let kind = parent.kind(); - ast::CastExpr::can_cast(kind) || ast::RefType::can_cast(kind) || ast::PtrType::can_cast(kind) -} - // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the // trait ref, and then go from the types in the substs back to the syntax). fn get_syntactic_substs(impl_def: ast::Impl) -> Option<AstSubsts> { diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 16aa49e357..8b7f91c937 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -187,6 +187,9 @@ pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type { ty_from_text(&format!("({contents})")) } +pub fn ty_paren(ty: ast::Type) -> ast::Type { + ty_from_text(&format!("({ty})")) +} pub fn ty_ref(target: ast::Type, exclusive: bool) -> ast::Type { ty_from_text(&if exclusive { format!("&mut {target}") } else { format!("&{target}") }) } diff --git a/crates/syntax/src/ast/prec.rs b/crates/syntax/src/ast/prec.rs index 2a50d233c3..a6c04b0650 100644 --- a/crates/syntax/src/ast/prec.rs +++ b/crates/syntax/src/ast/prec.rs @@ -575,3 +575,15 @@ impl Expr { } } } + +impl ast::Type { + pub fn needs_parens_in(&self, parent: &SyntaxNode) -> bool { + if !matches!(self, ast::Type::DynTraitType(_)) { + return false; + } + let kind = parent.kind(); + ast::CastExpr::can_cast(kind) + || ast::RefType::can_cast(kind) + || ast::PtrType::can_cast(kind) + } +} diff --git a/crates/syntax/src/ast/syntax_factory/constructors.rs b/crates/syntax/src/ast/syntax_factory/constructors.rs index 7f9cd1fce9..22c8c842d8 100644 --- a/crates/syntax/src/ast/syntax_factory/constructors.rs +++ b/crates/syntax/src/ast/syntax_factory/constructors.rs @@ -610,6 +610,18 @@ impl SyntaxFactory { ast } + pub fn ty_paren(&self, ty: ast::Type) -> ast::Type { + let ast::Type::ParenType(paren_ty) = make::ty_paren(ty.clone()) else { unreachable!() }; + + if let Some(mut mapping) = self.mappings() { + let mut builder = SyntaxMappingBuilder::new(paren_ty.syntax().clone()); + builder.map_node(ty.syntax().clone(), paren_ty.ty().unwrap().syntax().clone()); + builder.finish(&mut mapping); + } + + paren_ty.into() + } + pub fn path_segment_generics( &self, name_ref: ast::NameRef, |