mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-17 11:03:00 +00:00
Improve Ruff Formatter Interoperability (#6472)
This commit is contained in:
parent
50dab9cea6
commit
dc3275fe7f
2 changed files with 43 additions and 4 deletions
|
|
@ -92,7 +92,13 @@ impl FromStr for IndentStyle {
|
||||||
"tab" | "Tabs" => Ok(Self::Tab),
|
"tab" | "Tabs" => Ok(Self::Tab),
|
||||||
"space" | "Spaces" => Ok(Self::Space(IndentStyle::DEFAULT_SPACES)),
|
"space" | "Spaces" => Ok(Self::Space(IndentStyle::DEFAULT_SPACES)),
|
||||||
// TODO: replace this error with a diagnostic
|
// TODO: replace this error with a diagnostic
|
||||||
_ => Err("Value not supported for IndentStyle"),
|
v => {
|
||||||
|
let v = v.strip_prefix("Spaces, size: ").unwrap_or(v);
|
||||||
|
|
||||||
|
u8::from_str(v)
|
||||||
|
.map(Self::Space)
|
||||||
|
.map_err(|_| "Value not supported for IndentStyle")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use ruff_formatter::printer::{LineEnding, PrinterOptions};
|
||||||
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
|
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
|
||||||
use ruff_python_ast::PySourceType;
|
use ruff_python_ast::PySourceType;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
|
|
@ -16,6 +17,7 @@ pub struct PyFormatOptions {
|
||||||
/// Specifies the indent style:
|
/// Specifies the indent style:
|
||||||
/// * Either a tab
|
/// * Either a tab
|
||||||
/// * or a specific amount of spaces
|
/// * or a specific amount of spaces
|
||||||
|
#[cfg_attr(feature = "serde", serde(default = "default_indent_style"))]
|
||||||
indent_style: IndentStyle,
|
indent_style: IndentStyle,
|
||||||
|
|
||||||
/// The preferred line width at which the formatter should wrap lines.
|
/// The preferred line width at which the formatter should wrap lines.
|
||||||
|
|
@ -33,12 +35,16 @@ fn default_line_width() -> LineWidth {
|
||||||
LineWidth::try_from(88).unwrap()
|
LineWidth::try_from(88).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_indent_style() -> IndentStyle {
|
||||||
|
IndentStyle::Space(4)
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for PyFormatOptions {
|
impl Default for PyFormatOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
source_type: PySourceType::default(),
|
source_type: PySourceType::default(),
|
||||||
indent_style: IndentStyle::Space(4),
|
indent_style: default_indent_style(),
|
||||||
line_width: LineWidth::try_from(88).unwrap(),
|
line_width: default_line_width(),
|
||||||
quote_style: QuoteStyle::default(),
|
quote_style: QuoteStyle::default(),
|
||||||
magic_trailing_comma: MagicTrailingComma::default(),
|
magic_trailing_comma: MagicTrailingComma::default(),
|
||||||
}
|
}
|
||||||
|
|
@ -66,7 +72,8 @@ impl PyFormatOptions {
|
||||||
self.quote_style
|
self.quote_style
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_quote_style(&mut self, style: QuoteStyle) -> &mut Self {
|
#[must_use]
|
||||||
|
pub fn with_quote_style(mut self, style: QuoteStyle) -> Self {
|
||||||
self.quote_style = style;
|
self.quote_style = style;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -150,6 +157,19 @@ impl TryFrom<char> for QuoteStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for QuoteStyle {
|
||||||
|
type Err = &'static str;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
"\"" | "double" | "Double" => Ok(Self::Double),
|
||||||
|
"'" | "single" | "Single" => Ok(Self::Single),
|
||||||
|
// TODO: replace this error with a diagnostic
|
||||||
|
_ => Err("Value not supported for QuoteStyle"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Default)]
|
#[derive(Copy, Clone, Debug, Default)]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
feature = "serde",
|
feature = "serde",
|
||||||
|
|
@ -167,3 +187,16 @@ impl MagicTrailingComma {
|
||||||
matches!(self, Self::Respect)
|
matches!(self, Self::Respect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for MagicTrailingComma {
|
||||||
|
type Err = &'static str;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
"respect" | "Respect" => Ok(Self::Respect),
|
||||||
|
"ignore" | "Ignore" => Ok(Self::Ignore),
|
||||||
|
// TODO: replace this error with a diagnostic
|
||||||
|
_ => Err("Value not supported for MagicTrailingComma"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue