mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Reduce newlines in code gen (#807)
This commit is contained in:
parent
6ffe767252
commit
ee31fa6109
2 changed files with 98 additions and 102 deletions
182
src/code_gen.rs
182
src/code_gen.rs
|
@ -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)?;
|
});
|
||||||
if self.indentation == 0 {
|
self.body(body)?;
|
||||||
self.newlines(2)?;
|
if self.indentation == 0 {
|
||||||
}
|
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)?;
|
});
|
||||||
if self.indentation == 0 {
|
self.body(body)?;
|
||||||
self.newlines(2)?;
|
if self.indentation == 0 {
|
||||||
}
|
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)?;
|
});
|
||||||
if !orelse.is_empty() {
|
self.body(body)?;
|
||||||
statement!({
|
if !orelse.is_empty() {
|
||||||
self.p("else:")?;
|
statement!({
|
||||||
self.body(orelse)?;
|
self.p("else:")?;
|
||||||
});
|
});
|
||||||
}
|
self.body(orelse)?;
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
StmtKind::AsyncFor {
|
StmtKind::AsyncFor {
|
||||||
target,
|
target,
|
||||||
|
@ -321,59 +317,59 @@ 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)?;
|
});
|
||||||
if !orelse.is_empty() {
|
self.body(body)?;
|
||||||
statement!({
|
if !orelse.is_empty() {
|
||||||
self.p("else:")?;
|
statement!({
|
||||||
self.body(orelse)?;
|
self.p("else:")?;
|
||||||
});
|
});
|
||||||
}
|
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)?;
|
});
|
||||||
if !orelse.is_empty() {
|
self.body(body)?;
|
||||||
statement!({
|
if !orelse.is_empty() {
|
||||||
self.p("else:")?;
|
statement!({
|
||||||
self.body(orelse)?;
|
self.p("else:")?;
|
||||||
});
|
});
|
||||||
}
|
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)?;
|
|
||||||
|
|
||||||
let mut orelse_: &Vec<Stmt<U>> = orelse;
|
|
||||||
loop {
|
|
||||||
if orelse_.len() == 1 && matches!(orelse_[0].node, StmtKind::If { .. }) {
|
|
||||||
if let StmtKind::If { body, test, orelse } = &orelse_[0].node {
|
|
||||||
statement!({
|
|
||||||
self.p("elif ")?;
|
|
||||||
self.unparse_expr(test, precedence::TEST)?;
|
|
||||||
self.p(":")?;
|
|
||||||
self.body(body)?;
|
|
||||||
});
|
|
||||||
orelse_ = orelse;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !orelse_.is_empty() {
|
|
||||||
statement!({
|
|
||||||
self.p("else:")?;
|
|
||||||
self.body(orelse_)?;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
self.body(body)?;
|
||||||
|
|
||||||
|
let mut orelse_: &Vec<Stmt<U>> = orelse;
|
||||||
|
loop {
|
||||||
|
if orelse_.len() == 1 && matches!(orelse_[0].node, StmtKind::If { .. }) {
|
||||||
|
if let StmtKind::If { body, test, orelse } = &orelse_[0].node {
|
||||||
|
statement!({
|
||||||
|
self.p("elif ")?;
|
||||||
|
self.unparse_expr(test, precedence::TEST)?;
|
||||||
|
self.p(":")?;
|
||||||
|
});
|
||||||
|
self.body(body)?;
|
||||||
|
orelse_ = orelse;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !orelse_.is_empty() {
|
||||||
|
statement!({
|
||||||
|
self.p("else:")?;
|
||||||
|
});
|
||||||
|
self.body(orelse_)?;
|
||||||
|
}
|
||||||
|
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,27 +417,27 @@ impl SourceGenerator {
|
||||||
} => {
|
} => {
|
||||||
statement!({
|
statement!({
|
||||||
self.p("try:")?;
|
self.p("try:")?;
|
||||||
self.body(body)?;
|
});
|
||||||
|
self.body(body)?;
|
||||||
|
|
||||||
for handler in handlers {
|
for handler in handlers {
|
||||||
statement!({
|
statement!({
|
||||||
self.unparse_excepthandler(handler)?;
|
self.unparse_excepthandler(handler)?;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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!({
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue