Skip to content

Commit

Permalink
Change parser
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 23, 2023
1 parent b1df844 commit 7c7990c
Show file tree
Hide file tree
Showing 10 changed files with 3,939 additions and 500 deletions.
4 changes: 4 additions & 0 deletions crates/ruff_python_formatter/bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
match d3:
case ( # d 3
x):
pass
5 changes: 4 additions & 1 deletion crates/ruff_python_formatter/foo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
match x:
case (1):
case (
# foo
1
):
y = 0
case (1):
y = 1
1 change: 1 addition & 0 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ fn handle_match_case_comment<'a>(
comment: DecoratedComment<'a>,
match_case: &'a MatchCase,
) -> CommentPlacement<'a> {
println!("handle_match_case_comment: {:?}", comment);
if comment.line_position().is_end_of_line() && comment.start() < match_case.pattern.start() {
CommentPlacement::dangling(comment.enclosing_node(), comment)
} else {
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff_python_formatter/src/pattern/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::expression::parentheses::parenthesized;
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::Pattern;
use std::slice;

use crate::prelude::*;

Expand All @@ -17,7 +19,7 @@ pub struct FormatPattern;

impl FormatRule<Pattern, PyFormatContext<'_>> for FormatPattern {
fn fmt(&self, item: &Pattern, f: &mut PyFormatter) -> FormatResult<()> {
match item {
let format_pattern = format_with(|f| match item {
Pattern::MatchValue(p) => p.format().fmt(f),
Pattern::MatchSingleton(p) => p.format().fmt(f),
Pattern::MatchSequence(p) => p.format().fmt(f),
Expand All @@ -26,7 +28,9 @@ impl FormatRule<Pattern, PyFormatContext<'_>> for FormatPattern {
Pattern::MatchStar(p) => p.format().fmt(f),
Pattern::MatchAs(p) => p.format().fmt(f),
Pattern::MatchOr(p) => p.format().fmt(f),
}
});

format_pattern.fmt(f)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct FormatPatternMatchValue;
impl FormatNodeRule<PatternMatchValue> for FormatPatternMatchValue {
fn fmt_fields(&self, item: &PatternMatchValue, f: &mut PyFormatter) -> FormatResult<()> {
let PatternMatchValue { value, range: _ } = item;
let formatted = value.format().with_options(Parentheses::Preserve);
let formatted = value.format();
write!(f, [formatted])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ match foo:
"b",
]:
pass
match foo:
case 1:
y = 0
case (1):
y = 1
case ( # comment
1
):
y = 1
case (
# comment
1
):
y = 1
case (
1 # comment
):
y = 1
case (
1
# comment
):
y = 1
```

## Output
Expand Down Expand Up @@ -434,6 +459,31 @@ match foo:
"b",
]:
pass
match foo:
case 1:
y = 0
case (1):
y = 1
case ( # comment
1
):
y = 1
case (
# comment
1
):
y = 1
case (
1 # comment
):
y = 1
case (
1
# comment
):
y = 1
```


Expand Down
Loading

0 comments on commit 7c7990c

Please sign in to comment.