mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
nodeWillIndentChild from #4609
This commit is contained in:
@@ -282,19 +282,19 @@ namespace ts.formatting {
|
||||
*/
|
||||
function getOwnOrInheritedDelta(n: Node, options: FormatCodeOptions, sourceFile: SourceFile): number {
|
||||
let previousLine = Constants.Unknown;
|
||||
let childKind = SyntaxKind.Unknown;
|
||||
let child: Node = null;
|
||||
while (n) {
|
||||
let line = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)).line;
|
||||
if (previousLine !== Constants.Unknown && line !== previousLine) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (SmartIndenter.shouldIndentChildNode(n.kind, childKind)) {
|
||||
if (SmartIndenter.shouldIndentChildNode(n, child)) {
|
||||
return options.IndentSize;
|
||||
}
|
||||
|
||||
previousLine = line;
|
||||
childKind = n.kind;
|
||||
child = n;
|
||||
n = n.parent;
|
||||
}
|
||||
return 0;
|
||||
@@ -387,33 +387,17 @@ namespace ts.formatting {
|
||||
|
||||
let indentation = inheritedIndentation;
|
||||
if (indentation === Constants.Unknown) {
|
||||
if (isSomeBlock(node.kind)) {
|
||||
// blocks should be indented in
|
||||
// - other blocks
|
||||
// - source file
|
||||
// - switch\default clauses
|
||||
if (isSomeBlock(parent.kind) ||
|
||||
parent.kind === SyntaxKind.SourceFile ||
|
||||
parent.kind === SyntaxKind.CaseClause ||
|
||||
parent.kind === SyntaxKind.DefaultClause) {
|
||||
if (SmartIndenter.shouldInheritParentIndentation(parent, node) ||
|
||||
SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
|
||||
|
||||
indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta();
|
||||
}
|
||||
else {
|
||||
indentation = parentDynamicIndentation.getIndentation();
|
||||
}
|
||||
indentation = parentDynamicIndentation.getIndentation();
|
||||
}
|
||||
else {
|
||||
if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
|
||||
indentation = parentDynamicIndentation.getIndentation();
|
||||
}
|
||||
else {
|
||||
indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta();
|
||||
}
|
||||
indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta();
|
||||
}
|
||||
}
|
||||
|
||||
var delta = SmartIndenter.shouldIndentChildNode(node.kind, SyntaxKind.Unknown) ? options.IndentSize : 0;
|
||||
var delta = SmartIndenter.shouldIndentChildNode(node, null) ? options.IndentSize : 0;
|
||||
|
||||
if (effectiveParentStartLine === startLine) {
|
||||
// if node is located on the same line with the parent
|
||||
@@ -495,7 +479,7 @@ namespace ts.formatting {
|
||||
getIndentation: () => indentation,
|
||||
getDelta: () => delta,
|
||||
recomputeIndentation: lineAdded => {
|
||||
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent.kind, node.kind)) {
|
||||
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node)) {
|
||||
if (lineAdded) {
|
||||
indentation += options.IndentSize;
|
||||
}
|
||||
@@ -503,7 +487,7 @@ namespace ts.formatting {
|
||||
indentation -= options.IndentSize;
|
||||
}
|
||||
|
||||
if (SmartIndenter.shouldIndentChildNode(node.kind, SyntaxKind.Unknown)) {
|
||||
if (SmartIndenter.shouldIndentChildNode(node, null)) {
|
||||
delta = options.IndentSize;
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace ts.formatting {
|
||||
let indentationDelta: number;
|
||||
|
||||
while (current) {
|
||||
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current.kind, previous ? previous.kind : SyntaxKind.Unknown)) {
|
||||
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous)) {
|
||||
currentStart = getStartLineAndCharacterForNode(current, sourceFile);
|
||||
|
||||
if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) {
|
||||
@@ -133,7 +133,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.kind, current.kind) && !parentAndChildShareLine) {
|
||||
if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) {
|
||||
indentationDelta += options.IndentSize;
|
||||
}
|
||||
|
||||
@@ -446,11 +446,12 @@ namespace ts.formatting {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function shouldIndentChildNode(parent: SyntaxKind, child: SyntaxKind): boolean {
|
||||
if (nodeContentIsAlwaysIndented(parent)) {
|
||||
return true;
|
||||
}
|
||||
switch (parent) {
|
||||
/**
|
||||
* Function returns true when a node with conditional indentation rule will indent certain child node.
|
||||
*/
|
||||
function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean) {
|
||||
let childKind = child ? child.kind : SyntaxKind.Unknown;
|
||||
switch (parent.kind) {
|
||||
case SyntaxKind.DoStatement:
|
||||
case SyntaxKind.WhileStatement:
|
||||
case SyntaxKind.ForInStatement:
|
||||
@@ -464,10 +465,25 @@ namespace ts.formatting {
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
return child !== SyntaxKind.Block;
|
||||
default:
|
||||
return false;
|
||||
return childKind !== SyntaxKind.Block;
|
||||
}
|
||||
// No explicit rule for selected nodes, so result will follow the default value argument.
|
||||
return indentByDefault;
|
||||
}
|
||||
|
||||
export function shouldIndentChildNode(parent: TextRangeWithKind, child: TextRangeWithKind): boolean {
|
||||
if (nodeContentIsAlwaysIndented(parent.kind)) {
|
||||
return true;
|
||||
}
|
||||
return nodeWillIndentChild(parent, child, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function returns true if a node should not get additional indentation in its parent node.
|
||||
*/
|
||||
export function shouldInheritParentIndentation(parent: TextRangeWithKind, child: TextRangeWithKind): boolean {
|
||||
// Check if
|
||||
return !nodeWillIndentChild(parent, child, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user