Skip to content

Commit 8c50320

Browse files
committed
Add fourslash tests & address CR comments
1 parent 369253b commit 8c50320

File tree

6 files changed

+491
-16
lines changed

6 files changed

+491
-16
lines changed

src/harness/fourslash.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,24 +1663,25 @@ namespace FourSlash {
16631663
}
16641664
}
16651665

1666-
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle): number {
1666+
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle, baseIndentSize: number): number {
16671667

16681668
const formatOptions = ts.clone(this.formatCodeOptions);
16691669
formatOptions.IndentStyle = indentStyle;
1670+
formatOptions.BaseIndentSize = baseIndentSize;
16701671

16711672
return this.languageService.getIndentationAtPosition(fileName, position, formatOptions);
16721673
}
16731674

1674-
public verifyIndentationAtCurrentPosition(numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
1675-
const actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition, indentStyle);
1675+
public verifyIndentationAtCurrentPosition(numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
1676+
const actual = this.getIndentation(this.activeFile.fileName, this.currentCaretPosition, indentStyle, baseIndentSize);
16761677
const lineCol = this.getLineColStringAtPosition(this.currentCaretPosition);
16771678
if (actual !== numberOfSpaces) {
16781679
this.raiseError(`verifyIndentationAtCurrentPosition failed at ${lineCol} - expected: ${numberOfSpaces}, actual: ${actual}`);
16791680
}
16801681
}
16811682

1682-
public verifyIndentationAtPosition(fileName: string, position: number, numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart) {
1683-
const actual = this.getIndentation(fileName, position, indentStyle);
1683+
public verifyIndentationAtPosition(fileName: string, position: number, numberOfSpaces: number, indentStyle: ts.IndentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
1684+
const actual = this.getIndentation(fileName, position, indentStyle, baseIndentSize);
16841685
const lineCol = this.getLineColStringAtPosition(position);
16851686
if (actual !== numberOfSpaces) {
16861687
this.raiseError(`verifyIndentationAtPosition failed at ${lineCol} - expected: ${numberOfSpaces}, actual: ${actual}`);
@@ -2938,8 +2939,8 @@ namespace FourSlashInterface {
29382939
this.state.verifyIndentationAtCurrentPosition(numberOfSpaces);
29392940
}
29402941

2941-
public indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle = ts.IndentStyle.Smart) {
2942-
this.state.verifyIndentationAtPosition(fileName, position, numberOfSpaces, indentStyle);
2942+
public indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle = ts.IndentStyle.Smart, baseIndentSize = 0) {
2943+
this.state.verifyIndentationAtPosition(fileName, position, numberOfSpaces, indentStyle, baseIndentSize);
29432944
}
29442945

29452946
public textAtCaretIs(text: string) {

src/services/formatting/formatting.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,8 @@ namespace ts.formatting {
396396
if (startLine !== parentStartLine || startPos === column) {
397397
// Use the base indent size if it is greater than
398398
// the indentation of the inherited predecessor.
399-
if (options.BaseIndentSize > column) {
400-
return options.BaseIndentSize;
401-
}
402-
return column;
399+
const baseIndentSize = SmartIndenter.getBaseIndentation(options);
400+
return baseIndentSize > column ? baseIndentSize : column;
403401
}
404402
}
405403

src/services/formatting/smartIndenter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts.formatting {
1616
// no indentation when the indent style is set to none,
1717
// so we can return fast
1818
if (options.IndentStyle === IndentStyle.None) {
19-
return getBaseIndentation(options);
19+
return 0;
2020
}
2121

2222
const precedingToken = findPrecedingToken(position, sourceFile);
@@ -27,7 +27,7 @@ namespace ts.formatting {
2727
// no indentation in string \regex\template literals
2828
const precedingTokenIsLiteral = isStringOrRegularExpressionOrTemplateLiteral(precedingToken.kind);
2929
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
30-
return getBaseIndentation(options);
30+
return 0;
3131
}
3232

3333
const lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
@@ -103,7 +103,7 @@ namespace ts.formatting {
103103
return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options);
104104
}
105105

106-
function getBaseIndentation(options: EditorOptions) {
106+
export function getBaseIndentation(options: EditorOptions) {
107107
return options.BaseIndentSize || 0;
108108
}
109109

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////
4+
/////*1*/ module classes {
5+
/////*2*/ class Bar {
6+
////
7+
/////*3*/ constructor() {
8+
/////*4*/ }
9+
////
10+
/////*5*/private foo: string = "";
11+
////
12+
/////*6*/ private f() {
13+
/////*7*/ var a: any[] = [[1, 2], [3, 4], 5];
14+
/////*8*/ return ((1 + 1));
15+
/////*9*/ }
16+
////
17+
/////*10*/ private f2() {
18+
/////*11*/ if (true) { } { };
19+
/////*12*/ }
20+
/////*13*/ }
21+
/////*14*/ }
22+
////
23+
////
24+
/////*15*/ module interfaces {
25+
/////*16*/ interface Foo {
26+
////
27+
/////*17*/ x: number;
28+
////
29+
/////*18*/ foo(): number;
30+
/////*19*/ }
31+
/////*20*/ }
32+
////
33+
////
34+
/////*21*/ module nestedModules {
35+
/////*22*/ module Foo2 {
36+
/////*23*/ function f() {
37+
/////*24*/ }
38+
/////*25*/ var x: number;
39+
/////*26*/}
40+
/////*27*/ }
41+
////
42+
////
43+
/////*28*/ module Enums {
44+
/////*29*/ enum Foo3 {
45+
/////*30*/ val1 ,
46+
/////*31*/ val2,
47+
/////*32*/ }
48+
/////*33*/ }
49+
////
50+
////
51+
/////*34*/ function controlStatements() {
52+
/////*35*/ for (var i = 0; i < 10; i++) {
53+
/////*36*/ }
54+
////
55+
/////*37*/ for (var e in foo.bar) {
56+
/////*38*/ }
57+
////
58+
/////*39*/with (foo.bar) {
59+
/////*40*/ }
60+
////
61+
/////*41*/ while (false) {
62+
/////*42*/ }
63+
////
64+
/////*43*/ do {
65+
/////*44*/ } while (false);
66+
////
67+
/////*45*/ switch (foo.bar) {
68+
/////*46*/ }
69+
////
70+
/////*47*/ switch (foo.bar) {
71+
/////*48*/ case 1:
72+
/////*49*/ break;
73+
/////*50*/ default:
74+
/////*51*/ break;
75+
/////*52*/ }
76+
/////*53*/ }
77+
////
78+
////
79+
/////*54*/ function tryCatch() {
80+
/////*55*/try {
81+
/////*56*/}
82+
/////*57*/catch (err) {
83+
/////*58*/ }
84+
/////*59*/ }
85+
////
86+
////
87+
/////*60*/ function tryFinally() {
88+
/////*61*/ try {
89+
/////*62*/ }
90+
/////*63*/ finally {
91+
/////*64*/ }
92+
/////*65*/ }
93+
////
94+
////
95+
/////*66*/ function tryCatchFinally() {
96+
/////*67*/ try {
97+
/////*68*/ }
98+
/////*69*/ catch (err) {
99+
/////*70*/ }
100+
/////*71*/ finally {
101+
/////*72*/ }
102+
/////*73*/ }
103+
////
104+
////
105+
/////*74*/ class indentBeforeCurly
106+
/////*75*/ {
107+
/////*76*/ }
108+
////
109+
////
110+
/////*77*/ function argumentsListIndentation(bar,
111+
/////*78*/ blah,
112+
/////*79*/ );
113+
////
114+
////
115+
/////*80*/ function blockIndentAfterIndentedParameter1(bar,
116+
/////*81*/ blah) {
117+
/////*82*/ }
118+
////
119+
////
120+
/////*83*/ function blockIndentAfterIndentedParameter2(bar,
121+
/////*84*/ blah) {
122+
/////*85*/ if (foo) {
123+
/////*86*/ }
124+
/////*87*/}
125+
////
126+
/////*88*/ var templateLiterals = `abcdefghi
127+
/////*89*/jklmnop
128+
/////*90*/qrstuvwxyz`;
129+
130+
var originalOptions = format.copyFormatOptions();
131+
var copy = format.copyFormatOptions();
132+
copy.BaseIndentSize = 10;
133+
copy.IndentSize = 4;
134+
135+
format.setFormatOptions(copy);
136+
format.document();
137+
138+
verify.currentFileContentIs(`
139+
module classes {
140+
class Bar {
141+
142+
constructor() {
143+
}
144+
145+
private foo: string = "";
146+
147+
private f() {
148+
var a: any[] = [[1, 2], [3, 4], 5];
149+
return ((1 + 1));
150+
}
151+
152+
private f2() {
153+
if (true) { } { };
154+
}
155+
}
156+
}
157+
158+
159+
module interfaces {
160+
interface Foo {
161+
162+
x: number;
163+
164+
foo(): number;
165+
}
166+
}
167+
168+
169+
module nestedModules {
170+
module Foo2 {
171+
function f() {
172+
}
173+
var x: number;
174+
}
175+
}
176+
177+
178+
module Enums {
179+
enum Foo3 {
180+
val1,
181+
val2,
182+
}
183+
}
184+
185+
186+
function controlStatements() {
187+
for (var i = 0; i < 10; i++) {
188+
}
189+
190+
for (var e in foo.bar) {
191+
}
192+
193+
with (foo.bar) {
194+
}
195+
196+
while (false) {
197+
}
198+
199+
do {
200+
} while (false);
201+
202+
switch (foo.bar) {
203+
}
204+
205+
switch (foo.bar) {
206+
case 1:
207+
break;
208+
default:
209+
break;
210+
}
211+
}
212+
213+
214+
function tryCatch() {
215+
try {
216+
}
217+
catch (err) {
218+
}
219+
}
220+
221+
222+
function tryFinally() {
223+
try {
224+
}
225+
finally {
226+
}
227+
}
228+
229+
230+
function tryCatchFinally() {
231+
try {
232+
}
233+
catch (err) {
234+
}
235+
finally {
236+
}
237+
}
238+
239+
240+
class indentBeforeCurly {
241+
}
242+
243+
244+
function argumentsListIndentation(bar,
245+
blah,
246+
);
247+
248+
249+
function blockIndentAfterIndentedParameter1(bar,
250+
blah) {
251+
}
252+
253+
254+
function blockIndentAfterIndentedParameter2(bar,
255+
blah) {
256+
if (foo) {
257+
}
258+
}
259+
260+
var templateLiterals = \`abcdefghi
261+
jklmnop
262+
qrstuvwxyz\`;`);
263+
264+
format.setFormatOptions(originalOptions);

tests/cases/fourslash/fourslash.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ declare namespace FourSlashInterface {
6868
data?: any;
6969
}
7070
interface EditorOptions {
71+
BaseIndentSize?: number,
7172
IndentSize: number;
7273
TabSize: number;
7374
NewLineCharacter: string;
@@ -84,7 +85,7 @@ declare namespace FourSlashInterface {
8485
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean;
8586
PlaceOpenBraceOnNewLineForFunctions: boolean;
8687
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
87-
[s: string]: boolean | number | string;
88+
[s: string]: boolean | number | string | undefined;
8889
}
8990
interface Range {
9091
fileName: string;
@@ -140,7 +141,7 @@ declare namespace FourSlashInterface {
140141
assertHasRanges(ranges: Range[]): void;
141142
caretAtMarker(markerName?: string): void;
142143
indentationIs(numberOfSpaces: number): void;
143-
indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle?: ts.IndentStyle): void;
144+
indentationAtPositionIs(fileName: string, position: number, numberOfSpaces: number, indentStyle?: ts.IndentStyle, baseIndentSize?: number): void;
144145
textAtCaretIs(text: string): void;
145146
/**
146147
* Compiles the current file and evaluates 'expr' in a context containing

0 commit comments

Comments
 (0)