mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
Fix \r
and \r\n
handling in t- and f-string debug texts (#18673)
This commit is contained in:
parent
5e02d839d5
commit
8237d4670c
6 changed files with 79 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use ruff_formatter::{Buffer, RemoveSoftLinesBuffer, format_args, write};
|
||||
use ruff_formatter::{Buffer, FormatOptions as _, RemoveSoftLinesBuffer, format_args, write};
|
||||
use ruff_python_ast::{
|
||||
AnyStringFlags, ConversionFlag, Expr, InterpolatedElement, InterpolatedStringElement,
|
||||
InterpolatedStringLiteralElement, StringFlags,
|
||||
|
@ -178,9 +178,9 @@ impl Format<PyFormatContext<'_>> for FormatInterpolatedElement<'_> {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text(&debug_text.leading),
|
||||
NormalizedDebugText(&debug_text.leading),
|
||||
verbatim_text(&**expression),
|
||||
text(&debug_text.trailing),
|
||||
NormalizedDebugText(&debug_text.trailing),
|
||||
]
|
||||
)?;
|
||||
|
||||
|
@ -316,3 +316,18 @@ fn needs_bracket_spacing(expr: &Expr, context: &PyFormatContext) -> bool {
|
|||
Expr::Dict(_) | Expr::DictComp(_) | Expr::Set(_) | Expr::SetComp(_)
|
||||
)
|
||||
}
|
||||
|
||||
struct NormalizedDebugText<'a>(&'a str);
|
||||
|
||||
impl Format<PyFormatContext<'_>> for NormalizedDebugText<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let normalized = normalize_newlines(self.0, ['\r']);
|
||||
|
||||
f.write_element(FormatElement::Text {
|
||||
text_width: TextWidth::from_text(&normalized, f.options().indent_width()),
|
||||
text: normalized.into_owned().into_boxed_str(),
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue