From 1419ee67c46b5f29eae55c7130f29f099ca92626 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Sun, 3 Mar 2019 18:45:09 +0100 Subject: [PATCH] added whitespace.addLineCommentSpace to ensure whitespace after // (#388) * added whitespace.addLineCommentSpace to ensure whitespace after // --- CHANGELOG.md | 1 + resources/default-hxformat.json | 1 + resources/formatter-schema.json | 5 +++ src/formatter/config/WhitespaceConfig.hx | 5 +++ src/formatter/marker/MarkTokenText.hx | 5 ++- .../line_comments_between_types.hxtest | 14 +++--- ...ine_comments_between_types_no_space.hxtest | 44 +++++++++++++++++++ .../single_line_comments_no_space.hxtest | 27 ++++++++++++ 8 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 test/testcases/emptylines/line_comments_between_types_no_space.hxtest create mode 100644 test/testcases/whitespace/single_line_comments_no_space.hxtest diff --git a/CHANGELOG.md b/CHANGELOG.md index cd53ddff..205c1e80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 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 `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)) - Fixed line comments after typedefs, fixes [#331](https://github.com/HaxeCheckstyle/haxe-formatter/issues/331) ([#349](https://github.com/HaxeCheckstyle/haxe-formatter/issues/349)) diff --git a/resources/default-hxformat.json b/resources/default-hxformat.json index 783241aa..6f107fed 100644 --- a/resources/default-hxformat.json +++ b/resources/default-hxformat.json @@ -146,6 +146,7 @@ "whileBody": "next" }, "whitespace": { + "addLineCommentSpace": true, "arrowFunctionsPolicy": "around", "binopPolicy": "around", "bracesConfig": { diff --git a/resources/formatter-schema.json b/resources/formatter-schema.json index dcc091ca..09f30904 100644 --- a/resources/formatter-schema.json +++ b/resources/formatter-schema.json @@ -1131,6 +1131,11 @@ ], "propertyOrder": 12 }, + "addLineCommentSpace": { + "description": "ensure a space after '//'", + "type": "boolean", + "propertyOrder": 30 + }, "catchPolicy": { "type": "string", "enum": [ diff --git a/src/formatter/config/WhitespaceConfig.hx b/src/formatter/config/WhitespaceConfig.hx index 39b3e220..280142cd 100644 --- a/src/formatter/config/WhitespaceConfig.hx +++ b/src/formatter/config/WhitespaceConfig.hx @@ -63,6 +63,11 @@ typedef WhitespaceConfig = { only applies spaces, no newlines or wrapping **/ @:default(true) @:optional var formatStringInterpolation:Bool; + + /** + ensure a space after '//' + **/ + @:default(true) @:optional var addLineCommentSpace:Bool; } typedef ParenWhitespaceConfig = { diff --git a/src/formatter/marker/MarkTokenText.hx b/src/formatter/marker/MarkTokenText.hx index 398ce7dc..5a9bdb14 100644 --- a/src/formatter/marker/MarkTokenText.hx +++ b/src/formatter/marker/MarkTokenText.hx @@ -255,6 +255,9 @@ class MarkTokenText extends MarkerBase { if (~/^[\/\*\-\s]+/.match(text)) { return "//" + text.rtrim(); } - return "// " + text.trim(); + if (config.whitespace.addLineCommentSpace) { + return "// " + text.trim(); + } + return "//" + text.trim(); } } diff --git a/test/testcases/emptylines/line_comments_between_types.hxtest b/test/testcases/emptylines/line_comments_between_types.hxtest index 03f9b351..c9331d5e 100644 --- a/test/testcases/emptylines/line_comments_between_types.hxtest +++ b/test/testcases/emptylines/line_comments_between_types.hxtest @@ -3,20 +3,20 @@ --- -// class Main { +//class Main { class Main {} -// class Main { +//class Main { typedef Main = String; -// class Main { +//class Main { class Main {} -// class Main { -// } +//class Main { +//} typedef Main = String; -// class Main { -// } +//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 new file mode 100644 index 00000000..5fbdf3ec --- /dev/null +++ b/test/testcases/emptylines/line_comments_between_types_no_space.hxtest @@ -0,0 +1,44 @@ +{ + "whitespace" : { + "addLineCommentSpace": false + } +} + +--- + +//class Main { +class Main {} + +//class Main { +typedef Main = String; +//class Main { + +class Main {} + +//class Main { +//} +typedef Main = String; +//class Main { +//} + +--- + +//class Main { + +class Main {} + +//class Main { + +typedef Main = String; + +//class Main { + +class Main {} + +//class Main { +//} + +typedef Main = String; + +//class Main { +//} diff --git a/test/testcases/whitespace/single_line_comments_no_space.hxtest b/test/testcases/whitespace/single_line_comments_no_space.hxtest new file mode 100644 index 00000000..7ae6593a --- /dev/null +++ b/test/testcases/whitespace/single_line_comments_no_space.hxtest @@ -0,0 +1,27 @@ +{ + "whitespace" : { + "addLineCommentSpace": false + } +} + +--- + +class Main{ +//************************* +// Test +// Test +//Test +/////////////////////////// +static function main(){} +} + +--- + +class Main { + //************************* + // Test + // Test + //Test + /////////////////////////// + static function main() {} +}