mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +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
|
@ -5,9 +5,9 @@ use std::ops::Deref;
|
|||
|
||||
use ruff_python_ast::{
|
||||
self as ast, Alias, AnyStringFlags, ArgOrKeyword, BoolOp, BytesLiteralFlags, CmpOp,
|
||||
Comprehension, ConversionFlag, DebugText, ExceptHandler, Expr, FStringFlags, Identifier,
|
||||
MatchCase, Operator, Parameter, Parameters, Pattern, Singleton, Stmt, StringFlags, Suite,
|
||||
TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, WithItem,
|
||||
Comprehension, ConversionFlag, DebugText, ExceptHandler, Expr, Identifier, MatchCase, Operator,
|
||||
Parameter, Parameters, Pattern, Singleton, Stmt, StringFlags, Suite, TypeParam,
|
||||
TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, WithItem,
|
||||
};
|
||||
use ruff_python_ast::{ParameterWithDefault, TypeParams};
|
||||
use ruff_python_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
|
||||
|
@ -1112,6 +1112,9 @@ impl<'a> Generator<'a> {
|
|||
Expr::FString(ast::ExprFString { value, .. }) => {
|
||||
self.unparse_f_string_value(value);
|
||||
}
|
||||
Expr::TString(ast::ExprTString { value, .. }) => {
|
||||
self.unparse_t_string_value(value);
|
||||
}
|
||||
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => {
|
||||
self.unparse_string_literal_value(value);
|
||||
}
|
||||
|
@ -1326,24 +1329,24 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_string_literal(string_literal);
|
||||
}
|
||||
ast::FStringPart::FString(f_string) => {
|
||||
self.unparse_f_string(&f_string.elements, f_string.flags);
|
||||
self.unparse_interpolated_string(&f_string.elements, f_string.flags.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unparse_f_string_body(&mut self, values: &[ast::FStringElement]) {
|
||||
fn unparse_interpolated_string_body(&mut self, values: &[ast::InterpolatedStringElement]) {
|
||||
for value in values {
|
||||
self.unparse_f_string_element(value);
|
||||
self.unparse_interpolated_string_element(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn unparse_f_string_expression_element(
|
||||
fn unparse_interpolated_element(
|
||||
&mut self,
|
||||
val: &Expr,
|
||||
debug_text: Option<&DebugText>,
|
||||
conversion: ConversionFlag,
|
||||
spec: Option<&ast::FStringFormatSpec>,
|
||||
spec: Option<&ast::InterpolatedStringFormatSpec>,
|
||||
) {
|
||||
let mut generator = Generator::new(self.indent, self.line_ending);
|
||||
generator.unparse_expr(val, precedence::FORMATTED_VALUE);
|
||||
|
@ -1379,18 +1382,21 @@ impl<'a> Generator<'a> {
|
|||
self.p("}");
|
||||
}
|
||||
|
||||
fn unparse_f_string_element(&mut self, element: &ast::FStringElement) {
|
||||
fn unparse_interpolated_string_element(&mut self, element: &ast::InterpolatedStringElement) {
|
||||
match element {
|
||||
ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => {
|
||||
self.unparse_f_string_literal_element(value);
|
||||
ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement {
|
||||
value,
|
||||
..
|
||||
}) => {
|
||||
self.unparse_interpolated_string_literal_element(value);
|
||||
}
|
||||
ast::FStringElement::Expression(ast::FStringExpressionElement {
|
||||
ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement {
|
||||
expression,
|
||||
debug_text,
|
||||
conversion,
|
||||
format_spec,
|
||||
range: _,
|
||||
}) => self.unparse_f_string_expression_element(
|
||||
}) => self.unparse_interpolated_element(
|
||||
expression,
|
||||
debug_text.as_ref(),
|
||||
*conversion,
|
||||
|
@ -1399,24 +1405,46 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unparse_f_string_literal_element(&mut self, s: &str) {
|
||||
fn unparse_interpolated_string_literal_element(&mut self, s: &str) {
|
||||
let s = s.replace('{', "{{").replace('}', "}}");
|
||||
self.p(&s);
|
||||
}
|
||||
|
||||
fn unparse_f_string_specifier(&mut self, values: &[ast::FStringElement]) {
|
||||
self.unparse_f_string_body(values);
|
||||
fn unparse_f_string_specifier(&mut self, values: &[ast::InterpolatedStringElement]) {
|
||||
self.unparse_interpolated_string_body(values);
|
||||
}
|
||||
|
||||
/// Unparse `values` with [`Generator::unparse_f_string_body`], using `quote` as the preferred
|
||||
/// surrounding quote style.
|
||||
fn unparse_f_string(&mut self, values: &[ast::FStringElement], flags: FStringFlags) {
|
||||
fn unparse_interpolated_string(
|
||||
&mut self,
|
||||
values: &[ast::InterpolatedStringElement],
|
||||
flags: AnyStringFlags,
|
||||
) {
|
||||
let mut generator = Generator::new(self.indent, self.line_ending);
|
||||
generator.unparse_f_string_body(values);
|
||||
generator.unparse_interpolated_string_body(values);
|
||||
let body = &generator.buffer;
|
||||
self.p_str_repr(body, flags);
|
||||
}
|
||||
|
||||
fn unparse_t_string_value(&mut self, value: &ast::TStringValue) {
|
||||
let mut first = true;
|
||||
for t_string_part in value {
|
||||
self.p_delim(&mut first, " ");
|
||||
match t_string_part {
|
||||
ast::TStringPart::Literal(string_literal) => {
|
||||
self.unparse_string_literal(string_literal);
|
||||
}
|
||||
ast::TStringPart::FString(f_string) => {
|
||||
self.unparse_interpolated_string(&f_string.elements, f_string.flags.into());
|
||||
}
|
||||
ast::TStringPart::TString(t_string) => {
|
||||
self.unparse_interpolated_string(&t_string.elements, t_string.flags.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unparse_alias(&mut self, alias: &Alias) {
|
||||
self.p_id(&alias.name);
|
||||
if let Some(asname) = &alias.asname {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue