mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 15:45:27 -05:00
add option for object literal indent
This commit is contained in:
@@ -328,7 +328,7 @@ namespace ts.formatting {
|
||||
break;
|
||||
}
|
||||
|
||||
if (SmartIndenter.shouldIndentChildNode(n, child, sourceFile)) {
|
||||
if (SmartIndenter.shouldIndentChildNode(options, n, child, sourceFile)) {
|
||||
return options.indentSize;
|
||||
}
|
||||
|
||||
@@ -470,7 +470,7 @@ namespace ts.formatting {
|
||||
parentDynamicIndentation: DynamicIndentation,
|
||||
effectiveParentStartLine: number
|
||||
): { indentation: number, delta: number } {
|
||||
const delta = SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0;
|
||||
const delta = SmartIndenter.shouldIndentChildNode(options, node) ? options.indentSize : 0;
|
||||
|
||||
if (effectiveParentStartLine === startLine) {
|
||||
// if node is located on the same line with the parent
|
||||
@@ -541,9 +541,9 @@ namespace ts.formatting {
|
||||
getIndentation: () => indentation,
|
||||
getDelta,
|
||||
recomputeIndentation: lineAdded => {
|
||||
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node, sourceFile)) {
|
||||
if (node.parent && SmartIndenter.shouldIndentChildNode(options, node.parent, node, sourceFile)) {
|
||||
indentation += lineAdded ? options.indentSize : -options.indentSize;
|
||||
delta = SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0;
|
||||
delta = SmartIndenter.shouldIndentChildNode(options, node) ? options.indentSize : 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -581,9 +581,9 @@ namespace ts.formatting {
|
||||
&& !(node.decorators && kind === getFirstNonDecoratorTokenOfNode(node));
|
||||
}
|
||||
|
||||
function getDelta(child: Node) {
|
||||
function getDelta(child: TextRangeWithKind) {
|
||||
// Delta value should be zero when the node explicitly prevents indentation of the child node
|
||||
return SmartIndenter.nodeWillIndentChild(node, child, sourceFile, /*indentByDefault*/ true) ? delta : 0;
|
||||
return SmartIndenter.nodeWillIndentChild(options, node, child, sourceFile, /*indentByDefault*/ true) ? delta : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace ts.formatting {
|
||||
let previous: Node | undefined;
|
||||
let current = precedingToken;
|
||||
while (current) {
|
||||
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous, sourceFile, /*isNextChild*/ true)) {
|
||||
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(options, current, previous, sourceFile, /*isNextChild*/ true)) {
|
||||
const currentStart = getStartLineAndCharacterForNode(current, sourceFile);
|
||||
const nextTokenKind = nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile);
|
||||
const indentationDelta = nextTokenKind !== NextTokenKind.Unknown
|
||||
@@ -193,7 +193,7 @@ namespace ts.formatting {
|
||||
}
|
||||
|
||||
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
|
||||
if (shouldIndentChildNode(parent, current, sourceFile, isNextChild) && !parentAndChildShareLine) {
|
||||
if (shouldIndentChildNode(options, parent, current, sourceFile, isNextChild) && !parentAndChildShareLine) {
|
||||
indentationDelta += options.indentSize;
|
||||
}
|
||||
|
||||
@@ -531,18 +531,15 @@ namespace ts.formatting {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind | undefined, sourceFile: SourceFileLike | undefined, indentByDefault: boolean): boolean {
|
||||
export function nodeWillIndentChild(settings: FormatCodeSettings | undefined, parent: TextRangeWithKind, child: TextRangeWithKind | undefined, sourceFile: SourceFileLike | undefined, indentByDefault: boolean): boolean {
|
||||
const childKind = child ? child.kind : SyntaxKind.Unknown;
|
||||
|
||||
switch (parent.kind) {
|
||||
case SyntaxKind.VariableDeclaration:
|
||||
case SyntaxKind.PropertyAssignment:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
if (sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) {
|
||||
const childStart = skipTrivia(sourceFile.text, child.pos);
|
||||
const startLine = sourceFile.getLineAndCharacterOfPosition(childStart).line;
|
||||
const endLine = sourceFile.getLineAndCharacterOfPosition(child.end).line;
|
||||
return startLine === endLine;
|
||||
if (!settings.indentMultiLineObjectLiteralBeginningOnBlankLine && sourceFile && childKind === SyntaxKind.ObjectLiteralExpression) {
|
||||
return rangeIsOnOneLine(sourceFile, child);
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.DoStatement:
|
||||
@@ -596,9 +593,16 @@ namespace ts.formatting {
|
||||
* True when the parent node should indent the given child by an explicit rule.
|
||||
* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child.
|
||||
*/
|
||||
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: Node, sourceFile?: SourceFileLike, isNextChild = false): boolean {
|
||||
return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, sourceFile, /*indentByDefault*/ false))
|
||||
export function shouldIndentChildNode(settings: FormatCodeSettings | undefined, parent: TextRangeWithKind, child?: Node, sourceFile?: SourceFileLike, isNextChild = false): boolean {
|
||||
return (nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(settings, parent, child, sourceFile, /*indentByDefault*/ false))
|
||||
&& !(isNextChild && child && isControlFlowEndingStatement(child.kind, parent));
|
||||
}
|
||||
|
||||
function rangeIsOnOneLine(sourceFile: SourceFileLike, range: TextRangeWithKind) {
|
||||
const rangeStart = skipTrivia(sourceFile.text, range.pos);
|
||||
const startLine = sourceFile.getLineAndCharacterOfPosition(rangeStart).line;
|
||||
const endLine = sourceFile.getLineAndCharacterOfPosition(range.end).line;
|
||||
return startLine === endLine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -670,7 +670,7 @@ namespace ts.textChanges {
|
||||
const delta =
|
||||
options.delta !== undefined
|
||||
? options.delta
|
||||
: formatting.SmartIndenter.shouldIndentChildNode(nodeIn)
|
||||
: formatting.SmartIndenter.shouldIndentChildNode(formatContext.options, nodeIn)
|
||||
? (formatOptions.indentSize || 0)
|
||||
: 0;
|
||||
const file: SourceFileLike = { text, getLineAndCharacterOfPosition(pos) { return getLineAndCharacterOfPosition(this, pos); } };
|
||||
|
||||
@@ -629,6 +629,7 @@ namespace ts {
|
||||
placeOpenBraceOnNewLineForFunctions?: boolean;
|
||||
placeOpenBraceOnNewLineForControlBlocks?: boolean;
|
||||
insertSpaceBeforeTypeAnnotation?: boolean;
|
||||
indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
|
||||
}
|
||||
|
||||
export interface DefinitionInfo {
|
||||
|
||||
Reference in New Issue
Block a user