Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend inline_attribute_width to more AST types #5968

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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 @@ -29,11 +29,7 @@ use crate::spanned::Spanned;
use crate::stmt;
use crate::string::{rewrite_string, StringFormat};
use crate::types::{rewrite_path, PathContext};
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 @@ -425,7 +421,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 @@ -23,7 +23,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape;
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 @@ -351,13 +351,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 @@ -1884,7 +1884,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 @@ -3434,13 +3446,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 @@ -3461,13 +3477,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 @@ -701,6 +701,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