Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions packages/codemaker/lib/codemaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class CodeMaker {
private readonly files = new Array<FileBuffer>();
private readonly excludes = new Array<string>();

public get currentIndentLength(): number {
return this.currIndent * this.indentation;
}

/**
* Formats an block open statement.
*/
Expand All @@ -24,7 +28,7 @@ export class CodeMaker {
/**
* Formats a block close statement.
*/
public closeBlockFormatter: (s?: string) => string = () => '}';
public closeBlockFormatter: (s?: string) => string | false = () => '}';

/**
* Saves all the files created in this code maker.
Expand Down Expand Up @@ -102,7 +106,7 @@ export class CodeMaker {
/**
* Same as `close`.
*/
public unindent(textAfter?: string) {
public unindent(textAfter?: string | false) {
this.close(textAfter);
}

Expand All @@ -118,10 +122,14 @@ export class CodeMaker {
/**
* Decreases the indentation level by `indentation` for the next line.
* @param textAfter Text to emit in the line after indentation was decreased.
* If `false` no line will be emitted at all, but the indent
* counter will be decremented.
*/
public close(textAfter?: string) {
public close(textAfter?: string | false) {
this.currIndent--;
this.line(textAfter);
if (textAfter !== false) {
this.line(textAfter);
}
}

/**
Expand Down Expand Up @@ -170,13 +178,11 @@ export class CodeMaker {
return caseutils.toSnakeCase(s, sep);
}

private makeIndent() {
let spaces = '';
for (let i = 0; i < this.currIndent; ++i) {
for (let j = 0; j < this.indentation; ++j) {
spaces += ' ';
}
private makeIndent(): string {
const length = this.currentIndentLength;
if (length <= 0) {
return '';
}
return spaces;
return ' '.repeat(length);
}
}
Loading