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 16, 2023
1 parent e3b0614 commit 47988c9
Show file tree
Hide file tree
Showing 19 changed files with 975 additions and 743 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if (
# a leading condition comment
len([1, 23, 3, 4, 5]) > 2 # trailing condition comment
# trailing own line comment
): # fmt: skip
pass


if ( # trailing open parentheses comment
# a leading condition comment
len([1, 23, 3, 4, 5]) > 2
) and ((((y)))): # fmt: skip
pass


if ( # trailing open parentheses comment
# a leading condition comment
len([1, 23, 3, 4, 5]) > 2
) and y: # fmt: skip
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class TestTypeParam[ T ]: # fmt: skip
pass

class TestTypeParam [ # trailing open paren comment
# leading comment
T # trailing type param comment
# trailing type param own line comment
]: # fmt: skip
pass

class TestTrailingComment4[
T
] ( # trailing arguments open parenthesis comment
# leading argument comment
A # trailing argument comment
# trailing argument own line comment
): # fmt: skip
pass

def test [
# comment
A,

# another

B,
] (): # fmt: skip
...
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
61 changes: 32 additions & 29 deletions crates/ruff_python_formatter/src/other/match_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +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, SuppressionKind};
use crate::comments::{leading_comments, SourceComment};
use crate::expression::parentheses::parenthesized;
use crate::prelude::*;
use crate::verbatim::SuppressedClauseHeader;
use crate::statement::clause::{clause_header, ClauseHeader};
use crate::{FormatError, FormatNodeRule, PyFormatter};

#[derive(Default)]
Expand All @@ -24,36 +24,39 @@ 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()])?;

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()])?;
}

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

write!(
f,
[
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())
]
)
Expand Down
Loading

0 comments on commit 47988c9

Please sign in to comment.