Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 160c3aa

Browse files
scampitopecongiro
authored andcommitted
handle hard tabs when formatting trailing comments (rust-lang#3836)
1 parent 33e3667 commit 160c3aa

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

src/missed_spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::config::Version;
77
use crate::coverage::transform_missing_snippet;
88
use crate::shape::{Indent, Shape};
99
use crate::source_map::LineRangeUtils;
10-
use crate::utils::{count_lf_crlf, count_newlines, last_line_indent, last_line_width, mk_sp};
10+
use crate::utils::{count_lf_crlf, count_newlines, last_line_width, mk_sp};
1111
use crate::visitor::FmtVisitor;
1212

1313
struct SnippetStatus {
@@ -254,7 +254,7 @@ impl<'a> FmtVisitor<'a> {
254254
// the same level and avoid mixing it with possible other comment.
255255
on_same_line = true;
256256
self.push_str(" ");
257-
Indent::from_width(self.config, last_line_indent(&self.buffer))
257+
self.block_indent
258258
} else {
259259
self.push_str(" ");
260260
Indent::from_width(self.config, last_line_width(&self.buffer))

src/utils.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,6 @@ pub(crate) fn last_line_width(s: &str) -> usize {
205205
unicode_str_width(s.rsplitn(2, '\n').next().unwrap_or(""))
206206
}
207207

208-
/// The indent width of the last line in s.
209-
#[inline]
210-
pub(crate) fn last_line_indent(s: &str) -> usize {
211-
let last_line = s.rsplitn(2, '\n').next().unwrap_or("");
212-
last_line.chars().take_while(|c| c.is_whitespace()).count()
213-
}
214-
215208
/// The total used width of the last line.
216209
#[inline]
217210
pub(crate) fn last_line_used_width(s: &str, offset: usize) -> usize {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-version: Two
2+
// rustfmt-wrap_comments: true
3+
// rustfmt-hard_tabs: true
4+
5+
impl Foo {
6+
fn foo() {
7+
bar(); // comment 1
8+
// comment 2
9+
// comment 3
10+
baz();
11+
}
12+
}
13+
14+
fn lorem_ipsum() {
15+
let f = bar(); // Donec consequat mi. Quisque vitae dolor. Integer lobortis. Maecenas id nulla. Lorem.
16+
// Id turpis. Nam posuere lectus vitae nibh. Etiam tortor orci, sagittis malesuada, rhoncus quis, hendrerit eget, libero. Quisque commodo nulla at nunc. Mauris consequat, enim vitae venenatis sollicitudin, dolor orci bibendum enim, a sagittis nulla nunc quis elit. Phasellus augue. Nunc suscipit, magna tincidunt lacinia faucibus, lacus tellus ornare purus, a pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus. In hac habitasse platea dictumst. Vivamus a orci at nulla tristique condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit amet, ipsum. In pharetra sagittis nunc.
17+
let b = baz();
18+
19+
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
20+
// TODO(emilio): It may make sense to make this range [.01, 10.0], to align with css-fonts-4's range of [1, 1000].
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-version: Two
2+
// rustfmt-wrap_comments: true
3+
// rustfmt-hard_tabs: true
4+
5+
impl Foo {
6+
fn foo() {
7+
bar(); // comment 1
8+
// comment 2
9+
// comment 3
10+
baz();
11+
}
12+
}
13+
14+
fn lorem_ipsum() {
15+
let f = bar(); // Donec consequat mi. Quisque vitae dolor. Integer lobortis. Maecenas id nulla. Lorem.
16+
// Id turpis. Nam posuere lectus vitae nibh. Etiam tortor orci, sagittis
17+
// malesuada, rhoncus quis, hendrerit eget, libero. Quisque commodo nulla at
18+
// nunc. Mauris consequat, enim vitae venenatis sollicitudin, dolor orci
19+
// bibendum enim, a sagittis nulla nunc quis elit. Phasellus augue. Nunc
20+
// suscipit, magna tincidunt lacinia faucibus, lacus tellus ornare purus, a
21+
// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus.
22+
// In hac habitasse platea dictumst. Vivamus a orci at nulla tristique
23+
// condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit
24+
// amet, ipsum. In pharetra sagittis nunc.
25+
let b = baz();
26+
27+
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
28+
// TODO(emilio): It may make sense to make this range [.01, 10.0], to align
29+
// with css-fonts-4's range of [1, 1000].
30+
}

0 commit comments

Comments
 (0)