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

Merge pull request #10230 from ging-dev/lsp

feat(LSP): Improve the autocomplete for nested array shape
This commit is contained in:
orklah 2023-09-28 23:42:02 +02:00 committed by GitHub
commit 2d3a625379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
{