mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Refactor magic trailing comma (#5339)
## Summary This is small refactoring to reuse the code that detects the magic trailing comma across functions. I make this change now to avoid copying code in a later PR. @MichaReiser is planning on making a larger refactoring later that integrates with the join nodes builder ## Test Plan No functional changes. The magic trailing comma behaviour is checked by the fixtures.
This commit is contained in:
parent
cb580f960f
commit
4b65446de6
4 changed files with 28 additions and 42 deletions
|
@ -1,8 +1,9 @@
|
|||
use crate::context::NodeLevel;
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{lines_after, skip_trailing_trivia};
|
||||
use crate::trivia::{first_non_trivia_token, lines_after, skip_trailing_trivia, Token, TokenKind};
|
||||
use crate::USE_MAGIC_TRAILING_COMMA;
|
||||
use ruff_formatter::write;
|
||||
use ruff_text_size::TextSize;
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
use rustpython_parser::ast::Ranged;
|
||||
|
||||
/// Provides Python specific extensions to [`Formatter`].
|
||||
|
@ -145,6 +146,17 @@ impl<'fmt, 'ast, 'buf> JoinNodesBuilder<'fmt, 'ast, 'buf> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn use_magic_trailing_comma(f: &mut PyFormatter, range: TextRange) -> bool {
|
||||
USE_MAGIC_TRAILING_COMMA
|
||||
&& matches!(
|
||||
first_non_trivia_token(range.end(), f.context().contents()),
|
||||
Some(Token {
|
||||
kind: TokenKind::Comma,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::comments::Comments;
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
use crate::builders::use_magic_trailing_comma;
|
||||
use crate::comments::{dangling_node_comments, leading_comments, Comments};
|
||||
use crate::context::PyFormatContext;
|
||||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::Token;
|
||||
use crate::trivia::{first_non_trivia_token, TokenKind};
|
||||
use crate::USE_MAGIC_TRAILING_COMMA;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::format_args;
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
|
@ -69,14 +67,7 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
|
|||
}
|
||||
[.., last] => last,
|
||||
};
|
||||
let magic_trailing_comma = USE_MAGIC_TRAILING_COMMA
|
||||
&& matches!(
|
||||
first_non_trivia_token(last.range().end(), f.context().contents()),
|
||||
Some(Token {
|
||||
kind: TokenKind::Comma,
|
||||
..
|
||||
})
|
||||
);
|
||||
let magic_trailing_comma = use_magic_trailing_comma(f, last.range());
|
||||
|
||||
debug_assert_eq!(keys.len(), values.len());
|
||||
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use crate::builders::use_magic_trailing_comma;
|
||||
use crate::comments::{dangling_node_comments, Comments};
|
||||
use crate::context::PyFormatContext;
|
||||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::trivia::Token;
|
||||
use crate::trivia::{first_non_trivia_token, TokenKind};
|
||||
use crate::{AsFormat, FormatNodeRule, FormattedIterExt, PyFormatter, USE_MAGIC_TRAILING_COMMA};
|
||||
use crate::{AsFormat, FormatNodeRule, FormattedIterExt, PyFormatter};
|
||||
use ruff_formatter::formatter::Formatter;
|
||||
use ruff_formatter::prelude::{
|
||||
block_indent, group, if_group_breaks, soft_block_indent, soft_line_break_or_space, text,
|
||||
|
@ -88,14 +87,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
|||
[.., last] => last,
|
||||
};
|
||||
|
||||
let magic_trailing_comma = USE_MAGIC_TRAILING_COMMA
|
||||
&& matches!(
|
||||
first_non_trivia_token(last.range().end(), f.context().contents()),
|
||||
Some(Token {
|
||||
kind: TokenKind::Comma,
|
||||
..
|
||||
})
|
||||
);
|
||||
let magic_trailing_comma = use_magic_trailing_comma(f, last.range());
|
||||
|
||||
if magic_trailing_comma {
|
||||
// A magic trailing comma forces us to print in expanded mode since we have more than
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::builders::use_magic_trailing_comma;
|
||||
use crate::comments::trailing_comments;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{first_non_trivia_token, SimpleTokenizer, Token, TokenKind};
|
||||
use crate::USE_MAGIC_TRAILING_COMMA;
|
||||
use crate::trivia::{SimpleTokenizer, TokenKind};
|
||||
use ruff_formatter::{format_args, write};
|
||||
use ruff_text_size::TextRange;
|
||||
use rustpython_parser::ast::{Expr, Keyword, Ranged, StmtClassDef};
|
||||
|
@ -115,22 +115,13 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
|
|||
|
||||
if_group_breaks(&text(",")).fmt(f)?;
|
||||
|
||||
if USE_MAGIC_TRAILING_COMMA {
|
||||
let last_end = keywords
|
||||
.last()
|
||||
.map(Keyword::end)
|
||||
.or_else(|| bases.last().map(Expr::end))
|
||||
.unwrap();
|
||||
|
||||
if matches!(
|
||||
first_non_trivia_token(last_end, f.context().contents()),
|
||||
Some(Token {
|
||||
kind: TokenKind::Comma,
|
||||
..
|
||||
})
|
||||
) {
|
||||
hard_line_break().fmt(f)?;
|
||||
}
|
||||
let last = keywords
|
||||
.last()
|
||||
.map(Keyword::range)
|
||||
.or_else(|| bases.last().map(Expr::range))
|
||||
.unwrap();
|
||||
if use_magic_trailing_comma(f, last) {
|
||||
hard_line_break().fmt(f)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue