Skip to content

Commit

Permalink
changed line comment empty line options (#389)
Browse files Browse the repository at this point in the history
* changed line comment empty line options
* prepare version 1.5.0
  • Loading branch information
AlexHaxe authored Mar 4, 2019
1 parent 1419ee6 commit 5cc6aad
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 28 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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

## version 1.5.0 (2019-03-04)

- 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))
- Added `emptyLines.lineCommentsBetweenTypes` and `emptyLines.lineCommentsBetweenTypes` to separate line comments from types and functions ([#387](https://github.com/HaxeCheckstyle/haxe-formatter/issues/387) + [#389](https://github.com/HaxeCheckstyle/haxe-formatter/issues/389))
- Added `whitespace.addLineCommentSpace` to ensure a space after `//` ([#388](https://github.com/HaxeCheckstyle/haxe-formatter/issues/388))
- 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))
Expand Down
2 changes: 1 addition & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function haxeOptions() {
hxml: "buildAll.hxml"
},
test: {
hxml: "buildTest.hxml"
hxml: "test.hxml"
}
};
}
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"style"
],
"description": "A code formatter for Haxe",
"version": "1.4.0",
"releasenote": "Added wrapping options and support for file header comments - see CHANGELOG for details.",
"version": "1.5.0",
"releasenote": "Added wrapping for +/- chains and metadata parameter, added empty line options for macro classes, etc. - see CHANGELOG for details.",
"contributors": [
"AlexHaxe",
"Gama11"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haxe-formatter",
"version": "1.4.0",
"version": "1.5.0",
"description": "A code formatter for Haxe",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions resources/default-hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"betweenVars": 0,
"endType": 0
},
"lineCommentsBetweenFunctions": 1,
"lineCommentsBetweenTypes": 1,
"lineCommentsBetweenFunctions": "keep",
"lineCommentsBetweenTypes": "keep",
"macroClassEmptyLines": {
"afterPrivateFunctions": 1,
"afterPrivateVars": 1,
Expand Down
14 changes: 12 additions & 2 deletions resources/formatter-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,12 @@
},
"lineCommentsBetweenTypes": {
"description": "empty lines for line comments between types",
"type": "integer",
"type": "string",
"enum": [
"keep",
"one",
"none"
],
"propertyOrder": 6
},
"abstractEmptyLines": {
Expand Down Expand Up @@ -592,7 +597,12 @@
},
"lineCommentsBetweenFunctions": {
"description": "empty lines for line comments between functions",
"type": "integer",
"type": "string",
"enum": [
"keep",
"one",
"none"
],
"propertyOrder": 7
}
},
Expand Down
10 changes: 8 additions & 2 deletions src/formatter/config/EmptyLinesConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ typedef EmptyLinesConfig = {
/**
empty lines for line comments between types
**/
@:default(1) @:optional var lineCommentsBetweenTypes:Int;
@:default(Keep) @:optional var lineCommentsBetweenTypes:LineCommentEmptyLinePolicy;

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

/**
empty lines between two single line types
Expand Down Expand Up @@ -210,3 +210,9 @@ typedef ImportsEmptyLinesConfig = {
var FifthLevelPackage = "fifthLevelPackage";
var FullPackage = "fullPackage";
}

@:enum abstract LineCommentEmptyLinePolicy(String) {
var Keep = "keep";
var One = "one";
var None = "none";
}
20 changes: 14 additions & 6 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class MarkEmptyLines extends MarkerBase {

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

function markLineCommentsBefore(token:TokenTree, count:Int) {
if (count <= 0) {
function markLineCommentsBefore(token:TokenTree, policy:LineCommentEmptyLinePolicy) {
if (policy == None) {
return;
}
if (token.previousSibling == null) {
Expand All @@ -443,7 +443,15 @@ class MarkEmptyLines extends MarkerBase {
case CommentLine(_):
var prevInfo:Null<TokenInfo> = getPreviousToken(prev);
if ((prevInfo == null) || (prevInfo.whitespaceAfter == Newline)) {
emptyLinesAfter(prev, count);
switch (policy) {
case Keep:
if (parsedCode.linesBetweenOriginal(prev, token) > 1) {
emptyLinesAfter(prev, 1);
}
case One:
emptyLinesAfter(prev, 1);
case None:
}
}
return;
default:
Expand Down Expand Up @@ -712,7 +720,7 @@ class MarkEmptyLines extends MarkerBase {
for (type in types) {
var newTypeInfo:TypeEmptyLinesInfo = getTypeInfo(type);
markLineCommentsBefore(type, config.emptyLines.lineCommentsBetweenTypes);
markLineCommentsAfter(type, config.emptyLines.lineCommentsBetweenTypes);
markLineCommentsAfter(type, 1);
if (prevTypeInfo == null) {
prevTypeInfo = newTypeInfo;
continue;
Expand All @@ -731,7 +739,7 @@ class MarkEmptyLines extends MarkerBase {
emptyLines = config.emptyLines.betweenSingleLineTypes;
}
emptyLinesAfterSubTree(prevTypeInfo.lastToken, emptyLines);
markLineCommentsAfter(prevTypeInfo.typeToken, config.emptyLines.lineCommentsBetweenTypes);
markLineCommentsAfter(prevTypeInfo.typeToken, 1);
prevTypeInfo = newTypeInfo;
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/testcases/emptylines/line_comments_between_function.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,46 @@ class Main {
// }
}

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() {
// }
}

class Main {
var foo;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"emptyLines" : {
"lineCommentsBetweenFunctions" : 0
"lineCommentsBetweenFunctions" : "none"
}
}

Expand Down Expand Up @@ -39,6 +39,7 @@ class Main {

**/
static function main() {}

// static function main() {
// }
}
3 changes: 0 additions & 3 deletions test/testcases/emptylines/line_comments_between_types.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ typedef Main = String;
---

// class Main {

class Main {}

// class Main {

typedef Main = String;

// class Main {
Expand All @@ -34,7 +32,6 @@ class Main {}

// class Main {
// }

typedef Main = String;

// class Main {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"emptyLines" : {
"lineCommentsBetweenTypes" : 0
"lineCommentsBetweenTypes" : "none"
}
}

Expand All @@ -21,8 +21,10 @@ typedef Main = String;
// class Main {
// }
class Main {}

// class Main {
// }
typedef Main = String;

// class Main {
// }
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ typedef Main = String;
---

//class Main {

class Main {}

//class Main {

typedef Main = String;

//class Main {
Expand All @@ -37,7 +35,6 @@ class Main {}

//class Main {
//}

typedef Main = String;

//class Main {
Expand Down
28 changes: 27 additions & 1 deletion test/testcases/other/mixed_samples_2.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class UInt {
// space in { }
public function new() { }

// keep empty lines above
// space in { }

public function new() { }

// leading/trailing space in anon type
public function peer():{ host:Host, port:Int } {
var info = socket.peer();
Expand All @@ -68,8 +73,19 @@ class UInt {
sock.close()
catch(e:Dynamic) { };
}


// space in catch { }

public function request() {
try
sock.close()
catch(e:Dynamic) { };
}
}



---

class UInt {
Expand All @@ -90,13 +106,16 @@ class UInt {
var children:Array<Xml>;
var attributeMap:Map<String, String>;

// keep empty lines above
// space in { }
public function new() { }

// keep empty lines above
// space in { }

public function new() { }

// leading/trailing space in anon type

public function peer():{ host:Host, port:Int } {
var info = socket.peer();
var host:Host = Type.createEmptyInstance(Host);
Expand All @@ -107,6 +126,13 @@ class UInt {
return { host: host, port: info.port };
}

// space in catch { }
public function request() {
try
sock.close()
catch (e:Dynamic) { };
}

// space in catch { }

public function request() {
Expand Down
Loading

0 comments on commit 5cc6aad

Please sign in to comment.