Skip to content

Commit

Permalink
refactor(ruff_fmt): More idiomatic use of formatter API (#3)
Browse files Browse the repository at this point in the history
This PR makes the formatter code more idiomatic by:

* Unifying sequential `write!` calls into a single `write`
* Replace `format_args![item]` with `item` (`format_args` is to format multiple arguments). You can think of `format_args` as a `Format` implementation for tuples of arbitrary size
* Return an object that implements `Format` for `join_names` so that it can be used as part of the DSL `write!(f, [space, join_names(names)])`
* Use `space` instead of `text(" ")`
  • Loading branch information
MichaReiser authored and charliermarsh committed Feb 14, 2023
1 parent eb8874b commit 437a80a
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 199 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/ast/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ where
StmtKind::FunctionDef { .. } | StmtKind::AsyncFunctionDef { .. } => {
// Don't recurse.
}
StmtKind::Return { value } => self.returns.push(value.as_ref().map(|expr| &**expr)),
StmtKind::Return { value } => self.returns.push(value.as_deref()),
_ => visitor::walk_stmt(self, stmt),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ where
self,
stmt,
test,
msg.as_ref().map(|expr| &**expr),
msg.as_deref(),
);
}
if self.settings.rules.enabled(&Rule::Assert) {
Expand Down
20 changes: 15 additions & 5 deletions crates/ruff_fmt/src/format/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ pub fn block(body: &[Stmt]) -> Block {
Block { body }
}

pub fn join_names<Context>(f: &mut Formatter<Context>, names: &[String]) -> FormatResult<()> {
let mut join = f.join_with(text(", "));
for name in names {
join.entry(&dynamic_text(name, TextSize::default()));
pub(crate) const fn join_names( names: &[String]) -> JoinNames {
JoinNames { names }
}

pub(crate) struct JoinNames<'a> {
names: &'a [String],
}

impl<Context> Format<Context> for JoinNames<'_> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
let mut join = f.join_with(text(", "));
for name in self.names {
join.entry(&dynamic_text(name, TextSize::default()));
}
join.finish()
}
join.finish()
}
10 changes: 5 additions & 5 deletions crates/ruff_fmt/src/format/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
}
ExprKind::Await { value } => format_await(f, self.item, value),
ExprKind::Yield { value } => {
format_yield(f, self.item, value.as_ref().map(|expr| &**expr))
format_yield(f, self.item, value.as_deref())
}
ExprKind::YieldFrom { value } => format_yield_from(f, self.item, value),
ExprKind::Compare {
Expand All @@ -864,7 +864,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
// ExprKind::FormattedValue { .. } => {}
ExprKind::JoinedStr { values } => format_joined_str(f, self.item, values),
ExprKind::Constant { value, kind } => {
format_constant(f, self.item, value, kind.as_ref().map(String::as_str))
format_constant(f, self.item, value, kind.as_deref())
}
ExprKind::Attribute { value, attr, .. } => format_attribute(f, self.item, value, attr),
ExprKind::Subscript { value, slice, .. } => {
Expand All @@ -877,9 +877,9 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
ExprKind::Slice { lower, upper, step } => format_slice(
f,
self.item,
lower.as_ref().map(|expr| &**expr),
upper.as_ref().map(|expr| &**expr),
step.as_ref().map(|expr| &**expr),
lower.as_deref(),
upper.as_deref(),
step.as_deref(),
),
_ => {
unimplemented!("Implement ExprKind: {:?}", self.item.node)
Expand Down
Loading

0 comments on commit 437a80a

Please sign in to comment.