-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add formatting for StmtMatch
#6286
Changes from all commits
1b14772
e70b45a
4c514dc
52f670b
b39537e
16fc898
6691fdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also do this as a separate PR: Ideally, we call into ruff/crates/ruff_python_formatter/src/module/mod.rs Lines 1 to 35 in f45e864
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's already a work in progress in the next PR (https://github.com/astral-sh/ruff/pull/6360/files#diff-05760c9829e7694c86aaf97014143c0785c12da546f06dcfbc87caf67b15d773) ;) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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())]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
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(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!