mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-24 04:14:44 +00:00
Fix formatting of comments between function and arguments (#6826)
## Summary We now format comments between a function and its arguments as dangling. Like with other strange placements, I've biased towards preserving the existing formatting, rather than attempting to reorder the comments. Closes https://github.com/astral-sh/ruff/issues/6818. ## Test Plan `cargo test` Before: | project | similarity index | |--------------|------------------| | cpython | 0.76050 | | django | 0.99820 | | transformers | 0.99800 | | twine | 0.99876 | | typeshed | 0.99953 | | warehouse | 0.99615 | | zulip | 0.99729 | After: | project | similarity index | |--------------|------------------| | cpython | 0.76050 | | django | 0.99820 | | transformers | 0.99800 | | twine | 0.99876 | | typeshed | 0.99953 | | warehouse | 0.99615 | | zulip | 0.99729 |
This commit is contained in:
parent
f754ad5898
commit
59e70896c0
6 changed files with 186 additions and 32 deletions
|
@ -5,7 +5,7 @@ use ruff_formatter::{format_args, write, FormatError, SourceCode};
|
|||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::comments::{CommentLinePosition, SourceComment};
|
||||
use crate::context::NodeLevel;
|
||||
use crate::prelude::*;
|
||||
|
||||
|
@ -206,8 +206,15 @@ impl Format<PyFormatContext<'_>> for FormatDanglingComments<'_> {
|
|||
.iter()
|
||||
.filter(|comment| comment.is_unformatted())
|
||||
{
|
||||
if first && comment.line_position().is_end_of_line() {
|
||||
write!(f, [space(), space()])?;
|
||||
if first {
|
||||
match comment.line_position {
|
||||
CommentLinePosition::OwnLine => {
|
||||
write!(f, [hard_line_break()])?;
|
||||
}
|
||||
CommentLinePosition::EndOfLine => {
|
||||
write!(f, [space(), space()])?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
write!(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue