Skip to content

Commit

Permalink
fix conditional lineends / wrapping (#654)
Browse files Browse the repository at this point in the history
* fixed conditional line ends / wrapping
* fixed whitespace after from/to with arrow functions
  • Loading branch information
AlexHaxe authored Apr 13, 2022
1 parent 0c6518d commit 1c2dfa3
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

- Fixed conditional line ends / wrapping ([#654](https://github.com/HaxeCheckstyle/haxe-formatter/issues/654))
- Fixed whitespace after from/to with arrow functions ([#654](https://github.com/HaxeCheckstyle/haxe-formatter/issues/654))

## version 1.13.0 (2022-03-18)

- Added default type parameter support, fixes [#650](https://github.com/HaxeCheckstyle/haxe-formatter/issues/650) ([#651](https://github.com/HaxeCheckstyle/haxe-formatter/issues/651))
Expand Down
28 changes: 21 additions & 7 deletions src/formatter/marker/MarkLineEnds.hx
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,26 @@ class MarkLineEnds extends MarkerBase {
}
switch (sharpEnd.tok) {
case Sharp(SHARP_END):
if (parsedCode.linesBetweenOriginal(token, sharpEnd) > 5) {
var count:Int = parsedCode.linesBetweenOriginal(token, sharpEnd);
var next:Null<TokenInfo> = getNextToken(sharpEnd);
if (next != null) {
switch (next.token.tok) {
case Comma, Semicolon:
if (count > 5) {
return false;
}
return true;
default:
}
}

if (count == 0) {
return true;
}
if (count > 3) {
return false;
}
case Comma, Semicolon:
case Comma | Semicolon:
sharpEnd = sharpEnd.previousSibling;
if (sharpEnd == null) {
return false;
Expand All @@ -593,12 +609,10 @@ class MarkLineEnds extends MarkerBase {
return false;
}
switch (prev.token.tok) {
case Semicolon:
return false;
case BrClose:
return false;
case Comment(_), CommentLine(_):
case Semicolon | BrClose | Comment(_) | CommentLine(_) | BkOpen | Comma:
return false;
case Binop(_):
return true;
case PClose:
if (parsedCode.isOriginalSameLine(prev.token, token)) {
return true;
Expand Down
13 changes: 11 additions & 2 deletions src/formatter/marker/MarkWhitespace.hx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ class MarkWhitespace extends MarkerBase {
policy = policy.add(Before);
default:
}
case Const(CIdent("from")) | Const(CIdent("to")):
var parent:Null<TokenTree> = prev.token.parent;
if (parent != null) {
switch (parent.tok) {
case Const(_):
policy = policy.add(Before);
default:
}
}
default:
}
}
Expand Down Expand Up @@ -470,7 +479,7 @@ class MarkWhitespace extends MarkerBase {
var prev:Null<TokenInfo> = getPreviousToken(token);
if (prev != null) {
switch (prev.token.tok) {
case Const(_), Kwd(_):
case Const(_) | Kwd(_) | Binop(_):
whitespace(token, Before);
default:
}
Expand Down Expand Up @@ -505,7 +514,7 @@ class MarkWhitespace extends MarkerBase {
}
}
case Sharp("error"):
whitespace(token, After);
whitespace(token, Around);
default:
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/formatter/marker/wrapping/MarkWrappingBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ class MarkWrappingBase extends MarkerBase {
for (item in items) {
additionalIndent(item.first, addIndent);
lineEndBefore(item.first);
switch (item.last.tok) {
case Sharp(_):
lineEndBefore(item.last);
default:
}
}
}
if (keepFirst) {
Expand Down
115 changes: 115 additions & 0 deletions test/testcases/lineends/conditional_array.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"lineEnds": {
"metadataFunction": "none"
}
}

---

class Main{
function main() {
var test = [
new Node ("test", result.test, Leaf, node),
#if true
new Node ("test", result.test, Leaf, node),
#end
new Node ("test", result.test, Leaf, node),
];
var test = [
#if true
new Node ("test", result.test, Leaf, node),
#end
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
];
var test = [
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
#if true
new Node ("test", result.test, Leaf, node),
#end
];
var test = [
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
#if true
new Node ("test", result.test, Leaf, node)
#end
];
var test = [
#if true new Node ("test", result.test, Leaf, node), #end
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
];
var test = [
new Node ("test", result.test, Leaf, node),
#if true new Node ("test", result.test, Leaf, node), #end
new Node ("test", result.test, Leaf, node),
];
var test = [
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
#if true new Node ("test", result.test, Leaf, node), #end
];
var test = [
new Node ("test", result.test, Leaf, node),
new Node ("test", result.test, Leaf, node),
#if true new Node ("test", result.test, Leaf, node) #end
];
}
}

---

class Main {
function main() {
var test = [
new Node("test", result.test, Leaf, node),
#if true
new Node("test", result.test, Leaf, node),
#end
new Node("test", result.test, Leaf, node),
];
var test = [
#if true
new Node("test", result.test, Leaf, node),
#end
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
];
var test = [
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
#if true
new Node("test", result.test, Leaf, node),
#end
];
var test = [
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
#if true
new Node("test", result.test, Leaf, node)
#end
];
var test = [
#if true new Node("test", result.test, Leaf, node), #end
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
];
var test = [
new Node("test", result.test, Leaf, node),
#if true new Node("test", result.test, Leaf, node), #end
new Node("test", result.test, Leaf, node),
];
var test = [
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
#if true new Node("test", result.test, Leaf, node), #end
];
var test = [
new Node("test", result.test, Leaf, node),
new Node("test", result.test, Leaf, node),
#if true new Node("test", result.test, Leaf, node) #end
];
}
}
11 changes: 11 additions & 0 deletions test/testcases/lineends/issue_17_lineend_aftersharp_error.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@

#if js #error "js is defined" #elseif php #else #end

#if js trace("js is defined"); #elseif php #else #end

#if js
#error "js is defined"
#elseif php
#else
#end


---

#if js #error "js is defined" #elseif php #else #end
#if js trace("js is defined"); #elseif php #else #end
#if js
#error "js is defined"
#elseif php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ class Foo {}

---

#if foo
@:keep
#end
#if foo @:keep #end
class Foo {}
7 changes: 6 additions & 1 deletion test/testcases/sameline/inline_conditional.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ class Main {
class Main {
static function main() {
var debugDraw = #if debug System..... #else false #end;
var debugDraw = #if (debug) System.url.queryString.exists("debugDraw"); #else false; #end
var debugDraw =
#if (debug)
System.url.queryString.exists("debugDraw");
#else
false;
#end
#if (debug)
var debugDraw = System.url.queryString.exists("debugDraw");
#else
Expand Down
14 changes: 14 additions & 0 deletions test/testcases/whitespace/abstract_from_function.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{}


---

abstract A(Dynamic) from (String, Int) -> Int from (String, Bool) -> Bool from (Int) -> Void {}
abstract A(Dynamic) to (String, Int) -> Int to (String, Bool) -> Bool to (Int) -> Void {}
abstract A(Dynamic) from (String, Int) -> Int from (Int) -> Void to (String, Int) -> Int to (Int) -> Void {}

---

abstract A(Dynamic) from (String, Int) -> Int from (String, Bool) -> Bool from (Int) -> Void {}
abstract A(Dynamic) to (String, Int) -> Int to (String, Bool) -> Bool to (Int) -> Void {}
abstract A(Dynamic) from (String, Int) -> Int from (Int) -> Void to (String, Int) -> Int to (Int) -> Void {}

0 comments on commit 1c2dfa3

Please sign in to comment.