-
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
Conversation
Current dependencies on/for this PR: This comment was auto-generated by Graphite. |
PR Check ResultsBenchmarkLinux
Windows
|
b1591ba
to
73a6595
Compare
StmtMatch
and MatchCase
StmtMatch
[block_indent(&format_args![ | ||
text("case"), | ||
space(), | ||
not_yet_implemented_custom_text("NOT_YET_IMPLEMENTED_MatchCase"), | ||
text(":"), | ||
block_indent(&case.body.format()) | ||
])] |
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.
Temporary until the MatchCase
formatting is implemented.
match ( | ||
# leading expr comment |
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.
This seems to be a known issue where the comment right next to an opening parentheses is moved down instead.
- match ( # leading expr comment
+ match (
+ # leading expr comment
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.
)?; | ||
|
||
for case in cases { | ||
write!( |
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.
Do we need to retain empty lines between match cases?
Micha trying to write valid python code 😅
match test:
case "a":
pass
case "b":
pass
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.
I think the newline handling will be done in the MatchCase
implementation. Any newline between the match
statement and the first case will be removed as is done in black as well:
match test:
case "a":
pass
# vvvvv
match test:
case "a":
pass
e34d757
to
16fc898
Compare
@@ -73,7 +73,6 @@ impl<'a> CommentsVisitor<'a> { | |||
enclosing: enclosing_node, | |||
preceding: self.preceding_node, | |||
following: Some(node), | |||
parent: self.parents.iter().rev().nth(1).copied(), |
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!
[ | ||
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 comment
The 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 pattern.format()
here. This requires implementing AsFormat
and IntoFormat
for Pattern
, similar to how it is done for Mod
ruff/crates/ruff_python_formatter/src/module/mod.rs
Lines 1 to 35 in f45e864
use crate::context::PyFormatContext; | |
use crate::{AsFormat, IntoFormat, PyFormatter}; | |
use ruff_formatter::{Format, FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatRule}; | |
use ruff_python_ast::Mod; | |
pub(crate) mod mod_expression; | |
pub(crate) mod mod_module; | |
#[derive(Default)] | |
pub struct FormatMod; | |
impl FormatRule<Mod, PyFormatContext<'_>> for FormatMod { | |
fn fmt(&self, item: &Mod, f: &mut PyFormatter) -> FormatResult<()> { | |
match item { | |
Mod::Module(x) => x.format().fmt(f), | |
Mod::Expression(x) => x.format().fmt(f), | |
} | |
} | |
} | |
impl<'ast> AsFormat<PyFormatContext<'ast>> for Mod { | |
type Format<'a> = FormatRefWithRule<'a, Mod, FormatMod, PyFormatContext<'ast>>; | |
fn format(&self) -> Self::Format<'_> { | |
FormatRefWithRule::new(self, FormatMod) | |
} | |
} | |
impl<'ast> IntoFormat<PyFormatContext<'ast>> for Mod { | |
type Format = FormatOwnedWithRule<Mod, FormatMod, PyFormatContext<'ast>>; | |
fn into_format(self) -> Self::Format { | |
FormatOwnedWithRule::new(self, FormatMod) | |
} | |
} |
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.
Yes, it's already a work in progress in the next PR (https://github.com/astral-sh/ruff/pull/6360/files#diff-05760c9829e7694c86aaf97014143c0785c12da546f06dcfbc87caf67b15d773) ;)
## 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: astral-sh#6298
Summary
This PR adds support for
StmtMatch
with subs forMatchCase
.Test Plan
Add a few additional test cases around
match
statement, comments, line breaks.resolves: #6298