Skip to content

Commit 528e777

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 7713d05 commit 528e777

File tree

3 files changed

+11
-68
lines changed

3 files changed

+11
-68
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: 6 additions & 29 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::{
@@ -292,35 +291,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
292291
}
293292
}
294293
}
295-
} else {
296-
let sub_slice = sub_slice.trim();
297-
if comment_on_same_line {
298-
// 1 = a space before `//`
299-
let offset_len = 1 + last_line_width(&self.buffer)
300-
.saturating_sub(self.block_indent.width());
301-
match comment_shape
302-
.visual_indent(offset_len)
303-
.sub_width(offset_len)
304-
{
305-
Some(shp) => comment_shape = shp,
306-
None => comment_on_same_line = false,
307-
}
308-
};
309-
310-
if comment_on_same_line {
311-
self.push_str(" ");
312-
} else {
313-
if count_newlines(snippet_in_between) >= 2 || extra_newline {
314-
self.push_str("\n");
315-
}
316-
self.push_str(&self.block_indent.to_string_with_newline(config));
317-
}
294+
self.push_str(&self.block_indent.to_string_with_newline(config));
295+
}
318296

319-
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
320-
match comment_str {
321-
Some(ref s) => self.push_str(s),
322-
None => self.push_str(&sub_slice),
323-
}
297+
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
298+
match comment_str {
299+
Some(ref s) => self.push_str(s),
300+
None => self.push_str(&sub_slice),
324301
}
325302
}
326303
CodeCharKind::Normal if skip_normal(&sub_slice) => {

0 commit comments

Comments
 (0)