mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Merge pull request #9236 from Microsoft/BaseIndentationSupport
[Salsa] Adding base indentation for script block formatting and smart indent
This commit is contained in:
@@ -310,6 +310,7 @@ namespace FourSlash {
|
||||
}
|
||||
|
||||
this.formatCodeOptions = {
|
||||
BaseIndentSize: 0,
|
||||
IndentSize: 4,
|
||||
TabSize: 4,
|
||||
NewLineCharacter: Harness.IO.newLine(),
|
||||
@@ -1659,24 +1660,25 @@ namespace FourSlash {
|
||||
}
|
||||
}
|
||||
|
||||
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle): number {
|
||||
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle, baseIndentSize: number): number {
|
||||
|
||||
const formatOptions = ts.clone(this.formatCodeOptions);
|
||||
formatOptions.IndentStyle = indentStyle;
|
||||
formatOptions.BaseIndentSize = baseIndentSize;
|
||||
|
||||
return this.languageService.getIndentationAtPosition(fileName, position, formatOptions);
|
||||
}
|
||||
|
||||
public verifyIndentationAtCurrentPosition(numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
|
||||
const actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition, indentStyle);
|
||||
public verifyIndentationAtCurrentPosition(numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
|
||||
const actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition, indentStyle, baseIndentSize);
|
||||
const lineCol = this.getLineColStringAtPosition(this.currentCaretPosition);
|
||||
if (actual !== numberOfSpaces) {
|
||||
this.raiseError(`verifyIndentationAtCurrentPosition failed at ${lineCol} - expected: ${numberOfSpaces}, actual: ${actual}`);
|
||||
}
|
||||
}
|
||||
|
||||
public verifyIndentationAtPosition(fileName: string, position: number, numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
|
||||
const actual = this.getIndentation(fileName, position, indentStyle);
|
||||
public verifyIndentationAtPosition(fileName: string, position: number, numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
|
||||
const actual = this.getIndentation(fileName, position, indentStyle, baseIndentSize);
|
||||
const lineCol = this.getLineColStringAtPosition(position);
|
||||
if (actual !== numberOfSpaces) {
|
||||
this.raiseError(`verifyIndentationAtPosition failed at ${lineCol} - expected: ${numberOfSpaces}, actual: ${actual}`);
|
||||
@@ -2938,8 +2940,8 @@ namespace FourSlashInterface {
|
||||
this.state.verifyIndentationAtCurrentPosition(numberOfSpaces);
|
||||
}
|
||||
|
||||
public indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle = ts.IndentStyle.Smart) {
|
||||
this.state.verifyIndentationAtPosition(fileName, position, numberOfSpaces, indentStyle);
|
||||
public indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
|
||||
this.state.verifyIndentationAtPosition(fileName, position, numberOfSpaces, indentStyle, baseIndentSize);
|
||||
}
|
||||
|
||||
public textAtCaretIs(text: string) {
|
||||
|
||||
@@ -1566,6 +1566,7 @@ namespace ts.server {
|
||||
|
||||
static getDefaultFormatCodeOptions(host: ServerHost): ts.FormatCodeOptions {
|
||||
return ts.clone({
|
||||
BaseIndentSize: 0,
|
||||
IndentSize: 4,
|
||||
TabSize: 4,
|
||||
NewLineCharacter: host.newLine || "\n",
|
||||
|
||||
5
src/server/protocol.d.ts
vendored
5
src/server/protocol.d.ts
vendored
@@ -438,6 +438,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;
|
||||
|
||||
@@ -478,7 +481,7 @@ declare namespace ts.server.protocol {
|
||||
placeOpenBraceOnNewLineForControlBlocks?: boolean;
|
||||
|
||||
/** Index operator */
|
||||
[key: string]: string | number | boolean;
|
||||
[key: string]: string | number | boolean | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -703,6 +703,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,
|
||||
|
||||
@@ -394,7 +394,10 @@ namespace ts.formatting {
|
||||
const startLinePosition = getLineStartPositionForPosition(startPos, sourceFile);
|
||||
const column = SmartIndenter.findFirstNonWhitespaceColumn(startLinePosition, startPos, sourceFile, options);
|
||||
if (startLine !== parentStartLine || startPos === column) {
|
||||
return column;
|
||||
// Use the base indent size if it is greater than
|
||||
// the indentation of the inherited predecessor.
|
||||
const baseIndentSize = SmartIndenter.getBaseIndentation(options);
|
||||
return baseIndentSize > column ? baseIndentSize : column;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ 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,
|
||||
@@ -21,7 +21,7 @@ namespace ts.formatting {
|
||||
|
||||
const precedingToken = findPrecedingToken(position, sourceFile);
|
||||
if (!precedingToken) {
|
||||
return 0;
|
||||
return getBaseIndentation(options);
|
||||
}
|
||||
|
||||
// no indentation in string \regex\template literals
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
export 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user