mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Range formatting: Fix invalid syntax after parenthesizing expression (#9751)
This commit is contained in:
parent
50bfbcf568
commit
4f7fb566f0
24 changed files with 351 additions and 212 deletions
|
@ -430,31 +430,25 @@ pub(crate) struct FormatNormalizedComment<'a> {
|
|||
|
||||
impl Format<PyFormatContext<'_>> for FormatNormalizedComment<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext>) -> FormatResult<()> {
|
||||
let write_sourcemap = f.options().source_map_generation().is_enabled();
|
||||
|
||||
write_sourcemap
|
||||
.then_some(source_position(self.range.start()))
|
||||
.fmt(f)?;
|
||||
|
||||
match self.comment {
|
||||
Cow::Borrowed(borrowed) => {
|
||||
source_text_slice(TextRange::at(self.range.start(), borrowed.text_len())).fmt(f)?;
|
||||
|
||||
// Write the end position if the borrowed comment is shorter than the original comment
|
||||
// So that we still can map back the end of a comment to the formatted code.
|
||||
if f.options().source_map_generation().is_enabled()
|
||||
&& self.range.len() != borrowed.text_len()
|
||||
{
|
||||
source_position(self.range.end()).fmt(f)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Cow::Owned(ref owned) => {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text(owned, Some(self.range.start())),
|
||||
source_position(self.range.end())
|
||||
]
|
||||
)
|
||||
text(owned).fmt(f)?;
|
||||
}
|
||||
}
|
||||
|
||||
write_sourcemap
|
||||
.then_some(source_position(self.range.end()))
|
||||
.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue