mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:47 +00:00
Add tab width option (#6848)
This commit is contained in:
parent
f91bacbb94
commit
9d77552e18
20 changed files with 345 additions and 44 deletions
|
@ -51,7 +51,7 @@ pub use crate::diagnostics::{ActualStart, FormatError, InvalidDocumentError, Pri
|
|||
pub use format_element::{normalize_newlines, FormatElement, LINE_TERMINATORS};
|
||||
pub use group_id::GroupId;
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
use std::num::ParseIntError;
|
||||
use std::num::{NonZeroU8, ParseIntError, TryFromIntError};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
|
||||
|
@ -108,6 +108,33 @@ impl std::fmt::Display for IndentStyle {
|
|||
}
|
||||
}
|
||||
|
||||
/// The visual width of a `\t` character.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub struct TabWidth(NonZeroU8);
|
||||
|
||||
impl TabWidth {
|
||||
/// Return the numeric value for this [`LineWidth`]
|
||||
pub const fn value(&self) -> u32 {
|
||||
self.0.get() as u32
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TabWidth {
|
||||
fn default() -> Self {
|
||||
Self(NonZeroU8::new(2).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for TabWidth {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
NonZeroU8::try_from(value).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Validated value for the `line_width` formatter options
|
||||
///
|
||||
/// The allowed range of values is 1..=320
|
||||
|
@ -213,6 +240,17 @@ pub trait FormatOptions {
|
|||
/// The indent style.
|
||||
fn indent_style(&self) -> IndentStyle;
|
||||
|
||||
/// The visual width of a tab character.
|
||||
fn tab_width(&self) -> TabWidth;
|
||||
|
||||
/// The visual width of an indent
|
||||
fn indent_width(&self) -> u32 {
|
||||
match self.indent_style() {
|
||||
IndentStyle::Tab => self.tab_width().value(),
|
||||
IndentStyle::Space(spaces) => u32::from(spaces),
|
||||
}
|
||||
}
|
||||
|
||||
/// What's the max width of a line. Defaults to 80.
|
||||
fn line_width(&self) -> LineWidth;
|
||||
|
||||
|
@ -264,6 +302,10 @@ impl FormatOptions for SimpleFormatOptions {
|
|||
self.indent_style
|
||||
}
|
||||
|
||||
fn tab_width(&self) -> TabWidth {
|
||||
TabWidth::default()
|
||||
}
|
||||
|
||||
fn line_width(&self) -> LineWidth {
|
||||
self.line_width
|
||||
}
|
||||
|
@ -271,6 +313,7 @@ impl FormatOptions for SimpleFormatOptions {
|
|||
fn as_print_options(&self) -> PrinterOptions {
|
||||
PrinterOptions::default()
|
||||
.with_indent(self.indent_style)
|
||||
.with_tab_width(self.tab_width())
|
||||
.with_print_width(self.line_width.into())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue