Skip to content

Commit d72e49a

Browse files
committed
add formatting options to shouldIndentChildNode
1 parent c6dfa9b commit d72e49a

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ namespace FourSlash {
349349
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
350350
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
351351
insertSpaceAfterTypeAssertion: false,
352+
indentInsideTernaryOperator: false,
352353
placeOpenBraceOnNewLineForFunctions: false,
353354
placeOpenBraceOnNewLineForControlBlocks: false,
354355
};

src/server/utilities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ namespace ts.server {
8585
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
8686
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
8787
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
88+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
8889
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
8990
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
91+
insertSpaceAfterTypeAssertion: false,
92+
indentInsideTernaryOperator: false,
9093
placeOpenBraceOnNewLineForFunctions: false,
9194
placeOpenBraceOnNewLineForControlBlocks: false,
9295
};

src/services/formatting/formatting.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ namespace ts.formatting {
303303
break;
304304
}
305305

306-
if (SmartIndenter.shouldIndentChildNode(n, child)) {
306+
if (SmartIndenter.shouldIndentChildNode(n, options, child)) {
307307
return options.indentSize;
308308
}
309309

@@ -413,7 +413,7 @@ namespace ts.formatting {
413413
effectiveParentStartLine: number): Indentation {
414414

415415
let indentation = inheritedIndentation;
416-
let delta = SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0;
416+
let delta = SmartIndenter.shouldIndentChildNode(node, options) ? options.indentSize : 0;
417417

418418
if (effectiveParentStartLine === startLine) {
419419
// if node is located on the same line with the parent
@@ -504,15 +504,15 @@ namespace ts.formatting {
504504
getIndentation: () => indentation,
505505
getDelta: child => getEffectiveDelta(delta, child),
506506
recomputeIndentation: lineAdded => {
507-
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, node)) {
507+
if (node.parent && SmartIndenter.shouldIndentChildNode(node.parent, options, node)) {
508508
if (lineAdded) {
509509
indentation += options.indentSize;
510510
}
511511
else {
512512
indentation -= options.indentSize;
513513
}
514514

515-
if (SmartIndenter.shouldIndentChildNode(node)) {
515+
if (SmartIndenter.shouldIndentChildNode(node, options)) {
516516
delta = options.indentSize;
517517
}
518518
else {
@@ -524,7 +524,7 @@ namespace ts.formatting {
524524

525525
function getEffectiveDelta(delta: number, child: TextRangeWithKind) {
526526
// Delta value should be zero when the node explicitly prevents indentation of the child node
527-
return SmartIndenter.nodeWillIndentChild(node, child, true) ? delta : 0;
527+
return SmartIndenter.nodeWillIndentChild(node, child, true, options) ? delta : 0;
528528
}
529529
}
530530

src/services/formatting/smartIndenter.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts.formatting {
88
Unknown = -1
99
}
1010

11-
export function getIndentation(position: number, sourceFile: SourceFile, options: EditorSettings): number {
11+
export function getIndentation(position: number, sourceFile: SourceFile, options: FormatCodeSettings): number {
1212
if (position > sourceFile.text.length) {
1313
return getBaseIndentation(options); // past EOF
1414
}
@@ -68,7 +68,7 @@ namespace ts.formatting {
6868
let indentationDelta: number;
6969

7070
while (current) {
71-
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, previous)) {
71+
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current, options, previous)) {
7272
currentStart = getStartLineAndCharacterForNode(current, sourceFile);
7373

7474
if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) {
@@ -118,7 +118,7 @@ namespace ts.formatting {
118118
ignoreActualIndentationRange: TextRange,
119119
indentationDelta: number,
120120
sourceFile: SourceFile,
121-
options: EditorSettings): number {
121+
options: FormatCodeSettings): number {
122122

123123
let parent: Node = current.parent;
124124
let parentStart: LineAndCharacter;
@@ -157,7 +157,7 @@ namespace ts.formatting {
157157
}
158158

159159
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
160-
if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) {
160+
if (shouldIndentChildNode(parent, options, current) && !parentAndChildShareLine) {
161161
indentationDelta += options.indentSize;
162162
}
163163

@@ -474,7 +474,7 @@ namespace ts.formatting {
474474
}
475475

476476
/* @internal */
477-
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean) {
477+
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean, options: FormatCodeSettings) {
478478
const childKind = child ? child.kind : SyntaxKind.Unknown;
479479
switch (parent.kind) {
480480
case SyntaxKind.DoStatement:
@@ -492,7 +492,7 @@ namespace ts.formatting {
492492
case SyntaxKind.SetAccessor:
493493
return childKind !== SyntaxKind.Block;
494494
case SyntaxKind.ConditionalExpression:
495-
return (parent as ConditionalExpression).whenFalse !== child;
495+
return options.indentInsideTernaryOperator || (parent as ConditionalExpression).whenFalse !== child;
496496
case SyntaxKind.ExportDeclaration:
497497
return childKind !== SyntaxKind.NamedExports;
498498
case SyntaxKind.ImportDeclaration:
@@ -508,8 +508,8 @@ namespace ts.formatting {
508508
/*
509509
Function returns true when the parent node should indent the given child by an explicit rule
510510
*/
511-
export function shouldIndentChildNode(parent: TextRangeWithKind, child?: TextRangeWithKind): boolean {
512-
return nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, /*indentByDefault*/ false);
511+
export function shouldIndentChildNode(parent: TextRangeWithKind, options: FormatCodeSettings, child?: TextRangeWithKind): boolean {
512+
return nodeContentIsAlwaysIndented(parent.kind) || nodeWillIndentChild(parent, child, /*indentByDefault*/ false, options);
513513
}
514514
}
515515
}

src/services/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ namespace ts {
233233
getOutliningSpans(fileName: string): OutliningSpan[];
234234
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];
235235
getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[];
236-
getIndentationAtPosition(fileName: string, position: number, options: EditorOptions | EditorSettings): number;
236+
getIndentationAtPosition(fileName: string, position: number, options: EditorOptions | FormatCodeSettings): number;
237237

238238
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
239239
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
@@ -442,6 +442,7 @@ namespace ts {
442442
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean;
443443
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean;
444444
insertSpaceAfterTypeAssertion?: boolean;
445+
indentInsideTernaryOperator?: boolean;
445446
placeOpenBraceOnNewLineForFunctions?: boolean;
446447
placeOpenBraceOnNewLineForControlBlocks?: boolean;
447448
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
////const expectedScanAction =
4+
//// shouldRescanGreaterThanToken(n)
5+
//// ? ScanAction.RescanGreaterThanToken
6+
//// : shouldRescanSlashToken(n)
7+
//// ? ScanAction.RescanSlashToken
8+
//// : shouldRescanTemplateToken(n)
9+
//// ? ScanAction.RescanTemplateToken
10+
//// : shouldRescanJsxIdentifier(n)
11+
//// ? ScanAction.RescanJsxIdentifier
12+
//// : shouldRescanJsxText(n)
13+
//// ? ScanAction.RescanJsxText
14+
//// : ScanAction.Scan;
15+
16+
format.setOption("indentInsideTernaryOperator", false);
17+
format.document();
18+
verify.currentFileContentIs(`const expectedScanAction =
19+
shouldRescanGreaterThanToken(n)
20+
? ScanAction.RescanGreaterThanToken
21+
: shouldRescanSlashToken(n)
22+
? ScanAction.RescanSlashToken
23+
: shouldRescanTemplateToken(n)
24+
? ScanAction.RescanTemplateToken
25+
: shouldRescanJsxIdentifier(n)
26+
? ScanAction.RescanJsxIdentifier
27+
: shouldRescanJsxText(n)
28+
? ScanAction.RescanJsxText
29+
: ScanAction.Scan;`)
30+
31+
format.setOption("indentInsideTernaryOperator", true);
32+
format.document();
33+
verify.currentFileContentIs(`const expectedScanAction =
34+
shouldRescanGreaterThanToken(n)
35+
? ScanAction.RescanGreaterThanToken
36+
: shouldRescanSlashToken(n)
37+
? ScanAction.RescanSlashToken
38+
: shouldRescanTemplateToken(n)
39+
? ScanAction.RescanTemplateToken
40+
: shouldRescanJsxIdentifier(n)
41+
? ScanAction.RescanJsxIdentifier
42+
: shouldRescanJsxText(n)
43+
? ScanAction.RescanJsxText
44+
: ScanAction.Scan;`)

0 commit comments

Comments
 (0)