Skip to content

Commit 0d82ef6

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 31ba496 commit 0d82ef6

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;
@@ -236,7 +235,6 @@ impl<'a> FmtVisitor<'a> {
236235
.next();
237236

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

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

294260
status.last_wspace = None;
295261
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::{
@@ -296,35 +295,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
296295
}
297296
}
298297
}
299-
} else {
300-
let sub_slice = sub_slice.trim();
301-
if comment_on_same_line {
302-
// 1 = a space before `//`
303-
let offset_len = 1 + last_line_width(&self.buffer)
304-
.saturating_sub(self.block_indent.width());
305-
match comment_shape
306-
.visual_indent(offset_len)
307-
.sub_width(offset_len)
308-
{
309-
Some(shp) => comment_shape = shp,
310-
None => comment_on_same_line = false,
311-
}
312-
};
313-
314-
if comment_on_same_line {
315-
self.push_str(" ");
316-
} else {
317-
if count_newlines(snippet_in_between) >= 2 || extra_newline {
318-
self.push_str("\n");
319-
}
320-
self.push_str(&self.block_indent.to_string_with_newline(config));
321-
}
298+
self.push_str(&self.block_indent.to_string_with_newline(config));
299+
}
322300

323-
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
324-
match comment_str {
325-
Some(ref s) => self.push_str(s),
326-
None => self.push_str(&sub_slice),
327-
}
301+
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
302+
match comment_str {
303+
Some(ref s) => self.push_str(s),
304+
None => self.push_str(&sub_slice),
328305
}
329306
}
330307
CodeCharKind::Normal if skip_normal(&sub_slice) => {

0 commit comments

Comments
 (0)