Skip to content

Commit fe10eb5

Browse files
committed
Revert "do not force comments to be indented with a comment trailing a line of code (rust-lang#3833)"
This reverts commit fb01dc8.
1 parent e0077aa commit fe10eb5

File tree

3 files changed

+27
-90
lines changed

3 files changed

+27
-90
lines changed

src/missed_spans.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use syntax::source_map::{BytePos, Pos, Span};
33
use crate::comment::{is_last_comment_block, rewrite_comment, CodeCharKind, CommentCodeSlices};
44
use crate::config::file_lines::FileLines;
55
use crate::config::FileName;
6-
use crate::config::Version;
76
use crate::coverage::transform_missing_snippet;
87
use crate::shape::{Indent, Shape};
98
use crate::source_map::LineRangeUtils;
@@ -234,7 +233,6 @@ impl<'a> FmtVisitor<'a> {
234233
.next();
235234

236235
let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
237-
let mut on_same_line = false;
238236

239237
let comment_indent = if fix_indent {
240238
if let Some('{') = last_char {
@@ -243,13 +241,6 @@ impl<'a> FmtVisitor<'a> {
243241
let indent_str = self.block_indent.to_string(self.config);
244242
self.push_str(&indent_str);
245243
self.block_indent
246-
} else if self.config.version() == Version::Two && !snippet.starts_with('\n') {
247-
// The comment appears on the same line as the previous formatted code.
248-
// Assuming that comment is logically associated with that code, we want to keep it on
249-
// the same level and avoid mixing it with possible other comment.
250-
on_same_line = true;
251-
self.push_str(" ");
252-
self.block_indent
253244
} else {
254245
self.push_str(" ");
255246
Indent::from_width(self.config, last_line_width(&self.buffer))
@@ -260,34 +251,9 @@ impl<'a> FmtVisitor<'a> {
260251
self.config.max_width() - self.block_indent.width(),
261252
);
262253
let comment_shape = Shape::legacy(comment_width, comment_indent);
263-
264-
if on_same_line {
265-
match subslice.find("\n") {
266-
None => {
267-
self.push_str(subslice);
268-
}
269-
Some(offset) if offset + 1 == subslice.len() => {
270-
self.push_str(&subslice[..offset]);
271-
}
272-
Some(offset) => {
273-
// keep first line as is: if it were too long and wrapped, it may get mixed
274-
// with the other lines.
275-
let first_line = &subslice[..offset];
276-
self.push_str(first_line);
277-
self.push_str(&comment_indent.to_string_with_newline(self.config));
278-
279-
let other_lines = &subslice[offset + 1..];
280-
let comment_str =
281-
rewrite_comment(other_lines, false, comment_shape, self.config)
282-
.unwrap_or_else(|| String::from(other_lines));
283-
self.push_str(&comment_str);
284-
}
285-
}
286-
} else {
287-
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
288-
.unwrap_or_else(|| String::from(subslice));
289-
self.push_str(&comment_str);
290-
}
254+
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
255+
.unwrap_or_else(|| String::from(subslice));
256+
self.push_str(&comment_str);
291257

292258
status.last_wspace = None;
293259
status.line_start = offset + subslice.len();

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ pub(crate) fn is_attributes_extendable(attrs_str: &str) -> bool {
193193
!attrs_str.contains('\n') && !last_line_contains_single_line_comment(attrs_str)
194194
}
195195

196-
/// The width of the first line in s.
196+
// The width of the first line in s.
197197
#[inline]
198198
pub(crate) fn first_line_width(s: &str) -> usize {
199199
unicode_str_width(s.splitn(2, '\n').next().unwrap_or(""))
200200
}
201201

202-
/// The width of the last line in s.
202+
// The width of the last line in s.
203203
#[inline]
204204
pub(crate) fn last_line_width(s: &str) -> usize {
205205
unicode_str_width(s.rsplitn(2, '\n').next().unwrap_or(""))

src/visitor.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use syntax::{ast, visit};
66

77
use crate::attr::*;
88
use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
9-
use crate::config::Version;
109
use crate::config::{BraceStyle, Config};
1110
use crate::coverage::transform_missing_snippet;
1211
use crate::items::{
@@ -263,60 +262,32 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
263262

264263
let mut comment_shape =
265264
Shape::indented(self.block_indent, config).comment(config);
266-
if self.config.version() == Version::Two && comment_on_same_line {
267-
self.push_str(" ");
268-
// put the first line of the comment on the same line as the
269-
// block's last line
270-
match sub_slice.find("\n") {
271-
None => {
272-
self.push_str(&sub_slice);
273-
}
274-
Some(offset) if offset + 1 == sub_slice.len() => {
275-
self.push_str(&sub_slice[..offset]);
276-
}
277-
Some(offset) => {
278-
let first_line = &sub_slice[..offset];
279-
self.push_str(first_line);
280-
self.push_str(&self.block_indent.to_string_with_newline(config));
281-
282-
// put the other lines below it, shaping it as needed
283-
let other_lines = &sub_slice[offset + 1..];
284-
let comment_str =
285-
rewrite_comment(other_lines, false, comment_shape, config);
286-
match comment_str {
287-
Some(ref s) => self.push_str(s),
288-
None => self.push_str(other_lines),
289-
}
290-
}
265+
if comment_on_same_line {
266+
// 1 = a space before `//`
267+
let offset_len = 1 + last_line_width(&self.buffer)
268+
.saturating_sub(self.block_indent.width());
269+
match comment_shape
270+
.visual_indent(offset_len)
271+
.sub_width(offset_len)
272+
{
273+
Some(shp) => comment_shape = shp,
274+
None => comment_on_same_line = false,
291275
}
276+
};
277+
278+
if comment_on_same_line {
279+
self.push_str(" ");
292280
} else {
293-
if comment_on_same_line {
294-
// 1 = a space before `//`
295-
let offset_len = 1 + last_line_width(&self.buffer)
296-
.saturating_sub(self.block_indent.width());
297-
match comment_shape
298-
.visual_indent(offset_len)
299-
.sub_width(offset_len)
300-
{
301-
Some(shp) => comment_shape = shp,
302-
None => comment_on_same_line = false,
303-
}
304-
};
305-
306-
if comment_on_same_line {
307-
self.push_str(" ");
308-
} else {
309-
if count_newlines(snippet_in_between) >= 2 || extra_newline {
310-
self.push_str("\n");
311-
}
312-
self.push_str(&self.block_indent.to_string_with_newline(config));
281+
if count_newlines(snippet_in_between) >= 2 || extra_newline {
282+
self.push_str("\n");
313283
}
284+
self.push_str(&self.block_indent.to_string_with_newline(config));
285+
}
314286

315-
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
316-
match comment_str {
317-
Some(ref s) => self.push_str(s),
318-
None => self.push_str(&sub_slice),
319-
}
287+
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
288+
match comment_str {
289+
Some(ref s) => self.push_str(s),
290+
None => self.push_str(&sub_slice),
320291
}
321292
}
322293
CodeCharKind::Normal if skip_normal(&sub_slice) => {

0 commit comments

Comments
 (0)