mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00

<!-- 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 I started working on this because I assumed that I would need access to options inside of `NeedsParantheses` but it then turned out that I won't. Anyway, it kind of felt nice to pass fewer arguments. So I'm gonna put this out here to get your feedback if you prefer this over passing individual fiels. Oh, I sneeked in another change. I renamed `context.contents` to `source`. `contents` is too generic and doesn't tell you anything. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan It compiles
77 lines
2.3 KiB
Rust
77 lines
2.3 KiB
Rust
use rustpython_parser::ast::ExprSubscript;
|
|
|
|
use ruff_formatter::{format_args, write};
|
|
use ruff_python_ast::node::AstNode;
|
|
|
|
use crate::comments::trailing_comments;
|
|
use crate::context::NodeLevel;
|
|
use crate::context::PyFormatContext;
|
|
use crate::expression::parentheses::{
|
|
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
|
};
|
|
use crate::prelude::*;
|
|
use crate::FormatNodeRule;
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatExprSubscript;
|
|
|
|
impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
|
|
fn fmt_fields(&self, item: &ExprSubscript, f: &mut PyFormatter) -> FormatResult<()> {
|
|
let ExprSubscript {
|
|
range: _,
|
|
value,
|
|
slice,
|
|
ctx: _,
|
|
} = item;
|
|
|
|
let comments = f.context().comments().clone();
|
|
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
|
|
debug_assert!(
|
|
dangling_comments.len() <= 1,
|
|
"The subscript expression must have at most a single comment, the one after the bracket"
|
|
);
|
|
|
|
if let NodeLevel::Expression(Some(group_id)) = f.context().node_level() {
|
|
// Enforce the optional parentheses for parenthesized values.
|
|
f.context_mut().set_node_level(NodeLevel::Expression(None));
|
|
let result = value.format().fmt(f);
|
|
f.context_mut()
|
|
.set_node_level(NodeLevel::Expression(Some(group_id)));
|
|
result?;
|
|
} else {
|
|
value.format().fmt(f)?;
|
|
}
|
|
|
|
write!(
|
|
f,
|
|
[group(&format_args![
|
|
text("["),
|
|
trailing_comments(dangling_comments),
|
|
soft_block_indent(&slice.format()),
|
|
text("]")
|
|
])]
|
|
)
|
|
}
|
|
|
|
fn fmt_dangling_comments(
|
|
&self,
|
|
_node: &ExprSubscript,
|
|
_f: &mut PyFormatter,
|
|
) -> FormatResult<()> {
|
|
// Handled inside of `fmt_fields`
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl NeedsParentheses for ExprSubscript {
|
|
fn needs_parentheses(
|
|
&self,
|
|
parenthesize: Parenthesize,
|
|
context: &PyFormatContext,
|
|
) -> Parentheses {
|
|
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
|
|
Parentheses::Optional => Parentheses::Never,
|
|
parentheses => parentheses,
|
|
}
|
|
}
|
|
}
|