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
21 changes: 15 additions & 6 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ use crate::spanned::Spanned;
use crate::stmt;
use crate::string::{StringFormat, rewrite_string};
use crate::types::{PathContext, rewrite_path};
use crate::utils::{
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
semicolon_for_expr, unicode_str_width, wrap_str,
};
use crate::utils::*;
use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor;

Expand Down Expand Up @@ -467,7 +463,20 @@ pub(crate) fn format_expr(
attrs.last().map_or(expr.span.lo(), |attr| attr.span.hi()),
expr.span.lo(),
);
combine_strs_with_missing_comments(context, &attrs_str, &expr_str, span, shape, false)

// +1 = ";"
let allow_extend =
extend_inline_attr(&expr.attrs, shape, &attrs_str, expr_str.len() + 1, context)
&& expr_type == ExprType::Statement;

combine_strs_with_missing_comments(
context,
&attrs_str,
&expr_str,
span,
shape,
allow_extend,
)
})
}

Expand Down
11 changes: 3 additions & 8 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::shape::Shape;
use crate::sort::version_sort;
use crate::source_map::SpanUtils;
use crate::spanned::Spanned;
use crate::utils::{is_same_visibility, mk_sp, rewrite_ident};
use crate::utils::{extend_inline_attr, is_same_visibility, mk_sp, rewrite_ident};
use crate::visitor::FmtVisitor;

/// Returns a name imported by a `use` declaration.
Expand Down Expand Up @@ -353,13 +353,8 @@ impl UseTree {
let hi = self.span.lo();
let span = mk_sp(lo, hi);

let allow_extend = if attrs.len() == 1 {
let line_len = attr_str.len() + 1 + use_str.len();
!attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false
};
let allow_extend =
extend_inline_attr(attrs, shape, &attr_str, use_str.len(), context);

combine_strs_with_missing_comments(
context,
Expand Down
28 changes: 19 additions & 9 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1938,7 +1938,19 @@ pub(crate) fn rewrite_struct_field(
let prefix = rewrite_struct_field_prefix(context, field)?;

let attrs_str = field.attrs.rewrite_result(context, shape)?;
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
let ty_str = field.ty.rewrite_result(context, shape)?;

let allow_extend = extend_inline_attr(
&field.attrs,
shape,
&attrs_str,
// +1 = " ", +1 = ","
prefix.len() + 1 + ty_str.len() + 1,
context,
);

let attrs_extendable =
(field.ident.is_none() && is_attributes_extendable(&attrs_str)) || allow_extend;
let missing_span = if field.attrs.is_empty() {
mk_sp(field.span.lo(), field.span.lo())
} else {
Expand Down Expand Up @@ -3502,13 +3514,17 @@ impl Rewrite for ast::ForeignItem {
} else {
mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
};

let allow_extend =
extend_inline_attr(&self.attrs, shape, &attrs_str, item_str.len(), context);

combine_strs_with_missing_comments(
context,
&attrs_str,
&item_str,
missing_span,
shape,
false,
allow_extend,
)
}
}
Expand All @@ -3529,13 +3545,7 @@ fn rewrite_attrs(
mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
};

let allow_extend = if attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + item_str.len();
!attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false
};
let allow_extend = extend_inline_attr(&item.attrs, shape, &attrs_str, item_str.len(), context);

combine_strs_with_missing_comments(
context,
Expand Down
19 changes: 19 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,25 @@ pub(crate) fn unicode_str_width(s: &str) -> usize {
s.width()
}

/// Determine if we could place a single attribute on the same line as the rewritten AST node.
pub(crate) fn extend_inline_attr(
attrs: &[ast::Attribute],
shape: Shape,
attrs_str: &str,
rewrite_len: usize,
context: &RewriteContext<'_>,
) -> bool {
if attrs.len() > 1 {
return false;
}

attrs.first().is_some_and(|attr| {
// +1 = " "
let line_len = shape.indent.width() + attrs_str.len() + 1 + rewrite_len;
!attr.is_doc_comment() && context.config.inline_attribute_width() >= line_len
})
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading
Loading