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:
Zanie Blue 2023-08-02 15:29:28 -05:00 committed by GitHub
parent 9425ed72a0
commit 1a60d1e3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 825 additions and 172 deletions

View file

@ -2939,3 +2939,163 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ElifElseClause {
)
}
}
impl FormatRule<ast::TypeParams, PyFormatContext<'_>>
for crate::type_param::type_params::FormatTypeParams
{
#[inline]
fn fmt(
&self,
node: &ast::TypeParams,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParams>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParams {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParams,
crate::type_param::type_params::FormatTypeParams,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_params::FormatTypeParams::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParams {
type Format = FormatOwnedWithRule<
ast::TypeParams,
crate::type_param::type_params::FormatTypeParams,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_params::FormatTypeParams::default(),
)
}
}
impl FormatRule<ast::TypeParamTypeVar, PyFormatContext<'_>>
for crate::type_param::type_param_type_var::FormatTypeParamTypeVar
{
#[inline]
fn fmt(
&self,
node: &ast::TypeParamTypeVar,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVar>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVar {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVar,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(),
)
}
}
impl FormatRule<ast::TypeParamTypeVarTuple, PyFormatContext<'_>>
for crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple
{
#[inline]
fn fmt(
&self,
node: &ast::TypeParamTypeVarTuple,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamTypeVarTuple>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamTypeVarTuple {
type Format = FormatOwnedWithRule<
ast::TypeParamTypeVarTuple,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(),
)
}
}
impl FormatRule<ast::TypeParamParamSpec, PyFormatContext<'_>>
for crate::type_param::type_param_param_spec::FormatTypeParamParamSpec
{
#[inline]
fn fmt(
&self,
node: &ast::TypeParamParamSpec,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::TypeParamParamSpec>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeParamParamSpec {
type Format = FormatOwnedWithRule<
ast::TypeParamParamSpec,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(),
)
}
}