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:
Charlie Marsh 2023-08-25 00:06:56 -04:00 committed by GitHub
parent f754ad5898
commit 59e70896c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 186 additions and 32 deletions

View file

@ -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!(