Skip to content

Commit

Permalink
FStringPart for generator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Aug 10, 2023
1 parent 44abfdb commit f84a3de
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions crates/ruff_python_codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,20 +1092,8 @@ impl<'a> Generator<'a> {
}
self.p(")");
}
Expr::FormattedValue(ast::ExprFormattedValue {
value,
debug_text,
conversion,
format_spec,
range: _,
}) => self.unparse_formatted(
value,
debug_text.as_ref(),
*conversion,
format_spec.as_deref(),
),
Expr::FString(ast::ExprFString { values, range: _ }) => {
self.unparse_f_string(values, false);
self.unparse_fstring(values, false);
}
Expr::Constant(ast::ExprConstant {
value,
Expand Down Expand Up @@ -1289,9 +1277,9 @@ impl<'a> Generator<'a> {
}
}

fn unparse_f_string_body(&mut self, values: &[Expr], is_spec: bool) {
fn unparse_fstring_body(&mut self, values: &[ast::FStringPart]) {
for value in values {
self.unparse_f_string_elem(value, is_spec);
self.unparse_fstring_elem(value);
}
}

Expand All @@ -1300,7 +1288,7 @@ impl<'a> Generator<'a> {
val: &Expr,
debug_text: Option<&DebugText>,
conversion: ConversionFlag,
spec: Option<&Expr>,
spec: &[ast::FStringPart],
) {
let mut generator = Generator::new(self.indent, self.quote, self.line_ending);
generator.unparse_expr(val, precedence::FORMATTED_VALUE);
Expand Down Expand Up @@ -1328,50 +1316,37 @@ impl<'a> Generator<'a> {
self.p(&format!("{}", conversion as u8 as char));
}

if let Some(spec) = spec {
if !spec.is_empty() {
self.p(":");
self.unparse_f_string_elem(spec, true);
self.unparse_fstring(spec, true);
}

self.p("}");
}

fn unparse_f_string_elem(&mut self, expr: &Expr, is_spec: bool) {
match expr {
Expr::Constant(ast::ExprConstant { value, .. }) => {
if let Constant::Str(s) = value {
self.unparse_f_string_literal(s);
} else {
unreachable!()
}
}
Expr::FString(ast::ExprFString { values, range: _ }) => {
self.unparse_f_string(values, is_spec);
fn unparse_fstring_elem(&mut self, part: &ast::FStringPart) {
match part {
ast::FStringPart::String(ast::StringTodoName { value, .. }) => {
self.unparse_fstring_literal(value);
}
Expr::FormattedValue(ast::ExprFormattedValue {
value,
ast::FStringPart::FormattedValue(ast::FormattedValue {
expression,
debug_text,
conversion,
format_spec,
range: _,
}) => self.unparse_formatted(
value,
debug_text.as_ref(),
*conversion,
format_spec.as_deref(),
),
_ => unreachable!(),
}) => self.unparse_formatted(expression, debug_text.as_ref(), *conversion, format_spec),
}
}

fn unparse_f_string_literal(&mut self, s: &str) {
fn unparse_fstring_literal(&mut self, s: &str) {
let s = s.replace('{', "{{").replace('}', "}}");
self.p(&s);
}

fn unparse_f_string(&mut self, values: &[Expr], is_spec: bool) {
fn unparse_fstring(&mut self, values: &[ast::FStringPart], is_spec: bool) {
if is_spec {
self.unparse_f_string_body(values, is_spec);
self.unparse_fstring_body(values);
} else {
self.p("f");
let mut generator = Generator::new(
Expand All @@ -1382,7 +1357,7 @@ impl<'a> Generator<'a> {
},
self.line_ending,
);
generator.unparse_f_string_body(values, is_spec);
generator.unparse_fstring_body(values);
let body = &generator.buffer;
self.p_str_repr(body);
}
Expand Down Expand Up @@ -1687,7 +1662,7 @@ class Foo:
}

#[test]
fn self_documenting_f_string() {
fn self_documenting_fstring() {
assert_round_trip!(r#"f"{ chr(65) = }""#);
assert_round_trip!(r#"f"{ chr(65) = !s}""#);
assert_round_trip!(r#"f"{ chr(65) = !r}""#);
Expand Down

0 comments on commit f84a3de

Please sign in to comment.