disable indentation when in brace shell

This pattern can also be used to format JSX end tag.
This commit is contained in:
SaschaNaz
2015-09-03 20:02:48 +09:00
parent 66924a3718
commit f0ed672eb1
2 changed files with 23 additions and 14 deletions
+2 -13
View File
@@ -403,19 +403,8 @@ namespace ts.formatting {
indentation = parentDynamicIndentation.getIndentation();
}
}
else if (node.kind === SyntaxKind.NamedExports || node.kind === SyntaxKind.ImportClause) {
let blockParent = node.kind === SyntaxKind.ImportClause ?
(<ImportClause>node).namedBindings : node;
let children = (<NamedExports | ImportClause>blockParent).getChildren(sourceFile);
// Detect named export/import pseudo-block
if (children[0] && children[0].kind === SyntaxKind.OpenBraceToken &&
children[2] && children[2].kind === SyntaxKind.CloseBraceToken) {
indentation = parentDynamicIndentation.getIndentation();
}
else {
indentation = parentDynamicIndentation.getIndentation() + parentDynamicIndentation.getDelta();
}
else if (SmartIndenter.isIndentationPrevented(node)) {
indentation = parentDynamicIndentation.getIndentation();
}
else {
if (SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) {
+21 -1
View File
@@ -133,7 +133,9 @@ 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.kind, current.kind) &&
!isIndentationPrevented(current) && !parentAndChildShareLine) {
indentationDelta += options.IndentSize;
}
@@ -475,5 +477,23 @@ namespace ts.formatting {
return false;
}
}
function hasBraceShell(node: Node) {
let children = node.getChildren();
let last = children.length - 1;
// Check if node looks like a block
return children[0] && children[0].kind === SyntaxKind.OpenBraceToken &&
children[last] && children[last].kind === SyntaxKind.CloseBraceToken;
}
export function isIndentationPrevented(node: TextRangeWithKind) {
switch (node.kind) {
case SyntaxKind.NamedExports:
return hasBraceShell(<NamedExports>node);
case SyntaxKind.ImportClause:
return hasBraceShell((<ImportClause>node).namedBindings);
}
return false;
}
}
}