Skip to content

Commit c40698d

Browse files
authored
Fix abstract enum emptylines (#673)
* fixed empty lines between fields of enum abstract, fixes #672 * fixed empty lines for if with comment, fixes #556 * fixed empty lines for block level doc comments, fixes #511
1 parent 1e94f19 commit c40698d

7 files changed

Lines changed: 354 additions & 12 deletions

File tree

.github/workflows/formatter.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323
run: git config --global core.autocrlf false
2424
- name: Checkout Formatter sources
2525
uses: actions/checkout@v1
26-
- name: Use Node.js 12
26+
- name: Use Node.js 16
2727
uses: actions/setup-node@v1
2828
with:
29-
node-version: 12
29+
node-version: 16
3030
- name: Run npm install
3131
run: npm ci
3232
- name: Install Haxe version ${{ matrix.haxe-version }}

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

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

5-
- Added `sameLine.ifElseSemicolonNextLine` to allow breaking `if (true) foo; else foo;`, fixes [#612](https://github.com/HaxeCheckstyle/haxe-formatter/issues/612) ([#662](https://github.com/HaxeCheckstyle/haxe-formatter/issues/668))
5+
- Added `sameLine.ifElseSemicolonNextLine` to allow breaking `if (true) foo; else foo;`, fixes [#612](https://github.com/HaxeCheckstyle/haxe-formatter/issues/612) ([#668](https://github.com/HaxeCheckstyle/haxe-formatter/issues/668))
66
- Fixed whitespace before null safety operator
77
- Fixed keeping same line for `macro if` expressions
88
- Fixed wrapping with maxLineLength off by one, fixes [#670](https://github.com/HaxeCheckstyle/haxe-formatter/issues/670) ([#671](https://github.com/HaxeCheckstyle/haxe-formatter/issues/671))
99
- Fixed extends wrapping for interfaces, fixes [#669](https://github.com/HaxeCheckstyle/haxe-formatter/issues/669)
10+
- Fixed empty lines between fields of enum abstract, fixes [#672](https://github.com/HaxeCheckstyle/haxe-formatter/issues/672) ([#673](https://github.com/HaxeCheckstyle/haxe-formatter/issues/673))
11+
- Fixed empty lines for if with comment, fixes [#556](https://github. ([#673](https://github.com/HaxeCheckstyle/haxe-formatter/issues/673))com/HaxeCheckstyle/haxe-formatter/issues/556)
12+
- Fixed empty lines for block level doc comments, fixes [#511](https://github.com/HaxeCheckstyle/haxe-formatter/issues/51) ([#673](https://github.com/HaxeCheckstyle/haxe-formatter/issues/673))
1013

1114
## version 1.14.6 (2023-02-22)
1215

haxe_libraries/hxnodejs.hxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# @install: lix --silent download "gh://github.com/HaxeFoundation/hxnodejs#020fde75368cb8ff2d04eeea0b3358c0553de5b7" into hxnodejs/12.0.0/github/020fde75368cb8ff2d04eeea0b3358c0553de5b7
2-
-cp ${HAXE_LIBCACHE}/hxnodejs/12.0.0/github/020fde75368cb8ff2d04eeea0b3358c0553de5b7/src
3-
-D hxnodejs=12.0.0
1+
# @install: lix --silent download "gh://github.com/HaxeFoundation/hxnodejs#504066dc1ba5ad543afa5f6c3ea019f06136a82b" into hxnodejs/12.1.0/github/504066dc1ba5ad543afa5f6c3ea019f06136a82b
2+
-cp ${HAXE_LIBCACHE}/hxnodejs/12.1.0/github/504066dc1ba5ad543afa5f6c3ea019f06136a82b/src
3+
-D hxnodejs=12.1.0
44
--macro allowPackage('sys')
55
# should behave like other target defines and not be defined in macro context
66
--macro define('nodejs')

src/formatter/marker/MarkEmptyLines.hx

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class MarkEmptyLines extends MarkerBase {
315315
function findClassAndAbstractFields(c:TokenTree):Array<TokenTree> {
316316
return c.filterCallback(function(token:TokenTree, index:Int):FilterResult {
317317
return switch (token.tok) {
318-
case Kwd(KwdFunction), Kwd(KwdVar):
318+
case Kwd(KwdFunction) | Kwd(KwdVar):
319319
FoundSkipSubtree;
320320
case Kwd(KwdFinal):
321321
FoundSkipSubtree;
@@ -646,6 +646,12 @@ class MarkEmptyLines extends MarkerBase {
646646
if (prevToken == null) {
647647
return;
648648
}
649+
if (config.emptyLines.enumAbstractEmptyLines.existingBetweenFields == Keep) {
650+
if (hasEmptyLinesBetweenFields(prevToken, currToken)) {
651+
emptyLinesAfterSubTree(prevToken, 1);
652+
return;
653+
}
654+
}
649655
if (prevVar != currVar) {
650656
// transition between vars and functions
651657
emptyLinesAfterSubTree(prevToken, config.emptyLines.enumAbstractEmptyLines.afterVars);
@@ -712,7 +718,8 @@ class MarkEmptyLines extends MarkerBase {
712718
if (config.existingBetweenFields == Keep) {
713719
if (hasEmptyLinesBetweenFields(prevToken, child)) {
714720
emptyLinesAfterSubTree(prevToken, 1);
715-
return;
721+
prevToken = child;
722+
continue;
716723
}
717724
}
718725
emptyLinesAfterSubTree(prevToken, config.betweenFields);
@@ -998,6 +1005,9 @@ class MarkEmptyLines extends MarkerBase {
9981005
if (!found) {
9991006
continue;
10001007
}
1008+
if (!isField(next)) {
1009+
continue;
1010+
}
10011011
switch (config.emptyLines.beforeDocCommentEmptyLines) {
10021012
case Ignore:
10031013
case None:
@@ -1033,6 +1043,28 @@ class MarkEmptyLines extends MarkerBase {
10331043
}
10341044
}
10351045

1046+
function isField(token:TokenTree):Bool {
1047+
if (token == null) {
1048+
return false;
1049+
}
1050+
var parent:TokenTree = token.parent;
1051+
if (parent == null) {
1052+
return true;
1053+
}
1054+
return switch (parent.tok) {
1055+
case Kwd(KwdAbstract) | Kwd(KwdClass) | Kwd(KwdEnum) | Kwd(KwdInterface) | Kwd(KwdTypedef):
1056+
true;
1057+
case Kwd(_):
1058+
false;
1059+
case Dot | DblDot | QuestionDot | Arrow | Comma:
1060+
false;
1061+
case BkOpen | POpen:
1062+
false;
1063+
default:
1064+
isField(parent);
1065+
}
1066+
}
1067+
10361068
function markMultilineComments() {
10371069
var comments:Array<TokenTree> = parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
10381070
return switch (token.tok) {
@@ -1063,21 +1095,32 @@ class MarkEmptyLines extends MarkerBase {
10631095
parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
10641096
switch (token.tok) {
10651097
case Kwd(KwdIf):
1066-
if ((token.children != null) && (token.children.length > 0)) {
1067-
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
1098+
if ((token.children != null) && (token.children.length > 1)) {
1099+
for (child in token.children) {
1100+
switch (child.tok) {
1101+
case POpen:
1102+
continue;
1103+
default:
1104+
removeEmptyLinesAroundBlock(child, config.emptyLines.beforeBlocks, Keep);
1105+
}
1106+
}
10681107
}
10691108
var block:Null<TokenTree> = token.access().firstOf(Kwd(KwdElse)).previousSibling().token;
10701109
if (block != null) {
10711110
removeEmptyLinesAroundBlock(block, Keep, config.emptyLines.afterBlocks);
10721111
}
10731112
case Kwd(KwdElse):
1074-
removeEmptyLinesAroundBlock(token.getFirstChild(), config.emptyLines.beforeBlocks, Keep);
1113+
if (token.children != null) {
1114+
for (child in token.children) {
1115+
removeEmptyLinesAroundBlock(child, config.emptyLines.beforeBlocks, Keep);
1116+
}
1117+
}
10751118
case Kwd(KwdCase), Kwd(KwdDefault):
10761119
var block:Null<TokenTree> = token.access().firstOf(DblDot).firstChild().token;
10771120
removeEmptyLinesAroundBlock(block, config.emptyLines.beforeBlocks, Keep);
10781121
case Kwd(KwdFunction):
10791122
case Kwd(KwdFor):
1080-
if ((token.children != null) && (token.children.length > 0)) {
1123+
if ((token.children != null) && (token.children.length > 1)) {
10811124
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
10821125
}
10831126
case Kwd(KwdDo):
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{}
2+
3+
---
4+
5+
class Main {
6+
static function main() {
7+
8+
/** test **/
9+
foo();
10+
bar();
11+
12+
/** test **/
13+
14+
foo();
15+
16+
bar();
17+
18+
/** test **/
19+
20+
}
21+
}
22+
23+
---
24+
25+
class Main {
26+
static function main() {
27+
/** test **/
28+
foo();
29+
bar();
30+
31+
/** test **/
32+
33+
foo();
34+
35+
bar();
36+
37+
/** test **/
38+
}
39+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{}
2+
3+
---
4+
5+
class Main {
6+
static function main() {
7+
if (foo)
8+
9+
bar;
10+
if (foo) //
11+
12+
bar;
13+
14+
if (foo)
15+
16+
bar;
17+
else
18+
19+
baz;
20+
if (foo) //
21+
22+
bar;
23+
else //
24+
25+
baz;
26+
27+
if (foo)
28+
29+
//
30+
31+
bar;
32+
else
33+
34+
//
35+
36+
baz;
37+
38+
39+
if (foo)
40+
41+
//
42+
43+
//
44+
45+
bar;
46+
else
47+
48+
//
49+
50+
//
51+
52+
baz;
53+
54+
if (foo)
55+
56+
//
57+
58+
//
59+
60+
bar; //
61+
else
62+
63+
//
64+
65+
//
66+
67+
baz; //
68+
69+
if (foo) //
70+
71+
//
72+
73+
//
74+
75+
bar; //
76+
else //
77+
78+
//
79+
80+
//
81+
82+
baz; //
83+
}
84+
}
85+
86+
---
87+
88+
class Main {
89+
static function main() {
90+
if (foo)
91+
bar;
92+
if (foo) //
93+
bar;
94+
95+
if (foo)
96+
bar;
97+
else
98+
baz;
99+
if (foo) //
100+
bar;
101+
else //
102+
baz;
103+
104+
if (foo)
105+
//
106+
bar;
107+
else
108+
//
109+
baz;
110+
111+
if (foo)
112+
//
113+
//
114+
bar;
115+
else
116+
//
117+
//
118+
baz;
119+
120+
if (foo)
121+
//
122+
//
123+
bar; //
124+
else
125+
//
126+
//
127+
baz; //
128+
129+
if (foo) //
130+
//
131+
//
132+
bar; //
133+
else //
134+
//
135+
//
136+
baz; //
137+
}
138+
}

0 commit comments

Comments
 (0)