Skip to content

Commit e425a19

Browse files
committed
refrect scampi's review
- create span_before_last - create get_tactics and add comment
1 parent 5f7e470 commit e425a19

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/source_map.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub trait SpanUtils {
1111
fn span_after(&self, original: Span, needle: &str) -> BytePos;
1212
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
1313
fn span_before(&self, original: Span, needle: &str) -> BytePos;
14+
fn span_before_last(&self, original: Span, needle: &str) -> BytePos;
1415
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
1516
fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
1617
}
@@ -56,6 +57,17 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
5657
})
5758
}
5859

60+
fn span_before_last(&self, original: Span, needle: &str) -> BytePos {
61+
let snippet = self.span_to_snippet(original).unwrap();
62+
let mut offset = 0;
63+
64+
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
65+
offset += additional_offset + needle.len();
66+
}
67+
68+
original.lo() + BytePos(offset as u32 - 1)
69+
}
70+
5971
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
6072
self.opt_span_before(original, needle)
6173
.map(|bytepos| bytepos + BytePos(needle.len() as u32))

src/types.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,11 @@ where
319319

320320
let list_lo = context.snippet_provider.span_after(span, "(");
321321
let (list_str, tactic) = if inputs.len() == 0 {
322-
let tactic = if output.contains('\n') {
323-
DefinitiveListTactic::Vertical
324-
} else {
325-
definitive_tactic(
326-
&Vec::<ListItem>::new(),
327-
ListTactic::HorizontalVertical,
328-
Separator::Comma,
329-
shape.width.saturating_sub(2 + output.len()),
330-
)
331-
};
332-
let list_hi = context.snippet_provider.span_after_last(span, ")");
322+
let tactic = get_tactics(&Vec::<ListItem>::new(), &output, &shape);
323+
let list_hi = context.snippet_provider.span_before_last(span, ")");
333324
let comment = context
334325
.snippet_provider
335-
.span_to_snippet(mk_sp(list_lo, list_hi - BytePos(1)))?
326+
.span_to_snippet(mk_sp(list_lo, list_hi))?
336327
.trim();
337328
let comment = if comment.starts_with("//") {
338329
format!(
@@ -360,19 +351,7 @@ where
360351
);
361352

362353
let item_vec: Vec<_> = items.collect();
363-
364-
// If the return type is multi-lined, then force to use multiple lines for
365-
// arguments as well.
366-
let tactic = if output.contains('\n') {
367-
DefinitiveListTactic::Vertical
368-
} else {
369-
definitive_tactic(
370-
&*item_vec,
371-
ListTactic::HorizontalVertical,
372-
Separator::Comma,
373-
shape.width.saturating_sub(2 + output.len()),
374-
)
375-
};
354+
let tactic = get_tactics(&item_vec, &output, &shape);
376355
let trailing_separator = if !context.use_block_indent() || variadic {
377356
SeparatorTactic::Never
378357
} else {
@@ -413,6 +392,22 @@ fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
413392
colon_spaces(context.config)
414393
}
415394

395+
// If the return type is multi-lined, then force to use multiple lines for
396+
// arguments as well.
397+
fn get_tactics(item_vec: &Vec<ListItem>, output: &str, shape: &Shape) -> DefinitiveListTactic {
398+
if output.contains('\n') {
399+
DefinitiveListTactic::Vertical
400+
} else {
401+
definitive_tactic(
402+
&*item_vec,
403+
ListTactic::HorizontalVertical,
404+
Separator::Comma,
405+
// 2 is for the case of ',\n'
406+
shape.width.saturating_sub(2 + output.len()),
407+
)
408+
}
409+
}
410+
416411
impl Rewrite for ast::WherePredicate {
417412
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
418413
// FIXME: dead spans?

0 commit comments

Comments
 (0)