Unify enums used for internal representation of quoting style (#10383)

This commit is contained in:
Alex Waygood
2024-03-13 17:19:17 +00:00
committed by GitHub
parent d59433b12e
commit c2e15f38ee
16 changed files with 148 additions and 228 deletions

View File

@@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::iter::FusedIterator;
use ruff_formatter::FormatContext;
use ruff_python_ast::str::Quote;
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange};
@@ -9,13 +10,13 @@ use crate::context::FStringState;
use crate::options::PythonVersion;
use crate::prelude::*;
use crate::preview::is_f_string_formatting_enabled;
use crate::string::{QuoteChar, Quoting, StringPart, StringPrefix, StringQuotes};
use crate::string::{Quoting, StringPart, StringPrefix, StringQuotes};
use crate::QuoteStyle;
pub(crate) struct StringNormalizer {
quoting: Quoting,
preferred_quote_style: QuoteStyle,
parent_docstring_quote_char: Option<QuoteChar>,
parent_docstring_quote_char: Option<Quote>,
f_string_state: FStringState,
target_version: PythonVersion,
format_fstring: bool,
@@ -130,7 +131,7 @@ impl StringNormalizer {
// style from what the parent ultimately decided upon works, even
// if it doesn't have perfect alignment with PEP8.
if let Some(quote) = self.parent_docstring_quote_char {
QuoteStyle::from(quote.invert())
QuoteStyle::from(quote.opposite())
} else if self.preferred_quote_style.is_preserve() {
QuoteStyle::Preserve
} else {
@@ -140,7 +141,7 @@ impl StringNormalizer {
self.preferred_quote_style
};
if let Some(preferred_quote) = QuoteChar::from_style(preferred_style) {
if let Ok(preferred_quote) = Quote::try_from(preferred_style) {
if let Some(first_quote_or_normalized_char_offset) =
first_quote_or_normalized_char_offset
{
@@ -281,7 +282,7 @@ impl Format<PyFormatContext<'_>> for NormalizedString<'_> {
fn choose_quotes_for_raw_string(
input: &str,
quotes: StringQuotes,
preferred_quote: QuoteChar,
preferred_quote: Quote,
) -> StringQuotes {
let preferred_quote_char = preferred_quote.as_char();
let mut chars = input.chars().peekable();
@@ -337,11 +338,7 @@ fn choose_quotes_for_raw_string(
/// For triple quoted strings, the preferred quote style is always used, unless the string contains
/// a triplet of the quote character (e.g., if double quotes are preferred, double quotes will be
/// used unless the string contains `"""`).
fn choose_quotes_impl(
input: &str,
quotes: StringQuotes,
preferred_quote: QuoteChar,
) -> StringQuotes {
fn choose_quotes_impl(input: &str, quotes: StringQuotes, preferred_quote: Quote) -> StringQuotes {
let quote = if quotes.triple {
// True if the string contains a triple quote sequence of the configured quote style.
let mut uses_triple_quotes = false;
@@ -419,18 +416,18 @@ fn choose_quotes_impl(
}
match preferred_quote {
QuoteChar::Single => {
Quote::Single => {
if single_quotes > double_quotes {
QuoteChar::Double
Quote::Double
} else {
QuoteChar::Single
Quote::Single
}
}
QuoteChar::Double => {
Quote::Double => {
if double_quotes > single_quotes {
QuoteChar::Single
Quote::Single
} else {
QuoteChar::Double
Quote::Double
}
}
}
@@ -462,7 +459,7 @@ pub(crate) fn normalize_string(
let quote = quotes.quote_char;
let preferred_quote = quote.as_char();
let opposite_quote = quote.invert().as_char();
let opposite_quote = quote.opposite().as_char();
let mut chars = CharIndicesWithOffset::new(input, start_offset).peekable();
@@ -707,7 +704,9 @@ impl UnicodeEscape {
mod tests {
use std::borrow::Cow;
use crate::string::{QuoteChar, StringPrefix, StringQuotes};
use ruff_python_ast::str::Quote;
use crate::string::{StringPrefix, StringQuotes};
use super::{normalize_string, UnicodeEscape};
@@ -730,7 +729,7 @@ mod tests {
0,
StringQuotes {
triple: false,
quote_char: QuoteChar::Double,
quote_char: Quote::Double,
},
StringPrefix::BYTE,
true,