Skip to content

Commit 84979f9

Browse files
authored
Rename tab-size to indent-width (#8082)
## Summary This PR renames the `tab-size` configuration option to `indent-width` to express that the formatter uses the option to determine the indentation width AND as tab width. I first preferred naming the option `tab-width` but then decided to go with `indent-width` because: * It aligns with the `indent-style` option * It would allow us to write a lint rule that asserts that each indentation uses `indent-width` spaces. Closes #7643 ## Test Plan Added integration test
1 parent c3dabc1 commit 84979f9

File tree

10 files changed

+143
-49
lines changed

10 files changed

+143
-49
lines changed

crates/ruff_cli/tests/format.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn format_options() -> Result<()> {
5050
fs::write(
5151
&ruff_toml,
5252
r#"
53-
tab-size = 8
53+
indent-width = 8
5454
line-length = 84
5555
5656
[format]
@@ -278,6 +278,41 @@ if condition:
278278
Ok(())
279279
}
280280

281+
#[test]
282+
fn deprecated_options() -> Result<()> {
283+
let tempdir = TempDir::new()?;
284+
let ruff_toml = tempdir.path().join("ruff.toml");
285+
fs::write(
286+
&ruff_toml,
287+
r#"
288+
tab-size = 2
289+
"#,
290+
)?;
291+
292+
insta::with_settings!({filters => vec![
293+
(&*regex::escape(ruff_toml.to_str().unwrap()), "[RUFF-TOML-PATH]"),
294+
]}, {
295+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
296+
.args(["format", "--config"])
297+
.arg(&ruff_toml)
298+
.arg("-")
299+
.pass_stdin(r#"
300+
if True:
301+
pass
302+
"#), @r###"
303+
success: true
304+
exit_code: 0
305+
----- stdout -----
306+
if True:
307+
pass
308+
309+
----- stderr -----
310+
warning: The `tab-size` option has been renamed to `indent-width` to emphasize that it configures the indentation used by the formatter as well as the tab width. Please update your configuration to use `indent-width = <value>` instead.
311+
"###);
312+
});
313+
Ok(())
314+
}
315+
281316
/// Since 0.1.0 the legacy format option is no longer supported
282317
#[test]
283318
fn legacy_format_option() -> Result<()> {

crates/ruff_linter/src/fix/edits.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ruff_source_file::{Locator, NewlineWithTrailingNewline, UniversalNewlines};
1414
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
1515

1616
use crate::fix::codemods;
17-
use crate::line_width::{LineLength, LineWidthBuilder, TabSize};
17+
use crate::line_width::{IndentWidth, LineLength, LineWidthBuilder};
1818

1919
/// Return the `Fix` to use when deleting a `Stmt`.
2020
///
@@ -293,7 +293,7 @@ pub(crate) fn fits(
293293
node: AnyNodeRef,
294294
locator: &Locator,
295295
line_length: LineLength,
296-
tab_size: TabSize,
296+
tab_size: IndentWidth,
297297
) -> bool {
298298
all_lines_fit(fix, node, locator, line_length.value() as usize, tab_size)
299299
}
@@ -305,7 +305,7 @@ pub(crate) fn fits_or_shrinks(
305305
node: AnyNodeRef,
306306
locator: &Locator,
307307
line_length: LineLength,
308-
tab_size: TabSize,
308+
tab_size: IndentWidth,
309309
) -> bool {
310310
// Use the larger of the line length limit, or the longest line in the existing AST node.
311311
let line_length = std::iter::once(line_length.value() as usize)
@@ -327,7 +327,7 @@ fn all_lines_fit(
327327
node: AnyNodeRef,
328328
locator: &Locator,
329329
line_length: usize,
330-
tab_size: TabSize,
330+
tab_size: IndentWidth,
331331
) -> bool {
332332
let prefix = locator.slice(TextRange::new(
333333
locator.line_start(node.start()),

crates/ruff_linter/src/line_width.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ pub struct LineWidthBuilder {
129129
/// This is used to calculate the width of tabs.
130130
column: usize,
131131
/// The tab size to use when calculating the width of tabs.
132-
tab_size: TabSize,
132+
tab_size: IndentWidth,
133133
}
134134

135135
impl Default for LineWidthBuilder {
136136
fn default() -> Self {
137-
Self::new(TabSize::default())
137+
Self::new(IndentWidth::default())
138138
}
139139
}
140140

@@ -164,7 +164,7 @@ impl LineWidthBuilder {
164164
}
165165

166166
/// Creates a new `LineWidth` with the given tab size.
167-
pub fn new(tab_size: TabSize) -> Self {
167+
pub fn new(tab_size: IndentWidth) -> Self {
168168
LineWidthBuilder {
169169
width: 0,
170170
column: 0,
@@ -234,28 +234,28 @@ impl PartialOrd<LineLength> for LineWidthBuilder {
234234
/// The size of a tab.
235235
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, CacheKey)]
236236
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
237-
pub struct TabSize(NonZeroU8);
237+
pub struct IndentWidth(NonZeroU8);
238238

239-
impl TabSize {
239+
impl IndentWidth {
240240
pub(crate) fn as_usize(self) -> usize {
241241
self.0.get() as usize
242242
}
243243
}
244244

245-
impl Default for TabSize {
245+
impl Default for IndentWidth {
246246
fn default() -> Self {
247247
Self(NonZeroU8::new(4).unwrap())
248248
}
249249
}
250250

251-
impl From<NonZeroU8> for TabSize {
251+
impl From<NonZeroU8> for IndentWidth {
252252
fn from(tab_size: NonZeroU8) -> Self {
253253
Self(tab_size)
254254
}
255255
}
256256

257-
impl From<TabSize> for NonZeroU8 {
258-
fn from(value: TabSize) -> Self {
257+
impl From<IndentWidth> for NonZeroU8 {
258+
fn from(value: IndentWidth) -> Self {
259259
value.0
260260
}
261261
}

crates/ruff_linter/src/message/text.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ruff_source_file::{OneIndexed, SourceLocation};
1212
use ruff_text_size::{Ranged, TextRange, TextSize};
1313

1414
use crate::fs::relativize_path;
15-
use crate::line_width::{LineWidthBuilder, TabSize};
15+
use crate::line_width::{IndentWidth, LineWidthBuilder};
1616
use crate::message::diff::Diff;
1717
use crate::message::{Emitter, EmitterContext, Message};
1818
use crate::registry::AsRule;
@@ -300,7 +300,7 @@ fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode {
300300
let mut result = String::new();
301301
let mut last_end = 0;
302302
let mut range = annotation_range;
303-
let mut line_width = LineWidthBuilder::new(TabSize::default());
303+
let mut line_width = LineWidthBuilder::new(IndentWidth::default());
304304

305305
for (index, c) in source.char_indices() {
306306
let old_width = line_width.get();

crates/ruff_linter/src/rules/pycodestyle/overlong.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ruff_python_trivia::is_pragma_comment;
77
use ruff_source_file::Line;
88
use ruff_text_size::{TextLen, TextRange};
99

10-
use crate::line_width::{LineLength, LineWidthBuilder, TabSize};
10+
use crate::line_width::{IndentWidth, LineLength, LineWidthBuilder};
1111

1212
#[derive(Debug)]
1313
pub(super) struct Overlong {
@@ -23,7 +23,7 @@ impl Overlong {
2323
indexer: &Indexer,
2424
limit: LineLength,
2525
task_tags: &[String],
26-
tab_size: TabSize,
26+
tab_size: IndentWidth,
2727
) -> Option<Self> {
2828
// The maximum width of the line is the number of bytes multiplied by the tab size (the
2929
// worst-case scenario is that the line is all tabs). If the maximum width is less than the
@@ -158,7 +158,7 @@ impl<'a> Deref for StrippedLine<'a> {
158158
}
159159

160160
/// Returns the width of a given string, accounting for the tab size.
161-
fn measure(s: &str, tab_size: TabSize) -> LineWidthBuilder {
161+
fn measure(s: &str, tab_size: IndentWidth) -> LineWidthBuilder {
162162
let mut width = LineWidthBuilder::new(tab_size);
163163
width = width.add_str(s);
164164
width

crates/ruff_linter/src/settings/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::rules::{
2626
use crate::settings::types::{FilePatternSet, PerFileIgnore, PythonVersion};
2727
use crate::{codes, RuleSelector};
2828

29-
use super::line_width::TabSize;
29+
use super::line_width::IndentWidth;
3030

3131
use self::rule_table::RuleTable;
3232
use self::types::PreviewMode;
@@ -59,7 +59,7 @@ pub struct LinterSettings {
5959
pub logger_objects: Vec<String>,
6060
pub namespace_packages: Vec<PathBuf>,
6161
pub src: Vec<PathBuf>,
62-
pub tab_size: TabSize,
62+
pub tab_size: IndentWidth,
6363
pub task_tags: Vec<String>,
6464
pub typing_modules: Vec<String>,
6565

@@ -155,7 +155,7 @@ impl LinterSettings {
155155

156156
src: vec![path_dedot::CWD.clone()],
157157
// Needs duplicating
158-
tab_size: TabSize::default(),
158+
tab_size: IndentWidth::default(),
159159

160160
task_tags: TASK_TAGS.iter().map(ToString::to_string).collect(),
161161
typing_modules: vec![],

crates/ruff_wasm/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use wasm_bindgen::prelude::*;
66

77
use ruff_formatter::{FormatResult, Formatted, IndentStyle};
88
use ruff_linter::directives;
9-
use ruff_linter::line_width::{LineLength, TabSize};
9+
use ruff_linter::line_width::{IndentWidth, LineLength};
1010
use ruff_linter::linter::{check_path, LinterResult};
1111
use ruff_linter::registry::AsRule;
1212
use ruff_linter::settings::types::PythonVersion;
@@ -126,7 +126,7 @@ impl Workspace {
126126

127127
line_length: Some(LineLength::default()),
128128

129-
tab_size: Some(TabSize::default()),
129+
indent_width: Some(IndentWidth::default()),
130130
target_version: Some(PythonVersion::default()),
131131

132132
lint: Some(LintOptions {

crates/ruff_workspace/src/configuration.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use shellexpand::LookupError;
1616
use strum::IntoEnumIterator;
1717

1818
use ruff_cache::cache_dir;
19-
use ruff_formatter::{IndentStyle, IndentWidth, LineWidth};
20-
use ruff_linter::line_width::{LineLength, TabSize};
19+
use ruff_formatter::IndentStyle;
20+
use ruff_linter::line_width::{IndentWidth, LineLength};
2121
use ruff_linter::registry::RuleNamespace;
2222
use ruff_linter::registry::{Rule, RuleSet, INCOMPATIBLE_CODES};
2323
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
@@ -133,7 +133,7 @@ pub struct Configuration {
133133

134134
// Global formatting options
135135
pub line_length: Option<LineLength>,
136-
pub tab_size: Option<TabSize>,
136+
pub indent_width: Option<IndentWidth>,
137137

138138
pub lint: LintConfiguration,
139139
pub format: FormatConfiguration,
@@ -166,14 +166,14 @@ impl Configuration {
166166
line_width: self
167167
.line_length
168168
.map_or(format_defaults.line_width, |length| {
169-
LineWidth::from(NonZeroU16::from(length))
169+
ruff_formatter::LineWidth::from(NonZeroU16::from(length))
170170
}),
171171
line_ending: format.line_ending.unwrap_or(format_defaults.line_ending),
172172
indent_style: format.indent_style.unwrap_or(format_defaults.indent_style),
173173
indent_width: self
174-
.tab_size
174+
.indent_width
175175
.map_or(format_defaults.indent_width, |tab_size| {
176-
IndentWidth::from(NonZeroU8::from(tab_size))
176+
ruff_formatter::IndentWidth::from(NonZeroU8::from(tab_size))
177177
}),
178178
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
179179
magic_trailing_comma: format
@@ -228,7 +228,7 @@ impl Configuration {
228228
.unwrap_or_else(|| DUMMY_VARIABLE_RGX.clone()),
229229
external: FxHashSet::from_iter(lint.external.unwrap_or_default()),
230230
ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(),
231-
tab_size: self.tab_size.unwrap_or_default(),
231+
tab_size: self.indent_width.unwrap_or_default(),
232232
namespace_packages: self.namespace_packages.unwrap_or_default(),
233233
per_file_ignores: resolve_per_file_ignores(
234234
lint.per_file_ignores
@@ -389,6 +389,15 @@ impl Configuration {
389389
}
390390
};
391391

392+
#[allow(deprecated)]
393+
let indent_width = {
394+
if options.tab_size.is_some() {
395+
warn_user_once!("The `tab-size` option has been renamed to `indent-width` to emphasize that it configures the indentation used by the formatter as well as the tab width. Please update your configuration to use `indent-width = <value>` instead.");
396+
}
397+
398+
options.indent_width.or(options.tab_size)
399+
};
400+
392401
Ok(Self {
393402
builtins: options.builtins,
394403
cache_dir: options
@@ -456,7 +465,7 @@ impl Configuration {
456465
output_format: options.output_format,
457466
force_exclude: options.force_exclude,
458467
line_length: options.line_length,
459-
tab_size: options.tab_size,
468+
indent_width,
460469
namespace_packages: options
461470
.namespace_packages
462471
.map(|namespace_package| resolve_src(&namespace_package, project_root))
@@ -504,7 +513,7 @@ impl Configuration {
504513
output_format: self.output_format.or(config.output_format),
505514
force_exclude: self.force_exclude.or(config.force_exclude),
506515
line_length: self.line_length.or(config.line_length),
507-
tab_size: self.tab_size.or(config.tab_size),
516+
indent_width: self.indent_width.or(config.indent_width),
508517
namespace_packages: self.namespace_packages.or(config.namespace_packages),
509518
required_version: self.required_version.or(config.required_version),
510519
respect_gitignore: self.respect_gitignore.or(config.respect_gitignore),

0 commit comments

Comments
 (0)