Skip to content

Commit b647dab

Browse files
committed
refactor & add more tests
1 parent 151cf04 commit b647dab

File tree

6 files changed

+155
-44
lines changed

6 files changed

+155
-44
lines changed

src/expr.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ use crate::spanned::Spanned;
2929
use crate::stmt;
3030
use crate::string::{rewrite_string, StringFormat};
3131
use crate::types::{rewrite_path, PathContext};
32-
use crate::utils::{
33-
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
34-
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
35-
semicolon_for_expr, unicode_str_width, wrap_str,
36-
};
32+
use crate::utils::*;
3733
use crate::vertical::rewrite_with_alignment;
3834
use crate::visitor::FmtVisitor;
3935

@@ -417,13 +413,9 @@ pub(crate) fn format_expr(
417413
expr.span.lo(),
418414
);
419415

420-
let allow_extend = if attrs.len() == 1 {
421-
let line_len = attrs_str.len() + 1 + expr_str.len();
422-
!attrs.first().unwrap().is_doc_comment()
423-
&& context.config.inline_attribute_width() >= line_len
424-
} else {
425-
false
426-
};
416+
// +1 = ";"
417+
let allow_extend =
418+
extend_inline_attr(&expr.attrs, shape, &attrs_str, expr_str.len() + 1, context);
427419

428420
combine_strs_with_missing_comments(
429421
context,

src/imports.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
2323
use crate::shape::Shape;
2424
use crate::source_map::SpanUtils;
2525
use crate::spanned::Spanned;
26-
use crate::utils::{is_same_visibility, mk_sp, rewrite_ident};
26+
use crate::utils::{extend_inline_attr, is_same_visibility, mk_sp, rewrite_ident};
2727
use crate::visitor::FmtVisitor;
2828

2929
/// Returns a name imported by a `use` declaration.
@@ -351,13 +351,8 @@ impl UseTree {
351351
let hi = self.span.lo();
352352
let span = mk_sp(lo, hi);
353353

354-
let allow_extend = if attrs.len() == 1 {
355-
let line_len = attr_str.len() + 1 + use_str.len();
356-
!attrs.first().unwrap().is_doc_comment()
357-
&& context.config.inline_attribute_width() >= line_len
358-
} else {
359-
false
360-
};
354+
let allow_extend =
355+
extend_inline_attr(attrs, shape, &attr_str, use_str.len(), context);
361356

362357
combine_strs_with_missing_comments(
363358
context,

src/items.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,14 +1877,16 @@ pub(crate) fn rewrite_struct_field(
18771877
let prefix = rewrite_struct_field_prefix(context, field)?;
18781878

18791879
let attrs_str = field.attrs.rewrite(context, shape)?;
1880+
let ty_str = field.ty.rewrite(context, shape)?;
18801881

1881-
let allow_extend = if field.attrs.len() == 1 {
1882-
let line_len = attrs_str.len() + 1 + prefix.len();
1883-
!field.attrs.first().unwrap().is_doc_comment()
1884-
&& context.config.inline_attribute_width() >= line_len
1885-
} else {
1886-
false
1887-
};
1882+
let allow_extend = extend_inline_attr(
1883+
&field.attrs,
1884+
shape,
1885+
&attrs_str,
1886+
// +1 = " ", +1 = ","
1887+
prefix.len() + 1 + ty_str.len() + 1,
1888+
context,
1889+
);
18881890

18891891
let attrs_extendable =
18901892
(field.ident.is_none() && is_attributes_extendable(&attrs_str)) || allow_extend;
@@ -3394,13 +3396,8 @@ impl Rewrite for ast::ForeignItem {
33943396
mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
33953397
};
33963398

3397-
let allow_extend = if self.attrs.len() == 1 {
3398-
let line_len = attrs_str.len() + 1 + item_str.len();
3399-
!self.attrs.first().unwrap().is_doc_comment()
3400-
&& context.config.inline_attribute_width() >= line_len
3401-
} else {
3402-
false
3403-
};
3399+
let allow_extend =
3400+
extend_inline_attr(&self.attrs, shape, &attrs_str, item_str.len(), context);
34043401

34053402
combine_strs_with_missing_comments(
34063403
context,
@@ -3429,13 +3426,7 @@ fn rewrite_attrs(
34293426
mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
34303427
};
34313428

3432-
let allow_extend = if attrs.len() == 1 {
3433-
let line_len = attrs_str.len() + 1 + item_str.len();
3434-
!attrs.first().unwrap().is_doc_comment()
3435-
&& context.config.inline_attribute_width() >= line_len
3436-
} else {
3437-
false
3438-
};
3429+
let allow_extend = extend_inline_attr(&item.attrs, shape, &attrs_str, item_str.len(), context);
34393430

34403431
combine_strs_with_missing_comments(
34413432
context,

src/utils.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,25 @@ pub(crate) fn unicode_str_width(s: &str) -> usize {
691691
s.width()
692692
}
693693

694+
/// Determine if we could place a single attribute on the same line as the rewritten AST node.
695+
pub(crate) fn extend_inline_attr(
696+
attrs: &[ast::Attribute],
697+
shape: Shape,
698+
attrs_str: &str,
699+
rewrite_len: usize,
700+
context: &RewriteContext<'_>,
701+
) -> bool {
702+
if attrs.len() > 1 {
703+
return false;
704+
}
705+
706+
attrs.first().is_some_and(|attr| {
707+
// +1 = " "
708+
let line_len = shape.indent.width() + attrs_str.len() + 1 + rewrite_len;
709+
!attr.is_doc_comment() && context.config.inline_attribute_width() >= line_len
710+
})
711+
}
712+
694713
#[cfg(test)]
695714
mod test {
696715
use super::*;

tests/source/issue-3343.rs renamed to tests/source/configs/inline_attribute_width/50.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ use total_len_is::_50__;
99
#[cfg(feature = "alloc")]
1010
use total_len_is::_51___;
1111

12+
#[cfg(feature = "alloc")]
13+
// c
14+
use d;
15+
16+
#[cfg(feature = "alloc")]
17+
// comment
18+
use total_len_is::_50__;
19+
20+
#[cfg(feature = "alloc")]
21+
// comment
22+
use total_len_is::_51___;
23+
24+
fn foo() {
25+
#[cfg(feature = "alloc")]
26+
use total_len::_50_;
27+
28+
#[cfg(feature = "alloc")]
29+
use total_len::_51__;
30+
}
31+
1232
#[cfg(feature = "alloc")]
1333
extern crate len_is_50_;
1434

@@ -21,9 +41,32 @@ extern "C" {
2141
fn foo();
2242
}
2343

44+
extern "C" {
45+
#[no_mangle]
46+
fn total_len_is_49___________();
47+
}
48+
49+
extern "C" {
50+
#[no_mangle]
51+
fn total_len_is_50____________();
52+
}
53+
54+
extern "C" {
55+
#[no_mangle]
56+
fn total_len_is_51_____________();
57+
}
58+
2459
fn main() {
2560
#[cfg(feature = "alloc")]
26-
foo();
61+
["total_len_is_50"];
62+
#[cfg(feature = "alloc")]
63+
["total_len_is_51_"];
64+
#[cfg(feature = "alloc")]
65+
total_len_is_50__();
66+
#[cfg(feature = "alloc")]
67+
total_len_is_51___();
68+
#[cfg(feature = "alloc")]
69+
total_len_is_52____();
2770
#[cfg(feature = "alloc")]
2871
{
2972
foo();
@@ -40,6 +83,22 @@ struct EventConfigWidget {
4083
menu_delay: Spinner<u32>,
4184
}
4285

86+
struct foo {
87+
#[x]
88+
#[y]
89+
z: bool,
90+
}
91+
92+
struct foo {
93+
#[widget]
94+
len_is_50____________________: bool,
95+
}
96+
97+
struct foo {
98+
#[widget]
99+
len_is_51_____________________: bool,
100+
}
101+
43102
/// this is a comment to test is_sugared_doc property
44103
use core::convert;
45104

tests/target/issue-3343.rs renamed to tests/target/configs/inline_attribute_width/50.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
#[cfg(feature = "alloc")]
88
use total_len_is::_51___;
99

10+
#[cfg(feature = "alloc")]
11+
// c
12+
use d;
13+
14+
#[cfg(feature = "alloc")]
15+
// comment
16+
use total_len_is::_50__;
17+
18+
#[cfg(feature = "alloc")]
19+
// comment
20+
use total_len_is::_51___;
21+
22+
fn foo() {
23+
#[cfg(feature = "alloc")] use total_len::_50_;
24+
25+
#[cfg(feature = "alloc")]
26+
use total_len::_51__;
27+
}
28+
1029
#[cfg(feature = "alloc")] extern crate len_is_50_;
1130

1231
#[cfg(feature = "alloc")]
@@ -17,9 +36,30 @@ extern "C" {
1736
#[no_mangle] fn foo();
1837
}
1938

39+
extern "C" {
40+
#[no_mangle] fn total_len_is_49___________();
41+
}
42+
43+
extern "C" {
44+
#[no_mangle] fn total_len_is_50____________();
45+
}
46+
47+
extern "C" {
48+
#[no_mangle]
49+
fn total_len_is_51_____________();
50+
}
51+
2052
fn main() {
21-
#[cfg(feature = "alloc")] foo();
22-
#[cfg(feature = "alloc")] {
53+
#[cfg(feature = "alloc")] ["total_len_is_50"];
54+
#[cfg(feature = "alloc")]
55+
["total_len_is_51_"];
56+
#[cfg(feature = "alloc")] total_len_is_50__();
57+
#[cfg(feature = "alloc")]
58+
total_len_is_51___();
59+
#[cfg(feature = "alloc")]
60+
total_len_is_52____();
61+
#[cfg(feature = "alloc")]
62+
{
2363
foo();
2464
}
2565
{
@@ -32,6 +72,21 @@ struct EventConfigWidget {
3272
#[widget] menu_delay: Spinner<u32>,
3373
}
3474

75+
struct foo {
76+
#[x]
77+
#[y]
78+
z: bool,
79+
}
80+
81+
struct foo {
82+
#[widget] len_is_50____________________: bool,
83+
}
84+
85+
struct foo {
86+
#[widget]
87+
len_is_51_____________________: bool,
88+
}
89+
3590
/// this is a comment to test is_sugared_doc property
3691
use core::convert;
3792

0 commit comments

Comments
 (0)