-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathcode-printer.ts
More file actions
62 lines (53 loc) · 1.69 KB
/
code-printer.ts
File metadata and controls
62 lines (53 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as _ from "lodash";
import { EOL } from "os";
import { CodeEntityType } from "./code-entity";
import { CodeGeneration } from "./code-generation";
import { injector } from "../yok";
export class CodePrinter {
private static INDENT_CHAR = "\t";
private static NEW_LINE_CHAR = EOL;
private static START_BLOCK_CHAR = " {";
private static END_BLOCK_CHAR = "}";
public composeBlock(
block: CodeGeneration.IBlock,
indentSize?: number
): string {
indentSize = indentSize === undefined ? 0 : indentSize;
let content = this.getIndentation(indentSize);
if (block.opener) {
content += block.opener;
content += CodePrinter.START_BLOCK_CHAR;
content += CodePrinter.NEW_LINE_CHAR;
}
_.each(block.codeEntities, (codeEntity: CodeGeneration.ICodeEntity) => {
if (codeEntity.codeEntityType === CodeEntityType.Line) {
content += this.composeLine(
<CodeGeneration.ILine>codeEntity,
indentSize + 1
);
} else if (codeEntity.codeEntityType === CodeEntityType.Block) {
content += this.composeBlock(
<CodeGeneration.IBlock>codeEntity,
indentSize + 1
);
}
});
if (block.opener) {
content += this.getIndentation(indentSize);
content += CodePrinter.END_BLOCK_CHAR;
content += block.endingCharacter || "";
content += CodePrinter.NEW_LINE_CHAR;
}
return content;
}
private getIndentation(indentSize: number): string {
return Array(indentSize).join(CodePrinter.INDENT_CHAR);
}
private composeLine(line: CodeGeneration.ILine, indentSize: number): string {
let content = this.getIndentation(indentSize);
content += line.content;
content += CodePrinter.NEW_LINE_CHAR;
return content;
}
}
injector.register("swaggerCodePrinter", CodePrinter);