Skip to content

Commit

Permalink
added indentation.indentComplexValueExpressions, fixes #468 (#469)
Browse files Browse the repository at this point in the history
* added indentation.indentComplexValueExpressions
allows extra indentation for complex value expressions
(e.g. wrapped `return if`, `var for =` expressions)
  • Loading branch information
AlexHaxe authored May 16, 2019
1 parent f25c10b commit 2b15d8c
Show file tree
Hide file tree
Showing 25 changed files with 1,183 additions and 139 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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

## version 1.7.0 (2019-05-16)

- 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))
- 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))
- 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))
- 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))
- Added Java compilation and tests on TravisCI ([#456](https://github.com/HaxeCheckstyle/haxe-formatter/issues/456))
- Added browser JS compilation, fixes [#449](https://github.com/HaxeCheckstyle/haxe-formatter/issues/449) ([#456](https://github.com/HaxeCheckstyle/haxe-formatter/issues/456))
- Added cache for close tokens `]`, `)` and `}` ([#461](https://github.com/HaxeCheckstyle/haxe-formatter/issues/461))
- Added `indentation.indentComplexValueExpressions`, fixes [#468](https://github.com/HaxeCheckstyle/haxe-formatter/issues/468) ([#469](https://github.com/HaxeCheckstyle/haxe-formatter/issues/469))
- 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))
- Fixed wrapping of concatenated strings ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422)
- Fixed ECheckType detection with cast, fixes [#374](https://github.com/HaxeCheckstyle/haxe-formatter/issues/374) ([#422](https://github.com/HaxeCheckstyle/haxe-formatter/issues/422))
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"style"
],
"description": "A code formatter for Haxe",
"version": "1.6.0",
"releasenote": "New --stdin CLI parameter and bugfixes - see CHANGELOG for details.",
"version": "1.7.0",
"releasenote": "Added wrapping for matrixes and multiple var declarations; added more curly line end options; bugfixes - see CHANGELOG for details.",
"contributors": [
"AlexHaxe",
"Gama11"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haxe-formatter",
"version": "1.6.0",
"version": "1.7.0",
"description": "A code formatter for Haxe",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions resources/default-hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"indentation": {
"character": "tab",
"conditionalPolicy": "aligned",
"indentComplexValueExpressions": false,
"indentObjectLiteral": true,
"tabWidth": 4,
"trailingWhitespace": false
Expand Down
5 changes: 5 additions & 0 deletions resources/hxformat-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@
"description": "adds trailing whitespace to empty lines by copying indentation from preceeding line",
"type": "boolean",
"default": false
},
"indentComplexValueExpressions": {
"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;",
"type": "boolean",
"default": false
}
},
"type": "object"
Expand Down
14 changes: 14 additions & 0 deletions src/formatter/config/IndentationConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ typedef IndentationConfig = {
@:default(false) @:optional var trailingWhitespace:Bool;

@:default(true) @:optional var indentObjectLiteral:Bool;

/**
indent complex value expressions:
(true) (false)
var a = if (true) var a = if (true)
10; 10;
else else
20; vs. 20;
return if (true) return if (true)
10; 10;
else else
20; 20;
**/
@:default(false) @:optional var indentComplexValueExpressions:Bool;
}

@:enum
Expand Down
80 changes: 63 additions & 17 deletions src/formatter/marker/Indenter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Indenter {
return token;
}

function countLineBreaks(indentingTokensCandidates:Array<TokenTree>):Int {
function countLineBreaks(indentingTokensCandidates:Array<TokenTree>, indentComplexValueExpressions:Bool):Int {
var count:Int = 0;
var prevToken:Null<TokenTree> = null;
var currentToken:Null<TokenTree> = null;
Expand All @@ -199,23 +199,32 @@ class Indenter {
if (prevToken.index == currentToken.index) {
continue;
}
if (parsedCode.tokenList.isSameLineBetween(currentToken, prevToken, false)) {
var elseTok:Null<TokenTree> = prevToken.access().firstOf(Kwd(KwdElse)).token;
if (elseTok != null) {
if (parsedCode.tokenList.isSameLineBetween(prevToken, elseTok, false)) {
continue;
switch (currentToken.tok) {
case Binop(OpAssign):
if (indentComplexValueExpressions) {
mustIndent = true;
}
mustIndent = true;
}
var brOpen:Null<TokenTree> = prevToken.access().firstOf(BrOpen).token;
if (brOpen != null) {
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(brOpen);
switch (type) {
case BLOCK:
continue;
default:
default:
if (parsedCode.tokenList.isSameLineBetween(currentToken, prevToken, false)) {
var elseTok:Null<TokenTree> = prevToken.access().firstOf(Kwd(KwdElse)).token;
if (elseTok != null) {
if (parsedCode.tokenList.isSameLineBetween(prevToken, elseTok, false)) {
continue;
}
if (indentComplexValueExpressions) {
mustIndent = true;
}
}
var brOpen:Null<TokenTree> = prevToken.access().firstOf(BrOpen).token;
if (brOpen != null) {
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(brOpen);
switch (type) {
case BLOCK:
continue;
default:
}
}
}
}
}

case Kwd(KwdElse):
Expand All @@ -230,6 +239,10 @@ class Indenter {
}
case Kwd(KwdSwitch):
switch (currentToken.tok) {
case Binop(op):
if (indentComplexValueExpressions) {
mustIndent = true;
}
case POpen:
var type:POpenType = TokenTreeCheckUtils.getPOpenType(currentToken);
switch (type) {
Expand Down Expand Up @@ -342,6 +355,27 @@ class Indenter {
return count;
}

function isFieldLevelVar(indentingTokensCandidates:Array<TokenTree>):Bool {
var tokens:Array<TokenTree> = indentingTokensCandidates.copy();
tokens.reverse();
for (token in tokens) {
switch (token.tok) {
case Kwd(KwdFunction):
return false;
case Kwd(KwdVar):
return true;
case Const(CIdent(MarkEmptyLines.FINAL)):
#if (haxe_ver >= 4.0)
case Kwd(KwdFinal):
#end
case Binop(OpAssign):
return true;
default:
}
}
return false;
}

function calcFromCandidates(token:TokenTree):Int {
var indentingTokensCandidates:Array<TokenTree> = findIndentingCandidates(token);
#if debugIndent
Expand All @@ -350,7 +384,16 @@ class Indenter {
if (indentingTokensCandidates.length <= 0) {
return 0;
}
var count:Int = countLineBreaks(indentingTokensCandidates);

var indentComplexValueExpressions:Bool = config.indentComplexValueExpressions;
if (isFieldLevelVar(indentingTokensCandidates)) {
indentComplexValueExpressions = true;
}
if (indentComplexValueExpressions) {
indentingTokensCandidates = compressElseIfCandidates(indentingTokensCandidates);
}

var count:Int = countLineBreaks(indentingTokensCandidates, indentComplexValueExpressions);
if (hasConditional(indentingTokensCandidates)) {
switch (config.conditionalPolicy) {
case AlignedDecrease:
Expand Down Expand Up @@ -407,7 +450,10 @@ class Indenter {
}
}
}
return indentingTokensCandidates;
}

function compressElseIfCandidates(indentingTokensCandidates:Array<TokenTree>):Array<TokenTree> {
var compressedCandidates:Array<TokenTree> = [];
var state:IndentationCompressElseIf = Copy;
for (token in indentingTokensCandidates) {
Expand Down
2 changes: 2 additions & 0 deletions test/TestSuite.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import formatter.FormatStatsTest;
import testcases.EmptyLinesTestCases;
import testcases.ExpressionLevelTestCases;
import testcases.IndentationTestCases;
import testcases.LineEndsTestCases;
import testcases.MissingTestCases;
Expand All @@ -19,6 +20,7 @@ class TestSuite extends massive.munit.TestSuite {
}

add(EmptyLinesTestCases);
add(ExpressionLevelTestCases);
add(IndentationTestCases);
add(LineEndsTestCases);
add(MissingTestCases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ class Main {
class Main {
function loop(min, max) {
return if (mid == min)
{line: mid, pos: pos - start + 1};
else if (start > pos)
loop(min, mid);
{line: mid, pos: pos - start + 1};
else if (start > pos)
loop(min, mid);

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

return if (mid == min)
{line: mid, pos: pos - start + 1};
else if (start > pos)
loop(min, mid);
else
loop(mid, max);
{line: mid, pos: pos - start + 1};
else if (start > pos)
loop(min, mid);
else
loop(mid, max);

return if (mid == min) {
{line: mid, pos: pos - start + 1};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,46 @@ class Main {
return if (true) 10; else 20;

var a = if (true) 10;
else 20;
else 20;
var a = if (true) 10
else 20;
else 20;
return if (true) 10
else 20;
else 20;
return if (true) 10;
else 20;
else 20;

var a = if (true)
10;
else
20;
10;
else
20;
var a = if (true)
10
else
20;
10
else
20;
return if (true)
10
else
20;
10
else
20;
return if (true)
10;
else
20;
10;
else
20;

var a = if (true)
10;
else
20;
10;
else
20;
var a = if (true)
10
else
20;
10
else
20;
return if (true)
10
else
20;
10
else
20;
return if (true)
10;
else
20;
10;
else
20;
}
}
Loading

0 comments on commit 2b15d8c

Please sign in to comment.