Skip to content

Commit

Permalink
Introduce clause_header builder
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 15, 2023
1 parent 262ba25 commit b9667c7
Show file tree
Hide file tree
Showing 14 changed files with 794 additions and 733 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::comments::{trailing_comments, SourceComment, SuppressionKind};
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::verbatim::SuppressedClauseHeader;
use crate::statement::clause::{clause_header, ClauseHeader};
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::FormatRuleWithOptions;
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::ExceptHandlerExceptHandler;

#[derive(Copy, Clone, Default)]
pub enum ExceptHandlerKind {
Expand Down Expand Up @@ -47,41 +48,45 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
let comments_info = f.context().comments().clone();
let dangling_comments = comments_info.dangling_comments(item);

if SuppressionKind::has_skip_comment(dangling_comments, f.context().source()) {
SuppressedClauseHeader::ExceptHandler(item).fmt(f)?;
} else {
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()])?;
}
}

text(":").fmt(f)?;
}

write!(
f,
[
trailing_comments(dangling_comments),
block_indent(&body.format()),
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()])?;
}
}

Ok(())
}),
),
block_indent(&body.format())
]
)
}
Expand Down
67 changes: 35 additions & 32 deletions crates/ruff_python_formatter/src/other/match_case.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::MatchCase;

use crate::comments::{trailing_comments, SourceComment, SuppressionKind};
use crate::comments::SourceComment;
use crate::not_yet_implemented_custom_text;
use crate::prelude::*;
use crate::verbatim::SuppressedClauseHeader;
use crate::statement::clause::{clause_header, ClauseHeader};
use crate::{FormatNodeRule, PyFormatter};

#[derive(Default)]
Expand All @@ -22,40 +22,43 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
let comments = f.context().comments().clone();
let dangling_item_comments = comments.dangling_comments(item);

if SuppressionKind::has_skip_comment(dangling_item_comments, f.context().source()) {
SuppressedClauseHeader::MatchCase(item).fmt(f)?;
} else {
write!(
f,
[
text("case"),
space(),
format_with(|f: &mut PyFormatter| {
let comments = f.context().comments();

for comment in comments.leading_trailing_comments(pattern) {
// This is a lie, but let's go with it.
comment.mark_formatted();
}
write!(
f,
[
clause_header(
ClauseHeader::MatchCase(item),
dangling_item_comments,
&format_with(|f| {
write!(
f,
[
text("case"),
space(),
format_with(|f: &mut PyFormatter| {
let comments = f.context().comments();

// Replace the whole `format_with` with `pattern.format()` once pattern formatting is implemented.
not_yet_implemented_custom_text("NOT_YET_IMPLEMENTED_Pattern", pattern)
.fmt(f)
}),
]
)?;
for comment in comments.leading_trailing_comments(pattern) {
// This is a lie, but let's go with it.
comment.mark_formatted();
}

if let Some(guard) = guard {
write!(f, [space(), text("if"), space(), guard.format()])?;
}
// Replace the whole `format_with` with `pattern.format()` once pattern formatting is implemented.
not_yet_implemented_custom_text(
"NOT_YET_IMPLEMENTED_Pattern",
pattern,
)
.fmt(f)
}),
]
)?;

text(":").fmt(f)?;
}
if let Some(guard) = guard {
write!(f, [space(), text("if"), space(), guard.format()])?;
}

write!(
f,
[
trailing_comments(dangling_item_comments),
Ok(())
}),
),
block_indent(&body.format())
]
)
Expand Down
Loading

0 comments on commit b9667c7

Please sign in to comment.