mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-21 11:00:49 +00:00
Add formatter support for call and class definition Arguments
(#6274)
## Summary This PR leverages the `Arguments` AST node introduced in #6259 in the formatter, which ensures that we correctly handle trailing comments in calls, like: ```python f( 1, # comment ) pass ``` (Previously, this was treated as a leading comment on `pass`.) This also allows us to unify the argument handling across calls and class definitions. ## Test Plan A bunch of new fixture tests, plus improved Black compatibility.
This commit is contained in:
parent
b095b7204b
commit
4c53bfe896
19 changed files with 640 additions and 252 deletions
|
@ -2627,6 +2627,34 @@ impl AstNode for Comprehension {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for Arguments {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::Arguments(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::Arguments(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for Parameters {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -3458,6 +3486,11 @@ impl From<Comprehension> for AnyNode {
|
|||
AnyNode::Comprehension(node)
|
||||
}
|
||||
}
|
||||
impl From<Arguments> for AnyNode {
|
||||
fn from(node: Arguments) -> Self {
|
||||
AnyNode::Arguments(node)
|
||||
}
|
||||
}
|
||||
impl From<Parameters> for AnyNode {
|
||||
fn from(node: Parameters) -> Self {
|
||||
AnyNode::Parameters(node)
|
||||
|
@ -4909,6 +4942,11 @@ impl<'a> From<&'a Comprehension> for AnyNodeRef<'a> {
|
|||
AnyNodeRef::Comprehension(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a Arguments> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a Arguments) -> Self {
|
||||
AnyNodeRef::Arguments(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a Parameters> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a Parameters) -> Self {
|
||||
AnyNodeRef::Parameters(node)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue