mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
Format PatternMatchStar
(#6653)
This commit is contained in:
parent
4889b84338
commit
205d234856
7 changed files with 194 additions and 92 deletions
|
@ -206,6 +206,7 @@ fn handle_enclosed_comment<'a>(
|
|||
}
|
||||
AnyNodeRef::WithItem(_) => handle_with_item_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::StmtClassDef(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).
|
||||
///
|
||||
/// 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 crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||
use crate::comments::{dangling_comments, SourceComment};
|
||||
use crate::AsFormat;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchStar;
|
||||
|
||||
impl FormatNodeRule<PatternMatchStar> for FormatPatternMatchStar {
|
||||
fn fmt_fields(&self, item: &PatternMatchStar, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
"*NOT_YET_IMPLEMENTED_PatternMatchStar",
|
||||
item
|
||||
)]
|
||||
)
|
||||
let PatternMatchStar { name, .. } = item;
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling = comments.dangling(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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue