mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Add formatting of type parameters in class and function definitions (#6161)
Part of #5062 Closes https://github.com/astral-sh/ruff/issues/5931 Implements formatting of a sequence of type parameters in a dedicated struct for reuse by classes, functions, and type aliases (preparing for #5929). Adds formatting of type parameters in class and function definitions — previously, they were just elided.
This commit is contained in:
parent
9425ed72a0
commit
1a60d1e3c6
22 changed files with 825 additions and 172 deletions
|
@ -1,6 +1,7 @@
|
|||
use crate::node::AnyNodeRef;
|
||||
use crate::{
|
||||
Decorator, Expr, Identifier, Parameters, Ranged, StmtAsyncFunctionDef, StmtFunctionDef, Suite,
|
||||
TypeParams,
|
||||
};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
|
@ -79,6 +80,13 @@ impl<'a> AnyFunctionDefinition<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn type_params(self) -> Option<&'a TypeParams> {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => definition.type_params.as_ref(),
|
||||
Self::AsyncFunctionDefinition(definition) => definition.type_params.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this is [`Self::AsyncFunctionDefinition`]
|
||||
pub const fn is_async(self) -> bool {
|
||||
matches!(self, Self::AsyncFunctionDefinition(_))
|
||||
|
|
|
@ -2880,6 +2880,34 @@ impl AstNode for Decorator {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::TypeParams {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::TypeParams(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::TypeParams(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 ast::TypeParamTypeVar {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -3531,6 +3559,11 @@ impl From<Decorator> for AnyNode {
|
|||
AnyNode::Decorator(node)
|
||||
}
|
||||
}
|
||||
impl From<TypeParams> for AnyNode {
|
||||
fn from(node: TypeParams) -> Self {
|
||||
AnyNode::TypeParams(node)
|
||||
}
|
||||
}
|
||||
impl From<TypeParamTypeVar> for AnyNode {
|
||||
fn from(node: TypeParamTypeVar) -> Self {
|
||||
AnyNode::TypeParamTypeVar(node)
|
||||
|
@ -4804,6 +4837,11 @@ impl<'a> From<&'a Decorator> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::TypeParams> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::TypeParams) -> Self {
|
||||
AnyNodeRef::TypeParams(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a TypeParamTypeVar> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a TypeParamTypeVar) -> Self {
|
||||
AnyNodeRef::TypeParamTypeVar(node)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue