Skip to content

Commit ec27d90

Browse files
committed
we back
1 parent f3a59a8 commit ec27d90

22 files changed

+133
-64
lines changed

app/Commands/ParseCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public function handle(): void
3535
}
3636
}
3737

38-
echo $result->toJson();
38+
$autocompleting = $result->findAutocompleting();
39+
40+
echo json_encode($autocompleting->flip(), $this->option('debug') ? JSON_PRETTY_PRINT : 0);
41+
42+
// dd($autocompleting->flip(), 'Autocompleting');
43+
// // $toAutocomplete = $this->findFirstAutocompleting($result->toArray()['children']);
44+
45+
// echo $result->toJson($this->option('debug') ? JSON_PRETTY_PRINT : 0);
3946
}
4047
}

app/Contexts/AbstractContext.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Contexts;
44

5+
use Illuminate\Support\Arr;
6+
57
abstract class AbstractContext
68
{
79
public array $children = [];
@@ -30,6 +32,53 @@ public function __construct(protected $label = '')
3032
$this->freshObject = $this->freshArray();
3133
}
3234

35+
public function flip()
36+
{
37+
return array_merge(
38+
Arr::except($this->toArray(), ['children']),
39+
['parent' => $this->parent?->flip()],
40+
);
41+
}
42+
43+
public function findAutocompleting(?AbstractContext $context = null)
44+
{
45+
$context = $context ?? $this;
46+
$result = $this->seachForAutocompleting($context, true);
47+
$lastResult = null;
48+
49+
while ($result !== null) {
50+
$lastResult = $result;
51+
$result = $this->seachForAutocompleting($result);
52+
}
53+
54+
return $lastResult;
55+
}
56+
57+
protected function seachForAutocompleting(AbstractContext $context, $checkCurrent = false)
58+
{
59+
if ($checkCurrent && $context->autocompleting && ($context instanceof MethodCall || $context instanceof ObjectValue)) {
60+
return $context;
61+
}
62+
63+
$publicProps = Arr::except(get_object_vars($context), ['freshObject', 'parent']);
64+
65+
foreach ($publicProps as $child) {
66+
$child = is_array($child) ? $child : [$child];
67+
68+
foreach ($child as $subChild) {
69+
if ($subChild instanceof AbstractContext) {
70+
$result = $this->findAutocompleting($subChild);
71+
72+
if ($result) {
73+
return $result;
74+
}
75+
}
76+
}
77+
}
78+
79+
return null;
80+
}
81+
3382
protected function freshArray()
3483
{
3584
return $this->toArray();
@@ -46,7 +95,7 @@ public function initNew(AbstractContext $newContext)
4695

4796
public function searchForVar(string $name): AssignmentValue|string|null
4897
{
49-
if ($this instanceof ClosureValue) {
98+
if (property_exists($this, 'parameters') && $this->parameters instanceof Parameters) {
5099
foreach ($this->parameters->children as $param) {
51100
if ($param->name === $name) {
52101
return $param->types[0] ?? null;
@@ -89,7 +138,9 @@ public function toArray(): array
89138
$this->autocompleting ? ['autocompleting' => true] : [],
90139
$this->castToArray(),
91140
($this->label !== '') ? ['label' => $this->label] : [],
92-
($this->hasChildren) ? ['children' => array_map(fn ($child) => $child->toArray(), $this->children)] : [],
141+
($this->hasChildren)
142+
? ['children' => array_map(fn ($child) => $child->toArray(), $this->children)]
143+
: [],
93144
);
94145
}
95146

app/Contexts/Argument.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ public function type(): string
99
return 'argument';
1010
}
1111

12-
public function toArray(): array
12+
public function isAutoCompleting(): bool
1313
{
14-
return parent::toArray()['children'][0] ?? [];
14+
if ($this->autocompleting) {
15+
return true;
16+
}
17+
18+
return collect($this->children)->first(
19+
fn ($child) => $child->autocompleting
20+
) !== null;
1521
}
1622
}

app/Contexts/Arguments.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ public function type(): string
99
return 'arguments';
1010
}
1111

12-
public function toArray(): array
12+
public function castToArray(): array
1313
{
14-
return parent::toArray()['children'];
14+
$autocompletingIndex = collect($this->children)->search(
15+
fn ($child) => $child->isAutoCompleting(),
16+
);
17+
18+
if ($autocompletingIndex === false) {
19+
$autocompletingIndex = count($this->children);
20+
}
21+
22+
return [
23+
'autocompletingIndex' => $autocompletingIndex,
24+
];
1525
}
1626
}

app/Contexts/ArrayItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function type(): string
1717

1818
public function toArray(): array
1919
{
20-
return Arr::except(parent::toArray(), ['children', 'type']);
20+
return Arr::except(parent::toArray(), ['children']);
2121
}
2222

2323
public function castToArray(): array

app/Contexts/Assignment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Assignment extends AbstractContext
1313
public function init()
1414
{
1515
$this->value = new AssignmentValue;
16+
$this->value->parent = $this;
1617
}
1718

1819
public function type(): string

app/Contexts/AssignmentValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getValue()
2020

2121
if ($child) {
2222
return [
23-
'name' => $child->name,
23+
'name' => $child->name(),
2424
'type' => $child->type(),
2525
];
2626
}

app/Contexts/ClassDefinition.php

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

55
class ClassDefinition extends AbstractContext
66
{
7-
public ?string $name = null;
7+
public ?string $className = null;
88

99
public ?string $extends = null;
1010

@@ -20,10 +20,10 @@ public function type(): string
2020
public function castToArray(): array
2121
{
2222
return [
23-
'name' => $this->name,
24-
'extends' => $this->extends,
25-
'implements' => $this->implements,
26-
'properties' => $this->properties,
23+
'className' => $this->className,
24+
'extends' => $this->extends,
25+
'implements' => $this->implements,
26+
'properties' => $this->properties,
2727
];
2828
}
2929
}

app/Contexts/ClosureValue.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ClosureValue extends AbstractContext implements HasParameters
1111
public function init()
1212
{
1313
$this->parameters = new Parameters;
14+
$this->parameters->parent = $this;
1415
}
1516

1617
public function type(): string

app/Contexts/MethodCall.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
class MethodCall extends AbstractContext
66
{
7-
public ?string $name = null;
7+
public ?string $methodName = null;
88

9-
public ?string $class = null;
9+
public ?string $className = null;
1010

1111
public Arguments $arguments;
1212

1313
public function init()
1414
{
1515
$this->arguments = new Arguments;
16+
$this->arguments->parent = $this;
1617
}
1718

1819
public function type(): string
@@ -23,9 +24,14 @@ public function type(): string
2324
public function castToArray(): array
2425
{
2526
return [
26-
'name' => $this->name,
27-
'class' => $this->class,
28-
'arguments' => $this->arguments->toArray(),
27+
'methodName' => $this->methodName,
28+
'className' => $this->className,
29+
'arguments' => $this->arguments->toArray(),
2930
];
3031
}
32+
33+
public function name()
34+
{
35+
return $this->methodName;
36+
}
3137
}

app/Contexts/MethodDefinition.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ class MethodDefinition extends AbstractContext implements HasParameters
88
{
99
public Parameters $parameters;
1010

11-
public ?string $name = null;
11+
public ?string $methodName = null;
1212

1313
public function init()
1414
{
1515
$this->parameters = new Parameters;
16+
$this->parameters->parent = $this;
1617
}
1718

1819
public function type(): string
@@ -23,8 +24,8 @@ public function type(): string
2324
public function castToArray(): array
2425
{
2526
return [
26-
'name' => $this->name,
27-
'parameters' => $this->parameters->toArray(),
27+
'methodName' => $this->methodName,
28+
'parameters' => $this->parameters->toArray(),
2829
];
2930
}
3031
}

app/Contexts/ObjectValue.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
class ObjectValue extends AbstractContext
66
{
7-
public ?string $name = null;
7+
public ?string $className = null;
88

99
public Arguments $arguments;
1010

1111
public function init()
1212
{
1313
$this->arguments = new Arguments;
14+
$this->arguments->parent = $this;
1415
}
1516

1617
public function type(): string
@@ -21,7 +22,8 @@ public function type(): string
2122
public function castToArray(): array
2223
{
2324
return [
24-
'name' => $this->name,
25+
'className' => $this->className,
26+
'arguments' => $this->arguments->toArray(),
2527
];
2628
}
2729
}

app/Contexts/Parameter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Parameter extends AbstractContext
1717
public function init()
1818
{
1919
$this->value = new ParameterValue;
20+
$this->value->parent = $this;
2021
}
2122

2223
public function type(): string

app/Contexts/ParameterValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getValue()
2020

2121
if ($child) {
2222
return [
23-
'name' => $child->name,
23+
'name' => $child->name(),
2424
'type' => $child->type(),
2525
];
2626
}

app/Parsers/ArgumentExpressionParser.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,6 @@ class ArgumentExpressionParser extends AbstractParser
1717
public function parse(ArgumentExpression $node)
1818
{
1919
return $this->context;
20-
21-
// $array = [];
22-
// $lastValue = null;
23-
24-
// if ($node->arrayElements) {
25-
// foreach ($node->arrayElements->getElements() as $element) {
26-
// $array[] = [
27-
// 'key' => $this->parseArgument($element->elementKey),
28-
// 'value' => $this->parseArgument($element->elementValue),
29-
// ];
30-
31-
// $lastValue = $element->elementValue;
32-
// }
33-
// }
34-
35-
// if ($node->closeParenOrBracket instanceof MissingToken) {
36-
// $this->handleMissingArrayCloseToken($array, $lastValue);
37-
// }
38-
39-
// return [
40-
// 'type' => 'array',
41-
// 'value' => $array,
42-
// ];
43-
44-
// return $this->context;
4520
}
4621

4722
public function initNewContext(): ?AbstractContext

app/Parsers/CallExpressionParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class CallExpressionParser extends AbstractParser
1717

1818
public function parse(CallExpression $node)
1919
{
20-
if ($this->context->name) {
20+
if ($this->context->methodName) {
2121
return $this->context;
2222
}
2323

2424
if ($node->callableExpression instanceof QualifiedName) {
25-
$this->context->name = (string) ($node->callableExpression->getResolvedName() ?? $node->callableExpression->getText());
25+
$this->context->methodName = (string) ($node->callableExpression->getResolvedName() ?? $node->callableExpression->getText());
2626
}
2727

2828
$this->context->autocompleting = $node->closeParen instanceof MissingToken;

app/Parsers/ClassDeclarationParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ClassDeclarationParser extends AbstractParser
1515

1616
public function parse(ClassDeclaration $node)
1717
{
18-
$this->context->name = (string) $node->getNamespacedName();
18+
$this->context->className = (string) $node->getNamespacedName();
1919

2020
return $this->context;
2121
}

0 commit comments

Comments
 (0)