Skip to content

Commit

Permalink
format ExprJoinedStr
Browse files Browse the repository at this point in the history
wip to check direction

currently generates invalid python because it doesn't know to switch
quote style when the nesting is inside a formatted value like in
`f"MOAR {' '.join([])}"` so it makes the inner quotes double as well
  • Loading branch information
davidszotten committed Jul 20, 2023
1 parent e1d76b6 commit 83da9a0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ wsl = { version = "0.1.0" }
# v1.0.1
libcst = { git = "https://github.com/Instagram/LibCST.git", rev = "3cacca1a1029f05707e50703b49fe3dd860aa839", default-features = false }

ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["full-lexer", "num-bigint"] }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "472f0d480650bb1c3c95596fcb910895b8d25ab5" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "472f0d480650bb1c3c95596fcb910895b8d25ab5" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "472f0d480650bb1c3c95596fcb910895b8d25ab5", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "472f0d480650bb1c3c95596fcb910895b8d25ab5", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "472f0d480650bb1c3c95596fcb910895b8d25ab5" , default-features = false, features = ["full-lexer", "num-bigint"] }

[profile.release]
lto = "fat"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::context::PyFormatContext;
use crate::expression::parentheses::Parentheses;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use rustpython_parser::ast::ExprFormattedValue;
Expand All @@ -10,7 +12,21 @@ pub struct FormatExprFormattedValue;

impl FormatNodeRule<ExprFormattedValue> for FormatExprFormattedValue {
fn fmt_fields(&self, item: &ExprFormattedValue, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let ExprFormattedValue {
range: _,
value,
// TODO
conversion: _,
format_spec: _,
} = item;
write!(
f,
[
text("{"),
value.format().with_options(Parentheses::Never),
text("}")
]
)
}
}

Expand Down
42 changes: 32 additions & 10 deletions crates/ruff_python_formatter/src/expression/expr_joined_str.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
use crate::context::PyFormatContext;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
use crate::expression::string::{StringPrefix, StringQuotes};
use crate::expression::Expr;
use crate::prelude::*;
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatError, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use rustpython_parser::ast::ExprJoinedStr;

#[derive(Default)]
pub struct FormatExprJoinedStr;

impl FormatNodeRule<ExprJoinedStr> for FormatExprJoinedStr {
fn fmt_fields(&self, _item: &ExprJoinedStr, f: &mut PyFormatter) -> FormatResult<()> {
write!(
f,
[not_yet_implemented_custom_text(
r#"f"NOT_YET_IMPLEMENTED_ExprJoinedStr""#
)]
)
fn fmt_fields(&self, item: &ExprJoinedStr, f: &mut PyFormatter) -> FormatResult<()> {
let ExprJoinedStr { range, values } = item;

// TODO: can we re-use instead of copy/paste?
let string_content = f.context().locator().slice(*range);
let prefix = StringPrefix::parse(string_content);
let after_prefix = &string_content[usize::from(prefix.text_len())..];

let quotes = StringQuotes::parse(after_prefix).ok_or(FormatError::syntax_error(
"Didn't find string quotes after prefix",
))?;

prefix.fmt(f)?;
quotes.fmt(f)?;
// TODO: join_with empty? or some other join?
for value in values {
match value {
// TODO: can we do better?
Expr::Constant(_) => write!(f, [verbatim_text(value)])?,
Expr::FormattedValue(_) => {
write!(f, [value.format().with_options(Parentheses::Never)])?;
}
_ => unreachable!(),
};
}
quotes.fmt(f)
}
}

Expand Down

0 comments on commit 83da9a0

Please sign in to comment.