Skip to content

Commit

Permalink
Revert "Put single-expression functions on a single line"
Browse files Browse the repository at this point in the history
This reverts commit 4ce32d6.

Changing the default formatting using an unstable `rustfmt` feature
was a bad idea: it means that people who have auto-formatting turned
on (like me) will see the formatting flip-flop when switching between
stable and nightly.

The problem is that the unstable `fn_single_line` option only takes
effect on nightly, so when `rustfmt` runs on stable, it will add lots
of spurious changes.

The `imports_granularity` option does not have this problem: `rustfmt`
will not modify imports by default and so nothing happens when it is
run on stable.
  • Loading branch information
mgeisler committed Feb 27, 2022
1 parent c691e8e commit 99154ae
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/wrap_first_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Word {
penalty_width: f64,
}

#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/wrap_optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Word {
penalty_width: f64,
}

#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/wrap_optimal_fit_usize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Word {
penalty_width: usize,
}

#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width as f64 }
fn whitespace_width(&self) -> f64 { self.whitespace_width as f64 }
Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fn_single_line = true
imports_granularity = "Module"
20 changes: 15 additions & 5 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ pub(crate) fn skip_ansi_escape_sequence<I: Iterator<Item = char>>(ch: char, char

#[cfg(feature = "unicode-width")]
#[inline]
fn ch_width(ch: char) -> usize { unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) }
fn ch_width(ch: char) -> usize {
unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0)
}

/// First character which [`ch_width`] will classify as double-width.
/// Please see [`display_width`].
Expand Down Expand Up @@ -224,7 +226,9 @@ pub struct Word<'a> {
impl std::ops::Deref for Word<'_> {
type Target = str;

fn deref(&self) -> &Self::Target { self.word }
fn deref(&self) -> &Self::Target {
self.word
}
}

impl<'a> Word<'a> {
Expand Down Expand Up @@ -299,17 +303,23 @@ impl<'a> Word<'a> {

impl Fragment for Word<'_> {
#[inline]
fn width(&self) -> f64 { self.width as f64 }
fn width(&self) -> f64 {
self.width as f64
}

// We assume the whitespace consist of ' ' only. This allows us to
// compute the display width in constant time.
#[inline]
fn whitespace_width(&self) -> f64 { self.whitespace.len() as f64 }
fn whitespace_width(&self) -> f64 {
self.whitespace.len() as f64
}

// We assume the penalty is `""` or `"-"`. This allows us to
// compute the display width in constant time.
#[inline]
fn penalty_width(&self) -> f64 { self.penalty.len() as f64 }
fn penalty_width(&self) -> f64 {
self.penalty.len() as f64
}
}

/// Forcibly break words wider than `line_width` into smaller words.
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ impl<'a> From<&'a Options<'a>> for Options<'a> {
}

impl<'a> From<usize> for Options<'a> {
fn from(width: usize) -> Self { Options::new(width) }
fn from(width: usize) -> Self {
Options::new(width)
}
}

impl<'a> Options<'a> {
Expand Down Expand Up @@ -340,7 +342,9 @@ impl<'a> Options<'a> {
/// **Note:** Only available when the `terminal_size` feature is
/// enabled.
#[cfg(feature = "terminal_size")]
pub fn with_termwidth() -> Self { Self::new(termwidth()) }
pub fn with_termwidth() -> Self {
Self::new(termwidth())
}
}

impl<'a> Options<'a> {
Expand Down Expand Up @@ -1837,5 +1841,7 @@ mod tests {

#[test]
#[should_panic]
fn wrap_columns_panic_with_zero_columns() { wrap_columns("", 0, 10, "", "", ""); }
fn wrap_columns_panic_with_zero_columns() {
wrap_columns("", 0, 10, "", "", "");
}
}
9 changes: 7 additions & 2 deletions src/wrap_algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ impl WrapAlgorithm {
/// **Note:** Only available when the `smawk` Cargo feature is
/// enabled.
#[cfg(feature = "smawk")]
pub const fn new_optimal_fit() -> Self { WrapAlgorithm::OptimalFit(Penalties::new()) }
pub const fn new_optimal_fit() -> Self {
WrapAlgorithm::OptimalFit(Penalties::new())
}

/// Wrap words according to line widths.
///
Expand Down Expand Up @@ -157,7 +159,9 @@ impl WrapAlgorithm {
}

impl Default for WrapAlgorithm {
fn default() -> Self { WrapAlgorithm::new() }
fn default() -> Self {
WrapAlgorithm::new()
}
}

/// Wrap abstract fragments into lines with a first-fit algorithm.
Expand Down Expand Up @@ -341,6 +345,7 @@ mod tests {
#[derive(Debug, PartialEq)]
struct Word(f64);

#[rustfmt::skip]
impl Fragment for Word {
fn width(&self) -> f64 { self.0 }
fn whitespace_width(&self) -> f64 { 1.0 }
Expand Down
5 changes: 4 additions & 1 deletion src/wrap_algorithms/optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ impl Penalties {
}

impl Default for Penalties {
fn default() -> Self { Self::new() }
fn default() -> Self {
Self::new()
}
}

/// Cache for line numbers. This is necessary to avoid a O(n**2)
Expand Down Expand Up @@ -393,6 +395,7 @@ mod tests {
#[derive(Debug, PartialEq)]
struct Word(f64);

#[rustfmt::skip]
impl Fragment for Word {
fn width(&self) -> f64 { self.0 }
fn whitespace_width(&self) -> f64 { 1.0 }
Expand Down

0 comments on commit 99154ae

Please sign in to comment.