Skip to content

Commit

Permalink
Fix abstract enum emptylines (#673)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AlexHaxe authored Jun 5, 2023
1 parent 1e94f19 commit c40698d
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
run: git config --global core.autocrlf false
- name: Checkout Formatter sources
uses: actions/checkout@v1
- name: Use Node.js 12
- name: Use Node.js 16
uses: actions/setup-node@v1
with:
node-version: 12
node-version: 16
- name: Run npm install
run: npm ci
- name: Install Haxe version ${{ matrix.haxe-version }}
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

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

- 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))
- 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))
- Fixed whitespace before null safety operator
- Fixed keeping same line for `macro if` expressions
- 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))
- Fixed extends wrapping for interfaces, fixes [#669](https://github.com/HaxeCheckstyle/haxe-formatter/issues/669)
- 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))
- 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)
- 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))

## version 1.14.6 (2023-02-22)

Expand Down
6 changes: 3 additions & 3 deletions haxe_libraries/hxnodejs.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @install: lix --silent download "gh://github.com/HaxeFoundation/hxnodejs#020fde75368cb8ff2d04eeea0b3358c0553de5b7" into hxnodejs/12.0.0/github/020fde75368cb8ff2d04eeea0b3358c0553de5b7
-cp ${HAXE_LIBCACHE}/hxnodejs/12.0.0/github/020fde75368cb8ff2d04eeea0b3358c0553de5b7/src
-D hxnodejs=12.0.0
# @install: lix --silent download "gh://github.com/HaxeFoundation/hxnodejs#504066dc1ba5ad543afa5f6c3ea019f06136a82b" into hxnodejs/12.1.0/github/504066dc1ba5ad543afa5f6c3ea019f06136a82b
-cp ${HAXE_LIBCACHE}/hxnodejs/12.1.0/github/504066dc1ba5ad543afa5f6c3ea019f06136a82b/src
-D hxnodejs=12.1.0
--macro allowPackage('sys')
# should behave like other target defines and not be defined in macro context
--macro define('nodejs')
Expand Down
55 changes: 49 additions & 6 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class MarkEmptyLines extends MarkerBase {
function findClassAndAbstractFields(c:TokenTree):Array<TokenTree> {
return c.filterCallback(function(token:TokenTree, index:Int):FilterResult {
return switch (token.tok) {
case Kwd(KwdFunction), Kwd(KwdVar):
case Kwd(KwdFunction) | Kwd(KwdVar):
FoundSkipSubtree;
case Kwd(KwdFinal):
FoundSkipSubtree;
Expand Down Expand Up @@ -646,6 +646,12 @@ class MarkEmptyLines extends MarkerBase {
if (prevToken == null) {
return;
}
if (config.emptyLines.enumAbstractEmptyLines.existingBetweenFields == Keep) {
if (hasEmptyLinesBetweenFields(prevToken, currToken)) {
emptyLinesAfterSubTree(prevToken, 1);
return;
}
}
if (prevVar != currVar) {
// transition between vars and functions
emptyLinesAfterSubTree(prevToken, config.emptyLines.enumAbstractEmptyLines.afterVars);
Expand Down Expand Up @@ -712,7 +718,8 @@ class MarkEmptyLines extends MarkerBase {
if (config.existingBetweenFields == Keep) {
if (hasEmptyLinesBetweenFields(prevToken, child)) {
emptyLinesAfterSubTree(prevToken, 1);
return;
prevToken = child;
continue;
}
}
emptyLinesAfterSubTree(prevToken, config.betweenFields);
Expand Down Expand Up @@ -998,6 +1005,9 @@ class MarkEmptyLines extends MarkerBase {
if (!found) {
continue;
}
if (!isField(next)) {
continue;
}
switch (config.emptyLines.beforeDocCommentEmptyLines) {
case Ignore:
case None:
Expand Down Expand Up @@ -1033,6 +1043,28 @@ class MarkEmptyLines extends MarkerBase {
}
}

function isField(token:TokenTree):Bool {
if (token == null) {
return false;
}
var parent:TokenTree = token.parent;
if (parent == null) {
return true;
}
return switch (parent.tok) {
case Kwd(KwdAbstract) | Kwd(KwdClass) | Kwd(KwdEnum) | Kwd(KwdInterface) | Kwd(KwdTypedef):
true;
case Kwd(_):
false;
case Dot | DblDot | QuestionDot | Arrow | Comma:
false;
case BkOpen | POpen:
false;
default:
isField(parent);
}
}

function markMultilineComments() {
var comments:Array<TokenTree> = parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
return switch (token.tok) {
Expand Down Expand Up @@ -1063,21 +1095,32 @@ class MarkEmptyLines extends MarkerBase {
parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
switch (token.tok) {
case Kwd(KwdIf):
if ((token.children != null) && (token.children.length > 0)) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
if ((token.children != null) && (token.children.length > 1)) {
for (child in token.children) {
switch (child.tok) {
case POpen:
continue;
default:
removeEmptyLinesAroundBlock(child, config.emptyLines.beforeBlocks, Keep);
}
}
}
var block:Null<TokenTree> = token.access().firstOf(Kwd(KwdElse)).previousSibling().token;
if (block != null) {
removeEmptyLinesAroundBlock(block, Keep, config.emptyLines.afterBlocks);
}
case Kwd(KwdElse):
removeEmptyLinesAroundBlock(token.getFirstChild(), config.emptyLines.beforeBlocks, Keep);
if (token.children != null) {
for (child in token.children) {
removeEmptyLinesAroundBlock(child, config.emptyLines.beforeBlocks, Keep);
}
}
case Kwd(KwdCase), Kwd(KwdDefault):
var block:Null<TokenTree> = token.access().firstOf(DblDot).firstChild().token;
removeEmptyLinesAroundBlock(block, config.emptyLines.beforeBlocks, Keep);
case Kwd(KwdFunction):
case Kwd(KwdFor):
if ((token.children != null) && (token.children.length > 0)) {
if ((token.children != null) && (token.children.length > 1)) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
}
case Kwd(KwdDo):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{}

---

class Main {
static function main() {

/** test **/
foo();
bar();

/** test **/

foo();

bar();

/** test **/

}
}

---

class Main {
static function main() {
/** test **/
foo();
bar();

/** test **/

foo();

bar();

/** test **/
}
}
138 changes: 138 additions & 0 deletions test/testcases/emptylines/issue_556_if_body_with_comment.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{}

---

class Main {
static function main() {
if (foo)

bar;
if (foo) //

bar;

if (foo)

bar;
else

baz;
if (foo) //

bar;
else //

baz;

if (foo)

//

bar;
else

//

baz;


if (foo)

//

//

bar;
else

//

//

baz;

if (foo)

//

//

bar; //
else

//

//

baz; //

if (foo) //

//

//

bar; //
else //

//

//

baz; //
}
}

---

class Main {
static function main() {
if (foo)
bar;
if (foo) //
bar;

if (foo)
bar;
else
baz;
if (foo) //
bar;
else //
baz;

if (foo)
//
bar;
else
//
baz;

if (foo)
//
//
bar;
else
//
//
baz;

if (foo)
//
//
bar; //
else
//
//
baz; //

if (foo) //
//
//
bar; //
else //
//
//
baz; //
}
}
Loading

0 comments on commit c40698d

Please sign in to comment.