mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
Format PatternMatchStar
(#6653)
This commit is contained in:
parent
4889b84338
commit
205d234856
7 changed files with 194 additions and 92 deletions
|
@ -237,7 +237,6 @@ match foo:
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
match foo:
|
match foo:
|
||||||
case 1:
|
case 1:
|
||||||
y = 0
|
y = 0
|
||||||
|
@ -263,3 +262,40 @@ match foo:
|
||||||
# comment
|
# comment
|
||||||
):
|
):
|
||||||
y = 1
|
y = 1
|
||||||
|
|
||||||
|
match foo:
|
||||||
|
case [1, 2, *rest]:
|
||||||
|
pass
|
||||||
|
case [1, 2, *_]:
|
||||||
|
pass
|
||||||
|
case [*rest, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [*_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
*rest,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [1, 2, * # comment
|
||||||
|
rest]:
|
||||||
|
pass
|
||||||
|
case [1, 2, * # comment
|
||||||
|
_]:
|
||||||
|
pass
|
||||||
|
case [* # comment
|
||||||
|
rest, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # comment
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # end of line
|
||||||
|
# own line
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # end of line
|
||||||
|
# own line
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
|
@ -206,6 +206,7 @@ fn handle_enclosed_comment<'a>(
|
||||||
}
|
}
|
||||||
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
|
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
|
||||||
AnyNodeRef::PatternMatchAs(_) => handle_pattern_match_as_comment(comment, locator),
|
AnyNodeRef::PatternMatchAs(_) => handle_pattern_match_as_comment(comment, locator),
|
||||||
|
AnyNodeRef::PatternMatchStar(_) => handle_pattern_match_star_comment(comment),
|
||||||
AnyNodeRef::StmtFunctionDef(_) => handle_leading_function_with_decorators_comment(comment),
|
AnyNodeRef::StmtFunctionDef(_) => handle_leading_function_with_decorators_comment(comment),
|
||||||
AnyNodeRef::StmtClassDef(class_def) => {
|
AnyNodeRef::StmtClassDef(class_def) => {
|
||||||
handle_leading_class_with_decorators_comment(comment, class_def)
|
handle_leading_class_with_decorators_comment(comment, class_def)
|
||||||
|
@ -1187,6 +1188,20 @@ fn handle_pattern_match_as_comment<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles dangling comments between the `*` token and identifier of a pattern match star:
|
||||||
|
///
|
||||||
|
/// ```python
|
||||||
|
/// case [
|
||||||
|
/// ...,
|
||||||
|
/// * # dangling end of line comment
|
||||||
|
/// # dangling end of line comment
|
||||||
|
/// rest,
|
||||||
|
/// ]: ...
|
||||||
|
/// ```
|
||||||
|
fn handle_pattern_match_star_comment(comment: DecoratedComment) -> CommentPlacement {
|
||||||
|
CommentPlacement::dangling(comment.enclosing_node(), comment)
|
||||||
|
}
|
||||||
|
|
||||||
/// Handles comments around the `:=` token in a named expression (walrus operator).
|
/// Handles comments around the `:=` token in a named expression (walrus operator).
|
||||||
///
|
///
|
||||||
/// For example, here, `# 1` and `# 2` will be marked as dangling comments on the named expression,
|
/// For example, here, `# 1` and `# 2` will be marked as dangling comments on the named expression,
|
||||||
|
|
|
@ -1,19 +1,34 @@
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{prelude::text, write, Buffer, FormatResult};
|
||||||
use ruff_python_ast::PatternMatchStar;
|
use ruff_python_ast::PatternMatchStar;
|
||||||
|
|
||||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
use crate::comments::{dangling_comments, SourceComment};
|
||||||
|
use crate::AsFormat;
|
||||||
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct FormatPatternMatchStar;
|
pub struct FormatPatternMatchStar;
|
||||||
|
|
||||||
impl FormatNodeRule<PatternMatchStar> for FormatPatternMatchStar {
|
impl FormatNodeRule<PatternMatchStar> for FormatPatternMatchStar {
|
||||||
fn fmt_fields(&self, item: &PatternMatchStar, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &PatternMatchStar, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
write!(
|
let PatternMatchStar { name, .. } = item;
|
||||||
f,
|
|
||||||
[not_yet_implemented_custom_text(
|
let comments = f.context().comments().clone();
|
||||||
"*NOT_YET_IMPLEMENTED_PatternMatchStar",
|
let dangling = comments.dangling(item);
|
||||||
item
|
|
||||||
)]
|
write!(f, [text("*"), dangling_comments(dangling)])?;
|
||||||
)
|
|
||||||
|
match name {
|
||||||
|
Some(name) => write!(f, [name.format()]),
|
||||||
|
None => write!(f, [text("_")]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fmt_dangling_comments(
|
||||||
|
&self,
|
||||||
|
_dangling_comments: &[SourceComment],
|
||||||
|
_f: &mut PyFormatter,
|
||||||
|
) -> FormatResult<()> {
|
||||||
|
// Handled by `fmt_fields`
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,8 +179,7 @@ match x:
|
||||||
y = 0
|
y = 0
|
||||||
# case black_check_sequence_then_mapping
|
# case black_check_sequence_then_mapping
|
||||||
match x:
|
match x:
|
||||||
- case [*_]:
|
case [*_]:
|
||||||
+ case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
return "seq"
|
return "seq"
|
||||||
- case {}:
|
- case {}:
|
||||||
+ case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
+ case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
||||||
|
@ -204,7 +203,7 @@ match x:
|
||||||
x = True
|
x = True
|
||||||
# case black_test_patma_154
|
# case black_test_patma_154
|
||||||
match x:
|
match x:
|
||||||
@@ -54,15 +54,15 @@
|
@@ -54,11 +54,11 @@
|
||||||
y = 0
|
y = 0
|
||||||
# case black_test_patma_134
|
# case black_test_patma_134
|
||||||
match x:
|
match x:
|
||||||
|
@ -219,12 +218,7 @@ match x:
|
||||||
y = 2
|
y = 2
|
||||||
# case black_test_patma_185
|
# case black_test_patma_185
|
||||||
match Seq():
|
match Seq():
|
||||||
- case [*_]:
|
@@ -72,7 +72,7 @@
|
||||||
+ case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
y = 0
|
|
||||||
# case black_test_patma_063
|
|
||||||
match x:
|
|
||||||
@@ -72,11 +72,11 @@
|
|
||||||
y = 1
|
y = 1
|
||||||
# case black_test_patma_248
|
# case black_test_patma_248
|
||||||
match x:
|
match x:
|
||||||
|
@ -233,26 +227,7 @@ match x:
|
||||||
y = bar
|
y = bar
|
||||||
# case black_test_patma_019
|
# case black_test_patma_019
|
||||||
match (0, 1, 2):
|
match (0, 1, 2):
|
||||||
- case [0, 1, *x, 2]:
|
@@ -132,13 +132,13 @@
|
||||||
+ case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]:
|
|
||||||
y = 0
|
|
||||||
# case black_test_patma_052
|
|
||||||
match x:
|
|
||||||
@@ -88,7 +88,7 @@
|
|
||||||
y = 2
|
|
||||||
# case black_test_patma_191
|
|
||||||
match w:
|
|
||||||
- case [x, y, *_]:
|
|
||||||
+ case [x, y, *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
z = 0
|
|
||||||
# case black_test_patma_110
|
|
||||||
match x:
|
|
||||||
@@ -128,17 +128,17 @@
|
|
||||||
y = 0
|
|
||||||
# case black_test_patma_189
|
|
||||||
match w:
|
|
||||||
- case [x, y, *rest]:
|
|
||||||
+ case [x, y, *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
z = 0
|
z = 0
|
||||||
# case black_test_patma_042
|
# case black_test_patma_042
|
||||||
match x:
|
match x:
|
||||||
|
@ -300,7 +275,7 @@ match x:
|
||||||
y = 0
|
y = 0
|
||||||
# case black_check_sequence_then_mapping
|
# case black_check_sequence_then_mapping
|
||||||
match x:
|
match x:
|
||||||
case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case [*_]:
|
||||||
return "seq"
|
return "seq"
|
||||||
case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
||||||
return "map"
|
return "map"
|
||||||
|
@ -338,7 +313,7 @@ match x:
|
||||||
y = 2
|
y = 2
|
||||||
# case black_test_patma_185
|
# case black_test_patma_185
|
||||||
match Seq():
|
match Seq():
|
||||||
case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case [*_]:
|
||||||
y = 0
|
y = 0
|
||||||
# case black_test_patma_063
|
# case black_test_patma_063
|
||||||
match x:
|
match x:
|
||||||
|
@ -352,7 +327,7 @@ match x:
|
||||||
y = bar
|
y = bar
|
||||||
# case black_test_patma_019
|
# case black_test_patma_019
|
||||||
match (0, 1, 2):
|
match (0, 1, 2):
|
||||||
case [0, 1, *NOT_YET_IMPLEMENTED_PatternMatchStar, 2]:
|
case [0, 1, *x, 2]:
|
||||||
y = 0
|
y = 0
|
||||||
# case black_test_patma_052
|
# case black_test_patma_052
|
||||||
match x:
|
match x:
|
||||||
|
@ -364,7 +339,7 @@ match x:
|
||||||
y = 2
|
y = 2
|
||||||
# case black_test_patma_191
|
# case black_test_patma_191
|
||||||
match w:
|
match w:
|
||||||
case [x, y, *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case [x, y, *_]:
|
||||||
z = 0
|
z = 0
|
||||||
# case black_test_patma_110
|
# case black_test_patma_110
|
||||||
match x:
|
match x:
|
||||||
|
@ -404,7 +379,7 @@ match x:
|
||||||
y = 0
|
y = 0
|
||||||
# case black_test_patma_189
|
# case black_test_patma_189
|
||||||
match w:
|
match w:
|
||||||
case [x, y, *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case [x, y, *rest]:
|
||||||
z = 0
|
z = 0
|
||||||
# case black_test_patma_042
|
# case black_test_patma_042
|
||||||
match x:
|
match x:
|
||||||
|
|
|
@ -161,7 +161,7 @@ match bar1:
|
||||||
...
|
...
|
||||||
case another:
|
case another:
|
||||||
...
|
...
|
||||||
@@ -32,23 +32,32 @@
|
@@ -32,14 +32,23 @@
|
||||||
match maybe, multiple:
|
match maybe, multiple:
|
||||||
case perhaps, 5:
|
case perhaps, 5:
|
||||||
pass
|
pass
|
||||||
|
@ -188,11 +188,9 @@ match bar1:
|
||||||
pass
|
pass
|
||||||
case _:
|
case _:
|
||||||
pass
|
pass
|
||||||
|
@@ -48,7 +57,7 @@
|
||||||
|
|
||||||
match a, *b, c:
|
match a, *b, c:
|
||||||
- case [*_]:
|
case [*_]:
|
||||||
+ case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
assert "seq" == _
|
assert "seq" == _
|
||||||
- case {}:
|
- case {}:
|
||||||
+ case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
+ case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
||||||
|
@ -213,13 +211,7 @@ match bar1:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
case [a as match]:
|
case [a as match]:
|
||||||
@@ -80,17 +84,14 @@
|
@@ -85,12 +89,9 @@
|
||||||
|
|
||||||
|
|
||||||
match a, *b(), c:
|
|
||||||
- case d, *f, g:
|
|
||||||
+ case d, *NOT_YET_IMPLEMENTED_PatternMatchStar, g:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
match something:
|
match something:
|
||||||
|
@ -234,17 +226,12 @@ match bar1:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@@ -101,19 +102,22 @@
|
@@ -101,19 +102,17 @@
|
||||||
case 2 as b, 3 as c:
|
case 2 as b, 3 as c:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
- case 4 as d, (5 as e), (6 | 7 as g), *h:
|
- case 4 as d, (5 as e), (6 | 7 as g), *h:
|
||||||
+ case (
|
+ case 4 as d, 5 as e, NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, *h:
|
||||||
+ 4 as d,
|
|
||||||
+ 5 as e,
|
|
||||||
+ NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g,
|
|
||||||
+ *NOT_YET_IMPLEMENTED_PatternMatchStar,
|
|
||||||
+ ):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -324,7 +311,7 @@ match (
|
||||||
|
|
||||||
|
|
||||||
match a, *b, c:
|
match a, *b, c:
|
||||||
case [*NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case [*_]:
|
||||||
assert "seq" == _
|
assert "seq" == _
|
||||||
case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
case {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}:
|
||||||
assert "map" == b
|
assert "map" == b
|
||||||
|
@ -353,7 +340,7 @@ match match:
|
||||||
|
|
||||||
|
|
||||||
match a, *b(), c:
|
match a, *b(), c:
|
||||||
case d, *NOT_YET_IMPLEMENTED_PatternMatchStar, g:
|
case d, *f, g:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -371,12 +358,7 @@ match something:
|
||||||
case 2 as b, 3 as c:
|
case 2 as b, 3 as c:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
case (
|
case 4 as d, 5 as e, NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g, *h:
|
||||||
4 as d,
|
|
||||||
5 as e,
|
|
||||||
NOT_YET_IMPLEMENTED_PatternMatchOf | (y) as g,
|
|
||||||
*NOT_YET_IMPLEMENTED_PatternMatchStar,
|
|
||||||
):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -104,23 +104,7 @@ def where_is(point):
|
||||||
```diff
|
```diff
|
||||||
--- Black
|
--- Black
|
||||||
+++ Ruff
|
+++ Ruff
|
||||||
@@ -23,7 +23,7 @@
|
@@ -39,18 +39,18 @@
|
||||||
# The rest of your commands go here
|
|
||||||
|
|
||||||
match command.split():
|
|
||||||
- case ["drop", *objects]:
|
|
||||||
+ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
for obj in objects:
|
|
||||||
character.drop(obj, current_room)
|
|
||||||
# The rest of your commands go here
|
|
||||||
@@ -33,24 +33,24 @@
|
|
||||||
pass
|
|
||||||
case ["go", direction]:
|
|
||||||
print("Going:", direction)
|
|
||||||
- case ["drop", *objects]:
|
|
||||||
+ case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
|
||||||
print("Dropping: ", *objects)
|
|
||||||
case _:
|
|
||||||
print(f"Sorry, I couldn't understand {command!r}")
|
print(f"Sorry, I couldn't understand {command!r}")
|
||||||
|
|
||||||
match command.split():
|
match command.split():
|
||||||
|
@ -217,7 +201,7 @@ match command.split():
|
||||||
# The rest of your commands go here
|
# The rest of your commands go here
|
||||||
|
|
||||||
match command.split():
|
match command.split():
|
||||||
case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case ["drop", *objects]:
|
||||||
for obj in objects:
|
for obj in objects:
|
||||||
character.drop(obj, current_room)
|
character.drop(obj, current_room)
|
||||||
# The rest of your commands go here
|
# The rest of your commands go here
|
||||||
|
@ -227,7 +211,7 @@ match command.split():
|
||||||
pass
|
pass
|
||||||
case ["go", direction]:
|
case ["go", direction]:
|
||||||
print("Going:", direction)
|
print("Going:", direction)
|
||||||
case ["drop", *NOT_YET_IMPLEMENTED_PatternMatchStar]:
|
case ["drop", *objects]:
|
||||||
print("Dropping: ", *objects)
|
print("Dropping: ", *objects)
|
||||||
case _:
|
case _:
|
||||||
print(f"Sorry, I couldn't understand {command!r}")
|
print(f"Sorry, I couldn't understand {command!r}")
|
||||||
|
|
|
@ -243,7 +243,6 @@ match foo:
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
match foo:
|
match foo:
|
||||||
case 1:
|
case 1:
|
||||||
y = 0
|
y = 0
|
||||||
|
@ -269,6 +268,43 @@ match foo:
|
||||||
# comment
|
# comment
|
||||||
):
|
):
|
||||||
y = 1
|
y = 1
|
||||||
|
|
||||||
|
match foo:
|
||||||
|
case [1, 2, *rest]:
|
||||||
|
pass
|
||||||
|
case [1, 2, *_]:
|
||||||
|
pass
|
||||||
|
case [*rest, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [*_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
*rest,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [1, 2, * # comment
|
||||||
|
rest]:
|
||||||
|
pass
|
||||||
|
case [1, 2, * # comment
|
||||||
|
_]:
|
||||||
|
pass
|
||||||
|
case [* # comment
|
||||||
|
rest, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # comment
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # end of line
|
||||||
|
# own line
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [* # end of line
|
||||||
|
# own line
|
||||||
|
_, 1, 2]:
|
||||||
|
pass
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
@ -506,7 +542,6 @@ match foo:
|
||||||
]:
|
]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
match foo:
|
match foo:
|
||||||
case 1:
|
case 1:
|
||||||
y = 0
|
y = 0
|
||||||
|
@ -532,6 +567,66 @@ match foo:
|
||||||
# comment
|
# comment
|
||||||
):
|
):
|
||||||
y = 1
|
y = 1
|
||||||
|
|
||||||
|
match foo:
|
||||||
|
case [1, 2, *rest]:
|
||||||
|
pass
|
||||||
|
case [1, 2, *_]:
|
||||||
|
pass
|
||||||
|
case [*rest, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [*_, 1, 2]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
*rest,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
* # comment
|
||||||
|
rest,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
* # comment
|
||||||
|
_,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
* # comment
|
||||||
|
rest,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
* # comment
|
||||||
|
_,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
* # end of line
|
||||||
|
# own line
|
||||||
|
_,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
|
case [
|
||||||
|
* # end of line
|
||||||
|
# own line
|
||||||
|
_,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
]:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue