mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-04 07:36:11 +00:00
More enum work (#3212)
This commit is contained in:
parent
248590224a
commit
84e96cdcd9
12 changed files with 93 additions and 224 deletions
|
@ -6,7 +6,6 @@ use crate::builders::literal;
|
|||
use crate::context::ASTFormatContext;
|
||||
use crate::cst::Alias;
|
||||
use crate::shared_traits::AsFormat;
|
||||
use crate::trivia::{Relationship, TriviaKind};
|
||||
|
||||
pub struct FormatAlias<'a> {
|
||||
item: &'a Alias,
|
||||
|
@ -33,12 +32,8 @@ impl Format<ASTFormatContext<'_>> for FormatAlias<'_> {
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in alias.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ use crate::context::ASTFormatContext;
|
|||
use crate::cst::{Excepthandler, ExcepthandlerKind};
|
||||
use crate::format::builders::block;
|
||||
use crate::shared_traits::AsFormat;
|
||||
use crate::trivia::{Relationship, TriviaKind};
|
||||
|
||||
pub struct FormatExcepthandler<'a> {
|
||||
item: &'a Excepthandler,
|
||||
|
@ -46,12 +45,8 @@ impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in excepthandler.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ use crate::format::helpers::{is_self_closing, is_simple_power, is_simple_slice};
|
|||
use crate::format::numbers::{complex_literal, float_literal, int_literal};
|
||||
use crate::format::strings::string_literal;
|
||||
use crate::shared_traits::AsFormat;
|
||||
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
||||
use crate::trivia::{Parenthesize, TriviaKind};
|
||||
|
||||
pub struct FormatExpr<'a> {
|
||||
item: &'a Expr,
|
||||
|
@ -33,12 +33,8 @@ fn format_starred(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -62,12 +58,8 @@ fn format_name(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -96,7 +88,7 @@ fn format_subscript(
|
|||
|
||||
// Apply any dangling comments.
|
||||
for trivia in &expr.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if trivia.relationship.is_dangling() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
@ -136,7 +128,7 @@ fn format_tuple(
|
|||
write!(
|
||||
f,
|
||||
[group(&format_with(|f| {
|
||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
||||
if expr.parentheses.is_if_expanded() {
|
||||
write!(f, [if_group_breaks(&text("("))])?;
|
||||
}
|
||||
if matches!(
|
||||
|
@ -146,10 +138,8 @@ fn format_tuple(
|
|||
write!(
|
||||
f,
|
||||
[soft_block_indent(&format_with(|f| {
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma =
|
||||
expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
let is_unbroken =
|
||||
expr.location.row() == expr.end_location.unwrap().row();
|
||||
if magic_trailing_comma {
|
||||
|
@ -170,10 +160,8 @@ fn format_tuple(
|
|||
}))]
|
||||
)?;
|
||||
} else {
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma =
|
||||
expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
let is_unbroken = expr.location.row() == expr.end_location.unwrap().row();
|
||||
if magic_trailing_comma {
|
||||
write!(f, [expand_parent()])?;
|
||||
|
@ -190,7 +178,7 @@ fn format_tuple(
|
|||
}
|
||||
}
|
||||
}
|
||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
||||
if expr.parentheses.is_if_expanded() {
|
||||
write!(f, [if_group_breaks(&text(")"))])?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -236,7 +224,7 @@ fn format_slice(
|
|||
|
||||
// Apply any dangling comments.
|
||||
for trivia in &lower.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if trivia.relationship.is_dangling() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
@ -256,12 +244,8 @@ fn format_slice(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in lower.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -282,7 +266,7 @@ fn format_slice(
|
|||
|
||||
// Apply any dangling comments.
|
||||
for trivia in &upper.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if trivia.relationship.is_dangling() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
@ -295,12 +279,8 @@ fn format_slice(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in upper.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -329,7 +309,7 @@ fn format_slice(
|
|||
|
||||
// Apply any dangling comments.
|
||||
for trivia in &step.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if trivia.relationship.is_dangling() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
@ -342,12 +322,8 @@ fn format_slice(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in step.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -365,12 +341,8 @@ fn format_slice(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -391,10 +363,7 @@ fn format_list(
|
|||
) -> FormatResult<()> {
|
||||
write!(f, [text("[")])?;
|
||||
if !elts.is_empty() {
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||
|
@ -429,10 +398,7 @@ fn format_set(
|
|||
} else {
|
||||
write!(f, [text("{")])?;
|
||||
if !elts.is_empty() {
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||
|
@ -474,12 +440,8 @@ fn format_call(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -495,12 +457,8 @@ fn format_call(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -511,10 +469,7 @@ fn format_call(
|
|||
write!(f, [line_suffix(&literal(range))])?;
|
||||
}
|
||||
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||
|
@ -560,7 +515,7 @@ fn format_call(
|
|||
|
||||
// Apply any dangling trailing comments.
|
||||
for trivia in &expr.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if trivia.relationship.is_dangling() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
@ -757,12 +712,8 @@ fn format_compare(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -819,10 +770,7 @@ fn format_dict(
|
|||
) -> FormatResult<()> {
|
||||
write!(f, [text("{")])?;
|
||||
if !keys.is_empty() {
|
||||
let magic_trailing_comma = expr
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
f,
|
||||
[soft_block_indent(&format_with(|f| {
|
||||
|
@ -889,12 +837,8 @@ fn format_attribute(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -929,12 +873,8 @@ fn format_bool_op(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -971,12 +911,8 @@ fn format_bin_op(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -1004,7 +940,7 @@ fn format_unary_op(
|
|||
ExprKind::BoolOp { .. } | ExprKind::Compare { .. } | ExprKind::BinOp { .. }
|
||||
)
|
||||
{
|
||||
let parenthesized = matches!(operand.parentheses, Parenthesize::Always);
|
||||
let parenthesized = operand.parentheses.is_always();
|
||||
if !parenthesized {
|
||||
write!(f, [text("(")])?;
|
||||
}
|
||||
|
@ -1056,13 +992,13 @@ fn format_if_exp(
|
|||
|
||||
impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||
if matches!(self.item.parentheses, Parenthesize::Always) {
|
||||
if self.item.parentheses.is_always() {
|
||||
write!(f, [text("(")])?;
|
||||
}
|
||||
|
||||
// Any leading comments come on the line before.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Leading) {
|
||||
if trivia.relationship.is_leading() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
|
@ -1130,7 +1066,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
|||
|
||||
// Any trailing comments come on the lines after.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if trivia.relationship.is_trailing() {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
|
@ -1139,7 +1075,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
if matches!(self.item.parentheses, Parenthesize::Always) {
|
||||
if self.item.parentheses.is_always() {
|
||||
write!(f, [text(")")])?;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ use crate::cst::{
|
|||
use crate::format::builders::{block, join_names};
|
||||
use crate::format::helpers::is_self_closing;
|
||||
use crate::shared_traits::AsFormat;
|
||||
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
||||
use crate::trivia::TriviaKind;
|
||||
|
||||
fn format_break(f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||
write!(f, [text("break")])
|
||||
|
@ -25,12 +25,8 @@ fn format_pass(f: &mut Formatter<ASTFormatContext<'_>>, stmt: &Stmt) -> FormatRe
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -189,11 +185,7 @@ fn format_func_def(
|
|||
dynamic_text(name, TextSize::default()),
|
||||
text("("),
|
||||
group(&soft_block_indent(&format_with(|f| {
|
||||
if stmt
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma))
|
||||
{
|
||||
if stmt.trivia.iter().any(|c| c.kind.is_magic_trailing_comma()) {
|
||||
write!(f, [expand_parent()])?;
|
||||
}
|
||||
write!(f, [args.format()])
|
||||
|
@ -211,12 +203,8 @@ fn format_func_def(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -260,12 +248,8 @@ fn format_assign(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -436,12 +420,8 @@ fn format_return(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -585,10 +565,7 @@ fn format_import_from(
|
|||
if names.iter().any(|name| name.node.name == "*") {
|
||||
write!(f, [text("*")])?;
|
||||
} else {
|
||||
let magic_trailing_comma = stmt
|
||||
.trivia
|
||||
.iter()
|
||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
||||
let magic_trailing_comma = stmt.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
|
@ -616,12 +593,8 @@ fn format_import_from(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -640,7 +613,7 @@ fn format_expr(
|
|||
stmt: &Stmt,
|
||||
expr: &Expr,
|
||||
) -> FormatResult<()> {
|
||||
if matches!(stmt.parentheses, Parenthesize::Always) {
|
||||
if stmt.parentheses.is_always() {
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
|
@ -665,12 +638,8 @@ fn format_expr(
|
|||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -730,7 +699,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
|||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||
// Any leading comments come on the line before.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Leading) {
|
||||
if trivia.relationship.is_leading() {
|
||||
match trivia.kind {
|
||||
TriviaKind::EmptyLine => {
|
||||
write!(f, [empty_line()])?;
|
||||
|
@ -886,7 +855,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
|||
|
||||
// Any trailing comments come on the lines after.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if trivia.relationship.is_trailing() {
|
||||
match trivia.kind {
|
||||
TriviaKind::EmptyLine => {
|
||||
write!(f, [empty_line()])?;
|
||||
|
|
|
@ -8,7 +8,6 @@ use crate::context::ASTFormatContext;
|
|||
use crate::core::helpers::{leading_quote, trailing_quote};
|
||||
use crate::core::types::Range;
|
||||
use crate::cst::Expr;
|
||||
use crate::trivia::Parenthesize;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct StringLiteralPart {
|
||||
|
@ -142,7 +141,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteral<'_> {
|
|||
write!(
|
||||
f,
|
||||
[group(&format_with(|f| {
|
||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
||||
if expr.parentheses.is_if_expanded() {
|
||||
write!(f, [if_group_breaks(&text("("))])?;
|
||||
}
|
||||
for (i, elt) in elts.iter().enumerate() {
|
||||
|
@ -151,7 +150,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteral<'_> {
|
|||
write!(f, [soft_line_break_or_space()])?;
|
||||
}
|
||||
}
|
||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
||||
if expr.parentheses.is_if_expanded() {
|
||||
write!(f, [if_group_breaks(&text(")"))])?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue