Skip to content

Commit

Permalink
added line comment empty line options
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Mar 3, 2019
1 parent 3351e0d commit a18ca40
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added `wrapping.opAddSubChain` ([#370](https://github.com/HaxeCheckstyle/haxe-formatter/issues/370))
- Added `wrapping.metadataCallParameter` ([#370](https://github.com/HaxeCheckstyle/haxe-formatter/issues/370))
- Added `emptyLines.macroClassEmptyLines`, fixes [#377](https://github.com/HaxeCheckstyle/haxe-formatter/issues/377) ([#383](https://github.com/HaxeCheckstyle/haxe-formatter/issues/383))
- Added `emptyLines.lineCommentsBetweenTypes` and `emptyLines.lineCommentsBetweenTypes` to separate line comments from types and functions ([#387](https://github.com/HaxeCheckstyle/haxe-formatter/issues/387))
- Fixed type parameter constraint with structure type, fixes [#337](https://github.com/HaxeCheckstyle/haxe-formatter/issues/337) ([#349](https://github.com/HaxeCheckstyle/haxe-formatter/issues/349))
- Fixed wrapping of OpBool chains with null ([#349](https://github.com/HaxeCheckstyle/haxe-formatter/issues/349))
- Fixed line comments after typedefs, fixes [#331](https://github.com/HaxeCheckstyle/haxe-formatter/issues/331) ([#349](https://github.com/HaxeCheckstyle/haxe-formatter/issues/349))
Expand Down
2 changes: 2 additions & 0 deletions resources/default-hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"betweenVars": 0,
"endType": 0
},
"lineCommentsBetweenFunctions": 1,
"lineCommentsBetweenTypes": 1,
"macroClassEmptyLines": {
"afterPrivateFunctions": 1,
"afterPrivateVars": 1,
Expand Down
46 changes: 28 additions & 18 deletions resources/formatter-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@
"keep",
"remove"
],
"propertyOrder": 7
"propertyOrder": 9
},
"afterLeftCurly": {
"description": "Remove or keep empty lines below \"{\"",
Expand All @@ -467,7 +467,7 @@
"keep",
"remove"
],
"propertyOrder": 8
"propertyOrder": 10
},
"afterReturn": {
"description": "Remove or keep empty lines below \"return\"",
Expand All @@ -476,7 +476,7 @@
"keep",
"remove"
],
"propertyOrder": 9
"propertyOrder": 11
},
"afterPackage": {
"description": "empty lines after package declaration",
Expand All @@ -485,12 +485,12 @@
},
"macroClassEmptyLines": {
"$ref": "#/definitions/ClassFieldsEmptyLinesConfig",
"propertyOrder": 14
"propertyOrder": 16
},
"betweenMultilineComments": {
"description": "Adds empty lines between two consecutive multiline comments",
"type": "integer",
"propertyOrder": 23
"propertyOrder": 25
},
"finalNewline": {
"description": "adds a final newline",
Expand All @@ -508,32 +508,32 @@
"keep",
"remove"
],
"propertyOrder": 10
"propertyOrder": 12
},
"betweenSingleLineTypes": {
"description": "empty lines between two single line types",
"type": "integer",
"propertyOrder": 6
"propertyOrder": 8
},
"conditionalsEmptyLines": {
"$ref": "#/definitions/ConditionalEmptyLinesConfig",
"propertyOrder": 20
"propertyOrder": 22
},
"afterBlocks": {
"type": "string",
"enum": [
"keep",
"remove"
],
"propertyOrder": 11
"propertyOrder": 13
},
"enumEmptyLines": {
"$ref": "#/definitions/TypedefFieldsEmptyLinesConfig",
"propertyOrder": 18
"propertyOrder": 20
},
"externClassEmptyLines": {
"$ref": "#/definitions/InterfaceFieldsEmptyLinesConfig",
"propertyOrder": 15
"propertyOrder": 17
},
"betweenTypes": {
"description": "empty lines between types",
Expand All @@ -547,15 +547,20 @@
},
"typedefEmptyLines": {
"$ref": "#/definitions/TypedefFieldsEmptyLinesConfig",
"propertyOrder": 19
"propertyOrder": 21
},
"enumAbstractEmptyLines": {
"$ref": "#/definitions/EnumAbstractFieldsEmptyLinesConfig",
"propertyOrder": 12
"propertyOrder": 14
},
"lineCommentsBetweenTypes": {
"description": "empty lines for line comments between types",
"type": "integer",
"propertyOrder": 6
},
"abstractEmptyLines": {
"$ref": "#/definitions/ClassFieldsEmptyLinesConfig",
"propertyOrder": 16
"propertyOrder": 18
},
"beforePackage": {
"description": "empty lines before package declaration",
Expand All @@ -564,7 +569,7 @@
},
"classEmptyLines": {
"$ref": "#/definitions/ClassFieldsEmptyLinesConfig",
"propertyOrder": 13
"propertyOrder": 15
},
"beforeDocCommentEmptyLines": {
"description": "\"one\" adds one empty line above doc comments\n\t\t\"none\" removes all empty lines above doc comments\n\t\t\"ignore\" respects empty lines set via \"betweenVars\", \"betweenFunctions\", etc.",
Expand All @@ -574,16 +579,21 @@
"none",
"one"
],
"propertyOrder": 21
"propertyOrder": 23
},
"interfaceEmptyLines": {
"$ref": "#/definitions/InterfaceFieldsEmptyLinesConfig",
"propertyOrder": 17
"propertyOrder": 19
},
"afterFileHeaderComment": {
"description": "Adds empty lines when file starts with a multiline comment",
"type": "integer",
"propertyOrder": 22
"propertyOrder": 24
},
"lineCommentsBetweenFunctions": {
"description": "empty lines for line comments between functions",
"type": "integer",
"propertyOrder": 7
}
},
"type": "object"
Expand Down
1 change: 1 addition & 0 deletions schema/JsonSchemaGenerator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef ExtendedFieldsCB = Array<ObjectDeclField>->String->Position->DynamicAcce
#end

// adapted from https://github.com/nadako/haxe-type-to-json-schema

class JsonSchemaGenerator {
#if (haxe_ver >= 4.0)
static inline var SCHEMA_KEY:String = "$schema";
Expand Down
10 changes: 10 additions & 0 deletions src/formatter/config/EmptyLinesConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ typedef EmptyLinesConfig = {
**/
@:default(1) @:optional var betweenTypes:Int;

/**
empty lines for line comments between types
**/
@:default(1) @:optional var lineCommentsBetweenTypes:Int;

/**
empty lines for line comments between functions
**/
@:default(1) @:optional var lineCommentsBetweenFunctions:Int;

/**
empty lines between two single line types
**/
Expand Down
67 changes: 59 additions & 8 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ class MarkEmptyLines extends MarkerBase {
case UNKNOWN:
return;
}

if (!currVar) {
markLineCommentsBefore(currToken, config.emptyLines.lineCommentsBetweenFunctions);
markLineCommentsAfter(currToken, config.emptyLines.lineCommentsBetweenFunctions);
}
prevToken = skipSharpFields(prevToken);
if (prevToken == null) {
return;
Expand Down Expand Up @@ -424,6 +429,47 @@ class MarkEmptyLines extends MarkerBase {
}
}

function markLineCommentsBefore(token:TokenTree, count:Int) {
if (count <= 0) {
return;
}
if (token.previousSibling == null) {
return;
}
var prev:Null<TokenTree> = token.previousSibling;
while (prev != null) {
switch (prev.tok) {
case Comment(_):
case CommentLine(_):
var prevInfo:Null<TokenInfo> = getPreviousToken(prev);
if ((prevInfo == null) || (prevInfo.whitespaceAfter == Newline)) {
emptyLinesAfter(prev, count);
}
return;
default:
return;
}
prev = prev.previousSibling;
}
}

function markLineCommentsAfter(token:TokenTree, count:Int) {
if (count <= 0) {
return;
}
if (token.nextSibling == null) {
return;
}
var next:Null<TokenTree> = token.nextSibling;
switch (next.tok) {
case CommentLine(_):
if (isNewLineBefore(next)) {
emptyLinesBefore(next, count);
}
default:
}
}

function markExternClass(c:TokenTree, conf:InterfaceFieldsEmptyLinesConfig) {
var block:Null<TokenTree> = c.access().firstChild().firstOf(BrOpen).token;
if (block == null) {
Expand Down Expand Up @@ -665,11 +711,13 @@ class MarkEmptyLines extends MarkerBase {
var prevTypeInfo:Null<TypeEmptyLinesInfo> = null;
for (type in types) {
var newTypeInfo:TypeEmptyLinesInfo = getTypeInfo(type);
markLineCommentsBefore(type, config.emptyLines.lineCommentsBetweenTypes);
markLineCommentsAfter(type, config.emptyLines.lineCommentsBetweenTypes);
if (prevTypeInfo == null) {
prevTypeInfo = newTypeInfo;
continue;
}
var next:Null<TokenInfo> = getNextToken(prevTypeInfo.token);
var next:Null<TokenInfo> = getNextToken(prevTypeInfo.lastToken);
if (next != null) {
switch (next.token.tok) {
case Sharp(MarkLineEnds.SHARP_ELSE), Sharp(MarkLineEnds.SHARP_ELSE_IF):
Expand All @@ -682,33 +730,35 @@ class MarkEmptyLines extends MarkerBase {
if (prevTypeInfo.oneLine && newTypeInfo.oneLine) {
emptyLines = config.emptyLines.betweenSingleLineTypes;
}
emptyLinesAfterSubTree(prevTypeInfo.token, emptyLines);
emptyLinesAfterSubTree(prevTypeInfo.lastToken, emptyLines);
markLineCommentsAfter(prevTypeInfo.typeToken, config.emptyLines.lineCommentsBetweenTypes);
prevTypeInfo = newTypeInfo;
}
}

function getTypeInfo(token:TokenTree):TypeEmptyLinesInfo {
var info:TypeEmptyLinesInfo = {
token: TokenTreeCheckUtils.getLastToken(token),
lastToken: TokenTreeCheckUtils.getLastToken(token),
typeToken: token,
oneLine: false
};
if (isSameLine(token, info.token)) {
if (isSameLine(token, info.lastToken)) {
info.oneLine = true;
}
var atToken:Null<TokenTree> = token.access().firstChild().isCIdent().firstOf(At).token;
if (atToken != null) {
if (!isSameLine(atToken, info.token)) {
if (!isSameLine(atToken, info.lastToken)) {
info.oneLine = false;
}
}
while (true) {
var next:Null<TokenInfo> = getNextToken(info.token);
var next:Null<TokenInfo> = getNextToken(info.lastToken);
if (next == null) {
break;
}
switch (next.token.tok) {
case Sharp(MarkLineEnds.SHARP_END):
info.token = next.token;
info.lastToken = next.token;
default:
break;
}
Expand Down Expand Up @@ -994,6 +1044,7 @@ typedef ImportPackageInfo = {
}

typedef TypeEmptyLinesInfo = {
var token:TokenTree;
var lastToken:TokenTree;
var typeToken:TokenTree;
var oneLine:Bool;
}
43 changes: 43 additions & 0 deletions test/testcases/emptylines/line_comments_between_function.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
}

---

class Main {
var foo;
// static function main() {
// }
static function main() {
}
// static function main() {
// }
/**

**/
static function main() {
}
// static function main() {
// }
}

---

class Main {
var foo;

// static function main() {
// }

static function main() {}

// static function main() {
// }

/**

**/
static function main() {}

// static function main() {
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"emptyLines" : {
"lineCommentsBetweenFunctions" : 0
}
}

---

class Main {
var foo;
// static function main() {
// }
static function main() {
}
// static function main() {
// }
/**

**/
static function main() {
}
// static function main() {
// }
}

---

class Main {
var foo;

// static function main() {
// }
static function main() {}

// static function main() {
// }

/**

**/
static function main() {}
// static function main() {
// }
}
Loading

0 comments on commit a18ca40

Please sign in to comment.