Add tab width option (#6848)

This commit is contained in:
Micha Reiser 2023-08-26 12:29:58 +02:00 committed by GitHub
parent f91bacbb94
commit 9d77552e18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 345 additions and 44 deletions

View file

@ -1,5 +1,5 @@
use ruff_formatter::printer::{LineEnding, PrinterOptions};
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth, TabWidth};
use ruff_python_ast::PySourceType;
use std::path::Path;
use std::str::FromStr;
@ -24,6 +24,10 @@ pub struct PyFormatOptions {
#[cfg_attr(feature = "serde", serde(default = "default_line_width"))]
line_width: LineWidth,
/// The visual width of a tab character.
#[cfg_attr(feature = "serde", serde(default = "default_tab_width"))]
tab_width: TabWidth,
/// The preferred quote style to use (single vs double quotes).
quote_style: QuoteStyle,
@ -39,12 +43,17 @@ fn default_indent_style() -> IndentStyle {
IndentStyle::Space(4)
}
fn default_tab_width() -> TabWidth {
TabWidth::try_from(4).unwrap()
}
impl Default for PyFormatOptions {
fn default() -> Self {
Self {
source_type: PySourceType::default(),
indent_style: default_indent_style(),
line_width: default_line_width(),
tab_width: default_tab_width(),
quote_style: QuoteStyle::default(),
magic_trailing_comma: MagicTrailingComma::default(),
}
@ -106,13 +115,17 @@ impl FormatOptions for PyFormatOptions {
self.indent_style
}
fn tab_width(&self) -> TabWidth {
self.tab_width
}
fn line_width(&self) -> LineWidth {
self.line_width
}
fn as_print_options(&self) -> PrinterOptions {
PrinterOptions {
tab_width: 4,
tab_width: self.tab_width,
print_width: self.line_width.into(),
line_ending: LineEnding::LineFeed,
indent_style: self.indent_style,