Skip to content

Commit 2725b24

Browse files
author
Soc Sieng
committed
Add option to change indent string
1 parent 7c9d7d6 commit 2725b24

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/render/indentor.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
export default class Indentor {
2-
private _indentCache: string[];
3-
private _indentString: string;
2+
public static indentString: string = ' ';
43

5-
constructor(indentString: string = ' ') {
6-
this._indentCache = [];
4+
private _indentCache: { [key: string]: string };
5+
private _indentString?: string;
6+
7+
constructor(indentString?: string) {
8+
this._indentCache = {};
79
this._indentString = indentString;
810
}
911

1012
public getIndent(size: number): string {
1113
if (size === 0) return '';
1214

13-
let indent = this._indentCache[size];
15+
const indentString = this._indentString ?? Indentor.indentString;
16+
const key = `${indentString}_${size}`;
17+
18+
let indent = this._indentCache[key];
1419
if (indent === undefined) {
15-
indent = new Array(size).fill(this._indentString).join('');
20+
indent = new Array(size).fill(indentString).join('');
21+
this._indentCache[key] = indent;
1622
}
1723
return indent;
1824
}

src/render/reflection-formatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const sortMapping: { [key: string]: ReflectionSortFlags | undefined } = {
2525

2626
export default class ReflectionFormatter {
2727
public static sortOption: ReflectionSortFlags = ReflectionSortFlags.none;
28+
public static indentString: string = ' ';
2829

2930
public render(reflection?: Reflection, terminatorCharater?: string): string {
3031
if (reflection) {

src/typescript-declaration-plugin.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Renderer } from 'typedoc/dist/lib/output/renderer';
88
import { RendererComponent } from 'typedoc/dist/lib/output/components';
99
import { RendererEvent } from 'typedoc/dist/lib/output/events';
1010
import mkdir from 'make-dir';
11+
import Indentor from './render/indentor';
1112

1213
const declarationFileOption = {
1314
name: 'declarationFile',
@@ -30,11 +31,19 @@ const sortOptionOption = {
3031
map: sortMapping,
3132
} as DeclarationOption;
3233

34+
const indentStringOption = {
35+
name: 'indentString',
36+
type: ParameterType.String,
37+
help: 'Indent declarations using this string. Defaults to \' \'',
38+
defaultValue: ' ',
39+
} as DeclarationOption;
40+
3341
export class TypeScriptDeclarationPlugin extends RendererComponent {
3442
static options = [
3543
declarationFileOption,
3644
declarationOnlyOption,
3745
sortOptionOption,
46+
indentStringOption,
3847
];
3948

4049
@BindOption(declarationFileOption.name)
@@ -76,9 +85,11 @@ export class TypeScriptDeclarationPlugin extends RendererComponent {
7685
.filter(c => c.componentName !== 'typescript-declaration')
7786
.forEach(c => app.renderer.removeComponent(c.componentName));
7887
}
88+
89+
Indentor.indentString = app.options.getValue('indentString') as string;
7990
}
8091

81-
private veriftProject(project: ProjectReflection) {
92+
private verifyProject(project: ProjectReflection) {
8293
if (this._declarationOnly && !this._declarationFile) {
8394
throw new Error('--declarationFile file must be specified when using the --declarationOnly option');
8495
}
@@ -97,7 +108,7 @@ export class TypeScriptDeclarationPlugin extends RendererComponent {
97108
}
98109

99110
private onRenderBegin(event: RendererEvent) {
100-
this.veriftProject(event.project);
111+
this.verifyProject(event.project);
101112
}
102113

103114
public static generateTypeDeclarations(project: ProjectReflection, sortOption: ReflectionSortFlags, filename?: string) {

0 commit comments

Comments
 (0)