Adding base indentation for script block formatting and smart indent

This commit is contained in:
Jason Ramsay
2016-06-17 13:02:15 -07:00
parent 7bb739fa40
commit 218a5ba0bb
6 changed files with 20 additions and 9 deletions
+1
View File
@@ -310,6 +310,7 @@ namespace FourSlash {
}
this.formatCodeOptions = {
BaseIndentSize: 0,
IndentSize: 4,
TabSize: 4,
NewLineCharacter: Harness.IO.newLine(),
+1
View File
@@ -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",
+4 -1
View File
@@ -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;
}
/**
+1
View File
@@ -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,
+11 -7
View File
@@ -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);
}
+2 -1
View File
@@ -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 {