-
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.
## Summary Add formatting for `PatternMatchAs`. This closes #6641. ## Test Plan Add tests for comments.
- Loading branch information
Showing
11 changed files
with
159 additions
and
79 deletions.
There are no files selected for viewing
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
41 changes: 32 additions & 9 deletions
41
crates/ruff_python_formatter/src/pattern/pattern_match_as.rs
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,19 +1,42 @@ | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use ruff_python_ast::PatternMatchAs; | ||
use ruff_python_ast::{Pattern, PatternMatchAs}; | ||
|
||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; | ||
use crate::expression::parentheses::parenthesized; | ||
use crate::prelude::*; | ||
use crate::{FormatNodeRule, PyFormatter}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatPatternMatchAs; | ||
|
||
impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs { | ||
fn fmt_fields(&self, item: &PatternMatchAs, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!( | ||
f, | ||
[not_yet_implemented_custom_text( | ||
"x as NOT_YET_IMPLEMENTED_PatternMatchAs", | ||
item | ||
)] | ||
) | ||
let PatternMatchAs { | ||
range: _, | ||
pattern, | ||
name, | ||
} = item; | ||
|
||
if let Some(name) = name { | ||
if let Some(pattern) = pattern { | ||
// Parenthesize nested `PatternMatchAs` like `(a as b) as c`. | ||
if matches!( | ||
pattern.as_ref(), | ||
Pattern::MatchAs(PatternMatchAs { | ||
pattern: Some(_), | ||
.. | ||
}) | ||
) { | ||
parenthesized("(", &pattern.format(), ")").fmt(f)?; | ||
} else { | ||
pattern.format().fmt(f)?; | ||
} | ||
|
||
write!(f, [space(), text("as"), space()])?; | ||
} | ||
name.format().fmt(f) | ||
} else { | ||
debug_assert!(pattern.is_none()); | ||
text("_").fmt(f) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.