Skip to content

Commit 2b9d07c

Browse files
authored
chore: stop running generated Python though black (#1864)
Running `black` on the generated source is slow... And generating *properly* formatted code is faster. This removes `black` invocations and instead tries to generate Python that is closer to the prescriptions of PEP8 (more specifically, closer to the output of `black`). There are some minor changes introduced by this change in the generated Python files, compared to when `black` was post-processing, which are hopefully acceptable degradations: - Some long type signatures are single-lined now where `black` would have broken them down into multiple lines. Reproducing the `black` behavior is too close to requiring a full blown python parser. - Certain lines that `black` chose not to break down into multiple lines are now split. I could not understand the mechanisms `black` uses to make it's choice and decided the broken down lines are readable enough. - Some `pass` statements used to be generated in proxies when there were no members, although there existed a synthetic member. Those statements were not sparking joy and were gently disposed of. - Alphanumerically ordered elements in piecemeal import declarations, so as to reduce the likelyhood of fake changes if the order of processing changes (resulting in a different creation/registration order). Fixes #1856 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 5431262 commit 2b9d07c

7 files changed

Lines changed: 503 additions & 461 deletions

File tree

packages/codemaker/lib/codemaker.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class CodeMaker {
1616
private readonly files = new Array<FileBuffer>();
1717
private readonly excludes = new Array<string>();
1818

19+
public get currentIndentLength(): number {
20+
return this.currIndent * this.indentation;
21+
}
22+
1923
/**
2024
* Formats an block open statement.
2125
*/
@@ -24,7 +28,7 @@ export class CodeMaker {
2428
/**
2529
* Formats a block close statement.
2630
*/
27-
public closeBlockFormatter: (s?: string) => string = () => '}';
31+
public closeBlockFormatter: (s?: string) => string | false = () => '}';
2832

2933
/**
3034
* Saves all the files created in this code maker.
@@ -102,7 +106,7 @@ export class CodeMaker {
102106
/**
103107
* Same as `close`.
104108
*/
105-
public unindent(textAfter?: string) {
109+
public unindent(textAfter?: string | false) {
106110
this.close(textAfter);
107111
}
108112

@@ -118,10 +122,14 @@ export class CodeMaker {
118122
/**
119123
* Decreases the indentation level by `indentation` for the next line.
120124
* @param textAfter Text to emit in the line after indentation was decreased.
125+
* If `false` no line will be emitted at all, but the indent
126+
* counter will be decremented.
121127
*/
122-
public close(textAfter?: string) {
128+
public close(textAfter?: string | false) {
123129
this.currIndent--;
124-
this.line(textAfter);
130+
if (textAfter !== false) {
131+
this.line(textAfter);
132+
}
125133
}
126134

127135
/**
@@ -170,13 +178,11 @@ export class CodeMaker {
170178
return caseutils.toSnakeCase(s, sep);
171179
}
172180

173-
private makeIndent() {
174-
let spaces = '';
175-
for (let i = 0; i < this.currIndent; ++i) {
176-
for (let j = 0; j < this.indentation; ++j) {
177-
spaces += ' ';
178-
}
181+
private makeIndent(): string {
182+
const length = this.currentIndentLength;
183+
if (length <= 0) {
184+
return '';
179185
}
180-
return spaces;
186+
return ' '.repeat(length);
181187
}
182188
}

0 commit comments

Comments
 (0)