Skip to content

Commit

Permalink
Merge pull request #10230 from ging-dev/lsp
Browse files Browse the repository at this point in the history
feat(LSP): Improve the autocomplete for nested array shape
  • Loading branch information
orklah authored Sep 28, 2023
2 parents f570886 + 45807e2 commit 2d3a625
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ public static function getArrayAccessTypeGivenOffset(

$key_values = [];

if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeType(
$statements_analyzer->getFilePath(),
$stmt->var,
$array_type->getId(),
);
}

if ($stmt->dim instanceof PhpParser\Node\Scalar\String_) {
$value_type = Type::getAtomicStringFromLiteral($stmt->dim->value);
if ($value_type instanceof TLiteralString) {
Expand Down
33 changes: 33 additions & 0 deletions tests/LanguageServer/CompletionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,39 @@ public function testCompletionOnArrayKey(): void
$this->assertCount(2, $completion_items);
}

public function testCompletionOnNestedArrayKey(): void
{
$codebase = $this->codebase;
$config = $codebase->config;
$config->throw_exception = false;

$this->addFile(
'somefile.php',
'<?php
$my_array = ["foo" => ["bar" => 1]];
$my_array["foo"][]
',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());

$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(2, 33));
$this->assertSame(
[
'array{bar: 1}',
'[',
92,
],
$completion_data,
);

$completion_items = $codebase->getCompletionItemsForArrayKeys($completion_data[0]);

$this->assertCount(1, $completion_items);
}

public function testTypeContextForFunctionArgument(): void
{

Expand Down

0 comments on commit 2d3a625

Please sign in to comment.