mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-22 20:45:11 +00:00
Improve slice formatting (#5922)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary - Remove space when start of slice is empty - Treat unary op except `not` as simple expression ## Test Plan Add some simple tests for unary op expressions in slice Closes #5673
This commit is contained in:
parent
d351761f5d
commit
b866cbb33d
7 changed files with 33 additions and 212 deletions
|
@ -1,6 +1,5 @@
|
|||
use ruff_text_size::TextRange;
|
||||
use rustpython_parser::ast::ExprSlice;
|
||||
use rustpython_parser::ast::{Expr, Ranged};
|
||||
use rustpython_parser::ast::{Expr, ExprSlice, ExprUnaryOp, Ranged, UnaryOp};
|
||||
|
||||
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
|
||||
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
|
||||
|
@ -87,7 +86,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
|
|||
// e201 = "e"[a() :: 1]
|
||||
// e202 = "e"[a() :: a()]
|
||||
// ```
|
||||
if !all_simple {
|
||||
if !all_simple && lower.is_some() {
|
||||
space().fmt(f)?;
|
||||
}
|
||||
text(":").fmt(f)?;
|
||||
|
@ -196,7 +195,17 @@ pub(crate) fn find_colons(
|
|||
/// Determines whether this expression needs a space around the colon
|
||||
/// <https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#slices>
|
||||
fn is_simple_expr(expr: &Expr) -> bool {
|
||||
matches!(expr, Expr::Constant(_) | Expr::Name(_))
|
||||
// Unary op expressions except `not` can be simple.
|
||||
if let Some(ExprUnaryOp {
|
||||
op: UnaryOp::UAdd | UnaryOp::USub | UnaryOp::Invert,
|
||||
operand,
|
||||
..
|
||||
}) = expr.as_unary_op_expr()
|
||||
{
|
||||
is_simple_expr(operand)
|
||||
} else {
|
||||
matches!(expr, Expr::Constant(_) | Expr::Name(_))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum ExprSliceCommentSection {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue