1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

feat(LSP): Improve the autocomplete for nested array shape

This commit is contained in:
ging-dev 2023-09-26 01:52:23 +07:00
parent 7d8d74d32a
commit 45807e27b8
2 changed files with 44 additions and 0 deletions

View File

@ -480,6 +480,17 @@ class ArrayFetchAnalyzer
$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) {

View File

@ -1380,6 +1380,39 @@ class CompletionTest extends TestCase
$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
{