mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:41:12 +00:00
Unify enums used for internal representation of quoting style (#10383)
This commit is contained in:
parent
d59433b12e
commit
c2e15f38ee
16 changed files with 148 additions and 228 deletions
|
@ -11,7 +11,7 @@ use itertools::Itertools;
|
|||
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::{int, str::QuoteStyle, LiteralExpressionRef};
|
||||
use crate::{int, str::Quote, LiteralExpressionRef};
|
||||
|
||||
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
|
@ -1159,6 +1159,15 @@ pub enum FStringPart {
|
|||
FString(FString),
|
||||
}
|
||||
|
||||
impl FStringPart {
|
||||
pub fn quote_style(&self) -> Quote {
|
||||
match self {
|
||||
Self::Literal(string_literal) => string_literal.flags.quote_style(),
|
||||
Self::FString(f_string) => f_string.flags.quote_style(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for FStringPart {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
|
@ -1221,11 +1230,11 @@ impl FStringFlags {
|
|||
}
|
||||
|
||||
/// Does the f-string use single or double quotes in its opener and closer?
|
||||
pub const fn quote_style(self) -> QuoteStyle {
|
||||
pub const fn quote_style(self) -> Quote {
|
||||
if self.0.contains(FStringFlagsInner::DOUBLE) {
|
||||
QuoteStyle::Double
|
||||
Quote::Double
|
||||
} else {
|
||||
QuoteStyle::Single
|
||||
Quote::Single
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1535,11 +1544,11 @@ impl StringLiteralFlags {
|
|||
}
|
||||
|
||||
/// Does the string use single or double quotes in its opener and closer?
|
||||
pub const fn quote_style(self) -> QuoteStyle {
|
||||
pub const fn quote_style(self) -> Quote {
|
||||
if self.0.contains(StringLiteralFlagsInner::DOUBLE) {
|
||||
QuoteStyle::Double
|
||||
Quote::Double
|
||||
} else {
|
||||
QuoteStyle::Single
|
||||
Quote::Single
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1864,11 +1873,11 @@ impl BytesLiteralFlags {
|
|||
}
|
||||
|
||||
/// Does the bytestring use single or double quotes in its opener and closer?
|
||||
pub const fn quote_style(self) -> QuoteStyle {
|
||||
pub const fn quote_style(self) -> Quote {
|
||||
if self.0.contains(BytesLiteralFlagsInner::DOUBLE) {
|
||||
QuoteStyle::Double
|
||||
Quote::Double
|
||||
} else {
|
||||
QuoteStyle::Single
|
||||
Quote::Single
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,23 @@
|
|||
use std::fmt;
|
||||
|
||||
use aho_corasick::{AhoCorasick, AhoCorasickKind, Anchored, Input, MatchKind, StartKind};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use ruff_text_size::{TextLen, TextRange};
|
||||
|
||||
/// Enumeration of the two kinds of quotes that can be used
|
||||
/// for Python string/f-string/bytestring literals
|
||||
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq, is_macro::Is)]
|
||||
pub enum QuoteStyle {
|
||||
/// E.g. '
|
||||
pub enum Quote {
|
||||
/// E.g. `'`
|
||||
Single,
|
||||
/// E.g. "
|
||||
/// E.g. `"`
|
||||
#[default]
|
||||
Double,
|
||||
}
|
||||
|
||||
impl QuoteStyle {
|
||||
impl Quote {
|
||||
#[inline]
|
||||
pub const fn as_char(self) -> char {
|
||||
match self {
|
||||
Self::Single => '\'',
|
||||
|
@ -21,12 +26,39 @@ impl QuoteStyle {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn opposite(self) -> Self {
|
||||
match self {
|
||||
Self::Single => Self::Double,
|
||||
Self::Double => Self::Single,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn as_byte(self) -> u8 {
|
||||
match self {
|
||||
Self::Single => b'\'',
|
||||
Self::Double => b'"',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Quote {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.as_char())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<char> for Quote {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: char) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
'\'' => Ok(Quote::Single),
|
||||
'"' => Ok(Quote::Double),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Includes all permutations of `r`, `u`, `f`, and `fr` (`ur` is invalid, as is `uf`). This
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue