mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-21 04:03:11 +00:00
handle hard tabs when formatting trailing comments (#3836)
This commit is contained in:
parent
33e3667085
commit
160c3aafc5
@ -7,7 +7,7 @@ use crate::config::Version;
|
||||
use crate::coverage::transform_missing_snippet;
|
||||
use crate::shape::{Indent, Shape};
|
||||
use crate::source_map::LineRangeUtils;
|
||||
use crate::utils::{count_lf_crlf, count_newlines, last_line_indent, last_line_width, mk_sp};
|
||||
use crate::utils::{count_lf_crlf, count_newlines, last_line_width, mk_sp};
|
||||
use crate::visitor::FmtVisitor;
|
||||
|
||||
struct SnippetStatus {
|
||||
@ -254,7 +254,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
// the same level and avoid mixing it with possible other comment.
|
||||
on_same_line = true;
|
||||
self.push_str(" ");
|
||||
Indent::from_width(self.config, last_line_indent(&self.buffer))
|
||||
self.block_indent
|
||||
} else {
|
||||
self.push_str(" ");
|
||||
Indent::from_width(self.config, last_line_width(&self.buffer))
|
||||
|
@ -205,13 +205,6 @@ pub(crate) fn last_line_width(s: &str) -> usize {
|
||||
unicode_str_width(s.rsplitn(2, '\n').next().unwrap_or(""))
|
||||
}
|
||||
|
||||
/// The indent width of the last line in s.
|
||||
#[inline]
|
||||
pub(crate) fn last_line_indent(s: &str) -> usize {
|
||||
let last_line = s.rsplitn(2, '\n').next().unwrap_or("");
|
||||
last_line.chars().take_while(|c| c.is_whitespace()).count()
|
||||
}
|
||||
|
||||
/// The total used width of the last line.
|
||||
#[inline]
|
||||
pub(crate) fn last_line_used_width(s: &str, offset: usize) -> usize {
|
||||
|
21
tests/source/trailing_comments/hard_tabs.rs
Normal file
21
tests/source/trailing_comments/hard_tabs.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// rustfmt-version: Two
|
||||
// rustfmt-wrap_comments: true
|
||||
// rustfmt-hard_tabs: true
|
||||
|
||||
impl Foo {
|
||||
fn foo() {
|
||||
bar(); // comment 1
|
||||
// comment 2
|
||||
// comment 3
|
||||
baz();
|
||||
}
|
||||
}
|
||||
|
||||
fn lorem_ipsum() {
|
||||
let f = bar(); // Donec consequat mi. Quisque vitae dolor. Integer lobortis. Maecenas id nulla. Lorem.
|
||||
// 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.
|
||||
let b = baz();
|
||||
|
||||
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
|
||||
// TODO(emilio): It may make sense to make this range [.01, 10.0], to align with css-fonts-4's range of [1, 1000].
|
||||
}
|
30
tests/target/trailing_comments/hard_tabs.rs
Normal file
30
tests/target/trailing_comments/hard_tabs.rs
Normal file
@ -0,0 +1,30 @@
|
||||
// rustfmt-version: Two
|
||||
// rustfmt-wrap_comments: true
|
||||
// rustfmt-hard_tabs: true
|
||||
|
||||
impl Foo {
|
||||
fn foo() {
|
||||
bar(); // comment 1
|
||||
// comment 2
|
||||
// comment 3
|
||||
baz();
|
||||
}
|
||||
}
|
||||
|
||||
fn lorem_ipsum() {
|
||||
let f = bar(); // Donec consequat mi. Quisque vitae dolor. Integer lobortis. Maecenas id nulla. Lorem.
|
||||
// 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.
|
||||
let b = baz();
|
||||
|
||||
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]
|
||||
// TODO(emilio): It may make sense to make this range [.01, 10.0], to align
|
||||
// with css-fonts-4's range of [1, 1000].
|
||||
}
|
Loading…
Reference in New Issue
Block a user