Skip to content

Commit 213ea75

Browse files
committed
Rename tab-size to tab-width
1 parent bcfb73b commit 213ea75

File tree

10 files changed

+142
-48
lines changed

10 files changed

+142
-48
lines changed

crates/ruff_cli/tests/format.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn format_options() -> Result<()> {
5151
fs::write(
5252
&ruff_toml,
5353
r#"
54-
tab-size = 8
54+
indent-width = 8
5555
line-width = 84
5656
5757
[format]
@@ -281,6 +281,42 @@ if condition:
281281
Ok(())
282282
}
283283

284+
#[test]
285+
fn deprecated_options() -> Result<()> {
286+
let tempdir = TempDir::new()?;
287+
let ruff_toml = tempdir.path().join("ruff.toml");
288+
fs::write(
289+
&ruff_toml,
290+
r#"
291+
tab-size = 2
292+
"#,
293+
)?;
294+
295+
insta::with_settings!({filters => vec![
296+
(&*regex::escape(ruff_toml.to_str().unwrap()), "[RUFF-TOML-PATH]"),
297+
]}, {
298+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
299+
.args(["format", "--config"])
300+
.arg(&ruff_toml)
301+
.arg("-")
302+
.pass_stdin(r#"
303+
if True:
304+
pass
305+
"#), @r###"
306+
success: true
307+
exit_code: 0
308+
----- stdout -----
309+
if True:
310+
pass
311+
312+
----- stderr -----
313+
warning: `ruff format` is not yet stable, and subject to change in future versions.
314+
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.
315+
"###);
316+
});
317+
Ok(())
318+
}
319+
284320
/// Since 0.1.0 the legacy format option is no longer supported
285321
#[test]
286322
fn legacy_format_option() -> Result<()> {

crates/ruff_linter/src/fix/edits.rs

Lines changed: 4 additions & 4 deletions
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::{LineWidth, LineWidthBuilder, TabSize};
17+
use crate::line_width::{IndentWidth, LineWidth, 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_width: LineWidth,
296-
tab_size: TabSize,
296+
tab_size: IndentWidth,
297297
) -> bool {
298298
all_lines_fit(fix, node, locator, line_width.value() as usize, tab_size)
299299
}
@@ -305,7 +305,7 @@ pub(crate) fn fits_or_shrinks(
305305
node: AnyNodeRef,
306306
locator: &Locator,
307307
line_width: LineWidth,
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_width.value() as usize)
@@ -327,7 +327,7 @@ fn all_lines_fit(
327327
node: AnyNodeRef,
328328
locator: &Locator,
329329
line_width: 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

Lines changed: 9 additions & 9 deletions
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<LineWidth> 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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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::{LineWidth, LineWidthBuilder, TabSize};
10+
use crate::line_width::{IndentWidth, LineWidth, LineWidthBuilder};
1111

1212
#[derive(Debug)]
1313
pub(super) struct Overlong {
@@ -23,7 +23,7 @@ impl Overlong {
2323
indexer: &Indexer,
2424
limit: LineWidth,
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

Lines changed: 3 additions & 3 deletions
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::{LineWidth, TabSize};
29+
use super::line_width::{IndentWidth, LineWidth};
3030

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

@@ -157,7 +157,7 @@ impl LinterSettings {
157157

158158
src: vec![path_dedot::CWD.clone()],
159159
// Needs duplicating
160-
tab_size: TabSize::default(),
160+
tab_size: IndentWidth::default(),
161161

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

crates/ruff_wasm/src/lib.rs

Lines changed: 2 additions & 2 deletions
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::{LineWidth, TabSize};
9+
use ruff_linter::line_width::{IndentWidth, LineWidth};
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_width: Some(LineWidth::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

Lines changed: 16 additions & 8 deletions
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};
20-
use ruff_linter::line_width::{LineWidth, TabSize};
19+
use ruff_formatter::IndentStyle;
20+
use ruff_linter::line_width::{IndentWidth, LineWidth};
2121
use ruff_linter::registry::RuleNamespace;
2222
use ruff_linter::registry::{Rule, RuleSet, INCOMPATIBLE_CODES};
2323
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
@@ -132,7 +132,7 @@ pub struct Configuration {
132132

133133
// Global formatting options
134134
pub line_width: Option<LineWidth>,
135-
pub tab_size: Option<TabSize>,
135+
pub indent_width: Option<IndentWidth>,
136136

137137
pub lint: LintConfiguration,
138138
pub format: FormatConfiguration,
@@ -168,9 +168,9 @@ impl Configuration {
168168
line_ending: format.line_ending.unwrap_or(format_defaults.line_ending),
169169
indent_style: format.indent_style.unwrap_or(format_defaults.indent_style),
170170
indent_width: self
171-
.tab_size
171+
.indent_width
172172
.map_or(format_defaults.indent_width, |tab_size| {
173-
IndentWidth::from(NonZeroU8::from(tab_size))
173+
ruff_formatter::IndentWidth::from(NonZeroU8::from(tab_size))
174174
}),
175175
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
176176
magic_trailing_comma: format
@@ -224,7 +224,7 @@ impl Configuration {
224224
external: FxHashSet::from_iter(lint.external.unwrap_or_default()),
225225
ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(),
226226
line_width: self.line_width.unwrap_or_default(),
227-
tab_size: self.tab_size.unwrap_or_default(),
227+
tab_size: self.indent_width.unwrap_or_default(),
228228
namespace_packages: self.namespace_packages.unwrap_or_default(),
229229
per_file_ignores: resolve_per_file_ignores(
230230
lint.per_file_ignores
@@ -389,6 +389,14 @@ impl Configuration {
389389

390390
options.line_width.or(options.line_length)
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+
};
392400

393401
Ok(Self {
394402
builtins: options.builtins,
@@ -457,7 +465,7 @@ impl Configuration {
457465
output_format: options.output_format,
458466
force_exclude: options.force_exclude,
459467
line_width,
460-
tab_size: options.tab_size,
468+
indent_width,
461469
namespace_packages: options
462470
.namespace_packages
463471
.map(|namespace_package| resolve_src(&namespace_package, project_root))
@@ -505,7 +513,7 @@ impl Configuration {
505513
output_format: self.output_format.or(config.output_format),
506514
force_exclude: self.force_exclude.or(config.force_exclude),
507515
line_width: self.line_width.or(config.line_width),
508-
tab_size: self.tab_size.or(config.tab_size),
516+
indent_width: self.indent_width.or(config.indent_width),
509517
namespace_packages: self.namespace_packages.or(config.namespace_packages),
510518
required_version: self.required_version.or(config.required_version),
511519
respect_gitignore: self.respect_gitignore.or(config.respect_gitignore),

0 commit comments

Comments
 (0)