Skip to content

Commit 2b15d8c

Browse files
authored
added indentation.indentComplexValueExpressions, fixes #468 (#469)
* added indentation.indentComplexValueExpressions allows extra indentation for complex value expressions (e.g. wrapped `return if`, `var for =` expressions)
1 parent f25c10b commit 2b15d8c

25 files changed

+1183
-139
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
## dev branch / next version (1.x.x)
44

5+
## version 1.7.0 (2019-05-16)
6+
57
- Added `wrapping.multiVar`, fixes [#355](https://github.com/HaxeCheckstyle/haxe-formatter/issues/355), fixes [#430](https://github.com/HaxeCheckstyle/haxe-formatter/issues/430) ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422) + [#434](https://github.com/HaxeCheckstyle/haxe-formatter/issues/434))
68
- Added `emptylines.afterFieldsWithDocComments`, fixes [#385](https://github.com/HaxeCheckstyle/haxe-formatter/issues/385), fixes [#432](https://github.com/HaxeCheckstyle/haxe-formatter/issues/432) ([#425](https://github.com/HaxeCheckstyle/haxe-formatter/issues/425) + [#434](https://github.com/HaxeCheckstyle/haxe-formatter/issues/434))
79
- Added `lineEnds.anonTypeCurly`, `lineEnds.blockCurly`, `lineEnds.objectLiteralCurly`, `lineEnds.typedefCurly`, fixes [#346](https://github.com/HaxeCheckstyle/haxe-formatter/issues/346) ([#427](https://github.com/HaxeCheckstyle/haxe-formatter/issues/427) + [#434](https://github.com/HaxeCheckstyle/haxe-formatter/issues/434) + [#456](https://github.com/HaxeCheckstyle/haxe-formatter/issues/456))
810
- Added `wrapping.arrayMatrixWrap` for array matrix wrapping with column alignment, fixes [#433](https://github.com/HaxeCheckstyle/haxe-formatter/issues/433) ([#442](https://github.com/HaxeCheckstyle/haxe-formatter/issues/442))
911
- Added Java compilation and tests on TravisCI ([#456](https://github.com/HaxeCheckstyle/haxe-formatter/issues/456))
1012
- Added browser JS compilation, fixes [#449](https://github.com/HaxeCheckstyle/haxe-formatter/issues/449) ([#456](https://github.com/HaxeCheckstyle/haxe-formatter/issues/456))
1113
- Added cache for close tokens `]`, `)` and `}` ([#461](https://github.com/HaxeCheckstyle/haxe-formatter/issues/461))
14+
- Added `indentation.indentComplexValueExpressions`, fixes [#468](https://github.com/HaxeCheckstyle/haxe-formatter/issues/468) ([#469](https://github.com/HaxeCheckstyle/haxe-formatter/issues/469))
1215
- Fixed missing empty lines in classes with conditionals, fixes [#419](https://github.com/HaxeCheckstyle/haxe-formatter/issues/419) ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422))
1316
- Fixed wrapping of concatenated strings ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422)
1417
- Fixed ECheckType detection with cast, fixes [#374](https://github.com/HaxeCheckstyle/haxe-formatter/issues/374) ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422))

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"style"
99
],
1010
"description": "A code formatter for Haxe",
11-
"version": "1.6.0",
12-
"releasenote": "New --stdin CLI parameter and bugfixes - see CHANGELOG for details.",
11+
"version": "1.7.0",
12+
"releasenote": "Added wrapping for matrixes and multiple var declarations; added more curly line end options; bugfixes - see CHANGELOG for details.",
1313
"contributors": [
1414
"AlexHaxe",
1515
"Gama11"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "haxe-formatter",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"description": "A code formatter for Haxe",
55
"repository": {
66
"type": "git",

resources/default-hxformat.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"indentation": {
112112
"character": "tab",
113113
"conditionalPolicy": "aligned",
114+
"indentComplexValueExpressions": false,
114115
"indentObjectLiteral": true,
115116
"tabWidth": 4,
116117
"trailingWhitespace": false

resources/hxformat-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,11 @@
10041004
"description": "adds trailing whitespace to empty lines by copying indentation from preceeding line",
10051005
"type": "boolean",
10061006
"default": false
1007+
},
1008+
"indentComplexValueExpressions": {
1009+
"description": "indent complex value expressions: (true)\t\t\t\t\t\t(false) var a = if (true)\t\t\tvar a = if (true) 10;\t\t\t\t\t\t10; else\t\t\t\t\telse 20;\t\t\tvs.\t\t\t20; return if (true)\t\t\treturn if (true) 10;\t\t\t\t\t\t10; else\t\t\t\t\telse 20;\t\t\t\t\t\t20;",
1010+
"type": "boolean",
1011+
"default": false
10071012
}
10081013
},
10091014
"type": "object"

src/formatter/config/IndentationConfig.hx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ typedef IndentationConfig = {
2626
@:default(false) @:optional var trailingWhitespace:Bool;
2727

2828
@:default(true) @:optional var indentObjectLiteral:Bool;
29+
30+
/**
31+
indent complex value expressions:
32+
(true) (false)
33+
var a = if (true) var a = if (true)
34+
10; 10;
35+
else else
36+
20; vs. 20;
37+
return if (true) return if (true)
38+
10; 10;
39+
else else
40+
20; 20;
41+
**/
42+
@:default(false) @:optional var indentComplexValueExpressions:Bool;
2943
}
3044

3145
@:enum

src/formatter/marker/Indenter.hx

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class Indenter {
178178
return token;
179179
}
180180

181-
function countLineBreaks(indentingTokensCandidates:Array<TokenTree>):Int {
181+
function countLineBreaks(indentingTokensCandidates:Array<TokenTree>, indentComplexValueExpressions:Bool):Int {
182182
var count:Int = 0;
183183
var prevToken:Null<TokenTree> = null;
184184
var currentToken:Null<TokenTree> = null;
@@ -199,23 +199,32 @@ class Indenter {
199199
if (prevToken.index == currentToken.index) {
200200
continue;
201201
}
202-
if (parsedCode.tokenList.isSameLineBetween(currentToken, prevToken, false)) {
203-
var elseTok:Null<TokenTree> = prevToken.access().firstOf(Kwd(KwdElse)).token;
204-
if (elseTok != null) {
205-
if (parsedCode.tokenList.isSameLineBetween(prevToken, elseTok, false)) {
206-
continue;
202+
switch (currentToken.tok) {
203+
case Binop(OpAssign):
204+
if (indentComplexValueExpressions) {
205+
mustIndent = true;
207206
}
208-
mustIndent = true;
209-
}
210-
var brOpen:Null<TokenTree> = prevToken.access().firstOf(BrOpen).token;
211-
if (brOpen != null) {
212-
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(brOpen);
213-
switch (type) {
214-
case BLOCK:
215-
continue;
216-
default:
207+
default:
208+
if (parsedCode.tokenList.isSameLineBetween(currentToken, prevToken, false)) {
209+
var elseTok:Null<TokenTree> = prevToken.access().firstOf(Kwd(KwdElse)).token;
210+
if (elseTok != null) {
211+
if (parsedCode.tokenList.isSameLineBetween(prevToken, elseTok, false)) {
212+
continue;
213+
}
214+
if (indentComplexValueExpressions) {
215+
mustIndent = true;
216+
}
217+
}
218+
var brOpen:Null<TokenTree> = prevToken.access().firstOf(BrOpen).token;
219+
if (brOpen != null) {
220+
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(brOpen);
221+
switch (type) {
222+
case BLOCK:
223+
continue;
224+
default:
225+
}
226+
}
217227
}
218-
}
219228
}
220229

221230
case Kwd(KwdElse):
@@ -230,6 +239,10 @@ class Indenter {
230239
}
231240
case Kwd(KwdSwitch):
232241
switch (currentToken.tok) {
242+
case Binop(op):
243+
if (indentComplexValueExpressions) {
244+
mustIndent = true;
245+
}
233246
case POpen:
234247
var type:POpenType = TokenTreeCheckUtils.getPOpenType(currentToken);
235248
switch (type) {
@@ -342,6 +355,27 @@ class Indenter {
342355
return count;
343356
}
344357

358+
function isFieldLevelVar(indentingTokensCandidates:Array<TokenTree>):Bool {
359+
var tokens:Array<TokenTree> = indentingTokensCandidates.copy();
360+
tokens.reverse();
361+
for (token in tokens) {
362+
switch (token.tok) {
363+
case Kwd(KwdFunction):
364+
return false;
365+
case Kwd(KwdVar):
366+
return true;
367+
case Const(CIdent(MarkEmptyLines.FINAL)):
368+
#if (haxe_ver >= 4.0)
369+
case Kwd(KwdFinal):
370+
#end
371+
case Binop(OpAssign):
372+
return true;
373+
default:
374+
}
375+
}
376+
return false;
377+
}
378+
345379
function calcFromCandidates(token:TokenTree):Int {
346380
var indentingTokensCandidates:Array<TokenTree> = findIndentingCandidates(token);
347381
#if debugIndent
@@ -350,7 +384,16 @@ class Indenter {
350384
if (indentingTokensCandidates.length <= 0) {
351385
return 0;
352386
}
353-
var count:Int = countLineBreaks(indentingTokensCandidates);
387+
388+
var indentComplexValueExpressions:Bool = config.indentComplexValueExpressions;
389+
if (isFieldLevelVar(indentingTokensCandidates)) {
390+
indentComplexValueExpressions = true;
391+
}
392+
if (indentComplexValueExpressions) {
393+
indentingTokensCandidates = compressElseIfCandidates(indentingTokensCandidates);
394+
}
395+
396+
var count:Int = countLineBreaks(indentingTokensCandidates, indentComplexValueExpressions);
354397
if (hasConditional(indentingTokensCandidates)) {
355398
switch (config.conditionalPolicy) {
356399
case AlignedDecrease:
@@ -407,7 +450,10 @@ class Indenter {
407450
}
408451
}
409452
}
453+
return indentingTokensCandidates;
454+
}
410455

456+
function compressElseIfCandidates(indentingTokensCandidates:Array<TokenTree>):Array<TokenTree> {
411457
var compressedCandidates:Array<TokenTree> = [];
412458
var state:IndentationCompressElseIf = Copy;
413459
for (token in indentingTokensCandidates) {

test/TestSuite.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import formatter.FormatStatsTest;
22
import testcases.EmptyLinesTestCases;
3+
import testcases.ExpressionLevelTestCases;
34
import testcases.IndentationTestCases;
45
import testcases.LineEndsTestCases;
56
import testcases.MissingTestCases;
@@ -19,6 +20,7 @@ class TestSuite extends massive.munit.TestSuite {
1920
}
2021

2122
add(EmptyLinesTestCases);
23+
add(ExpressionLevelTestCases);
2224
add(IndentationTestCases);
2325
add(LineEndsTestCases);
2426
add(MissingTestCases);

test/testcases/indentation/issue_220_object_literal_if_else_chain.hxtest

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ class Main {
4040
class Main {
4141
function loop(min, max) {
4242
return if (mid == min)
43-
{line: mid, pos: pos - start + 1};
44-
else if (start > pos)
45-
loop(min, mid);
43+
{line: mid, pos: pos - start + 1};
44+
else if (start > pos)
45+
loop(min, mid);
4646

4747
return if (mid == min)
48-
{line: mid, pos: pos - start + 1};
49-
else
50-
loop(min, mid);
48+
{line: mid, pos: pos - start + 1};
49+
else
50+
loop(min, mid);
5151

5252
return if (mid == min)
53-
{line: mid, pos: pos - start + 1};
54-
else if (start > pos)
55-
loop(min, mid);
56-
else
57-
loop(mid, max);
53+
{line: mid, pos: pos - start + 1};
54+
else if (start > pos)
55+
loop(min, mid);
56+
else
57+
loop(mid, max);
5858

5959
return if (mid == min) {
6060
{line: mid, pos: pos - start + 1};

test/testcases/indentation/issue_304_expression_if_indentation_keep.hxtest

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,46 +64,46 @@ class Main {
6464
return if (true) 10; else 20;
6565

6666
var a = if (true) 10;
67-
else 20;
67+
else 20;
6868
var a = if (true) 10
69-
else 20;
69+
else 20;
7070
return if (true) 10
71-
else 20;
71+
else 20;
7272
return if (true) 10;
73-
else 20;
73+
else 20;
7474

7575
var a = if (true)
76-
10;
77-
else
78-
20;
76+
10;
77+
else
78+
20;
7979
var a = if (true)
80-
10
81-
else
82-
20;
80+
10
81+
else
82+
20;
8383
return if (true)
84-
10
85-
else
86-
20;
84+
10
85+
else
86+
20;
8787
return if (true)
88-
10;
89-
else
90-
20;
88+
10;
89+
else
90+
20;
9191

9292
var a = if (true)
93-
10;
94-
else
95-
20;
93+
10;
94+
else
95+
20;
9696
var a = if (true)
97-
10
98-
else
99-
20;
97+
10
98+
else
99+
20;
100100
return if (true)
101-
10
102-
else
103-
20;
101+
10
102+
else
103+
20;
104104
return if (true)
105-
10;
106-
else
107-
20;
105+
10;
106+
else
107+
20;
108108
}
109109
}

0 commit comments

Comments
 (0)