Skip to content

Commit dae76d2

Browse files
committed
Implement soft wrapping for comments
1 parent 3839987 commit dae76d2

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

src/comment.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ fn rewrite_comment_inner(
287287
.checked_sub(closer.len() + opener.len())
288288
.unwrap_or(1);
289289
let indent_str = shape.indent.to_string(config);
290-
let fmt = StringFormat {
290+
let fmt_indent = shape.indent + (opener.len() - line_start.len());
291+
let mut fmt = StringFormat {
291292
opener: "",
292293
closer: "",
293294
line_start: line_start,
294295
line_end: "",
295-
shape: Shape::legacy(max_chars, shape.indent + (opener.len() - line_start.len())),
296+
shape: Shape::legacy(max_chars, fmt_indent),
296297
trim_end: true,
297298
config: config,
298299
};
@@ -317,26 +318,69 @@ fn rewrite_comment_inner(
317318
});
318319

319320
let mut result = opener.to_owned();
321+
let mut is_prev_line_multi_line = false;
322+
let comment_line_separator = format!("\n{}{}", indent_str, line_start);
320323
for line in lines {
321324
if result == opener {
322325
if line.is_empty() {
323326
continue;
324327
}
325328
} else {
326-
result.push('\n');
327-
result.push_str(&indent_str);
328-
result.push_str(line_start);
329+
if is_prev_line_multi_line && !line.is_empty() {
330+
result.push(' ')
331+
} else {
332+
result.push_str(&comment_line_separator);
333+
}
329334
}
330335

331-
if config.wrap_comments() && line.len() > max_chars {
332-
let rewrite = rewrite_string(line, &fmt).unwrap_or_else(|| line.to_owned());
333-
result.push_str(&rewrite);
336+
if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
337+
match rewrite_string(line, &fmt, Some(max_chars)) {
338+
Some(ref s) => {
339+
is_prev_line_multi_line = s.contains('\n');
340+
result.push_str(s);
341+
}
342+
None if is_prev_line_multi_line => {
343+
// We failed to put the current `line` next to the previous `line`.
344+
// Remove the trailing space, then start rewrite on the next line.
345+
result.pop();
346+
result.push_str(&comment_line_separator);
347+
fmt.shape = Shape::legacy(max_chars, fmt_indent);
348+
match rewrite_string(line, &fmt, Some(max_chars)) {
349+
Some(ref s) => {
350+
is_prev_line_multi_line = s.contains('\n');
351+
result.push_str(s);
352+
}
353+
None => {
354+
is_prev_line_multi_line = false;
355+
result.push_str(line);
356+
}
357+
}
358+
}
359+
None => {
360+
is_prev_line_multi_line = false;
361+
result.push_str(line);
362+
}
363+
}
364+
365+
fmt.shape = if is_prev_line_multi_line {
366+
// 1 = " "
367+
let offset = 1 + last_line_width(&result) - line_start.len();
368+
Shape {
369+
width: max_chars.checked_sub(offset).unwrap_or(0),
370+
indent: fmt_indent,
371+
offset: fmt.shape.offset + offset,
372+
}
373+
} else {
374+
Shape::legacy(max_chars, fmt_indent)
375+
};
334376
} else {
335377
if line.is_empty() && result.ends_with(' ') {
336378
// Remove space if this is an empty comment or a doc comment.
337379
result.pop();
338380
}
339381
result.push_str(line);
382+
fmt.shape = Shape::legacy(max_chars, fmt_indent);
383+
is_prev_line_multi_line = false;
340384
}
341385
}
342386

0 commit comments

Comments
 (0)