Skip to content

Commit

Permalink
Fix auto comletion by partial property or method
Browse files Browse the repository at this point in the history
  • Loading branch information
issidorov committed Oct 26, 2023
1 parent 2726ad4 commit d6faff2
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 2 deletions.
47 changes: 46 additions & 1 deletion src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,9 @@ public function getCompletionDataAtPosition(string $file_path, Position $positio

$offset = $position->toOffset($file_contents);

$literal_part = $this->getBeginedLiteralPart($file_path, $position);
$begin_literal_offset = $offset - strlen($literal_part);

[$reference_map, $type_map] = $this->analyzer->getMapsForFile($file_path);

if (!$reference_map && !$type_map) {
Expand Down Expand Up @@ -1776,7 +1779,7 @@ public function getCompletionDataAtPosition(string $file_path, Position $positio
}
}

if ($offset - $end_pos === 2 || $offset - $end_pos === 3) {
if ($begin_literal_offset - $end_pos === 2) {
$candidate_gap = substr($file_contents, $end_pos, 2);

if ($candidate_gap === '->' || $candidate_gap === '::') {
Expand All @@ -1801,6 +1804,11 @@ public function getCompletionDataAtPosition(string $file_path, Position $positio
return [$possible_reference, '::', $offset];
}

if ($offset <= $end_pos && substr($file_contents, $begin_literal_offset - 2, 2) === '::') {
$class_name = explode('::', $possible_reference)[0];
return [$class_name, '::', $offset];
}

// Only continue for references that are partial / don't exist.
if ($possible_reference[0] !== '*') {
continue;
Expand All @@ -1816,6 +1824,23 @@ public function getCompletionDataAtPosition(string $file_path, Position $positio
return null;
}

public function getBeginedLiteralPart(string $file_path, Position $position): string
{
$is_open = $this->file_provider->isOpen($file_path);

if (!$is_open) {
throw new UnanalyzedFileException($file_path . ' is not open');
}

$file_contents = $this->getFileContents($file_path);

$offset = $position->toOffset($file_contents);

preg_match('/\$?\w+$/', substr($file_contents, 0, $offset), $matches);

return $matches[0] ?? '';
}

public function getTypeContextAtPosition(string $file_path, Position $position): ?Union
{
$file_contents = $this->getFileContents($file_path);
Expand Down Expand Up @@ -1957,6 +1982,26 @@ public function getCompletionItemsForClassishThing(
return $completion_items;
}

/**
* @param list<CompletionItem> $items
* @return list<CompletionItem>
*/
public function filterCompletionItemsByBeginLiteralPart(array $items, string $literal_part): array
{
if (!$literal_part) {
return $items;
}

$res = [];
foreach ($items as $item) {
if ($item->insertText && strpos($item->insertText, $literal_part) === 0) {
$res[] = $item;
}
}

return $res;
}

/**
* @return list<CompletionItem>
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Internal/LanguageServer/Server/TextDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public function completion(TextDocumentIdentifier $textDocument, Position $posit

try {
$completion_data = $this->codebase->getCompletionDataAtPosition($file_path, $position);
$literal_part = $this->codebase->getBeginedLiteralPart($file_path, $position);
if ($completion_data) {
[$recent_type, $gap, $offset] = $completion_data;

Expand All @@ -305,6 +306,8 @@ public function completion(TextDocumentIdentifier $textDocument, Position $posit
->textDocument->completion->completionItem->snippetSupport ?? false;
$completion_items =
$this->codebase->getCompletionItemsForClassishThing($recent_type, $gap, $snippetSupport);
$completion_items =
$this->codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
} elseif ($gap === '[') {
$completion_items = $this->codebase->getCompletionItemsForArrayKeys($recent_type);
} else {
Expand Down
198 changes: 197 additions & 1 deletion tests/LanguageServer/CompletionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psalm\Tests\TestConfig;
use Psalm\Type;

use function array_map;
use function count;

class CompletionTest extends TestCase
Expand Down Expand Up @@ -370,7 +371,7 @@ public function foo() : void {
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());

$this->assertNull($codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 41)));
$this->assertSame(['B\C', '->', 456], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 41)));
}

public function testCompletionOnTemplatedThisProperty(): void
Expand Down Expand Up @@ -725,6 +726,201 @@ public function baz() {}
$this->assertSame('baz()', $completion_items[1]->insertText);
}

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

$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public $aProp = 123;
public $bProp = 234;
public function bar() {
$this->aPr
}
}',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();

$this->analyzeFile('somefile.php', new Context());

$position = new Position(8, 34);
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', $position);
$literal_part = $codebase->getBeginedLiteralPart('somefile.php', $position);

$this->assertSame(['B\A&static', '->', 223], $completion_data);

$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1], true);
$completion_items = $codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
$completion_item_texts = array_map(fn($item) => $item->insertText, $completion_items);

$this->assertSame(['aProp'], $completion_item_texts);
}

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

$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public $aProp1 = 123;
public $aProp2 = 234;
public function bar() {
$this->aProp2;
}
}',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();

$this->analyzeFile('somefile.php', new Context());

$position = new Position(8, 34);
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', $position);
$literal_part = $codebase->getBeginedLiteralPart('somefile.php', $position);

$this->assertSame(['B\A&static', '->', 225], $completion_data);

$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1], true);
$completion_items = $codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
$completion_item_texts = array_map(fn($item) => $item->insertText, $completion_items);

$this->assertSame(['aProp1', 'aProp2'], $completion_item_texts);
}

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

$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public static $aProp = 123;
public static $bProp = 234;
public function bar() {
self::$aPr
}
}',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();

$this->analyzeFile('somefile.php', new Context());

$position = new Position(8, 34);
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', $position);
$literal_part = $codebase->getBeginedLiteralPart('somefile.php', $position);

$this->assertSame(['B\A', '::', 237], $completion_data);

$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1], true);
$completion_items = $codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
$completion_item_texts = array_map(fn($item) => $item->insertText, $completion_items);

$this->assertSame(['$aProp'], $completion_item_texts);
}

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

$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public static $aProp = 123;
public static $bProp = 234;
public function bar() {
static::$aPr
}
}',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();

$this->analyzeFile('somefile.php', new Context());

$position = new Position(8, 36);
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', $position);
$literal_part = $codebase->getBeginedLiteralPart('somefile.php', $position);

$this->assertSame(['B\A', '::', 239], $completion_data);

$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1], true);
$completion_items = $codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
$completion_item_texts = array_map(fn($item) => $item->insertText, $completion_items);

$this->assertSame(['$aProp'], $completion_item_texts);
}

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

$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public static $aProp1 = 123;
public static $aProp2 = 234;
public function bar() {
self::$aProp2;
}
}',
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();

$this->analyzeFile('somefile.php', new Context());

$position = new Position(8, 34);
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', $position);
$literal_part = $codebase->getBeginedLiteralPart('somefile.php', $position);

$this->assertSame(['B\A', '::', 239], $completion_data);

$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1], true);
$completion_items = $codebase->filterCompletionItemsByBeginLiteralPart($completion_items, $literal_part);
$completion_item_texts = array_map(fn($item) => $item->insertText, $completion_items);

$this->assertSame(['$aProp1', '$aProp2'], $completion_item_texts);
}

public function testCompletionOnNewExceptionWithoutNamespace(): void
{
$codebase = $this->codebase;
Expand Down

0 comments on commit d6faff2

Please sign in to comment.