Skip to content

Commit a205ebf

Browse files
committed
Rename tab-size to tab-width
1 parent 2587aef commit a205ebf

File tree

10 files changed

+143
-49
lines changed

10 files changed

+143
-49
lines changed

crates/ruff_cli/tests/format.rs

Lines changed: 36 additions & 1 deletion
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

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::{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

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<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

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::{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

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::{LineLength, TabSize};
29+
use super::line_width::{IndentWidth, LineLength};
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::{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

Lines changed: 18 additions & 9 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, 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};
@@ -132,7 +132,7 @@ pub struct Configuration {
132132

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

137137
pub lint: LintConfiguration,
138138
pub format: FormatConfiguration,
@@ -165,14 +165,14 @@ impl Configuration {
165165
line_width: self
166166
.line_length
167167
.map_or(format_defaults.line_width, |length| {
168-
LineWidth::from(NonZeroU16::from(length))
168+
ruff_formatter::LineWidth::from(NonZeroU16::from(length))
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),
172172
indent_width: self
173-
.tab_size
173+
.indent_width
174174
.map_or(format_defaults.indent_width, |tab_size| {
175-
IndentWidth::from(NonZeroU8::from(tab_size))
175+
ruff_formatter::IndentWidth::from(NonZeroU8::from(tab_size))
176176
}),
177177
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
178178
magic_trailing_comma: format
@@ -226,7 +226,7 @@ impl Configuration {
226226
external: FxHashSet::from_iter(lint.external.unwrap_or_default()),
227227
ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(),
228228
line_length: self.line_length.unwrap_or_default(),
229-
tab_size: self.tab_size.unwrap_or_default(),
229+
tab_size: self.indent_width.unwrap_or_default(),
230230
namespace_packages: self.namespace_packages.unwrap_or_default(),
231231
per_file_ignores: resolve_per_file_ignores(
232232
lint.per_file_ignores
@@ -383,6 +383,15 @@ impl Configuration {
383383
}
384384
};
385385

386+
#[allow(deprecated)]
387+
let indent_width = {
388+
if options.tab_size.is_some() {
389+
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.");
390+
}
391+
392+
options.indent_width.or(options.tab_size)
393+
};
394+
386395
Ok(Self {
387396
builtins: options.builtins,
388397
cache_dir: options
@@ -450,7 +459,7 @@ impl Configuration {
450459
output_format: options.output_format,
451460
force_exclude: options.force_exclude,
452461
line_length: options.line_length,
453-
tab_size: options.tab_size,
462+
indent_width,
454463
namespace_packages: options
455464
.namespace_packages
456465
.map(|namespace_package| resolve_src(&namespace_package, project_root))
@@ -498,7 +507,7 @@ impl Configuration {
498507
output_format: self.output_format.or(config.output_format),
499508
force_exclude: self.force_exclude.or(config.force_exclude),
500509
line_length: self.line_length.or(config.line_length),
501-
tab_size: self.tab_size.or(config.tab_size),
510+
indent_width: self.indent_width.or(config.indent_width),
502511
namespace_packages: self.namespace_packages.or(config.namespace_packages),
503512
required_version: self.required_version.or(config.required_version),
504513
respect_gitignore: self.respect_gitignore.or(config.respect_gitignore),

0 commit comments

Comments
 (0)