Skip to content

Commit

Permalink
fixed local metadata linebreaks, fixes #630 (#636)
Browse files Browse the repository at this point in the history
* fixed local metadata linebreaks, fixes #630
* prepared version 1.12.0
  • Loading branch information
AlexHaxe authored Feb 28, 2021
1 parent 084c876 commit 768bb49
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

## dev branch / next version (1.x.x)

- Added testcase for local metadata linebreak, fixes [#630](https://github.com/HaxeCheckstyle/haxe-formatter/issues/630) ([#631](https://github.com/HaxeCheckstyle/haxe-formatter/issues/631))
## version 1.12.0 (2021-02-28)

- Added `lineEnds.lineEndCharacter` to set line end character used for output ([#633](https://github.com/HaxeCheckstyle/haxe-formatter/issues/633))
- Fixed support for overload access modifier, fixes [#626](https://github.com/HaxeCheckstyle/haxe-formatter/issues/626) ([#627](https://github.com/HaxeCheckstyle/haxe-formatter/issues/627))
- Fixed parens after curly block, fixes [#629](https://github.com/HaxeCheckstyle/haxe-formatter/issues/629) ([#631](https://github.com/HaxeCheckstyle/haxe-formatter/issues/631))
- Fixed local metadata linebreak, fixes [#630](https://github.com/HaxeCheckstyle/haxe-formatter/issues/630) ([#631](https://github.com/HaxeCheckstyle/haxe-formatter/issues/631) + [#636](https://github.com/HaxeCheckstyle/haxe-formatter/issues/636))
- Fixed `is as` formatted as `isas`, fixes [#634](https://github.com/HaxeCheckstyle/haxe-formatter/issues/634) ([#635](https://github.com/HaxeCheckstyle/haxe-formatter/issues/635))
- Retired Haxe 3.4.7 compile support ([#627](https://github.com/HaxeCheckstyle/haxe-formatter/issues/627))

Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"style"
],
"description": "A code formatter for Haxe",
"version": "1.11.2",
"releasenote": "fixed array type parameter - see CHANGELOG for details.",
"version": "1.12.0",
"releasenote": "added lineEnds.lineEndCharacter and bugfixes - see CHANGELOG for details.",
"contributors": [
"AlexHaxe",
"Gama11"
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haxecheckstyle/haxe-formatter",
"version": "1.11.2",
"version": "1.12.0",
"description": "A code formatter for Haxe",
"repository": {
"type": "git",
Expand Down
24 changes: 22 additions & 2 deletions src/formatter/marker/MarkLineEnds.hx
Original file line number Diff line number Diff line change
Expand Up @@ -356,22 +356,42 @@ class MarkLineEnds extends MarkerBase {
var atTokens:Array<TokenTree> = parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
return switch (token.tok) {
case At:
FoundGoDeeper;
FoundSkipSubtree;
default:
GoDeeper;
}
});
function addLineEndBefore(atToken:TokenTree) {
var prev:TokenInfo = getPreviousToken(atToken);
if (prev == null) {
lineEndBefore(atToken);
return;
}
switch (prev.token.tok) {
case Kwd(KwdElse):
lineEndBefore(atToken);
case BrOpen:
lineEndBefore(atToken);
case Kwd(KwdFinal) | Kwd(KwdFunction) | Kwd(KwdVar):
noLineEndBefore(atToken);
default:
}
}
for (token in atTokens) {
var metadataPolicy:AtLineEndPolicy = determineMetadataPolicy(token);
var lastChild:Null<TokenTree> = TokenTreeCheckUtils.getLastToken(token);
if (lastChild == null) {
continue;
}
var isNotFirst:Bool = (token.previousSibling != null) && (token.previousSibling.tok.match(At));
if (!isNotFirst) {
addLineEndBefore(token);
}
if (metadataPolicy == After) {
lineEndAfter(lastChild);
continue;
}
if ((token.previousSibling != null) && (token.previousSibling.tok.match(At))) {
if (isNotFirst) {
// only look at first metadata
continue;
}
Expand Down
33 changes: 24 additions & 9 deletions src/formatter/marker/MarkSameLine.hx
Original file line number Diff line number Diff line change
Expand Up @@ -586,17 +586,24 @@ class MarkSameLine extends MarkerBase {
if (prev == null) {
noLineEndBefore(token);
} else {
switch (prev.token.tok) {
case POpen | Dot:
whitespace(token, NoneBefore);
case BrClose | Semicolon:
switch (token.tok) {
case At:
case _:
switch (token.tok) {
case At:
switch (prev.token.tok) {
case POpen | Dot:
whitespace(token, NoneBefore);
case BrOpen | BrClose | Semicolon | DblDot | Sharp(_) | Kwd(_):
case Binop(_):
lineEndBefore(token);
default:
noLineEndBefore(token);
}
case _:
switch (prev.token.tok) {
case POpen | Dot:
whitespace(token, NoneBefore);
default:
noLineEndBefore(token);
}
default:
noLineEndBefore(token);
}
}
var lastToken:Null<TokenTree> = TokenTreeCheckUtils.getLastToken(token);
Expand Down Expand Up @@ -625,6 +632,14 @@ class MarkSameLine extends MarkerBase {
}
default:
}
var prev:Null<TokenInfo> = getPreviousToken(token);
if ((prev == null) || (!TokenTreeCheckUtils.isMetadata(prev.token))) {
lineEndBefore(token);
return;
}
if (!parsedCode.isOriginalNewlineBefore(token)) {
return;
}
lineEndBefore(token);
}
}
Expand Down
151 changes: 151 additions & 0 deletions test/testcases/lineends/issue_630_local_metadata.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,160 @@ public inline function next():{key:K, value:V} {
var key = keys.next(); @:nullSafety(Off) return {value: map.get(key), key: key};
}

public inline function next():{key:K, value:V} {
var key = keys.next();
@:nullSafety(Off) return {value: map.get(key), key: key};
}

public function unserialize():Dynamic {
switch (get(pos++)) {
case "n".code: @:nullSafety(Off)
return null;
}
}

public function unserialize():Dynamic {
switch (get(pos++)) {
case "n".code:
@:nullSafety(Off) return null;
}
}

static inline function fastCharCodeAt(s:String, pos:Int):Int {
#if php
return php.Global.ord((s : php.NativeString)[pos]);
#else @:nullSafety(Off)
return s.charCodeAt(pos);
#end
}

static inline function fastCharCodeAt(s:String, pos:Int):Int {
#if php
return php.Global.ord((s : php.NativeString)[pos]);
#else
@:nullSafety(Off) return s.charCodeAt(pos);
#end
}

public function first():Null<T> {@:nullSafety(Off)
return if (h == null) null else h.item;
}

public function first():Null<T> {
@:nullSafety(Off) return if (h == null) null else h.item;
}

public inline function resolveClass(name:String):Null<Class<Dynamic>> @:nullSafety(Off)
return null;

public inline function resolveClass(name:String):Null<Class<Dynamic>>
@:nullSafety(Off) return null;

public inline function resolveClass(name:String):Null<Class<Dynamic>>
@:nullSafety(Off)
return null;

public function add(item:T) {
var x = ListNode.create(item, null);
if (h == null)
h = x;
else @:nullSafety(Off)
q.next = x;
q = x;
length++;
}

public function add(item:T) {
var x = ListNode.create(item, null);
if (h == null)
h = x;
else
@:nullSafety(Off) q.next = x;
q = x;
length++;
}

---

public inline function next():{key:K, value:V} {
var key = keys.next();
@:nullSafety(Off) return {value: map.get(key), key: key};
}

public inline function next():{key:K, value:V} {
var key = keys.next();
@:nullSafety(Off) return {value: map.get(key), key: key};
}

public function unserialize():Dynamic {
switch (get(pos++)) {
case "n".code:
@:nullSafety(Off)
return null;
}
}

public function unserialize():Dynamic {
switch (get(pos++)) {
case "n".code:
@:nullSafety(Off) return null;
}
}

static inline function fastCharCodeAt(s:String, pos:Int):Int {
#if php
return php.Global.ord((s : php.NativeString)[pos]);
#else
@:nullSafety(Off)
return s.charCodeAt(pos);
#end
}

static inline function fastCharCodeAt(s:String, pos:Int):Int {
#if php
return php.Global.ord((s : php.NativeString)[pos]);
#else
@:nullSafety(Off) return s.charCodeAt(pos);
#end
}

public function first():Null<T> {
@:nullSafety(Off)
return if (h == null) null else h.item;
}

public function first():Null<T> {
@:nullSafety(Off) return if (h == null) null else h.item;
}

public inline function resolveClass(name:String):Null<Class<Dynamic>>
@:nullSafety(Off)
return null;

public inline function resolveClass(name:String):Null<Class<Dynamic>>
@:nullSafety(Off) return null;

public inline function resolveClass(name:String):Null<Class<Dynamic>>
@:nullSafety(Off)
return null;

public function add(item:T) {
var x = ListNode.create(item, null);
if (h == null)
h = x;
else
@:nullSafety(Off)
q.next = x;
q = x;
length++;
}

public function add(item:T) {
var x = ListNode.create(item, null);
if (h == null)
h = x;
else
@:nullSafety(Off) q.next = x;
q = x;
length++;
}

0 comments on commit 768bb49

Please sign in to comment.