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
This commit is contained in:
Micha Reiser 2023-10-24 23:01:24 +09:00 committed by GitHub
parent c3dabc1933
commit 84979f9673
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 143 additions and 49 deletions

View file

@ -16,8 +16,8 @@ use shellexpand::LookupError;
use strum::IntoEnumIterator;
use ruff_cache::cache_dir;
use ruff_formatter::{IndentStyle, IndentWidth, LineWidth};
use ruff_linter::line_width::{LineLength, TabSize};
use ruff_formatter::IndentStyle;
use ruff_linter::line_width::{IndentWidth, LineLength};
use ruff_linter::registry::RuleNamespace;
use ruff_linter::registry::{Rule, RuleSet, INCOMPATIBLE_CODES};
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
@ -133,7 +133,7 @@ pub struct Configuration {
// Global formatting options
pub line_length: Option<LineLength>,
pub tab_size: Option<TabSize>,
pub indent_width: Option<IndentWidth>,
pub lint: LintConfiguration,
pub format: FormatConfiguration,
@ -166,14 +166,14 @@ impl Configuration {
line_width: self
.line_length
.map_or(format_defaults.line_width, |length| {
LineWidth::from(NonZeroU16::from(length))
ruff_formatter::LineWidth::from(NonZeroU16::from(length))
}),
line_ending: format.line_ending.unwrap_or(format_defaults.line_ending),
indent_style: format.indent_style.unwrap_or(format_defaults.indent_style),
indent_width: self
.tab_size
.indent_width
.map_or(format_defaults.indent_width, |tab_size| {
IndentWidth::from(NonZeroU8::from(tab_size))
ruff_formatter::IndentWidth::from(NonZeroU8::from(tab_size))
}),
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
magic_trailing_comma: format
@ -228,7 +228,7 @@ impl Configuration {
.unwrap_or_else(|| DUMMY_VARIABLE_RGX.clone()),
external: FxHashSet::from_iter(lint.external.unwrap_or_default()),
ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(),
tab_size: self.tab_size.unwrap_or_default(),
tab_size: self.indent_width.unwrap_or_default(),
namespace_packages: self.namespace_packages.unwrap_or_default(),
per_file_ignores: resolve_per_file_ignores(
lint.per_file_ignores
@ -389,6 +389,15 @@ impl Configuration {
}
};
#[allow(deprecated)]
let indent_width = {
if options.tab_size.is_some() {
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.");
}
options.indent_width.or(options.tab_size)
};
Ok(Self {
builtins: options.builtins,
cache_dir: options
@ -456,7 +465,7 @@ impl Configuration {
output_format: options.output_format,
force_exclude: options.force_exclude,
line_length: options.line_length,
tab_size: options.tab_size,
indent_width,
namespace_packages: options
.namespace_packages
.map(|namespace_package| resolve_src(&namespace_package, project_root))
@ -504,7 +513,7 @@ impl Configuration {
output_format: self.output_format.or(config.output_format),
force_exclude: self.force_exclude.or(config.force_exclude),
line_length: self.line_length.or(config.line_length),
tab_size: self.tab_size.or(config.tab_size),
indent_width: self.indent_width.or(config.indent_width),
namespace_packages: self.namespace_packages.or(config.namespace_packages),
required_version: self.required_version.or(config.required_version),
respect_gitignore: self.respect_gitignore.or(config.respect_gitignore),

View file

@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use ruff_formatter::IndentStyle;
use ruff_linter::line_width::{LineLength, TabSize};
use ruff_linter::line_width::{IndentWidth, LineLength};
use ruff_linter::rules::flake8_pytest_style::settings::SettingsError;
use ruff_linter::rules::flake8_pytest_style::types;
use ruff_linter::rules::flake8_quotes::settings::Quote;
@ -374,6 +374,24 @@ pub struct Options {
)]
pub line_length: Option<LineLength>,
/// The number of spaces per indentation level (tab).
///
/// Used by the formatter and when enforcing long-line violations (like `E501`) to determine the visual
/// width of a tab.
///
/// This option changes the number of spaces the formatter inserts when
/// using soft-tabs (`indent-style = space`).
///
/// PEP 8 recommends using 4 spaces per [indentation level](https://peps.python.org/pep-0008/#indentation).
#[option(
default = "4",
value_type = "int",
example = r#"
indent-width = 2
"#
)]
pub indent_width: Option<IndentWidth>,
/// The number of spaces a tab is equal to when enforcing long-line violations (like `E501`)
/// or formatting code with the formatter.
///
@ -383,10 +401,14 @@ pub struct Options {
default = "4",
value_type = "int",
example = r#"
tab-size = 8
tab-size = 2
"#
)]
pub tab_size: Option<TabSize>,
#[deprecated(
since = "0.1.2",
note = "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."
)]
pub tab_size: Option<IndentWidth>,
pub lint: Option<LintOptions>,
@ -2321,7 +2343,7 @@ impl PycodestyleOptions {
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct PydocstyleOptions {
/// Whether to use Google-style or NumPy-style conventions or the PEP257
/// Whether to use Google-style or NumPy-style conventions or the [PEP 257](https://peps.python.org/pep-0257/)
/// defaults when analyzing docstring sections.
///
/// Enabling a convention will force-disable any rules that are not
@ -2593,10 +2615,26 @@ pub struct FormatOptions {
)]
pub preview: Option<bool>,
/// Whether to use 4 spaces or hard tabs for indenting code.
/// Whether to use spaces or tabs for indentation.
///
/// Defaults to 4 spaces. We care about accessibility; if you do not need tabs for
/// accessibility, we do not recommend you use them.
/// `indent-style = "space"` (default):
///
/// ```python
/// def f():
/// print("Hello") # Spaces indent the `print` statement.
/// ```
///
/// `indent-style = "tab""`:
///
/// ```python
/// def f():
/// print("Hello") # A tab `\t` indents the `print` statement.
/// ```
///
/// PEP 8 recommends using spaces for [indentation](https://peps.python.org/pep-0008/#indentation).
/// We care about accessibility; if you do not need tabs for accessibility, we do not recommend you use them.
///
/// See [`indent-width`](#indent-width) to configure the number of spaces per indentation and the tab width.
#[option(
default = "space",
value_type = r#""space" | "tab""#,