Add most formatter options to ruff.toml / pyproject.toml (#7566)

This commit is contained in:
Micha Reiser 2023-09-22 17:47:57 +02:00 committed by GitHub
parent 82978ac9b5
commit 9d16e46129
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 734 additions and 145 deletions

View file

@ -30,6 +30,7 @@ memchr = { workspace = true }
once_cell = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, optional = true }
schemars = { workspace = true, optional = true }
smallvec = { workspace = true }
static_assertions = { workspace = true }
thiserror = { workspace = true }
@ -52,4 +53,5 @@ required-features = ["serde"]
[features]
serde = ["dep:serde", "ruff_formatter/serde", "ruff_source_file/serde", "ruff_python_ast/serde"]
default = ["serde"]
schemars = ["dep:schemars", "ruff_formatter/schemars"]
default = []

View file

@ -1,18 +1,18 @@
[
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 4
},
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 2
},
{
"indent_style": "Tab",
"indent_style": "tab",
"indent_width": 8
},
{
"indent_style": "Tab",
"indent_style": "tab",
"indent_width": 4
}
]

View file

@ -1,10 +1,10 @@
[
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 4
},
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 2
}
]

View file

@ -1,13 +1,13 @@
[
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 4
},
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 1
},
{
"indent_style": "Tab"
"indent_style": "tab"
}
]

View file

@ -1,13 +1,13 @@
[
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 4
},
{
"indent_style": "Space",
"indent_style": "space",
"indent_width": 2
},
{
"indent_style": "Tab"
"indent_style": "tab"
}
]

View file

@ -17,7 +17,6 @@ use crate::comments::{
pub use crate::context::PyFormatContext;
pub use crate::options::{MagicTrailingComma, PreviewMode, PyFormatOptions, QuoteStyle};
use crate::verbatim::suppressed_node;
pub use settings::FormatterSettings;
pub(crate) mod builders;
pub mod cli;
@ -30,7 +29,6 @@ mod options;
pub(crate) mod other;
pub(crate) mod pattern;
mod prelude;
mod settings;
pub(crate) mod statement;
pub(crate) mod type_param;
mod verbatim;

View file

@ -5,8 +5,8 @@ use ruff_python_ast::PySourceType;
use std::path::Path;
use std::str::FromStr;
/// Resolved options for formatting one individual file. This is different from [`crate::FormatterSettings`] which
/// represents the formatting settings for multiple files (the whole project, a subdirectory, ...)
/// Resolved options for formatting one individual file. The difference to `FormatterSettings`
/// is that `FormatterSettings` stores the settings for multiple files (the entire project, a subdirectory, ..)
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "serde",
@ -185,6 +185,7 @@ impl FormatOptions for PyFormatOptions {
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum QuoteStyle {
Single,
#[default]

View file

@ -1,49 +0,0 @@
use std::path::PathBuf;
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
use ruff_macros::CacheKey;
use ruff_python_ast::PySourceType;
use crate::{MagicTrailingComma, PreviewMode, PyFormatOptions, QuoteStyle};
#[derive(CacheKey, Clone, Debug)]
pub struct FormatterSettings {
/// The files that are excluded from formatting (but may be linted).
pub exclude: Vec<PathBuf>,
pub preview: PreviewMode,
pub line_width: LineWidth,
pub indent_style: IndentStyle,
pub quote_style: QuoteStyle,
pub magic_trailing_comma: MagicTrailingComma,
}
impl FormatterSettings {
pub fn to_format_options(&self, source_type: PySourceType) -> PyFormatOptions {
PyFormatOptions::from_source_type(source_type)
.with_indent_style(self.indent_style)
.with_quote_style(self.quote_style)
.with_magic_trailing_comma(self.magic_trailing_comma)
.with_preview(self.preview)
.with_line_width(self.line_width)
}
}
impl Default for FormatterSettings {
fn default() -> Self {
let default_options = PyFormatOptions::default();
Self {
exclude: Vec::default(),
preview: PreviewMode::Disabled,
line_width: default_options.line_width(),
indent_style: default_options.indent_style(),
quote_style: default_options.quote_style(),
magic_trailing_comma: default_options.magic_trailing_comma(),
}
}
}