mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Format PatternMatchSequence
(#6676)
This commit is contained in:
parent
c34a342ab4
commit
94f5f18ddb
9 changed files with 353 additions and 143 deletions
|
@ -1,19 +1,53 @@
|
|||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_formatter::prelude::format_with;
|
||||
use ruff_formatter::{Format, FormatResult};
|
||||
use ruff_python_ast::PatternMatchSequence;
|
||||
|
||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||
use crate::builders::PyFormatterExtensions;
|
||||
use crate::expression::parentheses::{empty_parenthesized, optional_parentheses, parenthesized};
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchSequence;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum SequenceType {
|
||||
Tuple,
|
||||
TupleNoParens,
|
||||
List,
|
||||
}
|
||||
|
||||
impl FormatNodeRule<PatternMatchSequence> for FormatPatternMatchSequence {
|
||||
fn fmt_fields(&self, item: &PatternMatchSequence, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
"[NOT_YET_IMPLEMENTED_PatternMatchSequence, 2]",
|
||||
item
|
||||
)]
|
||||
)
|
||||
let PatternMatchSequence { patterns, range } = item;
|
||||
let sequence_type = match &f.context().source()[*range].chars().next() {
|
||||
Some('(') => SequenceType::Tuple,
|
||||
Some('[') => SequenceType::List,
|
||||
_ => SequenceType::TupleNoParens,
|
||||
};
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling = comments.dangling(item);
|
||||
if patterns.is_empty() {
|
||||
return match sequence_type {
|
||||
SequenceType::Tuple => empty_parenthesized("(", dangling, ")").fmt(f),
|
||||
SequenceType::List => empty_parenthesized("[", dangling, "]").fmt(f),
|
||||
SequenceType::TupleNoParens => {
|
||||
unreachable!("If empty, it should be either tuple or list")
|
||||
}
|
||||
};
|
||||
}
|
||||
let items = format_with(|f| {
|
||||
f.join_comma_separated(range.end())
|
||||
.nodes(patterns.iter())
|
||||
.finish()
|
||||
});
|
||||
match sequence_type {
|
||||
SequenceType::Tuple => parenthesized("(", &items, ")")
|
||||
.with_dangling_comments(dangling)
|
||||
.fmt(f),
|
||||
SequenceType::List => parenthesized("[", &items, "]")
|
||||
.with_dangling_comments(dangling)
|
||||
.fmt(f),
|
||||
SequenceType::TupleNoParens => optional_parentheses(&items).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue