Range formatting: Fix invalid syntax after parenthesizing expression (#9751)

This commit is contained in:
Micha Reiser 2024-02-02 17:56:25 +01:00 committed by GitHub
parent 50bfbcf568
commit 4f7fb566f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 351 additions and 212 deletions

View file

@ -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)
}
}