Refactor: Remove StringPart and AnyStringPart in favor of StringLikePart (#13772)

This commit is contained in:
Micha Reiser 2024-10-16 12:52:06 +02:00 committed by GitHub
parent b85be6297e
commit 8f5b2aac9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 137 additions and 288 deletions

View file

@ -1,202 +0,0 @@
use std::iter::FusedIterator;
use memchr::memchr2;
use ruff_python_ast::{
self as ast, AnyNodeRef, AnyStringFlags, Expr, ExprBytesLiteral, ExprFString,
ExprStringLiteral, ExpressionRef, StringFlags, StringLiteral,
};
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange};
use crate::expression::expr_f_string::f_string_quoting;
use crate::string::Quoting;
/// Represents any kind of string expression. This could be either a string,
/// bytes or f-string.
#[derive(Copy, Clone, Debug)]
pub(crate) enum AnyString<'a> {
String(&'a ExprStringLiteral),
Bytes(&'a ExprBytesLiteral),
FString(&'a ExprFString),
}
impl<'a> AnyString<'a> {
/// Creates a new [`AnyString`] from the given [`Expr`].
///
/// Returns `None` if the expression is not either a string, bytes or f-string.
pub(crate) fn from_expression(expression: &'a Expr) -> Option<AnyString<'a>> {
match expression {
Expr::StringLiteral(string) => Some(AnyString::String(string)),
Expr::BytesLiteral(bytes) => Some(AnyString::Bytes(bytes)),
Expr::FString(fstring) => Some(AnyString::FString(fstring)),
_ => None,
}
}
/// Returns `true` if the string is implicitly concatenated.
pub(crate) fn is_implicit_concatenated(self) -> bool {
match self {
Self::String(ExprStringLiteral { value, .. }) => value.is_implicit_concatenated(),
Self::Bytes(ExprBytesLiteral { value, .. }) => value.is_implicit_concatenated(),
Self::FString(ExprFString { value, .. }) => value.is_implicit_concatenated(),
}
}
pub(crate) const fn is_fstring(self) -> bool {
matches!(self, Self::FString(_))
}
/// Returns the quoting to be used for this string.
pub(super) fn quoting(self, locator: &Locator<'_>) -> Quoting {
match self {
Self::String(_) | Self::Bytes(_) => Quoting::CanChange,
Self::FString(f_string) => f_string_quoting(f_string, locator),
}
}
/// Returns an iterator over the [`AnyStringPart`]s of this string.
pub(super) fn parts(self) -> AnyStringPartsIter<'a> {
match self {
Self::String(ExprStringLiteral { value, .. }) => {
AnyStringPartsIter::String(value.iter())
}
Self::Bytes(ExprBytesLiteral { value, .. }) => AnyStringPartsIter::Bytes(value.iter()),
Self::FString(ExprFString { value, .. }) => AnyStringPartsIter::FString(value.iter()),
}
}
pub(crate) fn is_multiline(self, source: &str) -> bool {
match self {
AnyString::String(_) | AnyString::Bytes(_) => {
self.parts()
.next()
.is_some_and(|part| part.flags().is_triple_quoted())
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
}
AnyString::FString(fstring) => {
memchr2(b'\n', b'\r', source[fstring.range].as_bytes()).is_some()
}
}
}
}
impl Ranged for AnyString<'_> {
fn range(&self) -> TextRange {
match self {
Self::String(expr) => expr.range(),
Self::Bytes(expr) => expr.range(),
Self::FString(expr) => expr.range(),
}
}
}
impl<'a> From<&AnyString<'a>> for AnyNodeRef<'a> {
fn from(value: &AnyString<'a>) -> Self {
match value {
AnyString::String(expr) => AnyNodeRef::ExprStringLiteral(expr),
AnyString::Bytes(expr) => AnyNodeRef::ExprBytesLiteral(expr),
AnyString::FString(expr) => AnyNodeRef::ExprFString(expr),
}
}
}
impl<'a> From<AnyString<'a>> for AnyNodeRef<'a> {
fn from(value: AnyString<'a>) -> Self {
AnyNodeRef::from(&value)
}
}
impl<'a> From<&AnyString<'a>> for ExpressionRef<'a> {
fn from(value: &AnyString<'a>) -> Self {
match value {
AnyString::String(expr) => ExpressionRef::StringLiteral(expr),
AnyString::Bytes(expr) => ExpressionRef::BytesLiteral(expr),
AnyString::FString(expr) => ExpressionRef::FString(expr),
}
}
}
impl<'a> From<&'a ExprBytesLiteral> for AnyString<'a> {
fn from(value: &'a ExprBytesLiteral) -> Self {
AnyString::Bytes(value)
}
}
impl<'a> From<&'a ExprStringLiteral> for AnyString<'a> {
fn from(value: &'a ExprStringLiteral) -> Self {
AnyString::String(value)
}
}
impl<'a> From<&'a ExprFString> for AnyString<'a> {
fn from(value: &'a ExprFString) -> Self {
AnyString::FString(value)
}
}
pub(super) enum AnyStringPartsIter<'a> {
String(std::slice::Iter<'a, StringLiteral>),
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
FString(std::slice::Iter<'a, ast::FStringPart>),
}
impl<'a> Iterator for AnyStringPartsIter<'a> {
type Item = AnyStringPart<'a>;
fn next(&mut self) -> Option<Self::Item> {
let part = match self {
Self::String(inner) => AnyStringPart::String(inner.next()?),
Self::Bytes(inner) => AnyStringPart::Bytes(inner.next()?),
Self::FString(inner) => match inner.next()? {
ast::FStringPart::Literal(string_literal) => AnyStringPart::String(string_literal),
ast::FStringPart::FString(f_string) => AnyStringPart::FString(f_string),
},
};
Some(part)
}
}
impl FusedIterator for AnyStringPartsIter<'_> {}
/// Represents any kind of string which is part of an implicitly concatenated
/// string. This could be either a string, bytes or f-string.
///
/// This is constructed from the [`AnyString::parts`] method on [`AnyString`].
#[derive(Clone, Debug)]
pub(super) enum AnyStringPart<'a> {
String(&'a ast::StringLiteral),
Bytes(&'a ast::BytesLiteral),
FString(&'a ast::FString),
}
impl AnyStringPart<'_> {
fn flags(&self) -> AnyStringFlags {
match self {
Self::String(part) => part.flags.into(),
Self::Bytes(bytes_literal) => bytes_literal.flags.into(),
Self::FString(part) => part.flags.into(),
}
}
}
impl<'a> From<&AnyStringPart<'a>> for AnyNodeRef<'a> {
fn from(value: &AnyStringPart<'a>) -> Self {
match value {
AnyStringPart::String(part) => AnyNodeRef::StringLiteral(part),
AnyStringPart::Bytes(part) => AnyNodeRef::BytesLiteral(part),
AnyStringPart::FString(part) => AnyNodeRef::FString(part),
}
}
}
impl Ranged for AnyStringPart<'_> {
fn range(&self) -> TextRange {
match self {
Self::String(part) => part.range(),
Self::Bytes(part) => part.range(),
Self::FString(part) => part.range(),
}
}
}

View file

@ -1,23 +1,24 @@
pub(crate) use any::AnyString;
use memchr::memchr2;
pub(crate) use normalize::{normalize_string, NormalizedString, StringNormalizer};
use ruff_formatter::format_args;
use ruff_python_ast::str::Quote;
use ruff_python_ast::{
self as ast,
str_prefix::{AnyStringPrefix, StringLiteralPrefix},
AnyStringFlags, StringFlags,
AnyStringFlags, StringFlags, StringLike, StringLikePart,
};
use ruff_text_size::{Ranged, TextRange};
use ruff_source_file::Locator;
use ruff_text_size::Ranged;
use crate::comments::{leading_comments, trailing_comments};
use crate::expression::expr_f_string::f_string_quoting;
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::string::any::AnyStringPart;
use crate::QuoteStyle;
mod any;
pub(crate) mod docstring;
mod normalize;
@ -31,11 +32,11 @@ pub(crate) enum Quoting {
/// Formats any implicitly concatenated string. This could be any valid combination
/// of string, bytes or f-string literals.
pub(crate) struct FormatImplicitConcatenatedString<'a> {
string: AnyString<'a>,
string: StringLike<'a>,
}
impl<'a> FormatImplicitConcatenatedString<'a> {
pub(crate) fn new(string: impl Into<AnyString<'a>>) -> Self {
pub(crate) fn new(string: impl Into<StringLike<'a>>) -> Self {
Self {
string: string.into(),
}
@ -53,7 +54,7 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedString<'_> {
let part_comments = comments.leading_dangling_trailing(&part);
let format_part = format_with(|f: &mut PyFormatter| match part {
AnyStringPart::String(part) => {
StringLikePart::String(part) => {
let kind = if self.string.is_fstring() {
#[allow(deprecated)]
StringLiteralKind::InImplicitlyConcatenatedFString(quoting)
@ -63,8 +64,8 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedString<'_> {
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),
StringLikePart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
StringLikePart::FString(part) => FormatFString::new(part, quoting).fmt(f),
});
joiner.entry(&format_args![
@ -141,57 +142,32 @@ impl From<Quote> for QuoteStyle {
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct StringPart {
flags: AnyStringFlags,
range: TextRange,
// Extension trait that adds formatter specific helper methods to `StringLike`.
pub(crate) trait StringLikeExtensions {
fn quoting(&self, locator: &Locator<'_>) -> Quoting;
fn is_multiline(&self, source: &str) -> bool;
}
impl Ranged for StringPart {
fn range(&self) -> TextRange {
self.range
}
}
impl StringPart {
/// Use the `kind()` method to retrieve information about the
fn flags(self) -> AnyStringFlags {
self.flags
impl StringLikeExtensions for ast::StringLike<'_> {
fn quoting(&self, locator: &Locator<'_>) -> Quoting {
match self {
Self::String(_) | Self::Bytes(_) => Quoting::CanChange,
Self::FString(f_string) => f_string_quoting(f_string, locator),
}
}
/// Returns the range of the string's content in the source (minus prefix and quotes).
fn content_range(self) -> TextRange {
let kind = self.flags();
TextRange::new(
self.start() + kind.opener_len(),
self.end() - kind.closer_len(),
)
}
}
impl From<&ast::StringLiteral> for StringPart {
fn from(value: &ast::StringLiteral) -> Self {
Self {
range: value.range,
flags: value.flags.into(),
}
}
}
impl From<&ast::BytesLiteral> for StringPart {
fn from(value: &ast::BytesLiteral) -> Self {
Self {
range: value.range,
flags: value.flags.into(),
}
}
}
impl From<&ast::FString> for StringPart {
fn from(value: &ast::FString) -> Self {
Self {
range: value.range,
flags: value.flags.into(),
fn is_multiline(&self, source: &str) -> bool {
match self {
Self::String(_) | Self::Bytes(_) => {
self.parts()
.next()
.is_some_and(|part| part.flags().is_triple_quoted())
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
}
Self::FString(fstring) => {
memchr2(b'\n', b'\r', source[fstring.range].as_bytes()).is_some()
}
}
}
}

View file

@ -3,13 +3,13 @@ use std::cmp::Ordering;
use std::iter::FusedIterator;
use ruff_formatter::FormatContext;
use ruff_python_ast::{str::Quote, AnyStringFlags, StringFlags};
use ruff_python_ast::{str::Quote, AnyStringFlags, StringFlags, StringLikePart};
use ruff_text_size::{Ranged, TextRange};
use crate::context::FStringState;
use crate::prelude::*;
use crate::preview::is_f_string_formatting_enabled;
use crate::string::{Quoting, StringPart, StringQuotes};
use crate::string::{Quoting, StringQuotes};
use crate::QuoteStyle;
pub(crate) struct StringNormalizer<'a, 'src> {
@ -37,7 +37,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
self
}
fn quoting(&self, string: StringPart) -> Quoting {
fn quoting(&self, string: StringLikePart) -> Quoting {
if let FStringState::InsideExpressionElement(context) = self.context.f_string_state() {
// If we're inside an f-string, we need to make sure to preserve the
// existing quotes unless we're inside a triple-quoted f-string and
@ -66,7 +66,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
}
/// Computes the strings preferred quotes.
pub(crate) fn choose_quotes(&self, string: StringPart) -> QuoteSelection {
pub(crate) fn choose_quotes(&self, string: StringLikePart) -> QuoteSelection {
let raw_content = self.context.locator().slice(string.content_range());
let first_quote_or_normalized_char_offset = raw_content
.bytes()
@ -168,7 +168,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
}
/// Computes the strings preferred quotes and normalizes its content.
pub(crate) fn normalize(&self, string: StringPart) -> NormalizedString<'src> {
pub(crate) fn normalize(&self, string: StringLikePart) -> NormalizedString<'src> {
let raw_content = self.context.locator().slice(string.content_range());
let quote_selection = self.choose_quotes(string);