mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Format PatternMatchOr
(#6905)
This commit is contained in:
parent
30ebf7fc86
commit
99f4c6886e
7 changed files with 142 additions and 913 deletions
|
@ -1,15 +1,13 @@
|
|||
use thiserror::Error;
|
||||
|
||||
use ruff_formatter::format_element::tag;
|
||||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::{format, FormatError, Formatted, PrintError, Printed, SourceCode};
|
||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||
use ruff_python_ast::node::AstNode;
|
||||
use ruff_python_ast::Mod;
|
||||
use ruff_python_index::{CommentRanges, CommentRangesBuilder};
|
||||
use ruff_python_parser::lexer::{lex, LexicalError};
|
||||
use ruff_python_parser::{parse_tokens, Mode, ParseError};
|
||||
use ruff_source_file::Locator;
|
||||
use ruff_text_size::TextLen;
|
||||
|
||||
use crate::comments::{
|
||||
dangling_comments, leading_comments, trailing_comments, Comments, SourceComment,
|
||||
|
@ -178,45 +176,6 @@ pub fn pretty_comments(formatted: &Formatted<PyFormatContext>, source: &str) ->
|
|||
)
|
||||
}
|
||||
|
||||
pub(crate) struct NotYetImplementedCustomText<'a> {
|
||||
text: &'static str,
|
||||
node: AnyNodeRef<'a>,
|
||||
}
|
||||
|
||||
/// Formats a placeholder for nodes that have not yet been implemented
|
||||
pub(crate) fn not_yet_implemented_custom_text<'a, T>(
|
||||
text: &'static str,
|
||||
node: T,
|
||||
) -> NotYetImplementedCustomText<'a>
|
||||
where
|
||||
T: Into<AnyNodeRef<'a>>,
|
||||
{
|
||||
NotYetImplementedCustomText {
|
||||
text,
|
||||
node: node.into(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for NotYetImplementedCustomText<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
||||
tag::VerbatimKind::Verbatim {
|
||||
length: self.text.text_len(),
|
||||
},
|
||||
)));
|
||||
|
||||
text(self.text).fmt(f)?;
|
||||
|
||||
f.write_element(FormatElement::Tag(Tag::EndVerbatim));
|
||||
|
||||
f.context()
|
||||
.comments()
|
||||
.mark_verbatim_node_comments_formatted(self.node);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::Path;
|
||||
|
|
|
@ -2,8 +2,10 @@ use ruff_formatter::write;
|
|||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use ruff_python_ast::PatternMatchOr;
|
||||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::not_yet_implemented_custom_text;
|
||||
use crate::comments::leading_comments;
|
||||
use crate::expression::parentheses::{
|
||||
in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -11,13 +13,35 @@ pub struct FormatPatternMatchOr;
|
|||
|
||||
impl FormatNodeRule<PatternMatchOr> for FormatPatternMatchOr {
|
||||
fn fmt_fields(&self, item: &PatternMatchOr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
"NOT_YET_IMPLEMENTED_PatternMatchOf | (y)",
|
||||
item
|
||||
)]
|
||||
)
|
||||
let PatternMatchOr { range: _, patterns } = item;
|
||||
let inner = format_with(|f: &mut PyFormatter| {
|
||||
let mut patterns = patterns.iter();
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let Some(first) = patterns.next() else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
first.format().fmt(f)?;
|
||||
|
||||
for pattern in patterns {
|
||||
let leading_value_comments = comments.leading(pattern);
|
||||
// Format the expressions leading comments **before** the operator
|
||||
if leading_value_comments.is_empty() {
|
||||
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
[hard_line_break(), leading_comments(leading_value_comments)]
|
||||
)?;
|
||||
}
|
||||
write!(f, [text("|"), space(), pattern.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
inner.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue