Skip to content

Commit ad57c3b

Browse files
committed
fix - improve invalid indentation in enums - kokororin/vscode-phpfmt#137
1 parent 7109815 commit ad57c3b

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

fmt.stub.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,6 +3915,7 @@ public function format($source) {
39153915

39163916
$foundStack = [];
39173917
$stack = [];
3918+
$is_enum = false;
39183919

39193920
while (list($index, $token) = $this->each($this->tkns)) {
39203921
list($id, $text) = $this->getToken($token);
@@ -3934,6 +3935,7 @@ public function format($source) {
39343935
$this->setIndent(+1);
39353936
}
39363937
}
3938+
39373939
switch ($id) {
39383940
case ST_QUOTE:
39393941
$this->appendCode($text);
@@ -3957,11 +3959,18 @@ public function format($source) {
39573959
$this->appendCode($text);
39583960
break;
39593961

3962+
case T_CURLY_OPEN:
3963+
case ST_CURLY_OPEN:
3964+
if ($is_enum) {
3965+
$stack[] = $is_enum;
3966+
$is_enum = false;
3967+
} else {
3968+
$stack[] = $id;
3969+
}
39603970
case T_DOLLAR_OPEN_CURLY_BRACES:
3961-
case T_CURLY_OPEN:
3962-
case ST_CURLY_OPEN:
39633971
case ST_PARENTHESES_OPEN:
39643972
case ST_BRACKET_OPEN:
3973+
39653974
$indentToken = [
39663975
'id' => $id,
39673976
'implicit' => true,
@@ -3976,7 +3985,7 @@ public function format($source) {
39763985

39773986
case T_ENUM:
39783987
$this->appendCode($text);
3979-
$stack[] = $id;
3988+
$is_enum = $id;
39803989
break;
39813990

39823991
case ST_CURLY_CLOSE:
@@ -4006,9 +4015,9 @@ public function format($source) {
40064015
case T_COMMENT:
40074016
case T_WHITESPACE:
40084017

4009-
$is_enum = false;
4018+
$is_enum2 = false;
40104019
if (count($stack) && $stack[count($stack) - 1] === T_ENUM) {
4011-
$is_enum = true;
4020+
$is_enum2 = true;
40124021
}
40134022

40144023
if (
@@ -4017,7 +4026,7 @@ public function format($source) {
40174026
(
40184027
$this->rightUsefulTokenIs([T_DEFAULT])
40194028
||
4020-
($this->rightUsefulTokenIs([T_CASE]) && ! $is_enum)
4029+
($this->rightUsefulTokenIs([T_CASE]) && ! $is_enum2)
40214030
)
40224031
) {
40234032
$this->setIndent(-1);

tests/Original/471-invalid-indentation.in

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,35 @@ enum ExampleEnum2:string {
2121
case Bar = 'b';
2222
// Note.
2323
case Qux = 'c';
24+
}
25+
enum Suit implements Colorful
26+
{
27+
// Fulfills the interface contract.
28+
case Hearts;
29+
/**
30+
* Note.
31+
*/
32+
case Diamonds;
33+
34+
// Fulfills the interface contract.
35+
public function color(): string
36+
{
37+
return match($this) {
38+
Suit::Hearts, Suit::Diamonds => 'Red',
39+
Suit::Clubs, Suit::Spades => 'Black',
40+
};
41+
}
42+
43+
// Not part of an interface; that's fine.
44+
public function shape(): string
45+
{
46+
return "Rectangle";
47+
}
48+
49+
# Note.
50+
case Clubs;
51+
/**
52+
* a
53+
* */
54+
case Spades;
2455
}

tests/Original/471-invalid-indentation.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,32 @@ enum ExampleEnum2: string {
2121
case Bar = 'b';
2222
// Note.
2323
case Qux = 'c';
24+
}
25+
enum Suit implements Colorful {
26+
// Fulfills the interface contract.
27+
case Hearts;
28+
/**
29+
* Note.
30+
*/
31+
case Diamonds;
32+
33+
// Fulfills the interface contract.
34+
public function color(): string {
35+
return match ($this) {
36+
Suit::Hearts, Suit::Diamonds => 'Red',
37+
Suit::Clubs, Suit::Spades => 'Black',
38+
};
39+
}
40+
41+
// Not part of an interface; that's fine.
42+
public function shape(): string {
43+
return "Rectangle";
44+
}
45+
46+
# Note.
47+
case Clubs;
48+
/**
49+
* a
50+
* */
51+
case Spades;
2452
}

0 commit comments

Comments
 (0)