prefer default over new

This commit is contained in:
BenjaminBrienen 2025-04-04 15:49:50 +02:00
parent 6ca780700d
commit 2462624a7d
46 changed files with 100 additions and 127 deletions

View file

@ -19,8 +19,8 @@ pub struct SyntaxFactory {
impl SyntaxFactory {
/// Creates a new [`SyntaxFactory`], generating mappings between input nodes and generated nodes.
pub fn new() -> Self {
Self { mappings: Some(RefCell::new(SyntaxMapping::new())) }
pub fn with_mappings() -> Self {
Self { mappings: Some(RefCell::new(SyntaxMapping::default())) }
}
/// Creates a [`SyntaxFactory`] without generating mappings.

View file

@ -33,7 +33,7 @@ pub struct SyntaxEditor {
impl SyntaxEditor {
/// Creates a syntax editor to start editing from `root`
pub fn new(root: SyntaxNode) -> Self {
Self { root, changes: vec![], mappings: SyntaxMapping::new(), annotations: vec![] }
Self { root, changes: vec![], mappings: SyntaxMapping::default(), annotations: vec![] }
}
pub fn add_annotation(&mut self, element: impl Element, annotation: SyntaxAnnotation) {
@ -151,9 +151,8 @@ impl SyntaxEdit {
#[repr(transparent)]
pub struct SyntaxAnnotation(NonZeroU32);
impl SyntaxAnnotation {
/// Creates a unique syntax annotation to attach data to.
pub fn new() -> Self {
impl Default for SyntaxAnnotation {
fn default() -> Self {
static COUNTER: AtomicU32 = AtomicU32::new(1);
// Only consistency within a thread matters, as SyntaxElements are !Send
@ -163,12 +162,6 @@ impl SyntaxAnnotation {
}
}
impl Default for SyntaxAnnotation {
fn default() -> Self {
Self::new()
}
}
/// Position describing where to insert elements
#[derive(Debug)]
pub struct Position {
@ -411,12 +404,12 @@ mod tests {
let to_replace = root.syntax().descendants().find_map(ast::BinExpr::cast).unwrap();
let mut editor = SyntaxEditor::new(root.syntax().clone());
let make = SyntaxFactory::new();
let make = SyntaxFactory::with_mappings();
let name = make::name("var_name");
let name_ref = make::name_ref("var_name").clone_for_update();
let placeholder_snippet = SyntaxAnnotation::new();
let placeholder_snippet = SyntaxAnnotation::default();
editor.add_annotation(name.syntax(), placeholder_snippet);
editor.add_annotation(name_ref.syntax(), placeholder_snippet);
@ -522,7 +515,7 @@ mod tests {
let second_let = root.syntax().descendants().find_map(ast::LetStmt::cast).unwrap();
let mut editor = SyntaxEditor::new(root.syntax().clone());
let make = SyntaxFactory::new();
let make = SyntaxFactory::with_mappings();
let new_block_expr = make.block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone())));
@ -574,7 +567,7 @@ mod tests {
let inner_block = root.clone();
let mut editor = SyntaxEditor::new(root.syntax().clone());
let make = SyntaxFactory::new();
let make = SyntaxFactory::with_mappings();
let new_block_expr = make.block_expr([], Some(ast::Expr::BlockExpr(inner_block.clone())));

View file

@ -20,10 +20,6 @@ pub struct SyntaxMapping {
}
impl SyntaxMapping {
pub fn new() -> Self {
Self::default()
}
/// Like [`SyntaxMapping::upmap_child`] but for syntax elements.
pub fn upmap_child_element(
&self,