From 218a5ba0bb9fd78ea9b06f21323199bad7ba77ec Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Fri, 17 Jun 2016 13:02:15 -0700 Subject: [PATCH] Adding base indentation for script block formatting and smart indent --- src/harness/fourslash.ts | 1 + src/server/editorServices.ts | 1 + src/server/protocol.d.ts | 5 ++++- src/server/session.ts | 1 + src/services/formatting/smartIndenter.ts | 18 +++++++++++------- src/services/services.ts | 3 ++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 695e19b4667..5aa31780092 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -310,6 +310,7 @@ namespace FourSlash { } this.formatCodeOptions = { + BaseIndentSize: 0, IndentSize: 4, TabSize: 4, NewLineCharacter: Harness.IO.newLine(), diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 5c1fe354d18..a7f11ac2757 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1389,6 +1389,7 @@ namespace ts.server { static getDefaultFormatCodeOptions(host: ServerHost): ts.FormatCodeOptions { return ts.clone({ + BaseIndentSize: 0, IndentSize: 4, TabSize: 4, NewLineCharacter: host.newLine || "\n", diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index b62d89ae520..bf1bef75db1 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -434,6 +434,9 @@ declare namespace ts.server.protocol { /** Number of spaces to indent during formatting. Default value is 4. */ indentSize?: number; + /** Number of additional spaces to indent during formatting to preserve base indentation (ex. script block indentation). Default value is 0. */ + baseIndentSize?: number; + /** The new line character to be used. Default value is the OS line delimiter. */ newLineCharacter?: string; @@ -474,7 +477,7 @@ declare namespace ts.server.protocol { placeOpenBraceOnNewLineForControlBlocks?: boolean; /** Index operator */ - [key: string]: string | number | boolean; + [key: string]: string | number | boolean | undefined; } /** diff --git a/src/server/session.ts b/src/server/session.ts index 2964dc66505..f4cb542f5c3 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -674,6 +674,7 @@ namespace ts.server { if (lineText.search("\\S") < 0) { // TODO: get these options from host const editorOptions: ts.EditorOptions = { + BaseIndentSize: formatOptions.BaseIndentSize, IndentSize: formatOptions.IndentSize, TabSize: formatOptions.TabSize, NewLineCharacter: formatOptions.NewLineCharacter, diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 23a1d937869..1de7a7cb74c 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -10,24 +10,24 @@ namespace ts.formatting { export function getIndentation(position: number, sourceFile: SourceFile, options: EditorOptions): number { if (position > sourceFile.text.length) { - return 0; // past EOF + return getBaseIndentation(options); // past EOF } // no indentation when the indent style is set to none, // so we can return fast if (options.IndentStyle === IndentStyle.None) { - return 0; + return getBaseIndentation(options); } const precedingToken = findPrecedingToken(position, sourceFile); if (!precedingToken) { - return 0; + return getBaseIndentation(options); } // no indentation in string \regex\template literals const precedingTokenIsLiteral = isStringOrRegularExpressionOrTemplateLiteral(precedingToken.kind); if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) { - return 0; + return getBaseIndentation(options); } const lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line; @@ -96,13 +96,17 @@ namespace ts.formatting { } if (!current) { - // no parent was found - return 0 to be indented on the level of SourceFile - return 0; + // no parent was found - return the base indentation of the SourceFile + return getBaseIndentation(options); } return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options); } + function getBaseIndentation(options: EditorOptions) { + return options.BaseIndentSize || 0; + } + export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number { const start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); @@ -162,7 +166,7 @@ namespace ts.formatting { parent = current.parent; } - return indentationDelta; + return indentationDelta + getBaseIndentation(options); } diff --git a/src/services/services.ts b/src/services/services.ts index 68fd95f1441..84f4216cb16 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1244,6 +1244,7 @@ namespace ts { } export interface EditorOptions { + BaseIndentSize?: number; IndentSize: number; TabSize: number; NewLineCharacter: string; @@ -1268,7 +1269,7 @@ namespace ts { InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean; PlaceOpenBraceOnNewLineForFunctions: boolean; PlaceOpenBraceOnNewLineForControlBlocks: boolean; - [s: string]: boolean | number | string; + [s: string]: boolean | number | string | undefined; } export interface DefinitionInfo {