mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 07:29:16 -05:00
Merge pull request #5197 from Microsoft/supportIndentStyle
Support different indentation styles
This commit is contained in:
committed by
Paul van Brenk
parent
12b436bb2c
commit
573652160c
@@ -100,6 +100,8 @@ namespace FourSlash {
|
||||
end: number;
|
||||
}
|
||||
|
||||
export import IndentStyle = ts.IndentStyle;
|
||||
|
||||
let entityMap: ts.Map<string> = {
|
||||
"&": "&",
|
||||
"\"": """,
|
||||
@@ -309,6 +311,7 @@ namespace FourSlash {
|
||||
TabSize: 4,
|
||||
NewLineCharacter: Harness.IO.newLine(),
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: ts.IndentStyle.Smart,
|
||||
InsertSpaceAfterCommaDelimiter: true,
|
||||
InsertSpaceAfterSemicolonInForStatements: true,
|
||||
InsertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
@@ -1695,24 +1698,28 @@ namespace FourSlash {
|
||||
}
|
||||
}
|
||||
|
||||
private getIndentation(fileName: string, position: number): number {
|
||||
return this.languageService.getIndentationAtPosition(fileName, position, this.formatCodeOptions);
|
||||
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle): number {
|
||||
|
||||
let formatOptions = ts.clone(this.formatCodeOptions);
|
||||
formatOptions.IndentStyle = indentStyle;
|
||||
|
||||
return this.languageService.getIndentationAtPosition(fileName, position, formatOptions);
|
||||
}
|
||||
|
||||
public verifyIndentationAtCurrentPosition(numberOfSpaces: number) {
|
||||
public verifyIndentationAtCurrentPosition(numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
|
||||
this.taoInvalidReason = "verifyIndentationAtCurrentPosition NYI";
|
||||
|
||||
let actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition);
|
||||
let actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition, indentStyle);
|
||||
let 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) {
|
||||
public verifyIndentationAtPosition(fileName: string, position: number, numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
|
||||
this.taoInvalidReason = "verifyIndentationAtPosition NYI";
|
||||
|
||||
let actual = this.getIndentation(fileName, position);
|
||||
let actual = this.getIndentation(fileName, position, indentStyle);
|
||||
let lineCol = this.getLineColStringAtPosition(position);
|
||||
if (actual !== numberOfSpaces) {
|
||||
this.raiseError(`verifyIndentationAtPosition failed at ${lineCol} - expected: ${numberOfSpaces}, actual: ${actual}`);
|
||||
|
||||
@@ -1177,6 +1177,7 @@ namespace ts.server {
|
||||
TabSize: 4,
|
||||
NewLineCharacter: ts.sys ? ts.sys.newLine : '\n',
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: ts.IndentStyle.Smart,
|
||||
InsertSpaceAfterCommaDelimiter: true,
|
||||
InsertSpaceAfterSemicolonInForStatements: true,
|
||||
InsertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
@@ -1187,7 +1188,6 @@ namespace ts.server {
|
||||
PlaceOpenBraceOnNewLineForFunctions: false,
|
||||
PlaceOpenBraceOnNewLineForControlBlocks: false,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export interface LineCollection {
|
||||
|
||||
@@ -606,6 +606,7 @@ namespace ts.server {
|
||||
TabSize: formatOptions.TabSize,
|
||||
NewLineCharacter: "\n",
|
||||
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces,
|
||||
IndentStyle: ts.IndentStyle.Smart,
|
||||
};
|
||||
var indentPosition =
|
||||
compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
|
||||
|
||||
@@ -13,6 +13,12 @@ namespace ts.formatting {
|
||||
return 0; // past EOF
|
||||
}
|
||||
|
||||
// no indentation when the indent style is set to none,
|
||||
// so we can return fast
|
||||
if (options.IndentStyle === IndentStyle.None) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let precedingToken = findPrecedingToken(position, sourceFile);
|
||||
if (!precedingToken) {
|
||||
return 0;
|
||||
@@ -26,6 +32,26 @@ namespace ts.formatting {
|
||||
|
||||
let lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
|
||||
|
||||
// indentation is first non-whitespace character in a previous line
|
||||
// for block indentation, we should look for a line which contains something that's not
|
||||
// whitespace.
|
||||
if (options.IndentStyle === IndentStyle.Block) {
|
||||
|
||||
// move backwards until we find a line with a non-whitespace character,
|
||||
// then find the first non-whitespace character for that line.
|
||||
let current = position;
|
||||
while (current > 0){
|
||||
let char = sourceFile.text.charCodeAt(current);
|
||||
if (!isWhiteSpace(char) && !isLineBreak(char)) {
|
||||
break;
|
||||
}
|
||||
current--;
|
||||
}
|
||||
|
||||
let lineStart = ts.getLineStartPositionForPosition(current, sourceFile);
|
||||
return SmartIndenter.findFirstNonWhitespaceColumn(lineStart, current, sourceFile, options);
|
||||
}
|
||||
|
||||
if (precedingToken.kind === SyntaxKind.CommaToken && precedingToken.parent.kind !== SyntaxKind.BinaryExpression) {
|
||||
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
|
||||
let actualIndentation = getActualIndentationForListItemBeforeComma(precedingToken, sourceFile, options);
|
||||
@@ -218,7 +244,7 @@ namespace ts.formatting {
|
||||
function getStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter {
|
||||
return sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile));
|
||||
}
|
||||
|
||||
|
||||
export function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFile): boolean {
|
||||
if (parent.kind === SyntaxKind.IfStatement && (<IfStatement>parent).elseStatement === child) {
|
||||
let elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile);
|
||||
@@ -319,7 +345,7 @@ namespace ts.formatting {
|
||||
}
|
||||
|
||||
return Value.Unknown;
|
||||
|
||||
|
||||
function getStartingExpression(node: PropertyAccessExpression | CallExpression | ElementAccessExpression) {
|
||||
while (true) {
|
||||
switch (node.kind) {
|
||||
@@ -465,4 +491,4 @@ namespace ts.formatting {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1189,6 +1189,13 @@ namespace ts {
|
||||
TabSize: number;
|
||||
NewLineCharacter: string;
|
||||
ConvertTabsToSpaces: boolean;
|
||||
IndentStyle: IndentStyle;
|
||||
}
|
||||
|
||||
export enum IndentStyle {
|
||||
None = 0,
|
||||
Block = 1,
|
||||
Smart = 2,
|
||||
}
|
||||
|
||||
export interface FormatCodeOptions extends EditorOptions {
|
||||
|
||||
Reference in New Issue
Block a user