Skip to content

Commit 6729294

Browse files
committed
refcator
1 parent ed1684d commit 6729294

File tree

6 files changed

+80
-62
lines changed

6 files changed

+80
-62
lines changed

src/imports.rs

+60-59
Original file line numberDiff line numberDiff line change
@@ -1079,74 +1079,75 @@ impl Rewrite for UseSegment {
10791079
impl Rewrite for UseTree {
10801080
// This does NOT format attributes and visibility or add a trailing `;`.
10811081
fn rewrite(&self, context: &RewriteContext<'_>, mut shape: Shape) -> Option<String> {
1082+
fn proceed(
1083+
context: &RewriteContext<'_>,
1084+
shape: &Shape,
1085+
curr_segment: &UseSegment,
1086+
curr_segment_is_allow_overflow: bool,
1087+
next_segment: Option<&&UseSegment>,
1088+
) -> Option<(String, Shape)> {
1089+
let mut rewritten_segment = curr_segment.rewrite(context, shape.clone())?;
1090+
if next_segment.is_some() {
1091+
rewritten_segment.push_str("::");
1092+
}
1093+
let reserved_room_for_brace = match next_segment.map(|s| &s.kind) {
1094+
Some(UseSegmentKind::List(_)) => "{".len(),
1095+
_ => 0,
1096+
};
1097+
let next_shape = if matches!(&curr_segment.kind, UseSegmentKind::List(_)) {
1098+
// This is the last segment and we won't use `next_shape`. Return `shape`
1099+
// unchanged.
1100+
shape.clone()
1101+
} else if curr_segment_is_allow_overflow {
1102+
// If the segment follows `use ` or newline, force to consume the segment with
1103+
// overflow.
1104+
1105+
let s = shape.offset_left_maybe_overflow(rewritten_segment.len());
1106+
if s.width == 0 {
1107+
// We have to to commit current segment in this line. Make a room for next
1108+
// round.
1109+
s.add_width(reserved_room_for_brace)
1110+
} else {
1111+
s.clone()
1112+
}
1113+
} else {
1114+
let ret = shape.offset_left(rewritten_segment.len())?;
1115+
// Check that there is a room for the next "{". If not, return null for retry with
1116+
// newline.
1117+
ret.offset_left(reserved_room_for_brace)?;
1118+
ret
1119+
};
1120+
Some((rewritten_segment, next_shape))
1121+
}
1122+
10821123
let shape_top_level = shape.clone();
10831124
let mut result = String::with_capacity(256);
10841125
let mut is_first = true;
1085-
let mut prev_is_allow_overflow = false;
10861126
let mut iter = self.path.iter().peekable();
10871127
while let Some(segment) = iter.next() {
1088-
// Try stacking the next segment, e.g. `use prev::next_segment::...` and
1089-
// `use prev::{next, segment}`.
1090-
let can_stack_with_constraint = (|| {
1091-
// If the segment follows `use ` or `{`, force to consume the segment with overflow.
1092-
if is_first {
1093-
let mut chunk = segment.rewrite(context, shape.infinite_width())?;
1094-
let next_shape = if iter.peek().is_some() {
1095-
chunk.push_str("::");
1096-
shape.offset_left_maybe_overflow(chunk.len())
1097-
} else {
1098-
shape.clone()
1099-
};
1100-
Some((chunk, next_shape))
1101-
} else {
1102-
// If the segment follows `use ` or newline, allow overflow by "{".
1103-
let s = if prev_is_allow_overflow
1104-
&& matches!(segment.kind, UseSegmentKind::List(_))
1105-
{
1106-
shape.add_width(1)
1107-
} else {
1108-
shape.clone()
1109-
};
1110-
let mut chunk = segment.rewrite(context, s)?;
1111-
let next_shape = match iter.peek().map(|s| &s.kind) {
1112-
Some(UseSegmentKind::List(_)) => {
1113-
chunk.push_str("::");
1114-
let ret = shape.offset_left(chunk.len())?;
1115-
// Ensure that there is a room for the next "{".
1116-
ret.offset_left(1)?;
1117-
ret
1118-
}
1119-
Some(_) => {
1120-
chunk.push_str("::");
1121-
shape.offset_left(chunk.len())?
1122-
}
1123-
None => shape.clone(),
1124-
};
1125-
Some((chunk, next_shape))
1128+
let allow_overflow = is_first;
1129+
is_first = false;
1130+
match proceed(context, &shape, segment, allow_overflow, iter.peek()) {
1131+
Some((rewritten_segment, next_shape)) => {
1132+
result.push_str(&rewritten_segment);
1133+
shape = next_shape;
1134+
continue;
11261135
}
1127-
})();
1128-
match can_stack_with_constraint {
1129-
Some((chunk, next_shape)) => {
1130-
result.push_str(&chunk);
1136+
None => (),
1137+
}
1138+
// If the first `proceed()` failed, retry with newline.
1139+
result.push_str("\n");
1140+
result.push_str(&" ".repeat(shape.indent.block_indent + 4));
1141+
shape = shape_top_level.clone();
1142+
let allow_overflow = true;
1143+
match proceed(context, &shape, segment, allow_overflow, iter.peek()) {
1144+
Some((rewritten_segment, next_shape)) => {
1145+
result.push_str(&rewritten_segment);
11311146
shape = next_shape;
1132-
prev_is_allow_overflow = is_first;
1133-
is_first = false;
11341147
}
1135-
// If the next segment exceeds the given width, continue with newline.
1148+
// Give up to format.
11361149
None => {
1137-
let segment_str = segment.rewrite(context, shape)?;
1138-
let mut chunk = format!(
1139-
"{}{}",
1140-
" ".repeat(shape.indent.block_indent + 4),
1141-
segment_str
1142-
);
1143-
if iter.peek().is_some() {
1144-
chunk.push_str("::");
1145-
}
1146-
result.push_str("\n");
1147-
result.push_str(&chunk);
1148-
shape = shape_top_level.offset_left_maybe_overflow(segment_str.len());
1149-
prev_is_allow_overflow = true;
1150+
return None;
11501151
}
11511152
}
11521153
}

tests/source/5131_module.rs

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ mod indent4 {
117117
Foo2,
118118
bar::Bar2,
119119
};
120+
121+
// Test for top-level `UseSegmentKind::List`.
122+
use {
123+
a,
124+
column_____________________________________________________________________________________102,
125+
};
120126
}
121127

122128
use smithay::{

tests/target/5131_crate.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ use smithay::{
112112
set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
113113
ServerDndGrabHandler,
114114
},
115-
primary_selection::{set_primary_focus, PrimarySelectionHandler, PrimarySelectionState},
115+
primary_selection::{
116+
set_primary_focus, PrimarySelectionHandler, PrimarySelectionState,
117+
},
116118
wlr_data_control::{DataControlHandler, DataControlState},
117119
SelectionHandler,
118120
},

tests/target/5131_module.rs

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ mod indent4 {
120120
column_____________________________________________________________________________096::{
121121
Foo, Foo2,
122122
};
123+
124+
// Test for top-level `UseSegmentKind::List`.
125+
use {
126+
a,
127+
column_____________________________________________________________________________________102,
128+
};
123129
}
124130

125131
use smithay::backend::renderer::element::utils::select_dmabuf_feedback;

tests/target/5131_one.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ use smithay::{
115115
set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
116116
ServerDndGrabHandler,
117117
},
118-
primary_selection::{set_primary_focus, PrimarySelectionHandler, PrimarySelectionState},
118+
primary_selection::{
119+
set_primary_focus, PrimarySelectionHandler, PrimarySelectionState,
120+
},
119121
wlr_data_control::{DataControlHandler, DataControlState},
120122
SelectionHandler,
121123
},

tests/target/issue_3033.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ mod indent4 {
88
c012::
99
column__This_segment_wraps_because_it_exceeds_100_and_follows_the_previous_segment___102::
1010
c012::c018::c024::c030::c036::c042::c048::c054::c060::c066::c072::c078::c084::c090::c096::
11-
c012::column__This_segment_doesnt_wrap_because_it_doesnt_exceed_100__________________096::Foo;
11+
c012::column__This_segment_doesnt_wrap_because_it_doesnt_exceed_100__________________096::
12+
Foo;
1213
}

0 commit comments

Comments
 (0)