Skip to content

Commit

Permalink
Run cargo fmt (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 15, 2023
1 parent b219d5c commit 41c10f8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 111 deletions.
7 changes: 1 addition & 6 deletions crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,12 +1560,7 @@ where
pyflakes::rules::assert_tuple(self, stmt, test);
}
if self.settings.rules.enabled(&Rule::AssertFalse) {
flake8_bugbear::rules::assert_false(
self,
stmt,
test,
msg.as_deref(),
);
flake8_bugbear::rules::assert_false(self, stmt, test, msg.as_deref());
}
if self.settings.rules.enabled(&Rule::Assert) {
self.diagnostics
Expand Down
7 changes: 0 additions & 7 deletions crates/ruff_fmt/Cargo.lock

This file was deleted.

2 changes: 1 addition & 1 deletion crates/ruff_fmt/src/format/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn block(body: &[Stmt]) -> Block {
Block { body }
}

pub(crate) const fn join_names( names: &[String]) -> JoinNames {
pub(crate) const fn join_names(names: &[String]) -> JoinNames {
JoinNames { names }
}

Expand Down
212 changes: 115 additions & 97 deletions crates/ruff_fmt/src/format/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ fn format_delete(f: &mut Formatter<ASTFormatContext<'_>>, targets: &[Expr]) -> F
})),
if_group_breaks(&text(")")),
])
])
]
)
}
}
}
Expand All @@ -106,7 +107,14 @@ fn format_class_def(
write!(f, [text("@"), decorator.format(), hard_line_break()])?;
}

write!(f, [text("class"), space(), dynamic_text(name, TextSize::default())])?;
write!(
f,
[
text("class"),
space(),
dynamic_text(name, TextSize::default())
]
)?;

if !bases.is_empty() || !keywords.is_empty() {
let format_bases = format_with(|f| {
Expand All @@ -121,7 +129,14 @@ fn format_class_def(

for (i, keyword) in keywords.iter().enumerate() {
if let Some(arg) = &keyword.node.arg {
write!(f, [dynamic_text(arg, TextSize::default()), text("="), keyword.node.value.format()])?;
write!(
f,
[
dynamic_text(arg, TextSize::default()),
text("="),
keyword.node.value.format()
]
)?;
} else {
write!(f, [text("**"), keyword.node.value.format()])?;
}
Expand All @@ -141,7 +156,8 @@ fn format_class_def(
text("("),
group(&soft_block_indent(&format_bases)),
text(")")
])?;
]
)?;
}

write!(f, [text(":"), block_indent(&block(body))])
Expand All @@ -163,25 +179,26 @@ fn format_func_def(
if async_ {
write!(f, [text("async"), space()])?;
}
write!(f, [
text("def"),
space(),
dynamic_text(name, TextSize::default()),
text("("),

group(&soft_block_indent(&format_with(|f| {
if stmt
.trivia
.iter()
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma))
{
write!(f, [expand_parent()])?;
}
write!(f, [args.format()])
}))),
text(")")
])?;

write!(
f,
[
text("def"),
space(),
dynamic_text(name, TextSize::default()),
text("("),
group(&soft_block_indent(&format_with(|f| {
if stmt
.trivia
.iter()
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma))
{
write!(f, [expand_parent()])?;
}
write!(f, [args.format()])
}))),
text(")")
]
)?;

if let Some(returns) = returns {
write!(f, [text(" -> "), returns.format()])?;
Expand Down Expand Up @@ -279,16 +296,19 @@ fn format_ann_assign(
write!(f, [text(": "), annotation.format()])?;

if let Some(value) = value {
write!(f, [
space(),
text("="),
space(),
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&value.format()),
if_group_breaks(&text(")")),
])
])?;
write!(
f,
[
space(),
text("="),
space(),
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&value.format()),
if_group_breaks(&text(")")),
])
]
)?;
}

Ok(())
Expand All @@ -303,17 +323,20 @@ fn format_for(
_orelse: &[Stmt],
_type_comment: Option<&str>,
) -> FormatResult<()> {
write!(f, [
text("for"),
space(),
target.format(),
space(),
text("in"),
space(),
group(&iter.format()),
text(":"),
block_indent(&block(body))
])
write!(
f,
[
text("for"),
space(),
target.format(),
space(),
text("in"),
space(),
group(&iter.format()),
text(":"),
block_indent(&block(body))
]
)
}

fn format_while(
Expand Down Expand Up @@ -435,26 +458,28 @@ fn format_import(
stmt: &Stmt,
names: &[Alias],
) -> FormatResult<()> {
write!(f, [
text("import"),
space(),

group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&format_with(|f| {
for (i, name) in names.iter().enumerate() {
write!(f, [name.format()])?;
if i < names.len() - 1 {
write!(f, [text(","), soft_line_break_or_space()])?;
} else {
write!(f, [if_group_breaks(&text(","))])?;
write!(
f,
[
text("import"),
space(),
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&format_with(|f| {
for (i, name) in names.iter().enumerate() {
write!(f, [name.format()])?;
if i < names.len() - 1 {
write!(f, [text(","), soft_line_break_or_space()])?;
} else {
write!(f, [if_group_breaks(&text(","))])?;
}
}
}
Ok(())
})),
if_group_breaks(&text(")")),
])
])
Ok(())
})),
if_group_breaks(&text(")")),
])
]
)
}

fn format_import_from(
Expand Down Expand Up @@ -564,27 +589,30 @@ fn format_with_(
write!(f, [text("async"), space()])?;
}

write!(f, [
text("with"),
space(),
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&format_with(|f| {
for (i, item) in items.iter().enumerate() {
write!(f, [item.format()])?;
if i < items.len() - 1 {
write!(f, [text(","), soft_line_break_or_space()])?;
} else {
write!(f, [if_group_breaks(&text(","))])?;
write!(
f,
[
text("with"),
space(),
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&format_with(|f| {
for (i, item) in items.iter().enumerate() {
write!(f, [item.format()])?;
if i < items.len() - 1 {
write!(f, [text(","), soft_line_break_or_space()])?;
} else {
write!(f, [if_group_breaks(&text(","))])?;
}
}
}
Ok(())
})),
if_group_breaks(&text(")")),
]),
text(":"),
block_indent(&block(body))
])
Ok(())
})),
if_group_breaks(&text(")")),
]),
text(":"),
block_indent(&block(body))
]
)
}

pub struct FormatStmt<'a> {
Expand Down Expand Up @@ -664,14 +692,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
annotation,
value,
simple,
} => format_ann_assign(
f,
self.item,
target,
annotation,
value.as_deref(),
*simple,
),
} => format_ann_assign(f, self.item, target, annotation, value.as_deref(), *simple),
StmtKind::For {
target,
iter,
Expand Down Expand Up @@ -717,12 +738,9 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
true,
),
// StmtKind::Match { .. } => {}
StmtKind::Raise { exc, cause } => format_raise(
f,
self.item,
exc.as_deref(),
cause.as_deref(),
),
StmtKind::Raise { exc, cause } => {
format_raise(f, self.item, exc.as_deref(), cause.as_deref())
}
// StmtKind::Try { .. } => {}
StmtKind::Assert { test, msg } => {
format_assert(f, self.item, test, msg.as_ref().map(|expr| &**expr))
Expand Down

0 comments on commit 41c10f8

Please sign in to comment.