Skip to content

Commit

Permalink
handle hard tabs when formatting trailing comments (rust-lang#3836)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampi authored and topecongiro committed Oct 7, 2019
1 parent 33e3667 commit 160c3aa
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 0 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions tests/source/trailing_comments/hard_tabs.rs
Original file line number Diff line number Diff line change
@@ -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].
}
File renamed without changes.
30 changes: 30 additions & 0 deletions tests/target/trailing_comments/hard_tabs.rs
Original file line number Diff line number Diff line change
@@ -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].
}
File renamed without changes.

0 comments on commit 160c3aa

Please sign in to comment.