mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Support fmt: skip
on compound statements (#6593)
This commit is contained in:
parent
4dc32a00d0
commit
fa7442da2f
23 changed files with 1385 additions and 625 deletions
|
@ -1,12 +1,14 @@
|
|||
use crate::comments::{trailing_comments, SourceComment};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::ExceptHandlerExceptHandler;
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::statement::clause::{clause_header, ClauseHeader};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Copy, Clone, Default)]
|
||||
pub enum ExceptHandlerKind {
|
||||
#[default]
|
||||
|
@ -49,32 +51,42 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("except"),
|
||||
match self.except_handler_kind {
|
||||
ExceptHandlerKind::Regular => None,
|
||||
ExceptHandlerKind::Starred => Some(text("*")),
|
||||
}
|
||||
]
|
||||
)?;
|
||||
clause_header(
|
||||
ClauseHeader::ExceptHandler(item),
|
||||
dangling_comments,
|
||||
&format_with(|f| {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text("except"),
|
||||
match self.except_handler_kind {
|
||||
ExceptHandlerKind::Regular => None,
|
||||
ExceptHandlerKind::Starred => Some(text("*")),
|
||||
}
|
||||
]
|
||||
)?;
|
||||
|
||||
if let Some(type_) = type_ {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
space(),
|
||||
maybe_parenthesize_expression(type_, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
)?;
|
||||
if let Some(name) = name {
|
||||
write!(f, [space(), text("as"), space(), name.format()])?;
|
||||
}
|
||||
}
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text(":"),
|
||||
trailing_comments(dangling_comments),
|
||||
block_indent(&body.format()),
|
||||
if let Some(type_) = type_ {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
space(),
|
||||
maybe_parenthesize_expression(
|
||||
type_,
|
||||
item,
|
||||
Parenthesize::IfBreaks
|
||||
)
|
||||
]
|
||||
)?;
|
||||
if let Some(name) = name {
|
||||
write!(f, [space(), text("as"), space(), name.format()])?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
),
|
||||
block_indent(&body.format())
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,10 @@ use ruff_python_ast::{MatchCase, Pattern, Ranged};
|
|||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments, SourceComment};
|
||||
use crate::comments::{leading_comments, SourceComment};
|
||||
use crate::expression::parentheses::parenthesized;
|
||||
use crate::prelude::*;
|
||||
use crate::statement::clause::{clause_header, ClauseHeader};
|
||||
use crate::{FormatError, FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -23,30 +24,39 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
let comments = f.context().comments().clone();
|
||||
let dangling_item_comments = comments.dangling_comments(item);
|
||||
|
||||
write!(f, [text("case"), space()])?;
|
||||
let leading_pattern_comments = comments.leading_comments(pattern);
|
||||
if !leading_pattern_comments.is_empty() {
|
||||
parenthesized(
|
||||
"(",
|
||||
&format_args![leading_comments(leading_pattern_comments), pattern.format()],
|
||||
")",
|
||||
)
|
||||
.fmt(f)?;
|
||||
} else if is_match_case_pattern_parenthesized(item, pattern, f.context())? {
|
||||
parenthesized("(", &pattern.format(), ")").fmt(f)?;
|
||||
} else {
|
||||
pattern.format().fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(guard) = guard {
|
||||
write!(f, [space(), text("if"), space(), guard.format()])?;
|
||||
}
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text(":"),
|
||||
trailing_comments(dangling_item_comments),
|
||||
clause_header(
|
||||
ClauseHeader::MatchCase(item),
|
||||
dangling_item_comments,
|
||||
&format_with(|f| {
|
||||
write!(f, [text("case"), space()])?;
|
||||
|
||||
let leading_pattern_comments = comments.leading_comments(pattern);
|
||||
if !leading_pattern_comments.is_empty() {
|
||||
parenthesized(
|
||||
"(",
|
||||
&format_args![
|
||||
leading_comments(leading_pattern_comments),
|
||||
pattern.format()
|
||||
],
|
||||
")",
|
||||
)
|
||||
.fmt(f)?;
|
||||
} else if is_match_case_pattern_parenthesized(item, pattern, f.context())? {
|
||||
parenthesized("(", &pattern.format(), ")").fmt(f)?;
|
||||
} else {
|
||||
pattern.format().fmt(f)?;
|
||||
}
|
||||
|
||||
if let Some(guard) = guard {
|
||||
write!(f, [space(), text("if"), space(), guard.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
),
|
||||
block_indent(&body.format())
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue