Format target: annotation = value? expressions (#5661)

This commit is contained in:
Micha Reiser 2023-07-11 16:40:28 +02:00 committed by GitHub
parent 0c8ec80d7b
commit f1d367655b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 318 additions and 988 deletions

View file

@ -1,5 +1,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::write;
use rustpython_parser::ast::StmtAnnAssign;
#[derive(Default)]
@ -7,6 +8,23 @@ pub struct FormatStmtAnnAssign;
impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
fn fmt_fields(&self, item: &StmtAnnAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let StmtAnnAssign {
range: _,
target,
annotation,
value,
simple: _,
} = item;
write!(
f,
[target.format(), text(":"), space(), annotation.format()]
)?;
if let Some(value) = value {
write!(f, [space(), text("="), space(), value.format()])?;
}
Ok(())
}
}

View file

@ -1,12 +1,11 @@
use crate::context::PyFormatContext;
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::formatter::Formatter;
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::Expr;
use rustpython_parser::ast::StmtAssign;
use ruff_formatter::write;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::FormatNodeRule;
// Note: This currently does wrap but not the black way so the types below likely need to be
// replaced entirely
//
@ -22,32 +21,11 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
value,
type_comment: _,
} = item;
write!(
f,
[
LhsAssignList::new(targets),
value.format().with_options(Parenthesize::IfBreaks)
]
)
}
}
#[derive(Debug)]
struct LhsAssignList<'a> {
lhs_assign_list: &'a [Expr],
}
impl<'a> LhsAssignList<'a> {
const fn new(lhs_assign_list: &'a [Expr]) -> Self {
Self { lhs_assign_list }
}
}
impl Format<PyFormatContext<'_>> for LhsAssignList<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
for element in self.lhs_assign_list {
write!(f, [&element.format(), space(), text("="), space(),])?;
for target in targets {
write!(f, [target.format(), space(), text("="), space()])?;
}
Ok(())
write!(f, [value.format().with_options(Parenthesize::IfBreaks)])
}
}

View file

@ -1,4 +1,4 @@
use crate::builders::{optional_parentheses, PyFormatterExtensions};
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
use crate::comments::dangling_node_comments;
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
@ -36,7 +36,7 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
}
targets => {
let item = format_with(|f| f.join_comma_separated().nodes(targets.iter()).finish());
optional_parentheses(&item).fmt(f)
parenthesize_if_expands(&item).fmt(f)
}
}
}

View file

@ -1,6 +1,6 @@
use crate::comments::{leading_comments, trailing_comments};
use crate::context::NodeLevel;
use crate::expression::parentheses::Parenthesize;
use crate::expression::parentheses::{optional_parentheses, Parenthesize};
use crate::prelude::*;
use crate::trivia::{lines_after, skip_trailing_trivia};
use crate::FormatNodeRule;
@ -97,9 +97,9 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
space(),
text("->"),
space(),
return_annotation
.format()
.with_options(Parenthesize::IfBreaks)
optional_parentheses(
&return_annotation.format().with_options(Parenthesize::Never)
)
]
)?;
}

View file

@ -1,4 +1,4 @@
use crate::builders::{optional_parentheses, PyFormatterExtensions};
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{dynamic_text, format_with, space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
@ -43,6 +43,6 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
.entries(names.iter().map(|name| (name, name.format())))
.finish()
});
optional_parentheses(&names).fmt(f)
parenthesize_if_expands(&names).fmt(f)
}
}

View file

@ -3,7 +3,7 @@ use ruff_python_ast::node::AnyNodeRef;
use ruff_text_size::TextRange;
use rustpython_parser::ast::{Ranged, StmtAsyncWith, StmtWith, Suite, WithItem};
use crate::builders::optional_parentheses;
use crate::builders::parenthesize_if_expands;
use crate::comments::trailing_comments;
use crate::prelude::*;
use crate::FormatNodeRule;
@ -80,7 +80,7 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
[
text("with"),
space(),
group(&optional_parentheses(&joined_items)),
group(&parenthesize_if_expands(&joined_items)),
text(":"),
trailing_comments(dangling_comments),
block_indent(&self.body().format())