From 68b6b5071748e56060142d9879e50e20f82ec6c2 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Tue, 1 Oct 2024 01:20:31 +0200 Subject: [PATCH] fixed conditionalized class declaration with constraints, fixes #431 fixed empty classes with conditional metadata fixed class fields with conditional function signatures fixed abstract enum abstracts with conditionals --- CHANGELOG.md | 4 + src/formatter/marker/MarkEmptyLines.hx | 86 ++++++++++++++++-- src/formatter/marker/MarkLineEnds.hx | 2 +- ...ith_conditional_function_signatures.hxtest | 90 +++++++++++++++++++ ...y_classes_with_conditional_metadata.hxtest | 23 +++++ ...ditionalised_class_with_constraints.hxtest | 41 +++++++++ .../private_abstract_with_conditionals.hxtest | 44 +++++++++ 7 files changed, 282 insertions(+), 8 deletions(-) create mode 100644 test/testcases/emptylines/class_fields_with_conditional_function_signatures.hxtest create mode 100644 test/testcases/emptylines/empty_classes_with_conditional_metadata.hxtest create mode 100644 test/testcases/emptylines/issue_431_conditionalised_class_with_constraints.hxtest create mode 100644 test/testcases/emptylines/private_abstract_with_conditionals.hxtest diff --git a/CHANGELOG.md b/CHANGELOG.md index f19ef60..f922164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - **Breaking Change** Fixed wrap condition type `AnyItemLengthLessThan` - use `AllItemLengthsLessThan` for old behaviour - Fixed modifiers with complex conditionals, fixes [#332](https://github.com/HaxeCheckstyle/haxe-formatter/issues/332) - Fixed unexpected array wrapping behavior, fixes [#340](https://github.com/HaxeCheckstyle/haxe-formatter/issues/340) +- Fixed conditionalized class declaration with constraints, fixes [#431](https://github.com/HaxeCheckstyle/haxe-formatter/issues/431) +- Fixed empty classes with conditional metadata +- Fixed class fields with conditional function signatures +- Fixed abstract enum abstracts with conditionals ## version 1.16.1 (2024-09-18) diff --git a/src/formatter/marker/MarkEmptyLines.hx b/src/formatter/marker/MarkEmptyLines.hx index 8a0a8a6..733c31a 100644 --- a/src/formatter/marker/MarkEmptyLines.hx +++ b/src/formatter/marker/MarkEmptyLines.hx @@ -433,6 +433,18 @@ class MarkEmptyLines extends MarkerBase { emptyLinesAfterSubTree(prevToken, conf.afterPrivateFunctions); return; } + switch (prevToken.tok) { + case Sharp(MarkLineEnds.SHARP_END): + var previous:TokenInfo = getPreviousToken(prevToken); + if (previous != null) { + switch (previous.token.tok) { + case Semicolon | BrClose: + default: + return; + } + } + default: + } emptyLinesAfterSubTree(prevToken, conf.betweenFunctions); return; } @@ -520,7 +532,7 @@ class MarkEmptyLines extends MarkerBase { var currToken:Null = null; var currTokenType:Null = null; for (field in fields) { - currToken = field; + currToken = skipSharpFields(field); currTokenType = FieldUtils.getFieldType(field, Public); markInterfaceEmptyLines(prevToken, prevTokenType, currToken, currTokenType, conf); prevToken = currToken; @@ -608,7 +620,7 @@ class MarkEmptyLines extends MarkerBase { var currToken:Null = null; var currTokenType:Null = null; for (func in functions) { - currToken = func; + currToken = skipSharpFields(func); currTokenType = FieldUtils.getFieldType(func, Public); markEnumAbstractFieldEmptyLines(prevToken, prevTokenType, currToken, currTokenType); prevToken = currToken; @@ -642,10 +654,6 @@ class MarkEmptyLines extends MarkerBase { case Unknown: return; } - prevToken = skipSharpFields(prevToken); - if (prevToken == null) { - return; - } if (config.emptyLines.enumAbstractEmptyLines.existingBetweenFields == Keep) { if (hasEmptyLinesBetweenFields(prevToken, currToken)) { emptyLinesAfterSubTree(prevToken, 1); @@ -760,6 +768,18 @@ class MarkEmptyLines extends MarkerBase { } switch (next.tok) { case Sharp(MarkLineEnds.SHARP_END): + var previous:TokenInfo = getPreviousToken(next); + if (previous != null) { + switch (previous.token.tok) { + case Semicolon | BrClose: + return next; + default: + var nextSibling:TokenTree = next.parent?.nextSibling; + if (nextSibling != null) { + return nextSibling; + } + } + } return next; case Sharp(MarkLineEnds.SHARP_IF): return prevToken; @@ -783,6 +803,8 @@ class MarkEmptyLines extends MarkerBase { FoundSkipSubtree; case Kwd(KwdFinal): FoundSkipSubtree; + case BrOpen: + SkipSubtree; default: GoDeeper; } @@ -812,7 +834,21 @@ class MarkEmptyLines extends MarkerBase { if (prevTypeInfo.oneLine && newTypeInfo.oneLine) { emptyLines = config.emptyLines.betweenSingleLineTypes; } - emptyLinesAfterSubTree(prevTypeInfo.lastToken, emptyLines); + var noEmptyLines:Bool = false; + switch (prevTypeInfo.lastToken.tok) { + case Sharp(MarkLineEnds.SHARP_END): + var sharpEnd:TokenInfo = getTokenInfo(prevTypeInfo.lastToken); + if (sharpEnd != null && sharpEnd.whitespaceAfter != Newline) { + noEmptyLines = true; + } + default: + } + if (prevTypeInfo.typeToken.index == prevTypeInfo.lastToken.index) { + noEmptyLines = true; + } + if (!noEmptyLines) { + emptyLinesAfterSubTree(prevTypeInfo.lastToken, emptyLines); + } markLineCommentsAfter(prevTypeInfo.typeToken, 1); prevTypeInfo = newTypeInfo; } @@ -825,7 +861,43 @@ class MarkEmptyLines extends MarkerBase { oneLine: false }; + switch (info.lastToken.tok) { + case Semicolon | BrClose: + default: + var afterLast:TokenInfo = getNextToken(info.lastToken); + if (afterLast != null) { + switch (afterLast.token.tok) { + case Sharp(MarkLineEnds.SHARP_ELSE) | Sharp(MarkLineEnds.SHARP_END) | Sharp(MarkLineEnds.SHARP_ELSE_IF): + var parent:TokenTree = afterLast.token.parent; + var lastChild:TokenTree = parent.getLastChild(); + var newLastToken:TokenTree = TokenTreeCheckUtils.getLastToken(lastChild); + if (newLastToken != null) { + info.lastToken = newLastToken; + } + default: + } + } + } + var start:TokenTree = parsedCode.tokenList.findLowestIndex(token); + if (token.previousSibling != null) { + switch (token.previousSibling.tok) { + case Sharp(MarkLineEnds.SHARP_IF) if (token.previousSibling.hasChildren()): + var allMeta:Bool = true; + for (child in token.previousSibling.children) { + switch (child.tok) { + case Const(_) | At | Sharp(MarkLineEnds.SHARP_END): + default: + allMeta = false; + break; + } + } + if (allMeta) { + start = token.previousSibling; + } + default: + } + } if (isSameLine(start, info.lastToken)) { info.oneLine = true; } diff --git a/src/formatter/marker/MarkLineEnds.hx b/src/formatter/marker/MarkLineEnds.hx index f1ce725..abc6819 100644 --- a/src/formatter/marker/MarkLineEnds.hx +++ b/src/formatter/marker/MarkLineEnds.hx @@ -459,7 +459,7 @@ class MarkLineEnds extends MarkerBase { case Kwd(KwdFunction): return config.lineEnds.metadataFunction; case Sharp(_): - return After; + return config.lineEnds.metadataOther; default: return config.lineEnds.metadataOther; } diff --git a/test/testcases/emptylines/class_fields_with_conditional_function_signatures.hxtest b/test/testcases/emptylines/class_fields_with_conditional_function_signatures.hxtest new file mode 100644 index 0000000..6fade71 --- /dev/null +++ b/test/testcases/emptylines/class_fields_with_conditional_function_signatures.hxtest @@ -0,0 +1,90 @@ +{ +} + + +--- + +class Test { + public inline function compileShader(shader:GLShader):Void + { + this.compileShader(shader); + } + + #if !lime_webgl + public inline function uniform3iv(location:GLUniformLocation, v:Int32Array, ?srcOffset:Int, ?srcLength:Int):Void + #else + public inline function uniform3iv(location:GLUniformLocation, v:Dynamic, ?srcOffset:Int, ?srcLength:Int):Void + #end + { + this.uniform3iv(location, v != null ? Std.int(v.length / 3) : 0, v); + } + public inline function uniform3ui(location:GLUniformLocation, v0:Int, v1:Int, v2:Int):Void + { + this.uniform3ui(location, v0, v1, v2); + } + + #if !lime_webgl + public function compressedTexImage2D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, border:Int, srcData:ArrayBufferView, + srcOffset:Int = 0, + ?srcLengthOverride:Int):Void + #else + public inline function compressedTexImage2D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, border:Int, srcData:Dynamic, ?srcOffset:Int, + ?srcLengthOverride:Int):Void + #end + + { + var imageSize = (srcLengthOverride != null) ? srcLengthOverride : (srcData != null) ? srcData.byteLength : 0; + + __tempPointer.set(srcData, srcOffset); + this.compressedTexImage2D(target, level, internalformat, width, height, border, imageSize, __tempPointer); + } + public function compressedTexImage3D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, depth:Int, border:Int, srcData:ArrayBufferView, + srcOffset:Int = 0, ?srcLengthOverride:Int):Void + { + var imageSize = (srcLengthOverride != null) ? srcLengthOverride : (srcData != null) ? srcData.byteLength : 0; + __tempPointer.set(srcData, srcOffset); + this.compressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, __tempPointer); + } +} + +--- + +class Test { + public inline function compileShader(shader:GLShader):Void { + this.compileShader(shader); + } + + #if !lime_webgl + public inline function uniform3iv(location:GLUniformLocation, v:Int32Array, ?srcOffset:Int, ?srcLength:Int):Void + #else + public inline function uniform3iv(location:GLUniformLocation, v:Dynamic, ?srcOffset:Int, ?srcLength:Int):Void + #end + { + this.uniform3iv(location, v != null ? Std.int(v.length / 3) : 0, v); + } + + public inline function uniform3ui(location:GLUniformLocation, v0:Int, v1:Int, v2:Int):Void { + this.uniform3ui(location, v0, v1, v2); + } + + #if !lime_webgl + public function compressedTexImage2D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, border:Int, srcData:ArrayBufferView, + srcOffset:Int = 0, ?srcLengthOverride:Int):Void + #else + public inline function compressedTexImage2D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, border:Int, srcData:Dynamic, ?srcOffset:Int, + ?srcLengthOverride:Int):Void + #end + { + var imageSize = (srcLengthOverride != null) ? srcLengthOverride : (srcData != null) ? srcData.byteLength : 0; + + __tempPointer.set(srcData, srcOffset); + this.compressedTexImage2D(target, level, internalformat, width, height, border, imageSize, __tempPointer); + } + + public function compressedTexImage3D(target:Int, level:Int, internalformat:Int, width:Int, height:Int, depth:Int, border:Int, srcData:ArrayBufferView, + srcOffset:Int = 0, ?srcLengthOverride:Int):Void { + var imageSize = (srcLengthOverride != null) ? srcLengthOverride : (srcData != null) ? srcData.byteLength : 0; + __tempPointer.set(srcData, srcOffset); + this.compressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, __tempPointer); + } +} diff --git a/test/testcases/emptylines/empty_classes_with_conditional_metadata.hxtest b/test/testcases/emptylines/empty_classes_with_conditional_metadata.hxtest new file mode 100644 index 0000000..c546d34 --- /dev/null +++ b/test/testcases/emptylines/empty_classes_with_conditional_metadata.hxtest @@ -0,0 +1,23 @@ +{ +} + + +--- + +#if FLX_DEBUG @:bitmap("assets/images/debugger/flixel.png") #end +private class GraphicFlixel extends BitmapData {} +#if FLX_DEBUG @:bitmap("assets/images/debugger/buttons/drawDebug.png") #end +private class GraphicDrawDebug extends BitmapData {} +#if FLX_DEBUG @:bitmap("assets/images/debugger/buttons/log.png") #end +@:noCompletion class GraphicLog extends BitmapData {} + +--- + +#if FLX_DEBUG @:bitmap("assets/images/debugger/flixel.png") #end +private class GraphicFlixel extends BitmapData {} + +#if FLX_DEBUG @:bitmap("assets/images/debugger/buttons/drawDebug.png") #end +private class GraphicDrawDebug extends BitmapData {} + +#if FLX_DEBUG @:bitmap("assets/images/debugger/buttons/log.png") #end +@:noCompletion class GraphicLog extends BitmapData {} diff --git a/test/testcases/emptylines/issue_431_conditionalised_class_with_constraints.hxtest b/test/testcases/emptylines/issue_431_conditionalised_class_with_constraints.hxtest new file mode 100644 index 0000000..726ca92 --- /dev/null +++ b/test/testcases/emptylines/issue_431_conditionalised_class_with_constraints.hxtest @@ -0,0 +1,41 @@ +{ +} + +--- + +#if (haxe_ver >= "4.0.0") +class FlxTypedEmitter extends FlxTypedGroup +#else +class FlxTypedEmitter extends FlxTypedGroup +#end +{} + +#if (haxe_ver >= "4.0.0") +class FlxTypedEmitter extends FlxTypedGroup +#else +class FlxTypedEmitter extends FlxTypedGroup +#end +{ + var foo:Int; +} +enum Foo {} + +--- + +#if (haxe_ver >= "4.0.0") +class FlxTypedEmitter extends FlxTypedGroup +#else +class FlxTypedEmitter extends FlxTypedGroup +#end +{} + +#if (haxe_ver >= "4.0.0") +class FlxTypedEmitter extends FlxTypedGroup +#else +class FlxTypedEmitter extends FlxTypedGroup +#end +{ + var foo:Int; +} + +enum Foo {} diff --git a/test/testcases/emptylines/private_abstract_with_conditionals.hxtest b/test/testcases/emptylines/private_abstract_with_conditionals.hxtest new file mode 100644 index 0000000..7c398c1 --- /dev/null +++ b/test/testcases/emptylines/private_abstract_with_conditionals.hxtest @@ -0,0 +1,44 @@ +{ +} + + +--- + +#if (haxe_ver >= 4.0) private enum #else @:enum private #end abstract SystemDirectory(Int) from Int to Int from UInt to UInt +{ + var APPLICATION = 0; + var APPLICATION_STORAGE = 1; + var DESKTOP = 2; + var DOCUMENTS = 3; + var FONTS = 4; + var USER = 5; +} + +#if (!lime_doc_gen || lime_cairo) +#if (haxe_ver >= 4.0) enum #else @:enum #end abstract CairoContent(Int) from Int to Int from UInt to UInt + +{ + public var COLOR = 0x1000; + public var ALPHA = 0x2000; + public var COLOR_ALPHA = 0x3000; +} +#end + +--- + +#if (haxe_ver >= 4.0) private enum #else @:enum private #end abstract SystemDirectory(Int) from Int to Int from UInt to UInt { + var APPLICATION = 0; + var APPLICATION_STORAGE = 1; + var DESKTOP = 2; + var DOCUMENTS = 3; + var FONTS = 4; + var USER = 5; +} + +#if (!lime_doc_gen || lime_cairo) +#if (haxe_ver >= 4.0) enum #else @:enum #end abstract CairoContent(Int) from Int to Int from UInt to UInt { + public var COLOR = 0x1000; + public var ALPHA = 0x2000; + public var COLOR_ALPHA = 0x3000; +} +#end