Skip to content

Commit

Permalink
refcator
Browse files Browse the repository at this point in the history
  • Loading branch information
kenoss committed Jun 4, 2024
1 parent a0cfa0b commit f150768
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 62 deletions.
119 changes: 60 additions & 59 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,74 +1077,75 @@ impl Rewrite for UseSegment {
impl Rewrite for UseTree {
// This does NOT format attributes and visibility or add a trailing `;`.
fn rewrite(&self, context: &RewriteContext<'_>, mut shape: Shape) -> Option<String> {
fn proceed(
context: &RewriteContext<'_>,
shape: &Shape,
curr_segment: &UseSegment,
curr_segment_is_allow_overflow: bool,
next_segment: Option<&&UseSegment>,
) -> Option<(String, Shape)> {
let mut rewritten_segment = curr_segment.rewrite(context, shape.clone())?;
if next_segment.is_some() {
rewritten_segment.push_str("::");
}
let reserved_room_for_brace = match next_segment.map(|s| &s.kind) {
Some(UseSegmentKind::List(_)) => "{".len(),
_ => 0,
};
let next_shape = if matches!(&curr_segment.kind, UseSegmentKind::List(_)) {
// This is the last segment and we won't use `next_shape`. Return `shape`
// unchanged.
shape.clone()
} else if curr_segment_is_allow_overflow {
// If the segment follows `use ` or newline, force to consume the segment with
// overflow.

let s = shape.offset_left_maybe_overflow(rewritten_segment.len());
if s.width == 0 {
// We have to to commit current segment in this line. Make a room for next
// round.
s.add_width(reserved_room_for_brace)
} else {
s.clone()
}
} else {
let ret = shape.offset_left(rewritten_segment.len())?;
// Check that there is a room for the next "{". If not, return null for retry with
// newline.
ret.offset_left(reserved_room_for_brace)?;
ret
};
Some((rewritten_segment, next_shape))
}

let shape_top_level = shape.clone();
let mut result = String::with_capacity(256);
let mut is_first = true;
let mut prev_is_allow_overflow = false;
let mut iter = self.path.iter().peekable();
while let Some(segment) = iter.next() {
// Try stacking the next segment, e.g. `use prev::next_segment::...` and
// `use prev::{next, segment}`.
let can_stack_with_constraint = (|| {
// If the segment follows `use ` or `{`, force to consume the segment with overflow.
if is_first {
let mut chunk = segment.rewrite(context, shape.infinite_width())?;
let next_shape = if iter.peek().is_some() {
chunk.push_str("::");
shape.offset_left_maybe_overflow(chunk.len())
} else {
shape.clone()
};
Some((chunk, next_shape))
} else {
// If the segment follows `use ` or newline, allow overflow by "{".
let s = if prev_is_allow_overflow
&& matches!(segment.kind, UseSegmentKind::List(_))
{
shape.add_width(1)
} else {
shape.clone()
};
let mut chunk = segment.rewrite(context, s)?;
let next_shape = match iter.peek().map(|s| &s.kind) {
Some(UseSegmentKind::List(_)) => {
chunk.push_str("::");
let ret = shape.offset_left(chunk.len())?;
// Ensure that there is a room for the next "{".
ret.offset_left(1)?;
ret
}
Some(_) => {
chunk.push_str("::");
shape.offset_left(chunk.len())?
}
None => shape.clone(),
};
Some((chunk, next_shape))
let allow_overflow = is_first;
is_first = false;
match proceed(context, &shape, segment, allow_overflow, iter.peek()) {
Some((rewritten_segment, next_shape)) => {
result.push_str(&rewritten_segment);
shape = next_shape;
continue;
}
})();
match can_stack_with_constraint {
Some((chunk, next_shape)) => {
result.push_str(&chunk);
None => (),
}
// If the first `proceed()` failed, retry with newline.
result.push_str("\n");
result.push_str(&" ".repeat(shape.indent.block_indent + 4));
shape = shape_top_level.clone();
let allow_overflow = true;
match proceed(context, &shape, segment, allow_overflow, iter.peek()) {
Some((rewritten_segment, next_shape)) => {
result.push_str(&rewritten_segment);
shape = next_shape;
prev_is_allow_overflow = is_first;
is_first = false;
}
// If the next segment exceeds the given width, continue with newline.
// Give up to format.
None => {
let segment_str = segment.rewrite(context, shape)?;
let mut chunk = format!(
"{}{}",
" ".repeat(shape.indent.block_indent + 4),
segment_str
);
if iter.peek().is_some() {
chunk.push_str("::");
}
result.push_str("\n");
result.push_str(&chunk);
shape = shape_top_level.offset_left_maybe_overflow(segment_str.len());
prev_is_allow_overflow = true;
return None;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/source/5131_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ mod indent4 {
Foo2,
bar::Bar2,
};

// Test for top-level `UseSegmentKind::List`.
use {
a,
column_____________________________________________________________________________________102,
};
}

use smithay::{
Expand Down
4 changes: 3 additions & 1 deletion tests/target/5131_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ use smithay::{
set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
ServerDndGrabHandler,
},
primary_selection::{set_primary_focus, PrimarySelectionHandler, PrimarySelectionState},
primary_selection::{
set_primary_focus, PrimarySelectionHandler, PrimarySelectionState,
},
wlr_data_control::{DataControlHandler, DataControlState},
SelectionHandler,
},
Expand Down
6 changes: 6 additions & 0 deletions tests/target/5131_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ mod indent4 {
column_____________________________________________________________________________096::{
Foo, Foo2,
};

// Test for top-level `UseSegmentKind::List`.
use {
a,
column_____________________________________________________________________________________102,
};
}

use smithay::backend::renderer::element::utils::select_dmabuf_feedback;
Expand Down
4 changes: 3 additions & 1 deletion tests/target/5131_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ use smithay::{
set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState,
ServerDndGrabHandler,
},
primary_selection::{set_primary_focus, PrimarySelectionHandler, PrimarySelectionState},
primary_selection::{
set_primary_focus, PrimarySelectionHandler, PrimarySelectionState,
},
wlr_data_control::{DataControlHandler, DataControlState},
SelectionHandler,
},
Expand Down
3 changes: 2 additions & 1 deletion tests/target/issue_3033.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ mod indent4 {
c012::
column__This_segment_wraps_because_it_exceeds_100_and_follows_the_previous_segment___102::
c012::c018::c024::c030::c036::c042::c048::c054::c060::c066::c072::c078::c084::c090::c096::
c012::column__This_segment_doesnt_wrap_because_it_doesnt_exceed_100__________________096::Foo;
c012::column__This_segment_doesnt_wrap_because_it_doesnt_exceed_100__________________096::
Foo;
}

0 comments on commit f150768

Please sign in to comment.