mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Join implicit concatenated strings when they fit on a line (#13663)
This commit is contained in:
parent
e402e27a09
commit
73ee72b665
50 changed files with 3907 additions and 363 deletions
|
@ -76,14 +76,9 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
|
|||
let quotes = StringQuotes::from(string_kind);
|
||||
write!(f, [string_kind.prefix(), quotes])?;
|
||||
|
||||
f.join()
|
||||
.entries(
|
||||
self.value
|
||||
.elements
|
||||
.iter()
|
||||
.map(|element| FormatFStringElement::new(element, context)),
|
||||
)
|
||||
.finish()?;
|
||||
for element in &self.value.elements {
|
||||
FormatFStringElement::new(element, context).fmt(f)?;
|
||||
}
|
||||
|
||||
// Ending quote
|
||||
quotes.fmt(f)
|
||||
|
@ -98,7 +93,7 @@ pub(crate) struct FStringContext {
|
|||
}
|
||||
|
||||
impl FStringContext {
|
||||
const fn new(flags: AnyStringFlags, layout: FStringLayout) -> Self {
|
||||
pub(crate) const fn new(flags: AnyStringFlags, layout: FStringLayout) -> Self {
|
||||
Self {
|
||||
enclosing_flags: flags,
|
||||
layout,
|
||||
|
@ -125,7 +120,7 @@ pub(crate) enum FStringLayout {
|
|||
}
|
||||
|
||||
impl FStringLayout {
|
||||
fn from_f_string(f_string: &FString, locator: &Locator) -> Self {
|
||||
pub(crate) fn from_f_string(f_string: &FString, locator: &Locator) -> Self {
|
||||
// Heuristic: Allow breaking the f-string expressions across multiple lines
|
||||
// only if there already is at least one multiline expression. This puts the
|
||||
// control in the hands of the user to decide if they want to break the
|
||||
|
|
|
@ -61,7 +61,8 @@ impl<'a> FormatFStringLiteralElement<'a> {
|
|||
impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let literal_content = f.context().locator().slice(self.element.range());
|
||||
let normalized = normalize_string(literal_content, 0, self.fstring_flags, true);
|
||||
let normalized =
|
||||
normalize_string(literal_content, 0, self.fstring_flags, false, false, true);
|
||||
match &normalized {
|
||||
Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f),
|
||||
Cow::Owned(normalized) => text(normalized).fmt(f),
|
||||
|
@ -235,11 +236,9 @@ impl Format<PyFormatContext<'_>> for FormatFStringExpressionElement<'_> {
|
|||
if let Some(format_spec) = format_spec.as_deref() {
|
||||
token(":").fmt(f)?;
|
||||
|
||||
f.join()
|
||||
.entries(format_spec.elements.iter().map(|element| {
|
||||
FormatFStringElement::new(element, self.context.f_string())
|
||||
}))
|
||||
.finish()?;
|
||||
for element in &format_spec.elements {
|
||||
FormatFStringElement::new(element, self.context.f_string()).fmt(f)?;
|
||||
}
|
||||
|
||||
// These trailing comments can only occur if the format specifier is
|
||||
// present. For example,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue