From 5cc6aad84ec0e32d109d6f3a4b1adc934b34c6f1 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Mon, 4 Mar 2019 18:05:43 +0100 Subject: [PATCH] changed line comment empty line options (#389) * changed line comment empty line options * prepare version 1.5.0 --- CHANGELOG.md | 4 +- gruntfile.js | 2 +- haxelib.json | 4 +- package.json | 2 +- resources/default-hxformat.json | 4 +- resources/formatter-schema.json | 14 +- src/formatter/config/EmptyLinesConfig.hx | 10 +- src/formatter/marker/MarkEmptyLines.hx | 20 ++- .../line_comments_between_function.hxtest | 38 +++++ ...ments_between_function_no_emptyline.hxtest | 3 +- .../line_comments_between_types.hxtest | 3 - ...comments_between_types_no_emptyline.hxtest | 4 +- ...ine_comments_between_types_no_space.hxtest | 3 - test/testcases/other/mixed_samples_2.hxtest | 28 +++- test/testcases/other/mixed_samples_3.hxtest | 29 +++- test/testcases/other/mixed_samples_4.hxtest | 146 ++++++++++++++++++ 16 files changed, 286 insertions(+), 28 deletions(-) create mode 100644 test/testcases/other/mixed_samples_4.hxtest diff --git a/CHANGELOG.md b/CHANGELOG.md index 205c1e80..7da18f6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/gruntfile.js b/gruntfile.js index d4af4d81..2f75e33e 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -40,7 +40,7 @@ function haxeOptions() { hxml: "buildAll.hxml" }, test: { - hxml: "buildTest.hxml" + hxml: "test.hxml" } }; } diff --git a/haxelib.json b/haxelib.json index f6ac9ce4..b02991af 100644 --- a/haxelib.json +++ b/haxelib.json @@ -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" diff --git a/package.json b/package.json index e63ef46d..e32cf439 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "haxe-formatter", - "version": "1.4.0", + "version": "1.5.0", "description": "A code formatter for Haxe", "repository": { "type": "git", diff --git a/resources/default-hxformat.json b/resources/default-hxformat.json index 6f107fed..0e97f592 100644 --- a/resources/default-hxformat.json +++ b/resources/default-hxformat.json @@ -80,8 +80,8 @@ "betweenVars": 0, "endType": 0 }, - "lineCommentsBetweenFunctions": 1, - "lineCommentsBetweenTypes": 1, + "lineCommentsBetweenFunctions": "keep", + "lineCommentsBetweenTypes": "keep", "macroClassEmptyLines": { "afterPrivateFunctions": 1, "afterPrivateVars": 1, diff --git a/resources/formatter-schema.json b/resources/formatter-schema.json index 09f30904..66d30390 100644 --- a/resources/formatter-schema.json +++ b/resources/formatter-schema.json @@ -555,7 +555,12 @@ }, "lineCommentsBetweenTypes": { "description": "empty lines for line comments between types", - "type": "integer", + "type": "string", + "enum": [ + "keep", + "one", + "none" + ], "propertyOrder": 6 }, "abstractEmptyLines": { @@ -592,7 +597,12 @@ }, "lineCommentsBetweenFunctions": { "description": "empty lines for line comments between functions", - "type": "integer", + "type": "string", + "enum": [ + "keep", + "one", + "none" + ], "propertyOrder": 7 } }, diff --git a/src/formatter/config/EmptyLinesConfig.hx b/src/formatter/config/EmptyLinesConfig.hx index 15ec2a48..9070074c 100644 --- a/src/formatter/config/EmptyLinesConfig.hx +++ b/src/formatter/config/EmptyLinesConfig.hx @@ -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 @@ -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"; +} diff --git a/src/formatter/marker/MarkEmptyLines.hx b/src/formatter/marker/MarkEmptyLines.hx index 3f786a39..1d27f083 100644 --- a/src/formatter/marker/MarkEmptyLines.hx +++ b/src/formatter/marker/MarkEmptyLines.hx @@ -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) { @@ -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) { @@ -443,7 +443,15 @@ class MarkEmptyLines extends MarkerBase { case CommentLine(_): var prevInfo:Null = 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: @@ -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; @@ -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; } } diff --git a/test/testcases/emptylines/line_comments_between_function.hxtest b/test/testcases/emptylines/line_comments_between_function.hxtest index d2d8fa13..a95764ff 100644 --- a/test/testcases/emptylines/line_comments_between_function.hxtest +++ b/test/testcases/emptylines/line_comments_between_function.hxtest @@ -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; diff --git a/test/testcases/emptylines/line_comments_between_function_no_emptyline.hxtest b/test/testcases/emptylines/line_comments_between_function_no_emptyline.hxtest index 87cfbcfb..ccc00b55 100644 --- a/test/testcases/emptylines/line_comments_between_function_no_emptyline.hxtest +++ b/test/testcases/emptylines/line_comments_between_function_no_emptyline.hxtest @@ -1,6 +1,6 @@ { "emptyLines" : { - "lineCommentsBetweenFunctions" : 0 + "lineCommentsBetweenFunctions" : "none" } } @@ -39,6 +39,7 @@ class Main { **/ static function main() {} + // static function main() { // } } diff --git a/test/testcases/emptylines/line_comments_between_types.hxtest b/test/testcases/emptylines/line_comments_between_types.hxtest index c9331d5e..184f0534 100644 --- a/test/testcases/emptylines/line_comments_between_types.hxtest +++ b/test/testcases/emptylines/line_comments_between_types.hxtest @@ -21,11 +21,9 @@ typedef Main = String; --- // class Main { - class Main {} // class Main { - typedef Main = String; // class Main { @@ -34,7 +32,6 @@ class Main {} // class Main { // } - typedef Main = String; // class Main { diff --git a/test/testcases/emptylines/line_comments_between_types_no_emptyline.hxtest b/test/testcases/emptylines/line_comments_between_types_no_emptyline.hxtest index dd0ca55b..5b68e3cb 100644 --- a/test/testcases/emptylines/line_comments_between_types_no_emptyline.hxtest +++ b/test/testcases/emptylines/line_comments_between_types_no_emptyline.hxtest @@ -1,6 +1,6 @@ { "emptyLines" : { - "lineCommentsBetweenTypes" : 0 + "lineCommentsBetweenTypes" : "none" } } @@ -21,8 +21,10 @@ typedef Main = String; // class Main { // } class Main {} + // class Main { // } typedef Main = String; + // class Main { // } diff --git a/test/testcases/emptylines/line_comments_between_types_no_space.hxtest b/test/testcases/emptylines/line_comments_between_types_no_space.hxtest index 5fbdf3ec..cbfa67c0 100644 --- a/test/testcases/emptylines/line_comments_between_types_no_space.hxtest +++ b/test/testcases/emptylines/line_comments_between_types_no_space.hxtest @@ -24,11 +24,9 @@ typedef Main = String; --- //class Main { - class Main {} //class Main { - typedef Main = String; //class Main { @@ -37,7 +35,6 @@ class Main {} //class Main { //} - typedef Main = String; //class Main { diff --git a/test/testcases/other/mixed_samples_2.hxtest b/test/testcases/other/mixed_samples_2.hxtest index a092b002..d7a183e1 100644 --- a/test/testcases/other/mixed_samples_2.hxtest +++ b/test/testcases/other/mixed_samples_2.hxtest @@ -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(); @@ -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 { @@ -90,13 +106,16 @@ class UInt { var children:Array; var attributeMap:Map; + // 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); @@ -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() { diff --git a/test/testcases/other/mixed_samples_3.hxtest b/test/testcases/other/mixed_samples_3.hxtest index 7ea05959..661a23cf 100644 --- a/test/testcases/other/mixed_samples_3.hxtest +++ b/test/testcases/other/mixed_samples_3.hxtest @@ -2,8 +2,8 @@ "emptyLines": { "maxAnywhereInFile": 2, "afterLeftCurly": "keep", - "lineCommentsBetweenFunctions" : 0, - "lineCommentsBetweenTypes" : 0 + "lineCommentsBetweenFunctions" : "none", + "lineCommentsBetweenTypes" : "none" }, "indentation": { "character": " " @@ -53,6 +53,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(); @@ -70,6 +75,15 @@ class UInt { sock.close() catch(e:Dynamic) { }; } + + + // space in catch { } + + public function request() { + try + sock.close() + catch(e:Dynamic) { }; + } } --- @@ -96,6 +110,10 @@ 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(); @@ -113,4 +131,11 @@ class UInt { sock.close() catch (e:Dynamic) { }; } + + // space in catch { } + public function request() { + try + sock.close() + catch (e:Dynamic) { }; + } } diff --git a/test/testcases/other/mixed_samples_4.hxtest b/test/testcases/other/mixed_samples_4.hxtest new file mode 100644 index 00000000..a72bb56a --- /dev/null +++ b/test/testcases/other/mixed_samples_4.hxtest @@ -0,0 +1,146 @@ +{ + "emptyLines": { + "maxAnywhereInFile": 2, + "afterLeftCurly": "keep", + "lineCommentsBetweenFunctions" : "one", + "lineCommentsBetweenTypes" : "one" + }, + "indentation": { + "character": " " + }, + "whitespace" : { + "bracesConfig": { + "blockBraces": { + "openingPolicy": "around", + "closingPolicy": "around", + "removeInnerWhenEmpty": false + }, + "anonTypeBraces": { + "openingPolicy": "around", + "closingPolicy": "around", + "removeInnerWhenEmpty": false + }, + "objectLiteralBraces": { + "openingPolicy": "around", + "closingPolicy": "around", + "removeInnerWhenEmpty": false + } + }, + "classEmptyLines": { + "beginType": 1 + } + } +} + +--- + +class UInt { + + /** empty line at type beginning & keep grouped functions/vars declarations */ + @:commutative @:op(A + B) private static function addI(lhs:UInt, rhs:Int):UInt; + @:commutative @:op(A + B) private static function addF(lhs:UInt, rhs:Float):Float; + @:op(A + B) private static function add(lhs:UInt, rhs:UInt):UInt; + + @:commutative @:op(A * B) private static function mulI(lhs:UInt, rhs:Int):UInt; + @:commutative @:op(A * B) private static function mulF(lhs:UInt, rhs:Float):Float; + @:op(A * B) private static function mul(lhs:UInt, rhs:UInt):UInt; + + var children:Array; + var attributeMap:Map; + + + // 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); + host.init(info.ip); + + + // keep empty lines above + return { host: host, port: info.port }; + } + + // space in catch { } + public function request() { + try + sock.close() + catch(e:Dynamic) { }; + } + + + // space in catch { } + + public function request() { + try + sock.close() + catch(e:Dynamic) { }; + } +} + +--- + +class UInt { + + /** empty line at type beginning & keep grouped functions/vars declarations */ + @:commutative @:op(A + B) private static function addI(lhs:UInt, rhs:Int):UInt; + + @:commutative @:op(A + B) private static function addF(lhs:UInt, rhs:Float):Float; + + @:op(A + B) private static function add(lhs:UInt, rhs:UInt):UInt; + + @:commutative @:op(A * B) private static function mulI(lhs:UInt, rhs:Int):UInt; + + @:commutative @:op(A * B) private static function mulF(lhs:UInt, rhs:Float):Float; + + @:op(A * B) private static function mul(lhs:UInt, rhs:UInt):UInt; + + var children:Array; + var attributeMap:Map; + + // 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); + host.init(info.ip); + + + // keep empty lines above + return { host: host, port: info.port }; + } + + // space in catch { } + + public function request() { + try + sock.close() + catch (e:Dynamic) { }; + } + + // space in catch { } + + public function request() { + try + sock.close() + catch (e:Dynamic) { }; + } +}