Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions crates/fmt/src/state/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{CommentConfig, Separator, State};
use crate::pp::{BreakToken, Printer, SIZE_INFINITY};
use crate::pp::{Printer, SIZE_INFINITY};
use foundry_common::iter::IterDelimited;
use foundry_config::fmt as config;
use itertools::{Either, Itertools};
Expand Down Expand Up @@ -573,12 +573,6 @@ impl<'ast> State<'_, 'ast> {
mut get_block_span: impl FnMut(&'ast T) -> Span,
pos_hi: BytePos,
) {
// Attempt to print in a single line.
if block_format.attempt_single_line() && block.len() == 1 {
self.print_single_line_block(block, block_format, print, get_block_span);
return;
}

// Empty blocks with comments require special attention.
if block.is_empty() {
self.print_empty_block(block_format, pos_hi);
Expand Down Expand Up @@ -706,37 +700,6 @@ impl<'ast> State<'_, 'ast> {
self.block_depth -= 1;
}

fn print_single_line_block<T: Debug>(
&mut self,
block: &'ast [T],
block_format: BlockFormat,
mut print: impl FnMut(&mut Self, &'ast T),
mut get_block_span: impl FnMut(&'ast T) -> Span,
) {
self.s.cbox(self.ind);

match block_format {
BlockFormat::Compact(true) => {
self.scan_break(BreakToken { pre_break: Some("{"), ..Default::default() });
print(self, &block[0]);
self.print_comments(get_block_span(&block[0]).hi(), CommentConfig::default());
self.s.scan_break(BreakToken { post_break: Some("}"), ..Default::default() });
self.s.offset(-self.ind);
}
_ => {
self.word("{");
self.space();
print(self, &block[0]);
self.print_comments(get_block_span(&block[0]).hi(), CommentConfig::default());
self.space_if_not_bol();
self.s.offset(-self.ind);
self.word("}");
}
}

self.end();
}

fn print_empty_block(&mut self, block_format: BlockFormat, pos_hi: BytePos) {
let has_braces = block_format.with_braces();

Expand Down Expand Up @@ -935,12 +898,8 @@ impl ListFormat {

/// Formatting style for code blocks
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[expect(dead_code)]
pub(crate) enum BlockFormat {
Regular,
/// Attempts to fit all elements in one line, before breaking consistently. Flags whether to
/// use braces or not.
Compact(bool),
/// Doesn't print braces. Flags the offset that should be applied before opening the block box.
/// Useful when the caller needs to manually handle the braces.
NoBraces(Option<isize>),
Expand All @@ -953,8 +912,4 @@ impl BlockFormat {
pub(crate) fn breaks(&self) -> bool {
matches!(self, Self::NoBraces(None))
}

pub(crate) fn attempt_single_line(&self) -> bool {
matches!(self, Self::Compact(_))
}
}
13 changes: 6 additions & 7 deletions crates/fmt/src/state/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,12 +2678,11 @@ impl MemberOrCallArgs {
}

#[derive(Debug, Clone)]
#[expect(dead_code)]
enum AttributeKind<'ast> {
Visibility(ast::Visibility),
StateMutability(ast::StateMutability),
Visibility,
StateMutability,
Virtual,
Override(&'ast ast::Override<'ast>),
Override,
Modifier(&'ast ast::Modifier<'ast>),
}

Expand Down Expand Up @@ -2757,14 +2756,14 @@ impl<'ast> AttributeCommentMapper<'ast> {
first_pos = v.span.lo()
}
self.attributes
.push(AttributeInfo { kind: AttributeKind::Visibility(*v), span: v.span });
.push(AttributeInfo { kind: AttributeKind::Visibility, span: v.span });
}
if let Some(sm) = header.state_mutability {
if sm.span.lo() < first_pos {
first_pos = sm.span.lo()
}
self.attributes
.push(AttributeInfo { kind: AttributeKind::StateMutability(*sm), span: sm.span });
.push(AttributeInfo { kind: AttributeKind::StateMutability, span: sm.span });
}
if let Some(span) = header.virtual_ {
if span.lo() < first_pos {
Expand All @@ -2776,7 +2775,7 @@ impl<'ast> AttributeCommentMapper<'ast> {
if o.span.lo() < first_pos {
first_pos = o.span.lo()
}
self.attributes.push(AttributeInfo { kind: AttributeKind::Override(o), span: o.span });
self.attributes.push(AttributeInfo { kind: AttributeKind::Override, span: o.span });
}
for m in header.modifiers.iter() {
if m.span().lo() < first_pos {
Expand Down
Loading