Reduce newlines in code gen (#807)

This commit is contained in:
Charlie Marsh 2022-11-18 12:27:56 -05:00 committed by GitHub
parent 6ffe767252
commit ee31fa6109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 102 deletions

View file

@ -55,18 +55,14 @@ impl SourceGenerator {
} }
fn newline(&mut self) -> fmt::Result { fn newline(&mut self) -> fmt::Result {
if self.initial { if !self.initial {
self.initial = false;
} else {
self.new_lines = std::cmp::max(self.new_lines, 1); self.new_lines = std::cmp::max(self.new_lines, 1);
} }
Ok(()) Ok(())
} }
fn newlines(&mut self, extra: usize) -> fmt::Result { fn newlines(&mut self, extra: usize) -> fmt::Result {
if self.initial { if !self.initial {
self.initial = false;
} else {
self.new_lines = std::cmp::max(self.new_lines, 1 + extra); self.new_lines = std::cmp::max(self.new_lines, 1 + extra);
} }
Ok(()) Ok(())
@ -121,6 +117,7 @@ impl SourceGenerator {
self.newline()?; self.newline()?;
self.p(&" ".repeat(self.indentation))?; self.p(&" ".repeat(self.indentation))?;
$body $body
self.initial = false;
}}; }};
} }
@ -145,12 +142,11 @@ impl SourceGenerator {
self.unparse_expr(returns, precedence::EXPR)?; self.unparse_expr(returns, precedence::EXPR)?;
} }
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if self.indentation == 0 { if self.indentation == 0 {
self.newlines(2)?; self.newlines(2)?;
} }
})
} }
StmtKind::AsyncFunctionDef { StmtKind::AsyncFunctionDef {
name, name,
@ -172,11 +168,11 @@ impl SourceGenerator {
self.unparse_expr(returns, precedence::EXPR)?; self.unparse_expr(returns, precedence::EXPR)?;
} }
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if self.indentation == 0 { if self.indentation == 0 {
self.newlines(2)?; self.newlines(2)?;
} }
})
} }
StmtKind::ClassDef { StmtKind::ClassDef {
name, name,
@ -209,11 +205,11 @@ impl SourceGenerator {
} }
self.p_if(!first, ")")?; self.p_if(!first, ")")?;
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if self.indentation == 0 { if self.indentation == 0 {
self.newlines(2)?; self.newlines(2)?;
} }
})
} }
StmtKind::Return { value } => { StmtKind::Return { value } => {
statement!({ statement!({
@ -299,14 +295,14 @@ impl SourceGenerator {
self.p(" in ")?; self.p(" in ")?;
self.unparse_expr(iter, precedence::TEST)?; self.unparse_expr(iter, precedence::TEST)?;
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if !orelse.is_empty() { if !orelse.is_empty() {
statement!({ statement!({
self.p("else:")?; self.p("else:")?;
self.body(orelse)?;
}); });
self.body(orelse)?;
} }
})
} }
StmtKind::AsyncFor { StmtKind::AsyncFor {
target, target,
@ -321,34 +317,35 @@ impl SourceGenerator {
self.p(" in ")?; self.p(" in ")?;
self.unparse_expr(iter, precedence::TEST)?; self.unparse_expr(iter, precedence::TEST)?;
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if !orelse.is_empty() { if !orelse.is_empty() {
statement!({ statement!({
self.p("else:")?; self.p("else:")?;
self.body(orelse)?;
}); });
self.body(orelse)?;
} }
})
} }
StmtKind::While { test, body, orelse } => { StmtKind::While { test, body, orelse } => {
statement!({ statement!({
self.p("while ")?; self.p("while ")?;
self.unparse_expr(test, precedence::TEST)?; self.unparse_expr(test, precedence::TEST)?;
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
if !orelse.is_empty() { if !orelse.is_empty() {
statement!({ statement!({
self.p("else:")?; self.p("else:")?;
self.body(orelse)?;
}); });
self.body(orelse)?;
} }
})
} }
StmtKind::If { test, body, orelse } => { StmtKind::If { test, body, orelse } => {
statement!({ statement!({
self.p("if ")?; self.p("if ")?;
self.unparse_expr(test, precedence::TEST)?; self.unparse_expr(test, precedence::TEST)?;
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
let mut orelse_: &Vec<Stmt<U>> = orelse; let mut orelse_: &Vec<Stmt<U>> = orelse;
@ -359,21 +356,20 @@ impl SourceGenerator {
self.p("elif ")?; self.p("elif ")?;
self.unparse_expr(test, precedence::TEST)?; self.unparse_expr(test, precedence::TEST)?;
self.p(":")?; self.p(":")?;
self.body(body)?;
}); });
self.body(body)?;
orelse_ = orelse; orelse_ = orelse;
} }
} else { } else {
if !orelse_.is_empty() { if !orelse_.is_empty() {
statement!({ statement!({
self.p("else:")?; self.p("else:")?;
self.body(orelse_)?;
}); });
self.body(orelse_)?;
} }
break; break;
} }
} }
});
} }
StmtKind::With { items, body, .. } => { StmtKind::With { items, body, .. } => {
statement!({ statement!({
@ -384,8 +380,8 @@ impl SourceGenerator {
self.unparse_withitem(item)?; self.unparse_withitem(item)?;
} }
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
})
} }
StmtKind::AsyncWith { items, body, .. } => { StmtKind::AsyncWith { items, body, .. } => {
statement!({ statement!({
@ -396,8 +392,8 @@ impl SourceGenerator {
self.unparse_withitem(item)?; self.unparse_withitem(item)?;
} }
self.p(":")?; self.p(":")?;
});
self.body(body)?; self.body(body)?;
})
} }
StmtKind::Match { .. } => {} StmtKind::Match { .. } => {}
StmtKind::Raise { exc, cause } => { StmtKind::Raise { exc, cause } => {
@ -421,6 +417,7 @@ impl SourceGenerator {
} => { } => {
statement!({ statement!({
self.p("try:")?; self.p("try:")?;
});
self.body(body)?; self.body(body)?;
for handler in handlers { for handler in handlers {
@ -432,16 +429,15 @@ impl SourceGenerator {
if !orelse.is_empty() { if !orelse.is_empty() {
statement!({ statement!({
self.p("else:")?; self.p("else:")?;
self.body(orelse)?;
}); });
self.body(orelse)?;
} }
if !finalbody.is_empty() { if !finalbody.is_empty() {
statement!({ statement!({
self.p("finally:")?; self.p("finally:")?;
self.body(finalbody)?;
}); });
self.body(finalbody)?;
} }
})
} }
StmtKind::Assert { test, msg } => { StmtKind::Assert { test, msg } => {
statement!({ statement!({

View file

@ -11,7 +11,7 @@ expression: checks
column: 52 column: 52
fix: fix:
patch: patch:
content: "\nclass MyType1(TypedDict):\n a: int\n b: str" content: "class MyType1(TypedDict):\n a: int\n b: str"
location: location:
row: 4 row: 4
column: 0 column: 0
@ -28,7 +28,7 @@ expression: checks
column: 50 column: 50
fix: fix:
patch: patch:
content: "\nclass MyType2(TypedDict):\n a: int\n b: str" content: "class MyType2(TypedDict):\n a: int\n b: str"
location: location:
row: 7 row: 7
column: 0 column: 0
@ -45,7 +45,7 @@ expression: checks
column: 44 column: 44
fix: fix:
patch: patch:
content: "\nclass MyType3(TypedDict):\n a: int\n b: str" content: "class MyType3(TypedDict):\n a: int\n b: str"
location: location:
row: 10 row: 10
column: 0 column: 0
@ -62,7 +62,7 @@ expression: checks
column: 30 column: 30
fix: fix:
patch: patch:
content: "\nclass MyType4(TypedDict):\n pass" content: "class MyType4(TypedDict):\n pass"
location: location:
row: 13 row: 13
column: 0 column: 0
@ -79,7 +79,7 @@ expression: checks
column: 46 column: 46
fix: fix:
patch: patch:
content: "\nclass MyType5(TypedDict):\n a: 'hello'" content: "class MyType5(TypedDict):\n a: 'hello'"
location: location:
row: 16 row: 16
column: 0 column: 0
@ -96,7 +96,7 @@ expression: checks
column: 41 column: 41
fix: fix:
patch: patch:
content: "\nclass MyType6(TypedDict):\n a: 'hello'" content: "class MyType6(TypedDict):\n a: 'hello'"
location: location:
row: 17 row: 17
column: 0 column: 0
@ -113,7 +113,7 @@ expression: checks
column: 56 column: 56
fix: fix:
patch: patch:
content: "\nclass MyType7(TypedDict):\n a: NotRequired[dict]" content: "class MyType7(TypedDict):\n a: NotRequired[dict]"
location: location:
row: 20 row: 20
column: 0 column: 0
@ -130,7 +130,7 @@ expression: checks
column: 65 column: 65
fix: fix:
patch: patch:
content: "\nclass MyType8(TypedDict, total=False):\n x: int\n y: int" content: "class MyType8(TypedDict, total=False):\n x: int\n y: int"
location: location:
row: 23 row: 23
column: 0 column: 0
@ -147,7 +147,7 @@ expression: checks
column: 59 column: 59
fix: fix:
patch: patch:
content: "\nclass MyType10(TypedDict):\n key: Literal['value']" content: "class MyType10(TypedDict):\n key: Literal['value']"
location: location:
row: 29 row: 29
column: 0 column: 0