Skip to content

Commit

Permalink
fixed conditionalized class declaration with constraints, fixes #431
Browse files Browse the repository at this point in the history
fixed empty classes with conditional metadata
fixed class fields with conditional function signatures
fixed abstract enum abstracts with conditionals
  • Loading branch information
AlexHaxe committed Sep 30, 2024
1 parent 0b5d3cd commit 68b6b50
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
86 changes: 79 additions & 7 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -520,7 +532,7 @@ class MarkEmptyLines extends MarkerBase {
var currToken:Null<TokenTree> = null;
var currTokenType:Null<TokenFieldType> = null;
for (field in fields) {
currToken = field;
currToken = skipSharpFields(field);
currTokenType = FieldUtils.getFieldType(field, Public);
markInterfaceEmptyLines(prevToken, prevTokenType, currToken, currTokenType, conf);
prevToken = currToken;
Expand Down Expand Up @@ -608,7 +620,7 @@ class MarkEmptyLines extends MarkerBase {
var currToken:Null<TokenTree> = null;
var currTokenType:Null<TokenFieldType> = null;
for (func in functions) {
currToken = func;
currToken = skipSharpFields(func);
currTokenType = FieldUtils.getFieldType(func, Public);
markEnumAbstractFieldEmptyLines(prevToken, prevTokenType, currToken, currTokenType);
prevToken = currToken;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -783,6 +803,8 @@ class MarkEmptyLines extends MarkerBase {
FoundSkipSubtree;
case Kwd(KwdFinal):
FoundSkipSubtree;
case BrOpen:
SkipSubtree;
default:
GoDeeper;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/marker/MarkLineEnds.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
}

---

#if (haxe_ver >= "4.0.0")
class FlxTypedEmitter<T:FlxSprite & IFlxParticle> extends FlxTypedGroup<T>
#else
class FlxTypedEmitter<T:(FlxSprite, IFlxParticle)> extends FlxTypedGroup<T>
#end
{}

#if (haxe_ver >= "4.0.0")
class FlxTypedEmitter<T:FlxSprite & IFlxParticle> extends FlxTypedGroup<T>
#else
class FlxTypedEmitter<T:(FlxSprite, IFlxParticle) > extends FlxTypedGroup<T>
#end
{
var foo:Int;
}
enum Foo {}

---

#if (haxe_ver >= "4.0.0")
class FlxTypedEmitter<T:FlxSprite & IFlxParticle> extends FlxTypedGroup<T>
#else
class FlxTypedEmitter<T:(FlxSprite, IFlxParticle)> extends FlxTypedGroup<T>
#end
{}

#if (haxe_ver >= "4.0.0")
class FlxTypedEmitter<T:FlxSprite & IFlxParticle> extends FlxTypedGroup<T>
#else
class FlxTypedEmitter<T:(FlxSprite, IFlxParticle)> extends FlxTypedGroup<T>
#end
{
var foo:Int;
}

enum Foo {}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 68b6b50

Please sign in to comment.