Handle end-of-line comments on excepthandler and alias (#3196)

This commit is contained in:
Charlie Marsh 2023-02-23 22:35:39 -05:00 committed by GitHub
parent 6eaacf96be
commit 159422071e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 27 deletions

View file

@ -2,9 +2,11 @@ use ruff_formatter::prelude::*;
use ruff_formatter::write; use ruff_formatter::write;
use ruff_text_size::TextSize; use ruff_text_size::TextSize;
use crate::builders::literal;
use crate::context::ASTFormatContext; use crate::context::ASTFormatContext;
use crate::cst::Alias; use crate::cst::Alias;
use crate::shared_traits::AsFormat; use crate::shared_traits::AsFormat;
use crate::trivia::{Relationship, TriviaKind};
pub struct FormatAlias<'a> { pub struct FormatAlias<'a> {
item: &'a Alias, item: &'a Alias,
@ -28,6 +30,25 @@ impl Format<ASTFormatContext<'_>> for FormatAlias<'_> {
write!(f, [dynamic_text(asname, TextSize::default())])?; write!(f, [dynamic_text(asname, TextSize::default())])?;
} }
// Format any end-of-line comments.
let mut first = true;
for range in alias.trivia.iter().filter_map(|trivia| {
if matches!(trivia.relationship, Relationship::Trailing) {
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
Some(range)
} else {
None
}
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}
Ok(()) Ok(())
} }
} }

View file

@ -2,10 +2,12 @@ use ruff_formatter::prelude::*;
use ruff_formatter::write; use ruff_formatter::write;
use ruff_text_size::TextSize; use ruff_text_size::TextSize;
use crate::builders::literal;
use crate::context::ASTFormatContext; use crate::context::ASTFormatContext;
use crate::cst::{Excepthandler, ExcepthandlerKind}; use crate::cst::{Excepthandler, ExcepthandlerKind};
use crate::format::builders::block; use crate::format::builders::block;
use crate::shared_traits::AsFormat; use crate::shared_traits::AsFormat;
use crate::trivia::{Relationship, TriviaKind};
pub struct FormatExcepthandler<'a> { pub struct FormatExcepthandler<'a> {
item: &'a Excepthandler, item: &'a Excepthandler,
@ -21,7 +23,8 @@ impl AsFormat<ASTFormatContext<'_>> for Excepthandler {
impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> { impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> { fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let ExcepthandlerKind::ExceptHandler { type_, name, body } = &self.item.node; let excepthandler = self.item;
let ExcepthandlerKind::ExceptHandler { type_, name, body } = &excepthandler.node;
write!(f, [text("except")])?; write!(f, [text("except")])?;
if let Some(type_) = &type_ { if let Some(type_) = &type_ {
@ -39,6 +42,26 @@ impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
} }
} }
write!(f, [text(":")])?; write!(f, [text(":")])?;
// Format any end-of-line comments.
let mut first = true;
for range in excepthandler.trivia.iter().filter_map(|trivia| {
if matches!(trivia.relationship, Relationship::Trailing) {
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
Some(range)
} else {
None
}
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}
write!(f, [block_indent(&block(body))])?; write!(f, [block_indent(&block(body))])?;
Ok(()) Ok(())

View file

@ -178,17 +178,6 @@ instruction()#comment with bad spacing
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -1,8 +1,8 @@
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent, # NOT DRY
+ MyLovelyCompanyTeamProjectComponent,
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent as component, # DRY
+ MyLovelyCompanyTeamProjectComponent as component,
)
# Please keep __all__ alphabetized within each category.
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
"Callable", "Callable",
"ClassVar", "ClassVar",
@ -333,10 +322,10 @@ instruction()#comment with bad spacing
```py ```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, MyLovelyCompanyTeamProjectComponent, # NOT DRY
) )
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, MyLovelyCompanyTeamProjectComponent as component, # DRY
) )
# Please keep __all__ alphabetized within each category. # Please keep __all__ alphabetized within each category.

View file

@ -107,17 +107,6 @@ def foo3(list_a, list_b):
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -1,8 +1,8 @@
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent, # NOT DRY
+ MyLovelyCompanyTeamProjectComponent,
)
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
- MyLovelyCompanyTeamProjectComponent as component, # DRY
+ MyLovelyCompanyTeamProjectComponent as component,
)
@@ -10,39 +10,50 @@ @@ -10,39 +10,50 @@
@pytest.mark.parametrize( @pytest.mark.parametrize(
("post_data", "message"), ("post_data", "message"),
@ -249,10 +238,10 @@ def foo3(list_a, list_b):
```py ```py
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent, MyLovelyCompanyTeamProjectComponent, # NOT DRY
) )
from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import (
MyLovelyCompanyTeamProjectComponent as component, MyLovelyCompanyTeamProjectComponent as component, # DRY
) )