mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:39:12 +00:00
Implement template strings (#17851)
This PR implements template strings (t-strings) in the parser and formatter for Ruff. Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
parent
ad024f9a09
commit
9bbf4987e8
261 changed files with 18023 additions and 1802 deletions
|
@ -6,8 +6,8 @@ use {
|
|||
|
||||
use ruff_python_ast::visitor::transformer::Transformer;
|
||||
use ruff_python_ast::{
|
||||
self as ast, BytesLiteralFlags, Expr, FStringElement, FStringFlags, FStringLiteralElement,
|
||||
FStringPart, Stmt, StringFlags,
|
||||
self as ast, BytesLiteralFlags, Expr, FStringFlags, FStringPart, InterpolatedStringElement,
|
||||
InterpolatedStringLiteralElement, Stmt, StringFlags,
|
||||
};
|
||||
use ruff_python_ast::{StringLiteralFlags, visitor::transformer};
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
@ -117,7 +117,7 @@ impl Transformer for Normalizer {
|
|||
if can_join {
|
||||
#[derive(Default)]
|
||||
struct Collector {
|
||||
elements: Vec<FStringElement>,
|
||||
elements: Vec<InterpolatedStringElement>,
|
||||
}
|
||||
|
||||
impl Collector {
|
||||
|
@ -127,7 +127,7 @@ impl Transformer for Normalizer {
|
|||
// `elements` vector, while subsequent strings
|
||||
// are concatenated onto this top string.
|
||||
fn push_literal(&mut self, literal: &str, range: TextRange) {
|
||||
if let Some(FStringElement::Literal(existing_literal)) =
|
||||
if let Some(InterpolatedStringElement::Literal(existing_literal)) =
|
||||
self.elements.last_mut()
|
||||
{
|
||||
let value = std::mem::take(&mut existing_literal.value);
|
||||
|
@ -137,8 +137,8 @@ impl Transformer for Normalizer {
|
|||
existing_literal.range =
|
||||
TextRange::new(existing_literal.start(), range.end());
|
||||
} else {
|
||||
self.elements.push(FStringElement::Literal(
|
||||
FStringLiteralElement {
|
||||
self.elements.push(InterpolatedStringElement::Literal(
|
||||
InterpolatedStringLiteralElement {
|
||||
range,
|
||||
value: literal.into(),
|
||||
},
|
||||
|
@ -146,11 +146,9 @@ impl Transformer for Normalizer {
|
|||
}
|
||||
}
|
||||
|
||||
fn push_expression(
|
||||
&mut self,
|
||||
expression: ast::FStringExpressionElement,
|
||||
) {
|
||||
self.elements.push(FStringElement::Expression(expression));
|
||||
fn push_expression(&mut self, expression: ast::InterpolatedElement) {
|
||||
self.elements
|
||||
.push(InterpolatedStringElement::Interpolation(expression));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,11 +163,13 @@ impl Transformer for Normalizer {
|
|||
ast::FStringPart::FString(fstring) => {
|
||||
for element in &fstring.elements {
|
||||
match element {
|
||||
ast::FStringElement::Literal(literal) => {
|
||||
ast::InterpolatedStringElement::Literal(literal) => {
|
||||
collector
|
||||
.push_literal(&literal.value, literal.range);
|
||||
}
|
||||
ast::FStringElement::Expression(expression) => {
|
||||
ast::InterpolatedStringElement::Interpolation(
|
||||
expression,
|
||||
) => {
|
||||
collector.push_expression(expression.clone());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue