mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
Remove layout values from AnyStringPart
(#13681)
This commit is contained in:
parent
93eff7f174
commit
b9827a4122
5 changed files with 45 additions and 62 deletions
|
@ -9,7 +9,6 @@ pub struct FormatBytesLiteral;
|
||||||
impl FormatNodeRule<BytesLiteral> for FormatBytesLiteral {
|
impl FormatNodeRule<BytesLiteral> for FormatBytesLiteral {
|
||||||
fn fmt_fields(&self, item: &BytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &BytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
StringNormalizer::from_context(f.context())
|
StringNormalizer::from_context(f.context())
|
||||||
.with_preferred_quote_style(f.options().quote_style())
|
|
||||||
.normalize(item.into())
|
.normalize(item.into())
|
||||||
.fmt(f)
|
.fmt(f)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
|
||||||
self.quoting
|
self.quoting
|
||||||
};
|
};
|
||||||
|
|
||||||
let normalizer = StringNormalizer::from_context(f.context())
|
let normalizer = StringNormalizer::from_context(f.context()).with_quoting(quoting);
|
||||||
.with_quoting(quoting)
|
|
||||||
.with_preferred_quote_style(f.options().quote_style());
|
|
||||||
|
|
||||||
// If f-string formatting is disabled (not in preview), then we will
|
// If f-string formatting is disabled (not in preview), then we will
|
||||||
// fall back to the previous behavior of normalizing the f-string.
|
// fall back to the previous behavior of normalizing the f-string.
|
||||||
|
|
|
@ -10,9 +10,6 @@ use ruff_source_file::Locator;
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::expression::expr_f_string::f_string_quoting;
|
use crate::expression::expr_f_string::f_string_quoting;
|
||||||
use crate::other::f_string::FormatFString;
|
|
||||||
use crate::other::string_literal::StringLiteralKind;
|
|
||||||
use crate::prelude::*;
|
|
||||||
use crate::string::Quoting;
|
use crate::string::Quoting;
|
||||||
|
|
||||||
/// Represents any kind of string expression. This could be either a string,
|
/// Represents any kind of string expression. This could be either a string,
|
||||||
|
@ -46,6 +43,10 @@ impl<'a> AnyString<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) const fn is_fstring(self) -> bool {
|
||||||
|
matches!(self, Self::FString(_))
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the quoting to be used for this string.
|
/// Returns the quoting to be used for this string.
|
||||||
pub(super) fn quoting(self, locator: &Locator<'_>) -> Quoting {
|
pub(super) fn quoting(self, locator: &Locator<'_>) -> Quoting {
|
||||||
match self {
|
match self {
|
||||||
|
@ -54,23 +55,21 @@ impl<'a> AnyString<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a vector of all the [`AnyStringPart`] of this string.
|
/// Returns an iterator over the [`AnyStringPart`]s of this string.
|
||||||
pub(super) fn parts(self, quoting: Quoting) -> AnyStringPartsIter<'a> {
|
pub(super) fn parts(self) -> AnyStringPartsIter<'a> {
|
||||||
match self {
|
match self {
|
||||||
Self::String(ExprStringLiteral { value, .. }) => {
|
Self::String(ExprStringLiteral { value, .. }) => {
|
||||||
AnyStringPartsIter::String(value.iter())
|
AnyStringPartsIter::String(value.iter())
|
||||||
}
|
}
|
||||||
Self::Bytes(ExprBytesLiteral { value, .. }) => AnyStringPartsIter::Bytes(value.iter()),
|
Self::Bytes(ExprBytesLiteral { value, .. }) => AnyStringPartsIter::Bytes(value.iter()),
|
||||||
Self::FString(ExprFString { value, .. }) => {
|
Self::FString(ExprFString { value, .. }) => AnyStringPartsIter::FString(value.iter()),
|
||||||
AnyStringPartsIter::FString(value.iter(), quoting)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_multiline(self, source: &str) -> bool {
|
pub(crate) fn is_multiline(self, source: &str) -> bool {
|
||||||
match self {
|
match self {
|
||||||
AnyString::String(_) | AnyString::Bytes(_) => {
|
AnyString::String(_) | AnyString::Bytes(_) => {
|
||||||
self.parts(Quoting::default())
|
self.parts()
|
||||||
.next()
|
.next()
|
||||||
.is_some_and(|part| part.flags().is_triple_quoted())
|
.is_some_and(|part| part.flags().is_triple_quoted())
|
||||||
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
|
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
|
||||||
|
@ -139,7 +138,7 @@ impl<'a> From<&'a ExprFString> for AnyString<'a> {
|
||||||
pub(super) enum AnyStringPartsIter<'a> {
|
pub(super) enum AnyStringPartsIter<'a> {
|
||||||
String(std::slice::Iter<'a, StringLiteral>),
|
String(std::slice::Iter<'a, StringLiteral>),
|
||||||
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
|
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
|
||||||
FString(std::slice::Iter<'a, ast::FStringPart>, Quoting),
|
FString(std::slice::Iter<'a, ast::FStringPart>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for AnyStringPartsIter<'a> {
|
impl<'a> Iterator for AnyStringPartsIter<'a> {
|
||||||
|
@ -147,28 +146,12 @@ impl<'a> Iterator for AnyStringPartsIter<'a> {
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let part = match self {
|
let part = match self {
|
||||||
Self::String(inner) => {
|
Self::String(inner) => AnyStringPart::String(inner.next()?),
|
||||||
let part = inner.next()?;
|
|
||||||
AnyStringPart::String {
|
|
||||||
part,
|
|
||||||
layout: StringLiteralKind::String,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Self::Bytes(inner) => AnyStringPart::Bytes(inner.next()?),
|
Self::Bytes(inner) => AnyStringPart::Bytes(inner.next()?),
|
||||||
Self::FString(inner, quoting) => {
|
Self::FString(inner) => match inner.next()? {
|
||||||
let part = inner.next()?;
|
ast::FStringPart::Literal(string_literal) => AnyStringPart::String(string_literal),
|
||||||
match part {
|
ast::FStringPart::FString(f_string) => AnyStringPart::FString(f_string),
|
||||||
ast::FStringPart::Literal(string_literal) => AnyStringPart::String {
|
},
|
||||||
part: string_literal,
|
|
||||||
#[allow(deprecated)]
|
|
||||||
layout: StringLiteralKind::InImplicitlyConcatenatedFString(*quoting),
|
|
||||||
},
|
|
||||||
ast::FStringPart::FString(f_string) => AnyStringPart::FString {
|
|
||||||
part: f_string,
|
|
||||||
quoting: *quoting,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(part)
|
Some(part)
|
||||||
|
@ -183,23 +166,17 @@ impl FusedIterator for AnyStringPartsIter<'_> {}
|
||||||
/// This is constructed from the [`AnyString::parts`] method on [`AnyString`].
|
/// This is constructed from the [`AnyString::parts`] method on [`AnyString`].
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(super) enum AnyStringPart<'a> {
|
pub(super) enum AnyStringPart<'a> {
|
||||||
String {
|
String(&'a ast::StringLiteral),
|
||||||
part: &'a ast::StringLiteral,
|
|
||||||
layout: StringLiteralKind,
|
|
||||||
},
|
|
||||||
Bytes(&'a ast::BytesLiteral),
|
Bytes(&'a ast::BytesLiteral),
|
||||||
FString {
|
FString(&'a ast::FString),
|
||||||
part: &'a ast::FString,
|
|
||||||
quoting: Quoting,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyStringPart<'_> {
|
impl AnyStringPart<'_> {
|
||||||
fn flags(&self) -> AnyStringFlags {
|
fn flags(&self) -> AnyStringFlags {
|
||||||
match self {
|
match self {
|
||||||
Self::String { part, .. } => part.flags.into(),
|
Self::String(part) => part.flags.into(),
|
||||||
Self::Bytes(bytes_literal) => bytes_literal.flags.into(),
|
Self::Bytes(bytes_literal) => bytes_literal.flags.into(),
|
||||||
Self::FString { part, .. } => part.flags.into(),
|
Self::FString(part) => part.flags.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -207,9 +184,9 @@ impl AnyStringPart<'_> {
|
||||||
impl<'a> From<&AnyStringPart<'a>> for AnyNodeRef<'a> {
|
impl<'a> From<&AnyStringPart<'a>> for AnyNodeRef<'a> {
|
||||||
fn from(value: &AnyStringPart<'a>) -> Self {
|
fn from(value: &AnyStringPart<'a>) -> Self {
|
||||||
match value {
|
match value {
|
||||||
AnyStringPart::String { part, .. } => AnyNodeRef::StringLiteral(part),
|
AnyStringPart::String(part) => AnyNodeRef::StringLiteral(part),
|
||||||
AnyStringPart::Bytes(part) => AnyNodeRef::BytesLiteral(part),
|
AnyStringPart::Bytes(part) => AnyNodeRef::BytesLiteral(part),
|
||||||
AnyStringPart::FString { part, .. } => AnyNodeRef::FString(part),
|
AnyStringPart::FString(part) => AnyNodeRef::FString(part),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,19 +194,9 @@ impl<'a> From<&AnyStringPart<'a>> for AnyNodeRef<'a> {
|
||||||
impl Ranged for AnyStringPart<'_> {
|
impl Ranged for AnyStringPart<'_> {
|
||||||
fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
match self {
|
match self {
|
||||||
Self::String { part, .. } => part.range(),
|
Self::String(part) => part.range(),
|
||||||
Self::Bytes(part) => part.range(),
|
Self::Bytes(part) => part.range(),
|
||||||
Self::FString { part, .. } => part.range(),
|
Self::FString(part) => part.range(),
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Format<PyFormatContext<'_>> for AnyStringPart<'_> {
|
|
||||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
match self {
|
|
||||||
AnyStringPart::String { part, layout } => part.format().with_options(*layout).fmt(f),
|
|
||||||
AnyStringPart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
|
|
||||||
AnyStringPart::FString { part, quoting } => FormatFString::new(part, *quoting).fmt(f),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,10 @@ use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::comments::{leading_comments, trailing_comments};
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
use crate::expression::parentheses::in_parentheses_only_soft_line_break_or_space;
|
use crate::expression::parentheses::in_parentheses_only_soft_line_break_or_space;
|
||||||
|
use crate::other::f_string::FormatFString;
|
||||||
|
use crate::other::string_literal::StringLiteralKind;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use crate::string::any::AnyStringPart;
|
||||||
use crate::QuoteStyle;
|
use crate::QuoteStyle;
|
||||||
|
|
||||||
mod any;
|
mod any;
|
||||||
|
@ -46,12 +49,28 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedString<'_> {
|
||||||
|
|
||||||
let mut joiner = f.join_with(in_parentheses_only_soft_line_break_or_space());
|
let mut joiner = f.join_with(in_parentheses_only_soft_line_break_or_space());
|
||||||
|
|
||||||
for part in self.string.parts(quoting) {
|
for part in self.string.parts() {
|
||||||
let part_comments = comments.leading_dangling_trailing(&part);
|
let part_comments = comments.leading_dangling_trailing(&part);
|
||||||
|
|
||||||
|
let format_part = format_with(|f: &mut PyFormatter| match part {
|
||||||
|
AnyStringPart::String(part) => {
|
||||||
|
let kind = if self.string.is_fstring() {
|
||||||
|
#[allow(deprecated)]
|
||||||
|
StringLiteralKind::InImplicitlyConcatenatedFString(quoting)
|
||||||
|
} else {
|
||||||
|
StringLiteralKind::String
|
||||||
|
};
|
||||||
|
|
||||||
|
part.format().with_options(kind).fmt(f)
|
||||||
|
}
|
||||||
|
AnyStringPart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
|
||||||
|
AnyStringPart::FString(part) => FormatFString::new(part, quoting).fmt(f),
|
||||||
|
});
|
||||||
|
|
||||||
joiner.entry(&format_args![
|
joiner.entry(&format_args![
|
||||||
line_suffix_boundary(),
|
line_suffix_boundary(),
|
||||||
leading_comments(part_comments.leading),
|
leading_comments(part_comments.leading),
|
||||||
part,
|
format_part,
|
||||||
trailing_comments(part_comments.trailing)
|
trailing_comments(part_comments.trailing)
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
|
||||||
pub(crate) fn from_context(context: &'a PyFormatContext<'src>) -> Self {
|
pub(crate) fn from_context(context: &'a PyFormatContext<'src>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
quoting: Quoting::default(),
|
quoting: Quoting::default(),
|
||||||
preferred_quote_style: QuoteStyle::default(),
|
preferred_quote_style: context.options().quote_style(),
|
||||||
context,
|
context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue