mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-25 14:03:51 +00:00
Accept any Into<AnyNodeRef>
as Comments
arguments (#5205)
This commit is contained in:
parent
6f7d3cc798
commit
b369288833
9 changed files with 73 additions and 41 deletions
|
@ -37,7 +37,7 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
|||
) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let dangling_comments = comments.dangling_comments(item.into());
|
||||
let dangling_comments = comments.dangling_comments(item);
|
||||
let trailing_definition_comments_start =
|
||||
dangling_comments.partition_point(|comment| comment.position().is_own_line());
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
|||
} = current_statement;
|
||||
|
||||
let first_statement = body.first().ok_or(FormatError::SyntaxError)?;
|
||||
let trailing = comments.dangling_comments(current_statement.into());
|
||||
let trailing = comments.dangling_comments(current_statement);
|
||||
|
||||
let trailing_if_comments_end = trailing
|
||||
.partition_point(|comment| comment.slice().start() < first_statement.start());
|
||||
|
@ -32,7 +32,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
|||
trailing.split_at(trailing_if_comments_end);
|
||||
|
||||
if current.is_elif() {
|
||||
let elif_leading = comments.leading_comments(current_statement.into());
|
||||
let elif_leading = comments.leading_comments(current_statement);
|
||||
// Manually format the leading comments because the formatting bypasses `NodeRule::fmt`
|
||||
write!(
|
||||
f,
|
||||
|
|
|
@ -96,13 +96,12 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
// the leading comment. This is why the suite handling counts the lines before the
|
||||
// start of the next statement or before the first leading comments for compound statements.
|
||||
let separator = format_with(|f| {
|
||||
let start = if let Some(first_leading) =
|
||||
comments.leading_comments(statement.into()).first()
|
||||
{
|
||||
first_leading.slice().start()
|
||||
} else {
|
||||
statement.start()
|
||||
};
|
||||
let start =
|
||||
if let Some(first_leading) = comments.leading_comments(statement).first() {
|
||||
first_leading.slice().start()
|
||||
} else {
|
||||
statement.start()
|
||||
};
|
||||
|
||||
match lines_before(start, source) {
|
||||
0 | 1 => hard_line_break().fmt(f),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue