Skip to content

Commit 218a5ba

Browse files
committed
Adding base indentation for script block formatting and smart indent
1 parent 7bb739f commit 218a5ba

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ namespace FourSlash {
310310
}
311311

312312
this.formatCodeOptions = {
313+
BaseIndentSize: 0,
313314
IndentSize: 4,
314315
TabSize: 4,
315316
NewLineCharacter: Harness.IO.newLine(),

src/server/editorServices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,7 @@ namespace ts.server {
13891389

13901390
static getDefaultFormatCodeOptions(host: ServerHost): ts.FormatCodeOptions {
13911391
return ts.clone({
1392+
BaseIndentSize: 0,
13921393
IndentSize: 4,
13931394
TabSize: 4,
13941395
NewLineCharacter: host.newLine || "\n",

src/server/protocol.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ declare namespace ts.server.protocol {
434434
/** Number of spaces to indent during formatting. Default value is 4. */
435435
indentSize?: number;
436436

437+
/** Number of additional spaces to indent during formatting to preserve base indentation (ex. script block indentation). Default value is 0. */
438+
baseIndentSize?: number;
439+
437440
/** The new line character to be used. Default value is the OS line delimiter. */
438441
newLineCharacter?: string;
439442

@@ -474,7 +477,7 @@ declare namespace ts.server.protocol {
474477
placeOpenBraceOnNewLineForControlBlocks?: boolean;
475478

476479
/** Index operator */
477-
[key: string]: string | number | boolean;
480+
[key: string]: string | number | boolean | undefined;
478481
}
479482

480483
/**

src/server/session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ namespace ts.server {
674674
if (lineText.search("\\S") < 0) {
675675
// TODO: get these options from host
676676
const editorOptions: ts.EditorOptions = {
677+
BaseIndentSize: formatOptions.BaseIndentSize,
677678
IndentSize: formatOptions.IndentSize,
678679
TabSize: formatOptions.TabSize,
679680
NewLineCharacter: formatOptions.NewLineCharacter,

src/services/formatting/smartIndenter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ namespace ts.formatting {
1010

1111
export function getIndentation(position: number, sourceFile: SourceFile, options: EditorOptions): number {
1212
if (position > sourceFile.text.length) {
13-
return 0; // past EOF
13+
return getBaseIndentation(options); // past EOF
1414
}
1515

1616
// no indentation when the indent style is set to none,
1717
// so we can return fast
1818
if (options.IndentStyle === IndentStyle.None) {
19-
return 0;
19+
return getBaseIndentation(options);
2020
}
2121

2222
const precedingToken = findPrecedingToken(position, sourceFile);
2323
if (!precedingToken) {
24-
return 0;
24+
return getBaseIndentation(options);
2525
}
2626

2727
// no indentation in string \regex\template literals
2828
const precedingTokenIsLiteral = isStringOrRegularExpressionOrTemplateLiteral(precedingToken.kind);
2929
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
30-
return 0;
30+
return getBaseIndentation(options);
3131
}
3232

3333
const lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
@@ -96,13 +96,17 @@ namespace ts.formatting {
9696
}
9797

9898
if (!current) {
99-
// no parent was found - return 0 to be indented on the level of SourceFile
100-
return 0;
99+
// no parent was found - return the base indentation of the SourceFile
100+
return getBaseIndentation(options);
101101
}
102102

103103
return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options);
104104
}
105105

106+
function getBaseIndentation(options: EditorOptions) {
107+
return options.BaseIndentSize || 0;
108+
}
109+
106110
export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number {
107111
const start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile));
108112
return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options);
@@ -162,7 +166,7 @@ namespace ts.formatting {
162166
parent = current.parent;
163167
}
164168

165-
return indentationDelta;
169+
return indentationDelta + getBaseIndentation(options);
166170
}
167171

168172

src/services/services.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ namespace ts {
12441244
}
12451245

12461246
export interface EditorOptions {
1247+
BaseIndentSize?: number;
12471248
IndentSize: number;
12481249
TabSize: number;
12491250
NewLineCharacter: string;
@@ -1268,7 +1269,7 @@ namespace ts {
12681269
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean;
12691270
PlaceOpenBraceOnNewLineForFunctions: boolean;
12701271
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
1271-
[s: string]: boolean | number | string;
1272+
[s: string]: boolean | number | string | undefined;
12721273
}
12731274

12741275
export interface DefinitionInfo {

0 commit comments

Comments
 (0)