Skip to content

Commit

Permalink
added emptyLines.*.existingBetweenFields, fixes #455 (#484)
Browse files Browse the repository at this point in the history
* added emptyLines.*.existingBetweenFields, fixes #455
  • Loading branch information
AlexHaxe authored Jun 3, 2019
1 parent c0be91d commit 471de7c
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## dev branch / next version (1.x.x)

- Added `sameLine.returnBodySingleLine`, fixes [#303](https://github.com/HaxeCheckstyle/haxe-formatter/issues/303) ([#483](https://github.com/HaxeCheckstyle/haxe-formatter/issues/483))
- Added `emptyLines.*.existingBetweenFields`, fixes [#455](https://github.com/HaxeCheckstyle/haxe-formatter/issues/455) ([#484](https://github.com/HaxeCheckstyle/haxe-formatter/issues/484))
- Fixed same line handling of if-else with try catch body, fixes [#360](https://github.com/HaxeCheckstyle/haxe-formatter/issues/360) ([#483](https://github.com/HaxeCheckstyle/haxe-formatter/issues/483))
- Changed `sameLine.returnBody` to only apply to multiline or loop/switch/try/if expressions, fixes [#303](https://github.com/HaxeCheckstyle/haxe-formatter/issues/303) ([#483](https://github.com/HaxeCheckstyle/haxe-formatter/issues/483))

Expand Down
24 changes: 16 additions & 8 deletions resources/default-hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"betweenStaticFunctions": 1,
"betweenStaticVars": 0,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"afterBlocks": "remove",
"afterFieldsWithDocComments": "one",
Expand All @@ -38,7 +39,8 @@
"betweenStaticFunctions": 1,
"betweenStaticVars": 0,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"conditionalsEmptyLines": {
"afterElse": 0,
Expand All @@ -53,19 +55,22 @@
"beginType": 0,
"betweenFunctions": 1,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"enumEmptyLines": {
"beginType": 0,
"betweenFields": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"externClassEmptyLines": {
"afterVars": 0,
"beginType": 0,
"betweenFunctions": 0,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"finalNewline": true,
"importAndUsing": {
Expand All @@ -79,7 +84,8 @@
"beginType": 0,
"betweenFunctions": 0,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"lineCommentsBetweenFunctions": "keep",
"lineCommentsBetweenTypes": "keep",
Expand All @@ -94,13 +100,15 @@
"betweenStaticFunctions": 1,
"betweenStaticVars": 0,
"betweenVars": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
},
"maxAnywhereInFile": 1,
"typedefEmptyLines": {
"beginType": 0,
"betweenFields": 0,
"endType": 0
"endType": 0,
"existingBetweenFields": "keep"
}
},
"excludes": [
Expand Down
16 changes: 16 additions & 0 deletions resources/hxformat-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@
"type": "integer",
"default": 0
},
"existingBetweenFields": {
"$ref": "#/definitions/formatter.config.KeepEmptyLinesPolicy",
"default": "keep"
},
"endType": {
"description": "add empty lines before closing \"}\" of type",
"type": "integer",
Expand Down Expand Up @@ -437,6 +441,10 @@
"type": "integer",
"default": 1
},
"existingBetweenFields": {
"$ref": "#/definitions/formatter.config.KeepEmptyLinesPolicy",
"default": "keep"
},
"endType": {
"description": "add empty lines before closing \"}\" of type",
"type": "integer",
Expand Down Expand Up @@ -916,6 +924,10 @@
"type": "integer",
"default": 1
},
"existingBetweenFields": {
"$ref": "#/definitions/formatter.config.KeepEmptyLinesPolicy",
"default": "keep"
},
"endType": {
"description": "add empty lines before closing \"}\" of type",
"type": "integer",
Expand Down Expand Up @@ -1136,6 +1148,10 @@
"type": "integer",
"default": 0
},
"existingBetweenFields": {
"$ref": "#/definitions/formatter.config.KeepEmptyLinesPolicy",
"default": "keep"
},
"endType": {
"description": "add empty lines before closing \"}\" of type",
"type": "integer",
Expand Down
4 changes: 4 additions & 0 deletions src/formatter/config/EmptyLinesConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ typedef ClassFieldsEmptyLinesConfig = {
@:default(1) @:optional var betweenStaticFunctions:Int;
@:default(1) @:optional var afterPrivateFunctions:Int;
@:default(1) @:optional var betweenFunctions:Int;
@:default(Keep) @:optional var existingBetweenFields:KeepEmptyLinesPolicy;
}

typedef EnumAbstractFieldsEmptyLinesConfig = {
Expand All @@ -143,6 +144,7 @@ typedef EnumAbstractFieldsEmptyLinesConfig = {
@:default(0) @:optional var betweenVars:Int;
@:default(1) @:optional var afterVars:Int;
@:default(1) @:optional var betweenFunctions:Int;
@:default(Keep) @:optional var existingBetweenFields:KeepEmptyLinesPolicy;
}

typedef InterfaceFieldsEmptyLinesConfig = {
Expand All @@ -159,6 +161,7 @@ typedef InterfaceFieldsEmptyLinesConfig = {
@:default(0) @:optional var betweenVars:Int;
@:default(0) @:optional var afterVars:Int;
@:default(0) @:optional var betweenFunctions:Int;
@:default(Keep) @:optional var existingBetweenFields:KeepEmptyLinesPolicy;
}

typedef TypedefFieldsEmptyLinesConfig = {
Expand All @@ -173,6 +176,7 @@ typedef TypedefFieldsEmptyLinesConfig = {
@:default(0) @:optional var endType:Int;

@:default(0) @:optional var betweenFields:Int;
@:default(Keep) @:optional var existingBetweenFields:KeepEmptyLinesPolicy;
}

@:enum
Expand Down
37 changes: 37 additions & 0 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ class MarkEmptyLines extends MarkerBase {
if (prevToken == null) {
return;
}
if (conf.existingBetweenFields == Keep) {
if (hasEmptyLinesBetweenFields(prevToken, currToken)) {
emptyLinesAfterSubTree(prevToken, 1);
return;
}
}
if (prevVar != currVar) {
// transition between vars and functions
emptyLinesAfterSubTree(prevToken, conf.afterVars);
Expand Down Expand Up @@ -429,6 +435,25 @@ class MarkEmptyLines extends MarkerBase {
}
}

function hasEmptyLinesBetweenFields(prevToken:TokenTree, currToken:TokenTree):Bool {
var lastToken:TokenTree = TokenTreeCheckUtils.getLastToken(prevToken);
if (lastToken == null) {
return false;
}
var prevLine:Int = parsedCode.getLinePos(lastToken.pos.max).line;
var currLine:Int = parsedCode.getLinePos(currToken.pos.min).line;
for (emptyLine in parsedCode.emptyLines) {
if (prevLine >= emptyLine) {
continue;
}
if (currLine > emptyLine) {
return true;
}
return false;
}
return false;
}

function markLineCommentsBefore(token:TokenTree, policy:LineCommentEmptyLinePolicy) {
if (policy == None) {
return;
Expand Down Expand Up @@ -539,6 +564,12 @@ class MarkEmptyLines extends MarkerBase {
if (prevToken == null) {
return;
}
if (conf.existingBetweenFields == Keep) {
if (hasEmptyLinesBetweenFields(prevToken, currToken)) {
emptyLinesAfterSubTree(prevToken, 1);
return;
}
}
if (prevVar != currVar) {
// transition between vars and functions
emptyLinesAfterSubTree(prevToken, conf.afterVars);
Expand Down Expand Up @@ -658,6 +689,12 @@ class MarkEmptyLines extends MarkerBase {
prevToken = child;
continue;
}
if (config.existingBetweenFields == Keep) {
if (hasEmptyLinesBetweenFields(prevToken, child)) {
emptyLinesAfterSubTree(prevToken, 1);
return;
}
}
emptyLinesAfterSubTree(prevToken, config.betweenFields);
prevToken = child;
}
Expand Down
3 changes: 2 additions & 1 deletion test/testcases/emptylines/enum_before_fields.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"enumEmptyLines": {
"beginType": 1,
"endType": 1,
"betweenFields": 1
"betweenFields": 1,
"existingBetweenFields": "remove"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"emptyLines": {
"afterFieldsWithDocComments": "ignore"
"afterFieldsWithDocComments": "ignore",
"externClassEmptyLines": {
"existingBetweenFields": "remove"
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"emptyLines": {
"beforeDocCommentEmptyLines": "ignore",
"afterFieldsWithDocComments": "ignore"
"afterFieldsWithDocComments": "ignore",
"classEmptyLines": {
"existingBetweenFields": "remove"
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"emptyLines": {
"classEmptyLines": {
"betweenFunctions": 0
}
}
}

---

class Main {
var foo:Int;
var foo:Int;

var foo:Int;
static var foo:Int;
static var foo:Int;

static var foo:Int;
function main (){}
function main (){}

function main (){}
}


interface Main {
var foo:Int;
var foo:Int;

var foo:Int;
static var foo:Int;
static var foo:Int;

static var foo:Int;
function main ();
function main ();

function main ();
}

---

class Main {
var foo:Int;
var foo:Int;

var foo:Int;

static var foo:Int;
static var foo:Int;

static var foo:Int;

function main() {}
function main() {}

function main() {}
}

interface Main {
var foo:Int;
var foo:Int;

var foo:Int;
static var foo:Int;
static var foo:Int;

static var foo:Int;
function main();
function main();

function main();
}
Loading

0 comments on commit 471de7c

Please sign in to comment.