Skip to content

Commit 91e3219

Browse files
committed
wipppppp
1 parent af3a192 commit 91e3219

File tree

7 files changed

+159
-40
lines changed

7 files changed

+159
-40
lines changed

app/Contexts/AbstractContext.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
abstract class AbstractContext
66
{
7-
public $children = [];
7+
public array $children = [];
8+
9+
public bool $autocompleting = false;
810

911
protected array $freshObject;
1012

@@ -74,9 +76,8 @@ public function touched(): bool
7476
public function toArray(): array
7577
{
7678
return array_merge(
77-
[
78-
'type' => $this->type(),
79-
],
79+
['type' => $this->type()],
80+
$this->autocompleting ? ['autocompleting' => true] : [],
8081
$this->castToArray(),
8182
($this->label !== '') ? ['label' => $this->label] : [],
8283
($this->hasChildren) ? ['children' => array_map(fn($child) => $child->toArray(), $this->children)] : [],

app/Contexts/ArrayItem.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class ArrayItem extends AbstractContext
88
{
9+
public bool $hasKey = false;
10+
11+
public bool $autocompletingValue = false;
12+
913
public function type(): string
1014
{
1115
return 'array_item';
@@ -18,18 +22,56 @@ public function toArray(): array
1822

1923
public function castToArray(): array
2024
{
21-
$key = null;
22-
$value = null;
25+
return [
26+
'key' => $this->getKey()?->toArray(),
27+
'value' => $this->getValue()?->toArray(),
2328

24-
if (count($this->children) === 1) {
25-
[$value] = $this->children;
26-
} elseif (count($this->children) === 2) {
27-
[$key, $value] = $this->children;
29+
] + $this->getAutoCompletingValueData();
30+
}
31+
32+
protected function getAutoCompletingValueData(): array
33+
{
34+
if ($this->autocompletingValue || $this->hasAutoCompletingChild($this)) {
35+
return ['autocompletingValue' => true];
2836
}
2937

30-
return [
31-
'key' => $key?->toArray(),
32-
'value' => $value?->toArray(),
33-
];
38+
return [];
39+
}
40+
41+
protected function hasAutoCompletingChild(AbstractContext $context): bool
42+
{
43+
foreach ($context->children as $child) {
44+
if ($child->autocompleting || $this->hasAutoCompletingChild($child)) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
52+
protected function getKey(): ?AbstractContext
53+
{
54+
if (count($this->children) === 1 && $this->hasKey) {
55+
return $this->children[0];
56+
}
57+
58+
if (count($this->children) === 2) {
59+
return $this->children[0];
60+
}
61+
62+
return null;
63+
}
64+
65+
protected function getValue(): ?AbstractContext
66+
{
67+
if (count($this->children) === 1 && !$this->hasKey) {
68+
return $this->children[0];
69+
}
70+
71+
if (count($this->children) === 2) {
72+
return $this->children[1];
73+
}
74+
75+
return null;
3476
}
3577
}

app/Contexts/ArrayValue.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,41 @@ public function type(): string
88
{
99
return 'array';
1010
}
11+
12+
public function toArray(): array
13+
{
14+
return array_merge(parent::toArray(), $this->extraData());
15+
}
16+
17+
protected function extraData(): array
18+
{
19+
if (!$this->autocompleting) {
20+
return [];
21+
}
22+
23+
if (count($this->children) === 0) {
24+
return [
25+
'autocompletingKey' => true,
26+
'autocompletingValue' => true,
27+
];
28+
}
29+
30+
$valueToAutocomplete = collect($this->children)->first(
31+
fn($child) => $child->toArray()['autocompletingValue'] ?? false,
32+
);
33+
34+
if ($valueToAutocomplete) {
35+
return [
36+
'autocompletingKey' => false,
37+
'autocompletingValue' => true,
38+
];
39+
}
40+
41+
$firstChild = $this->children[0];
42+
43+
return [
44+
'autocompletingKey' => $firstChild->hasKey,
45+
'autocompletingValue' => !$firstChild->hasKey,
46+
];
47+
}
1148
}

app/Parsers/ArrayCreationExpressionParser.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Contexts\ArrayValue;
66
use App\Contexts\AbstractContext;
7+
use Microsoft\PhpParser\MissingToken;
78
use Microsoft\PhpParser\Node\Expression\ArrayCreationExpression;
89

910
class ArrayCreationExpressionParser extends AbstractParser
@@ -15,32 +16,9 @@ class ArrayCreationExpressionParser extends AbstractParser
1516

1617
public function parse(ArrayCreationExpression $node)
1718
{
18-
return $this->context;
19-
20-
// $array = [];
21-
// $lastValue = null;
22-
23-
// if ($node->arrayElements) {
24-
// foreach ($node->arrayElements->getElements() as $element) {
25-
// $array[] = [
26-
// 'key' => $this->parseArgument($element->elementKey),
27-
// 'value' => $this->parseArgument($element->elementValue),
28-
// ];
29-
30-
// $lastValue = $element->elementValue;
31-
// }
32-
// }
19+
$this->context->autocompleting = $node->closeParenOrBracket instanceof MissingToken;
3320

34-
// if ($node->closeParenOrBracket instanceof MissingToken) {
35-
// $this->handleMissingArrayCloseToken($array, $lastValue);
36-
// }
37-
38-
// return [
39-
// 'type' => 'array',
40-
// 'value' => $array,
41-
// ];
42-
43-
// return $this->context;
21+
return $this->context;
4422
}
4523

4624
public function initNewContext(): ?AbstractContext

app/Parsers/ArrayElementParser.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@
44

55
use App\Contexts\ArrayItem;
66
use App\Contexts\AbstractContext;
7+
use Microsoft\PhpParser\MissingToken;
8+
use Microsoft\PhpParser\Node\ArrayElement;
79

810
class ArrayElementParser extends AbstractParser
911
{
12+
/**
13+
* @var ArrayItem
14+
*/
15+
protected AbstractContext $context;
16+
17+
public function parse(ArrayElement $node)
18+
{
19+
$this->context->hasKey = $node->elementKey !== null;
20+
$this->context->autocompletingValue = $node->elementValue instanceof MissingToken;
21+
22+
return $this->context;
23+
}
24+
1025
public function initNewContext(): ?AbstractContext
1126
{
1227
return new ArrayItem;

app/Parsers/CallExpressionParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Contexts\AssignmentValue;
66
use App\Contexts\AbstractContext;
77
use App\Contexts\MethodCall;
8+
use Microsoft\PhpParser\MissingToken;
89
use Microsoft\PhpParser\Node\Expression\CallExpression;
910
use Microsoft\PhpParser\Node\QualifiedName;
1011

@@ -25,6 +26,8 @@ public function parse(CallExpression $node)
2526
$this->context->name = (string) ($node->callableExpression->getResolvedName() ?? $node->callableExpression->getText());
2627
}
2728

29+
$this->context->autocompleting = $node->closeParen instanceof MissingToken;
30+
2831
return $this->context;
2932

3033
// $lastChild = null;

0 commit comments

Comments
 (0)