-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add formatting for
StmtMatch
(#6286)
## Summary This PR adds support for `StmtMatch` with subs for `MatchCase`. ## Test Plan Add a few additional test cases around `match` statement, comments, line breaks. resolves: #6298
- Loading branch information
1 parent
87984e9
commit 001aa48
Showing
12 changed files
with
881 additions
and
443 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# leading match comment | ||
match foo: # dangling match comment | ||
case "bar": | ||
pass | ||
|
||
|
||
# leading match comment | ||
match ( # leading expr comment | ||
# another leading expr comment | ||
foo # trailing expr comment | ||
# another trailing expr comment | ||
): # dangling match comment | ||
case "bar": | ||
pass | ||
|
||
|
||
# leading match comment | ||
match ( # hello | ||
foo # trailing expr comment | ||
, # another | ||
): # dangling match comment | ||
case "bar": | ||
pass | ||
|
||
|
||
match [ # comment | ||
first, | ||
second, | ||
third | ||
]: # another comment | ||
case ["a", "b", "c"]: | ||
pass | ||
|
||
match ( # comment | ||
"a b c" | ||
).split(): # another comment | ||
case ["a", "b", "c"]: | ||
pass | ||
|
||
|
||
match ( # comment | ||
# let's go | ||
yield foo | ||
): # another comment | ||
case ["a", "b", "c"]: | ||
pass | ||
|
||
|
||
match aaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh: # comment | ||
case "sshhhhhhhh": | ||
pass | ||
|
||
|
||
def foo(): | ||
match inside_func: # comment | ||
case "bar": | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,45 @@ | ||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use ruff_python_ast::MatchCase; | ||
|
||
use crate::expression::maybe_parenthesize_expression; | ||
use crate::expression::parentheses::Parenthesize; | ||
use crate::not_yet_implemented_custom_text; | ||
use crate::prelude::*; | ||
use crate::{FormatNodeRule, PyFormatter}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatMatchCase; | ||
|
||
impl FormatNodeRule<MatchCase> for FormatMatchCase { | ||
fn fmt_fields(&self, item: &MatchCase, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!(f, [not_yet_implemented(item)]) | ||
let MatchCase { | ||
range: _, | ||
pattern: _, | ||
guard, | ||
body, | ||
} = item; | ||
|
||
write!( | ||
f, | ||
[ | ||
text("case"), | ||
space(), | ||
not_yet_implemented_custom_text("NOT_YET_IMPLEMENTED_Pattern"), | ||
] | ||
)?; | ||
|
||
if let Some(guard) = guard { | ||
write!( | ||
f, | ||
[ | ||
space(), | ||
text("if"), | ||
space(), | ||
maybe_parenthesize_expression(guard, item, Parenthesize::IfBreaks) | ||
] | ||
)?; | ||
} | ||
|
||
write!(f, [text(":"), block_indent(&body.format())]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,49 @@ | ||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use ruff_python_ast::StmtMatch; | ||
|
||
use crate::comments::trailing_comments; | ||
use crate::expression::maybe_parenthesize_expression; | ||
use crate::expression::parentheses::Parenthesize; | ||
use crate::prelude::*; | ||
use crate::{FormatNodeRule, PyFormatter}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatStmtMatch; | ||
|
||
impl FormatNodeRule<StmtMatch> for FormatStmtMatch { | ||
fn fmt_fields(&self, item: &StmtMatch, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!(f, [not_yet_implemented(item)]) | ||
let StmtMatch { | ||
range: _, | ||
subject, | ||
cases, | ||
} = item; | ||
|
||
let comments = f.context().comments().clone(); | ||
let dangling_item_comments = comments.dangling_comments(item); | ||
|
||
// There can be at most one dangling comment after the colon in a match statement. | ||
debug_assert!(dangling_item_comments.len() <= 1); | ||
|
||
write!( | ||
f, | ||
[ | ||
text("match"), | ||
space(), | ||
maybe_parenthesize_expression(subject, item, Parenthesize::IfBreaks), | ||
text(":"), | ||
trailing_comments(dangling_item_comments) | ||
] | ||
)?; | ||
|
||
for case in cases { | ||
write!(f, [block_indent(&case.format())])?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn fmt_dangling_comments(&self, _node: &StmtMatch, _f: &mut PyFormatter) -> FormatResult<()> { | ||
// Handled as part of `fmt_fields` | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.