Skip to content

Commit b8f7047

Browse files
authored
added sameLine.untypedBody, fixes #362 (#506)
* added sameLine.untypedBody, fixes #362
1 parent c88d2aa commit b8f7047

13 files changed

+343
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Added `emptyLines.*.existingBetweenFields`, fixes [#455](https://github.com/HaxeCheckstyle/haxe-formatter/issues/455) ([#484](https://github.com/HaxeCheckstyle/haxe-formatter/issues/484))
77
- Added printing config filename in verbose mode, fixes [#460](https://github.com/HaxeCheckstyle/haxe-formatter/issues/460) ([#493](https://github.com/HaxeCheckstyle/haxe-formatter/issues/493))
88
- Added `indentation.indentCaseLabels`, fixes [#478](https://github.com/HaxeCheckstyle/haxe-formatter/issues/478) ([#502](https://github.com/HaxeCheckstyle/haxe-formatter/issues/502))
9+
- Added `sameLine.untypedBody`, fixes [#362](https://github.com/HaxeCheckstyle/haxe-formatter/issues/362) ([#506](https://github.com/HaxeCheckstyle/haxe-formatter/issues/506))
910
- Fixed same line handling of if-else with try catch body, fixes [#360](https://github.com/HaxeCheckstyle/haxe-formatter/issues/360) ([#483](https://github.com/HaxeCheckstyle/haxe-formatter/issues/483))
1011
- Fixed line end handling of structure type as type parameter, fixes [#475](https://github.com/HaxeCheckstyle/haxe-formatter/issues/475) ([#486](https://github.com/HaxeCheckstyle/haxe-formatter/issues/486))
1112
- Fixed wrapping function parameters with comments, fixes [#472](https://github.com/HaxeCheckstyle/haxe-formatter/issues/472) ([#487](https://github.com/HaxeCheckstyle/haxe-formatter/issues/487))

resources/default-hxformat.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
"returnBodySingleLine": "same",
162162
"tryBody": "next",
163163
"tryCatch": "same",
164+
"untypedBody": "same",
164165
"whileBody": "next"
165166
},
166167
"whitespace": {

resources/hxformat-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@
741741
"$ref": "#/definitions/formatter.config.SameLinePolicy",
742742
"default": "same"
743743
},
744+
"untypedBody": {
745+
"description": "same line policy for untyped {…} as a body * same = place return and body on same line * next = place body on next line * keep = keep same / next line from source",
746+
"$ref": "#/definitions/formatter.config.SameLinePolicy",
747+
"default": "same"
748+
},
744749
"anonFunctionBody": {
745750
"description": "same line policy for non block body of anon \"function\" * same = place function and body on same line * next = place body on next line * keep = keep same / next line from source",
746751
"$ref": "#/definitions/formatter.config.SameLinePolicy",

src/formatter/config/SameLineConfig.hx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ typedef SameLineConfig = {
140140
* keep = keep same / next line from source
141141
**/
142142
@:default(Same) @:optional var returnBodySingleLine:SameLinePolicy;
143+
144+
/**
145+
same line policy for untyped {…} as a body
146+
* same = place return and body on same line
147+
* next = place body on next line
148+
* keep = keep same / next line from source
149+
**/
150+
@:default(Same) @:optional var untypedBody:SameLinePolicy;
143151
}
144152

145153
@:enum

src/formatter/marker/Indenter.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class Indenter {
295295
case BrOpen:
296296
switch (currentToken.tok) {
297297
case Kwd(KwdIf), Kwd(KwdElse), Kwd(KwdTry), Kwd(KwdCatch), Kwd(KwdDo), Kwd(KwdWhile), Kwd(KwdFor), Kwd(KwdFunction), Kwd(KwdSwitch),
298-
Kwd(KwdReturn):
298+
Kwd(KwdReturn), Kwd(KwdUntyped):
299299
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(prevToken);
300300
switch (type) {
301301
case OBJECTDECL:

src/formatter/marker/MarkSameLine.hx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class MarkSameLine extends MarkerBase {
3939
markMacro(token);
4040
case Kwd(KwdReturn):
4141
markReturn(token);
42+
case Kwd(KwdUntyped):
43+
markUntyped(token);
4244
default:
4345
}
4446
return GO_DEEPER;
@@ -733,6 +735,32 @@ class MarkSameLine extends MarkerBase {
733735
}
734736
}
735737

738+
function markUntyped(token:TokenTree) {
739+
if (!token.access().firstChild().is(BrOpen).exists()) {
740+
return;
741+
}
742+
var parent:Null<TokenTree> = token.parent;
743+
if ((parent == null) || (token.tok == null)) {
744+
return;
745+
}
746+
switch (parent.tok) {
747+
case BrOpen:
748+
var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(parent);
749+
switch (type) {
750+
case BLOCK:
751+
return;
752+
case TYPEDEFDECL:
753+
return;
754+
case OBJECTDECL:
755+
case ANONTYPE:
756+
case UNKNOWN:
757+
}
758+
default:
759+
}
760+
761+
applySameLinePolicy(token, config.sameLine.untypedBody);
762+
}
763+
736764
function shouldReturnBeSameLine(token:TokenTree):Bool {
737765
var lastToken:Null<TokenTree> = TokenTreeCheckUtils.getLastToken(token);
738766
if (lastToken == null) {

test/testcases/indentation/issue_114_untyped.hxtest

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,19 @@ class Main {
2929
---
3030

3131
class Main {
32-
function foo()
33-
untyped {
34-
trace("");
35-
}
32+
function foo() untyped {
33+
trace("");
34+
}
3635

3736
function foo()
3837
return {
3938
x: 0,
4039
y: 0
4140
}
4241

43-
function foo()
44-
untyped {
45-
trace("");
46-
}
42+
function foo() untyped {
43+
trace("");
44+
}
4745

4846
function foo()
4947
return {

test/testcases/indentation/issue_114_untyped_keep.hxtest

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"sameLine": {
3-
"functionBody": "keep"
3+
"functionBody": "keep",
4+
"untypedBody": "keep"
45
}
56
}
67

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{}
2+
3+
---
4+
5+
class Main {
6+
public static function listNames():Array<String> untyped {
7+
return __keys__(__resources__.list);
8+
}
9+
public static function listNames():Array<String> { untyped {
10+
return __keys__(__resources__.list);
11+
}
12+
}
13+
function main(){
14+
try untyped {
15+
var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._"+name.split(".").join("_")),Class);
16+
return __new__(c);
17+
} catch (e:Dynamic) {
18+
return null;
19+
}
20+
}
21+
}
22+
23+
---
24+
25+
class Main {
26+
public static function listNames():Array<String> untyped {
27+
return __keys__(__resources__.list);
28+
}
29+
30+
public static function listNames():Array<String> {
31+
untyped {
32+
return __keys__(__resources__.list);
33+
}
34+
}
35+
36+
function main() {
37+
try untyped {
38+
var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._" + name.split(".").join("_")), Class);
39+
return __new__(c);
40+
} catch (e:Dynamic) {
41+
return null;
42+
}
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"lineEnds": {
3+
"leftCurly": "both"
4+
}
5+
}
6+
7+
---
8+
9+
class Main {
10+
public static function listNames():Array<String> untyped {
11+
return __keys__(__resources__.list);
12+
}
13+
public static function listNames():Array<String> { untyped {
14+
return __keys__(__resources__.list);
15+
}
16+
}
17+
function main(){
18+
try untyped {
19+
var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._"+name.split(".").join("_")),Class);
20+
return __new__(c);
21+
} catch (e:Dynamic) {
22+
return null;
23+
}
24+
}
25+
}
26+
27+
---
28+
29+
class Main
30+
{
31+
public static function listNames():Array<String> untyped
32+
{
33+
return __keys__(__resources__.list);
34+
}
35+
36+
public static function listNames():Array<String>
37+
{
38+
untyped
39+
{
40+
return __keys__(__resources__.list);
41+
}
42+
}
43+
44+
function main()
45+
{
46+
try untyped
47+
{
48+
var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._" + name.split(".").join("_")), Class);
49+
return __new__(c);
50+
} catch (e:Dynamic)
51+
{
52+
return null;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)