Skip to content

Commit 4786aba

Browse files
authored
Respect tab-size setting in formatter (#8006)
1 parent 46d5db5 commit 4786aba

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

crates/ruff_cli/tests/format.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ fn format_options() -> Result<()> {
5151
fs::write(
5252
&ruff_toml,
5353
r#"
54+
tab-size = 8
55+
line-length = 84
56+
5457
[format]
5558
indent-style = "tab"
5659
quote-style = "single"
@@ -65,7 +68,7 @@ line-ending = "cr-lf"
6568
.arg("-")
6669
.pass_stdin(r#"
6770
def foo(arg1, arg2,):
68-
print("Shouldn't change quotes")
71+
print("Shouldn't change quotes. It exceeds the line width with the tab size 8")
6972
7073
7174
if condition:
@@ -77,7 +80,9 @@ if condition:
7780
exit_code: 0
7881
----- stdout -----
7982
def foo(arg1, arg2):
80-
print("Shouldn't change quotes")
83+
print(
84+
"Shouldn't change quotes. It exceeds the line width with the tab size 8"
85+
)
8186
8287
8388
if condition:

crates/ruff_formatter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl std::fmt::Display for IndentStyle {
9595
///
9696
/// Determines the visual width of a tab character (`\t`) and the number of
9797
/// spaces per indent when using [`IndentStyle::Space`].
98-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98+
#[derive(Clone, Copy, Debug, Eq, PartialEq, CacheKey)]
9999
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100100
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
101101
pub struct IndentWidth(NonZeroU8);

crates/ruff_linter/src/line_width.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,9 @@ impl From<NonZeroU8> for TabSize {
253253
Self(tab_size)
254254
}
255255
}
256+
257+
impl From<TabSize> for NonZeroU8 {
258+
fn from(value: TabSize) -> Self {
259+
value.0
260+
}
261+
}

crates/ruff_workspace/src/configuration.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use std::borrow::Cow;
66
use std::env::VarError;
7-
use std::num::NonZeroU16;
7+
use std::num::{NonZeroU16, NonZeroU8};
88
use std::path::{Path, PathBuf};
99

1010
use anyhow::{anyhow, Result};
@@ -16,7 +16,7 @@ use shellexpand::LookupError;
1616
use strum::IntoEnumIterator;
1717

1818
use ruff_cache::cache_dir;
19-
use ruff_formatter::{IndentStyle, LineWidth};
19+
use ruff_formatter::{IndentStyle, IndentWidth, LineWidth};
2020
use ruff_linter::line_width::{LineLength, TabSize};
2121
use ruff_linter::registry::RuleNamespace;
2222
use ruff_linter::registry::{Rule, RuleSet, INCOMPATIBLE_CODES};
@@ -155,7 +155,7 @@ impl Configuration {
155155

156156
let format = self.format;
157157
let format_defaults = FormatterSettings::default();
158-
// TODO(micha): Support changing the tab-width but disallow changing the number of spaces
158+
159159
let formatter = FormatterSettings {
160160
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
161161
preview: match format.preview.unwrap_or(global_preview) {
@@ -169,6 +169,11 @@ impl Configuration {
169169
}),
170170
line_ending: format.line_ending.unwrap_or(format_defaults.line_ending),
171171
indent_style: format.indent_style.unwrap_or(format_defaults.indent_style),
172+
indent_width: self
173+
.tab_size
174+
.map_or(format_defaults.indent_width, |tab_size| {
175+
IndentWidth::from(NonZeroU8::from(tab_size))
176+
}),
172177
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
173178
magic_trailing_comma: format
174179
.magic_trailing_comma

crates/ruff_workspace/src/options.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,11 @@ pub struct Options {
363363
)]
364364
pub line_length: Option<LineLength>,
365365

366-
/// The tabulation size to calculate line length.
366+
/// The number of spaces a tab is equal to when enforcing long-line violations (like `E501`)
367+
/// or formatting code with the formatter.
368+
///
369+
/// This option changes the number of spaces inserted by the formatter when
370+
/// using soft-tabs (`indent-style = space`).
367371
#[option(
368372
default = "4",
369373
value_type = "int",

crates/ruff_workspace/src/settings.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use path_absolutize::path_dedot;
22
use ruff_cache::cache_dir;
3-
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
3+
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
44
use ruff_linter::settings::types::{FilePattern, FilePatternSet, SerializationFormat, UnsafeFixes};
55
use ruff_linter::settings::LinterSettings;
66
use ruff_macros::CacheKey;
@@ -117,6 +117,7 @@ pub struct FormatterSettings {
117117
pub line_width: LineWidth,
118118

119119
pub indent_style: IndentStyle,
120+
pub indent_width: IndentWidth,
120121

121122
pub quote_style: QuoteStyle,
122123

@@ -150,6 +151,7 @@ impl FormatterSettings {
150151

151152
PyFormatOptions::from_source_type(source_type)
152153
.with_indent_style(self.indent_style)
154+
.with_indent_width(self.indent_width)
153155
.with_quote_style(self.quote_style)
154156
.with_magic_trailing_comma(self.magic_trailing_comma)
155157
.with_preview(self.preview)
@@ -164,10 +166,11 @@ impl Default for FormatterSettings {
164166

165167
Self {
166168
exclude: FilePatternSet::default(),
167-
preview: ruff_python_formatter::PreviewMode::Disabled,
169+
preview: PreviewMode::Disabled,
168170
line_width: default_options.line_width(),
169171
line_ending: LineEnding::Lf,
170172
indent_style: default_options.indent_style(),
173+
indent_width: default_options.indent_width(),
171174
quote_style: default_options.quote_style(),
172175
magic_trailing_comma: default_options.magic_trailing_comma(),
173176
}

ruff.schema.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)