Inline strStartsWith and remove it
parent
9ad3c5aef6
commit
4b573acde1
|
@ -12,7 +12,6 @@ use LanguageServer\Protocol\{
|
||||||
CompletionItem,
|
CompletionItem,
|
||||||
CompletionItemKind
|
CompletionItemKind
|
||||||
};
|
};
|
||||||
use function LanguageServer\strStartsWith;
|
|
||||||
use Microsoft\PhpParser;
|
use Microsoft\PhpParser;
|
||||||
use Microsoft\PhpParser\Node;
|
use Microsoft\PhpParser\Node;
|
||||||
|
|
||||||
|
@ -248,7 +247,7 @@ class CompletionProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->index->getDefinitions() as $fqn => $def) {
|
foreach ($this->index->getDefinitions() as $fqn => $def) {
|
||||||
$fqnStartsWithPrefix = strStartsWith($fqn, $prefix);
|
$fqnStartsWithPrefix = substr($fqn, 0, strlen($prefix)) === $prefix;
|
||||||
$fqnContainsPrefix = empty($prefix) || strpos($fqn, $prefix) !== false;
|
$fqnContainsPrefix = empty($prefix) || strpos($fqn, $prefix) !== false;
|
||||||
if (($def->canBeInstantiated || ($def->isGlobal && !isset($creation))) && $fqnContainsPrefix) {
|
if (($def->canBeInstantiated || ($def->isGlobal && !isset($creation))) && $fqnContainsPrefix) {
|
||||||
if ($namespaceDefinition !== null && $namespaceDefinition->name !== null) {
|
if ($namespaceDefinition !== null && $namespaceDefinition->name !== null) {
|
||||||
|
@ -259,7 +258,7 @@ class CompletionProvider
|
||||||
$isNotFullyQualified = !($class instanceof Node\QualifiedName) || !$class->isFullyQualifiedName();
|
$isNotFullyQualified = !($class instanceof Node\QualifiedName) || !$class->isFullyQualifiedName();
|
||||||
if ($isNotFullyQualified) {
|
if ($isNotFullyQualified) {
|
||||||
foreach ($namespaceImportTable as $alias => $name) {
|
foreach ($namespaceImportTable as $alias => $name) {
|
||||||
if (strStartsWith($fqn, $name)) {
|
if (substr($fqn, 0, strlen($name)) === $name) {
|
||||||
$fqn = $alias;
|
$fqn = $alias;
|
||||||
$isAliased = true;
|
$isAliased = true;
|
||||||
break;
|
break;
|
||||||
|
@ -267,12 +266,13 @@ class CompletionProvider
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$prefixWithNamespace = $namespacePrefix . "\\" . $prefix;
|
||||||
$isFullyQualifiedAndPrefixMatches = !$isNotFullyQualified && ($fqnStartsWithPrefix || strStartsWith($fqn, $namespacePrefix . "\\" . $prefix));
|
$fqnMatchesPrefixWithNamespace = substr($fqn, 0, strlen($prefixWithNamespace)) === $prefixWithNamespace;
|
||||||
|
$isFullyQualifiedAndPrefixMatches = !$isNotFullyQualified && ($fqnStartsWithPrefix || $fqnMatchesPrefixWithNamespace);
|
||||||
if (!$isFullyQualifiedAndPrefixMatches && !$isAliased && !array_search($fqn, array_values($namespaceImportTable))) {
|
if (!$isFullyQualifiedAndPrefixMatches && !$isAliased && !array_search($fqn, array_values($namespaceImportTable))) {
|
||||||
if (empty($prefix)) {
|
if (empty($prefix)) {
|
||||||
$fqn = '\\' . $fqn;
|
$fqn = '\\' . $fqn;
|
||||||
} elseif (strStartsWith($fqn, $namespacePrefix . "\\" . $prefix)) {
|
} elseif ($fqnMatchesPrefixWithNamespace) {
|
||||||
$fqn = substr($fqn, strlen($namespacePrefix) + 1);
|
$fqn = substr($fqn, strlen($namespacePrefix) + 1);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -171,8 +171,3 @@ function getVendorDir(\stdClass $composerJson = null): string
|
||||||
{
|
{
|
||||||
return $composerJson->config->{'vendor-dir'} ?? 'vendor';
|
return $composerJson->config->{'vendor-dir'} ?? 'vendor';
|
||||||
}
|
}
|
||||||
|
|
||||||
function strStartsWith(string $haystack, string $prefix): bool
|
|
||||||
{
|
|
||||||
return substr($haystack, 0, strlen($prefix)) === $prefix;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
<?php
|
|
||||||
declare(strict_types = 1);
|
|
||||||
|
|
||||||
namespace LanguageServer\Tests\Utils;
|
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
use function LanguageServer\strStartsWith;
|
|
||||||
|
|
||||||
class UtilsTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testStrStartsWithDataProvider(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
['a', 'b', false],
|
|
||||||
['', 'a', false],
|
|
||||||
['foobar', 'bar', false],
|
|
||||||
|
|
||||||
['a', '', true],
|
|
||||||
['', '', true],
|
|
||||||
|
|
||||||
['foobar', 'foob', true],
|
|
||||||
['foobar', 'f', true],
|
|
||||||
['FOOBAR', 'foo', false],
|
|
||||||
['foobar', 'foobar', true]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider testStrStartsWithDataProvider
|
|
||||||
*/
|
|
||||||
public function testStrStartsWith($haystack, $prefix, $expectedResult)
|
|
||||||
{
|
|
||||||
$this->assertEquals(strStartsWith($haystack, $prefix), $expectedResult);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue