Skip to content

Commit 0bda191

Browse files
authored
Create dedicated is_*_enabled functions for each preview style (#8988)
1 parent 7e390d3 commit 0bda191

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

crates/ruff_python_formatter/src/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ impl<'a> PyFormatContext<'a> {
7474
..self
7575
}
7676
}
77+
78+
/// Returns `true` if preview mode is enabled.
79+
pub(crate) const fn is_preview(&self) -> bool {
80+
self.options.preview().is_enabled()
81+
}
7782
}
7883

7984
impl FormatContext for PyFormatContext<'_> {

crates/ruff_python_formatter/src/expression/binary_like.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::{Deref, Index};
33

44
use smallvec::SmallVec;
55

6-
use ruff_formatter::{write, FormatContext};
6+
use ruff_formatter::write;
77
use ruff_python_ast::{
88
Expr, ExprAttribute, ExprBinOp, ExprBoolOp, ExprCompare, ExprUnaryOp, UnaryOp,
99
};
@@ -21,6 +21,7 @@ use crate::expression::parentheses::{
2121
use crate::expression::string::{AnyString, FormatString, StringLayout};
2222
use crate::expression::OperatorPrecedence;
2323
use crate::prelude::*;
24+
use crate::preview::is_fix_power_op_line_length_enabled;
2425

2526
#[derive(Copy, Clone, Debug)]
2627
pub(super) enum BinaryLike<'a> {
@@ -719,7 +720,7 @@ impl Format<PyFormatContext<'_>> for FlatBinaryExpressionSlice<'_> {
719720
{
720721
hard_line_break().fmt(f)?;
721722
} else if is_pow {
722-
if f.context().options().preview().is_enabled() {
723+
if is_fix_power_op_line_length_enabled(f.context()) {
723724
in_parentheses_only_if_group_breaks(&space()).fmt(f)?;
724725
}
725726
} else {

crates/ruff_python_formatter/src/expression/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::expression::parentheses::{
2121
OptionalParentheses, Parentheses, Parenthesize,
2222
};
2323
use crate::prelude::*;
24-
use crate::PyFormatOptions;
24+
use crate::preview::is_hug_parens_with_braces_and_square_brackets_enabled;
2525

2626
mod binary_like;
2727
pub(crate) mod expr_attribute;
@@ -129,7 +129,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
129129
let node_comments = comments.leading_dangling_trailing(expression);
130130
if !node_comments.has_leading() && !node_comments.has_trailing() {
131131
parenthesized("(", &format_expr, ")")
132-
.with_indent(!is_expression_huggable(expression, f.options()))
132+
.with_indent(!is_expression_huggable(expression, f.context()))
133133
.fmt(f)
134134
} else {
135135
format_with_parentheses_comments(expression, &node_comments, f)
@@ -448,7 +448,7 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
448448
OptionalParentheses::Never => match parenthesize {
449449
Parenthesize::IfBreaksOrIfRequired => {
450450
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
451-
.with_indent(!is_expression_huggable(expression, f.options()))
451+
.with_indent(!is_expression_huggable(expression, f.context()))
452452
.fmt(f)
453453
}
454454

@@ -1061,8 +1061,8 @@ pub(crate) fn has_own_parentheses(
10611061
/// ]
10621062
/// )
10631063
/// ```
1064-
pub(crate) fn is_expression_huggable(expr: &Expr, options: &PyFormatOptions) -> bool {
1065-
if !options.preview().is_enabled() {
1064+
pub(crate) fn is_expression_huggable(expr: &Expr, context: &PyFormatContext) -> bool {
1065+
if !is_hug_parens_with_braces_and_square_brackets_enabled(context) {
10661066
return false;
10671067
}
10681068

crates/ruff_python_formatter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod options;
3232
pub(crate) mod other;
3333
pub(crate) mod pattern;
3434
mod prelude;
35+
mod preview;
3536
mod shared_traits;
3637
pub(crate) mod statement;
3738
pub(crate) mod type_param;

crates/ruff_python_formatter/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl PyFormatOptions {
119119
self.docstring_code
120120
}
121121

122-
pub fn preview(&self) -> PreviewMode {
122+
pub const fn preview(&self) -> PreviewMode {
123123
self.preview
124124
}
125125

crates/ruff_python_formatter/src/other/arguments.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::expression::is_expression_huggable;
99
use crate::expression::parentheses::{empty_parenthesized, parenthesized, Parentheses};
1010
use crate::other::commas;
1111
use crate::prelude::*;
12+
use crate::preview::is_hug_parens_with_braces_and_square_brackets_enabled;
1213

1314
#[derive(Default)]
1415
pub struct FormatArguments;
@@ -177,8 +178,7 @@ fn is_single_argument_parenthesized(argument: &Expr, call_end: TextSize, source:
177178
/// Hugging should only be applied to single-argument collections, like lists, or starred versions
178179
/// of those collections.
179180
fn is_argument_huggable(item: &Arguments, context: &PyFormatContext) -> bool {
180-
let options = context.options();
181-
if !options.preview().is_enabled() {
181+
if !is_hug_parens_with_braces_and_square_brackets_enabled(context) {
182182
return false;
183183
}
184184

@@ -192,7 +192,7 @@ fn is_argument_huggable(item: &Arguments, context: &PyFormatContext) -> bool {
192192
};
193193

194194
// If the expression itself isn't huggable, then we can't hug it.
195-
if !is_expression_huggable(arg, options) {
195+
if !is_expression_huggable(arg, context) {
196196
return false;
197197
}
198198

@@ -202,6 +202,8 @@ fn is_argument_huggable(item: &Arguments, context: &PyFormatContext) -> bool {
202202
return false;
203203
}
204204

205+
let options = context.options();
206+
205207
// If the expression has a trailing comma, then we can't hug it.
206208
if options.magic_trailing_comma().is_respect()
207209
&& commas::has_magic_trailing_comma(TextRange::new(arg.end(), item.end()), options, context)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Helpers to test if a specific preview style is enabled or not.
2+
//!
3+
//! The motivation for these functions isn't to avoid code duplication but to ease promoting preview styles
4+
//! to stable. The challenge with directly using [`is_preview`](PyFormatContext::is_preview) is that it is unclear
5+
//! for which specific feature this preview check is for. Having named functions simplifies the promotion:
6+
//! Simply delete the function and let Rust tell you which checks you have to remove.
7+
use crate::PyFormatContext;
8+
9+
/// Returns `true` if the [`fix_power_op_line_length`](https://github.com/astral-sh/ruff/issues/8938) preview style is enabled.
10+
pub(crate) const fn is_fix_power_op_line_length_enabled(context: &PyFormatContext) -> bool {
11+
context.is_preview()
12+
}
13+
14+
/// Returns `true` if the [`hug_parens_with_braces_and_square_brackets`](https://github.com/astral-sh/ruff/issues/8279) preview style is enabled.
15+
pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
16+
context: &PyFormatContext,
17+
) -> bool {
18+
context.is_preview()
19+
}

0 commit comments

Comments
 (0)