mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 03:15:44 +00:00
Formatter: Add SourceType to context to enable special formatting for stub files (#6331)
**Summary** This adds the information whether we're in a .py python source file or in a .pyi stub file to enable people working on #5822 and related issues. I'm not completely happy with `Default` for something that depends on the input. **Test Plan** None, this is currently unused, i'm leaving this to first implementation of stub file specific formatting. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
fe97a2a302
commit
1031bb6550
12 changed files with 137 additions and 97 deletions
|
@ -1,5 +1,7 @@
|
|||
use ruff_formatter::printer::{LineEnding, PrinterOptions};
|
||||
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
|
||||
use ruff_python_ast::PySourceType;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(
|
||||
|
@ -8,6 +10,9 @@ use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
|
|||
serde(default)
|
||||
)]
|
||||
pub struct PyFormatOptions {
|
||||
/// Whether we're in a `.py` file or `.pyi` file, which have different rules
|
||||
source_type: PySourceType,
|
||||
|
||||
/// Specifies the indent style:
|
||||
/// * Either a tab
|
||||
/// * or a specific amount of spaces
|
||||
|
@ -28,7 +33,31 @@ fn default_line_width() -> LineWidth {
|
|||
LineWidth::try_from(88).unwrap()
|
||||
}
|
||||
|
||||
impl Default for PyFormatOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source_type: PySourceType::default(),
|
||||
indent_style: IndentStyle::Space(4),
|
||||
line_width: LineWidth::try_from(88).unwrap(),
|
||||
quote_style: QuoteStyle::default(),
|
||||
magic_trailing_comma: MagicTrailingComma::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PyFormatOptions {
|
||||
/// Otherwise sets the defaults. Returns none if the extension is unknown
|
||||
pub fn from_extension(path: &Path) -> Self {
|
||||
Self::from_source_type(PySourceType::from(path))
|
||||
}
|
||||
|
||||
pub fn from_source_type(source_type: PySourceType) -> Self {
|
||||
Self {
|
||||
source_type,
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
|
||||
self.magic_trailing_comma
|
||||
}
|
||||
|
@ -42,17 +71,20 @@ impl PyFormatOptions {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_magic_trailing_comma(&mut self, trailing_comma: MagicTrailingComma) -> &mut Self {
|
||||
#[must_use]
|
||||
pub fn with_magic_trailing_comma(mut self, trailing_comma: MagicTrailingComma) -> Self {
|
||||
self.magic_trailing_comma = trailing_comma;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_indent_style(&mut self, indent_style: IndentStyle) -> &mut Self {
|
||||
#[must_use]
|
||||
pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self {
|
||||
self.indent_style = indent_style;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_line_width(&mut self, line_width: LineWidth) -> &mut Self {
|
||||
#[must_use]
|
||||
pub fn with_line_width(mut self, line_width: LineWidth) -> Self {
|
||||
self.line_width = line_width;
|
||||
self
|
||||
}
|
||||
|
@ -77,17 +109,6 @@ impl FormatOptions for PyFormatOptions {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for PyFormatOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
indent_style: IndentStyle::Space(4),
|
||||
line_width: LineWidth::try_from(88).unwrap(),
|
||||
quote_style: QuoteStyle::default(),
|
||||
magic_trailing_comma: MagicTrailingComma::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue