From a84c93c43a6f397059e65fa19ece142640f4158f Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Sun, 12 Apr 2020 22:51:12 +0200 Subject: [PATCH] fixed indentation of anon function body, fixes #577 --- CHANGELOG.md | 1 + src/formatter/marker/Indenter.hx | 14 +++++-- src/formatter/marker/MarkTokenText.hx | 5 +-- .../issue_577_anon_function_body.hxtest | 32 +++++++++++++++ ...e_577_anon_function_body_curly_next.hxtest | 39 +++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 test/testcases/indentation/issue_577_anon_function_body.hxtest create mode 100644 test/testcases/indentation/issue_577_anon_function_body_curly_next.hxtest diff --git a/CHANGELOG.md b/CHANGELOG.md index e84c3678..752115f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fixed broken indentation after case with OpOr pattern, fixes [#576](https://github.com/HaxeCheckstyle/haxe-formatter/issues/576) - Fixed missing linebreak between metadata and doc comment, fixes [#578](https://github.com/HaxeCheckstyle/haxe-formatter/issues/578) +- Fixed indentation in anon function body, fixes [#577](https://github.com/HaxeCheckstyle/haxe-formatter/issues/577) - Changed default wrapping location of `casePattern` to `afterLast`, fixes [#579](https://github.com/HaxeCheckstyle/haxe-formatter/issues/579) ## version 1.10.0 (2020-04-11) diff --git a/src/formatter/marker/Indenter.hx b/src/formatter/marker/Indenter.hx index 28d9fec3..35c29192 100644 --- a/src/formatter/marker/Indenter.hx +++ b/src/formatter/marker/Indenter.hx @@ -1,18 +1,18 @@ package formatter.marker; +import formatter.config.Config; +import formatter.config.IndentationConfig; #if debugIndent -import haxe.PosInfos; import sys.io.File; import sys.io.FileOutput; #end -import formatter.config.IndentationConfig; class Indenter { var config:IndentationConfig; var parsedCode:Null; - public function new(config:IndentationConfig) { - this.config = config; + public function new(config:Config) { + this.config = fconfig; if (config.character.toLowerCase() == "tab") { config.character = "\t"; } @@ -311,6 +311,12 @@ class Indenter { while (firstToken != null && !parsedCode.tokenList.isNewLineBefore(firstToken.token)) { firstToken = parsedCode.tokenList.getPreviousToken(firstToken.token); } + var brOpen:Null = prevToken.access().firstOf(BrOpen).token; + if (brOpen != null) { + if (!parsedCode.tokenList.isSameLineBetween(prevToken, brOpen, false)) { + continue; + } + } return count + calcIndent(firstToken.token); } default: diff --git a/src/formatter/marker/MarkTokenText.hx b/src/formatter/marker/MarkTokenText.hx index 634fd339..2d9a959a 100644 --- a/src/formatter/marker/MarkTokenText.hx +++ b/src/formatter/marker/MarkTokenText.hx @@ -1,15 +1,12 @@ package formatter.marker; -import haxe.zip.Entry; import haxeparser.HaxeLexer; +import tokentree.TokenStream; import tokentree.TokenStreamProgress; import tokentree.walk.WalkStatement; -import tokentree.TokenStream; import formatter.codedata.CodeLines; import formatter.codedata.FormatterInputData; -import formatter.codedata.ParseFile; import formatter.codedata.ParsedCode; -import formatter.codedata.TokenData; class MarkTokenText extends MarkerBase { public function run() { diff --git a/test/testcases/indentation/issue_577_anon_function_body.hxtest b/test/testcases/indentation/issue_577_anon_function_body.hxtest new file mode 100644 index 00000000..dd255034 --- /dev/null +++ b/test/testcases/indentation/issue_577_anon_function_body.hxtest @@ -0,0 +1,32 @@ +{} + +--- + +class PlayState extends FlxState +{ + function createAttachmentControls():Void + { + add(attachmentDropDown = new FlxUIDropDownMenu(modelDropDownLoc.x, modelDropDownLoc.y - 30, + FlxUIDropDownMenu.makeStrIdLabelArray(FlxGamepadAttachment.getConstructors()), function(attachment) + { + var gamepad = FlxG.gamepads.lastActive; + if (gamepad != null) + gamepad.attachment = FlxGamepadAttachment.createByName(attachment); + updateConnectedGamepads(true); + }, new FlxUIDropDownHeader(150))); + } +} + +--- + +class PlayState extends FlxState { + function createAttachmentControls():Void { + add(attachmentDropDown = new FlxUIDropDownMenu(modelDropDownLoc.x, modelDropDownLoc.y - 30, + FlxUIDropDownMenu.makeStrIdLabelArray(FlxGamepadAttachment.getConstructors()), function(attachment) { + var gamepad = FlxG.gamepads.lastActive; + if (gamepad != null) + gamepad.attachment = FlxGamepadAttachment.createByName(attachment); + updateConnectedGamepads(true); + }, new FlxUIDropDownHeader(150))); + } +} diff --git a/test/testcases/indentation/issue_577_anon_function_body_curly_next.hxtest b/test/testcases/indentation/issue_577_anon_function_body_curly_next.hxtest new file mode 100644 index 00000000..21a6319c --- /dev/null +++ b/test/testcases/indentation/issue_577_anon_function_body_curly_next.hxtest @@ -0,0 +1,39 @@ +{ + "lineEnds": { + "leftCurly": "both" + } +} + +--- + +class PlayState extends FlxState +{ + function createAttachmentControls():Void + { + add(attachmentDropDown = new FlxUIDropDownMenu(modelDropDownLoc.x, modelDropDownLoc.y - 30, + FlxUIDropDownMenu.makeStrIdLabelArray(FlxGamepadAttachment.getConstructors()), function(attachment) + { + var gamepad = FlxG.gamepads.lastActive; + if (gamepad != null) + gamepad.attachment = FlxGamepadAttachment.createByName(attachment); + updateConnectedGamepads(true); + }, new FlxUIDropDownHeader(150))); + } +} + +--- + +class PlayState extends FlxState +{ + function createAttachmentControls():Void + { + add(attachmentDropDown = new FlxUIDropDownMenu(modelDropDownLoc.x, modelDropDownLoc.y - 30, + FlxUIDropDownMenu.makeStrIdLabelArray(FlxGamepadAttachment.getConstructors()), function(attachment) + { + var gamepad = FlxG.gamepads.lastActive; + if (gamepad != null) + gamepad.attachment = FlxGamepadAttachment.createByName(attachment); + updateConnectedGamepads(true); + }, new FlxUIDropDownHeader(150))); + } +}