Files
ruff/crates/ruff_python_formatter/src/statement/stmt_global.rs
Douglas Creager 98ef564170 Remove AstNode and AnyNode (#15479)
While looking into potential AST optimizations, I noticed the `AstNode`
trait and `AnyNode` type aren't used anywhere in Ruff or Red Knot. It
looks like they might be historical artifacts of previous ways of
consuming AST nodes?

- `AstNode::cast`, `AstNode::cast_ref`, and `AstNode::can_cast` are not
used anywhere.
- Since `cast_ref` isn't needed anymore, the `Ref` associated type isn't
either.

This is a pure refactoring, with no intended behavior changes.
2025-01-17 17:11:00 -05:00

59 lines
1.8 KiB
Rust

use ruff_formatter::{format_args, write};
use ruff_python_ast::StmtGlobal;
use crate::comments::SourceComment;
use crate::has_skip_comment;
use crate::prelude::*;
#[derive(Default)]
pub struct FormatStmtGlobal;
impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
fn fmt_fields(&self, item: &StmtGlobal, f: &mut PyFormatter) -> FormatResult<()> {
// Join the `global` names, breaking across continuation lines if necessary, unless the
// `global` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `global` statement.
if f.context().comments().has_trailing(item) {
let joined = format_with(|f| {
f.join_with(format_args![token(","), space()])
.entries(item.names.iter().formatted())
.finish()
});
write!(f, [token("global"), space(), &joined])
} else {
let joined = format_with(|f| {
f.join_with(&format_args![
token(","),
space(),
if_group_breaks(&token("\\")),
soft_line_break(),
])
.entries(item.names.iter().formatted())
.finish()
});
write!(
f,
[
token("global"),
space(),
group(&format_args!(
if_group_breaks(&token("\\")),
soft_line_break(),
soft_block_indent(&joined)
))
]
)
}
}
fn is_suppressed(
&self,
trailing_comments: &[SourceComment],
context: &PyFormatContext,
) -> bool {
has_skip_comment(trailing_comments, context.source())
}
}