Skip to content

Commit

Permalink
Add target_version to formatter options
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 21, 2023
1 parent ef4bd8d commit 37ddbea
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::comments::{
pub use crate::context::PyFormatContext;
pub use crate::options::{
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
QuoteStyle,
PythonVersion, QuoteStyle,
};
pub use crate::shared_traits::{AsFormat, FormattedIter, FormattedIterExt, IntoFormat};
use crate::verbatim::suppressed_node;
Expand Down
46 changes: 39 additions & 7 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub struct PyFormatOptions {
/// Whether we're in a `.py` file or `.pyi` file, which have different rules.
source_type: PySourceType,

/// The (minimum) Python version used to run the formatted code. This is used
/// to determine the supported Python syntax.
target_version: PythonVersion,

/// Specifies the indent style:
/// * Either a tab
/// * or a specific amount of spaces
Expand Down Expand Up @@ -74,6 +78,7 @@ impl Default for PyFormatOptions {
fn default() -> Self {
Self {
source_type: PySourceType::default(),
target_version: PythonVersion::default(),
indent_style: default_indent_style(),
line_width: default_line_width(),
indent_width: default_indent_width(),
Expand Down Expand Up @@ -101,38 +106,48 @@ impl PyFormatOptions {
}
}

pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
pub const fn target_version(&self) -> PythonVersion {
self.target_version
}

pub const fn magic_trailing_comma(&self) -> MagicTrailingComma {
self.magic_trailing_comma
}

pub fn quote_style(&self) -> QuoteStyle {
pub const fn quote_style(&self) -> QuoteStyle {
self.quote_style
}

pub fn source_type(&self) -> PySourceType {
pub const fn source_type(&self) -> PySourceType {
self.source_type
}

pub fn source_map_generation(&self) -> SourceMapGeneration {
pub const fn source_map_generation(&self) -> SourceMapGeneration {
self.source_map_generation
}

pub fn line_ending(&self) -> LineEnding {
pub const fn line_ending(&self) -> LineEnding {
self.line_ending
}

pub fn docstring_code(&self) -> DocstringCode {
pub const fn docstring_code(&self) -> DocstringCode {
self.docstring_code
}

pub fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
pub const fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
self.docstring_code_line_width
}

pub const fn preview(&self) -> PreviewMode {
self.preview
}

#[must_use]
pub fn with_target_version(mut self, target_version: PythonVersion) -> Self {
self.target_version = target_version;
self
}

#[must_use]
pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self {
self.indent_width = indent_width;
Expand Down Expand Up @@ -349,3 +364,20 @@ where
)),
}
}

#[derive(CacheKey, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum PythonVersion {
Py37,
#[default]
Py38,
Py39,
Py310,
Py311,
Py312,
}
6 changes: 4 additions & 2 deletions crates/ruff_python_formatter/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ line-ending = {line_ending:?}
magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
docstring-code-line-width = {docstring_code_line_width:?}
preview = {preview:?}"#,
preview = {preview:?}
target_version = {target_version:?}"#,
indent_style = self.0.indent_style(),
indent_width = self.0.indent_width().value(),
line_width = self.0.line_width().value(),
Expand All @@ -364,7 +365,8 @@ preview = {preview:?}"#,
magic_trailing_comma = self.0.magic_trailing_comma(),
docstring_code = self.0.docstring_code(),
docstring_code_line_width = self.0.docstring_code_line_width(),
preview = self.0.preview()
preview = self.0.preview(),
target_version = self.0.target_version()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -349,6 +350,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -523,6 +525,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -697,6 +700,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -871,6 +875,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -2738,6 +2739,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -4108,6 +4110,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -5478,6 +5481,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -6848,6 +6852,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -8215,6 +8220,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -9582,6 +9588,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -10958,6 +10965,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -12325,6 +12333,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 60
preview = Disabled
target_version = Py38
```
```python
Expand Down Expand Up @@ -13701,6 +13710,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -545,6 +546,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -841,6 +843,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -1147,6 +1150,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -290,6 +291,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -329,6 +330,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -73,6 +74,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand All @@ -35,6 +36,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand All @@ -52,6 +54,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -66,6 +67,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down Expand Up @@ -99,6 +101,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```

```python
Expand Down
Loading

0 comments on commit 37ddbea

Please sign in to comment.