From 67081c4abeff23a23525d635c6ca20991e11d996 Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Thu, 18 May 2017 14:26:34 -0700 Subject: [PATCH] Clean up completions, fix exceptions thrown from running strpos on empty strings --- src/CompletionProvider.php | 33 ++++++++++++++------------------- src/utils.php | 4 ++++ tests/Utils/UtilsTest.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 tests/Utils/UtilsTest.php diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index a5de029..cd78e61 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -13,6 +13,7 @@ use LanguageServer\Protocol\{ CompletionItem, CompletionItemKind }; +use function LanguageServer\{strStartsWith}; use Microsoft\PhpParser as Tolerant; class CompletionProvider @@ -245,10 +246,8 @@ class CompletionProvider } foreach ($this->index->getDefinitions() as $fqn => $def) { - if ( - ($def->canBeInstantiated || ($def->isGlobal && !isset($creation))) && (empty($prefix) || strpos($fqn, $prefix) !== false) - - ) { + $fqnStartsWithPrefix = strStartsWith($fqn, $prefix); + if (($def->canBeInstantiated || ($def->isGlobal && !isset($creation))) && strStartsWith($fqn, $prefix)) { if ($namespaceDefinition !== null && $namespaceDefinition->name !== null) { $namespacePrefix = (string)Tolerant\ResolvedName::buildName($namespaceDefinition->name->nameParts, $node->getFileContents()); @@ -257,7 +256,7 @@ class CompletionProvider $isNotFullyQualified = !($class instanceof Tolerant\Node\QualifiedName) || !$class->isFullyQualifiedName(); if ($isNotFullyQualified) { foreach ($namespaceImportTable as $alias => $name) { - if (strpos($fqn, $name) === 0) { + if (strStartsWith($fqn, $name)) { $fqn = $alias; $isAliased = true; break; @@ -266,13 +265,13 @@ class CompletionProvider } - if (!$isNotFullyQualified && ((strpos($fqn, $prefix) === 0) || strpos($fqn, $namespacePrefix . "\\" . $prefix) === 0)) { - $fqn = $fqn; + if (!$isNotFullyQualified && ($fqnStartsWithPrefix || strStartsWith($fqn, $namespacePrefix . "\\" . $prefix))) { + // $fqn = $fqn; } elseif (!$isAliased && !array_search($fqn, array_values($namespaceImportTable))) { if (empty($prefix)) { $fqn = '\\' . $fqn; - } elseif (strpos($fqn, $namespacePrefix . "\\" . $prefix) === 0) { + } elseif (strStartsWith($fqn, $namespacePrefix . "\\" . $prefix)) { $fqn = substr($fqn, strlen($namespacePrefix) + 1); } else { continue; @@ -280,7 +279,7 @@ class CompletionProvider } elseif (!$isAliased) { continue; } - } elseif (strpos($fqn, $prefix) === 0 && $class->isFullyQualifiedName()) { + } elseif ($fqnStartsWithPrefix && $class->isFullyQualifiedName()) { $fqn = '\\' . $fqn; } @@ -293,21 +292,17 @@ class CompletionProvider if (!isset($creation)) { foreach (self::KEYWORDS as $keyword) { - if (strpos($keyword, $prefix) === 0) { - $item = new CompletionItem($keyword, CompletionItemKind::KEYWORD); - $item->insertText = $keyword . ' '; - $list->items[] = $item; - } + $item = new CompletionItem($keyword, CompletionItemKind::KEYWORD); + $item->insertText = $keyword . ' '; + $list->items[] = $item; } } } elseif (TolerantParserHelpers::isConstantFetch($node)) { $prefix = (string) ($node->getResolvedName() ?? Tolerant\ResolvedName::buildName($node->nameParts, $node->getFileContents())); foreach (self::KEYWORDS as $keyword) { - if (strpos($keyword, $prefix) === 0) { - $item = new CompletionItem($keyword, CompletionItemKind::KEYWORD); - $item->insertText = $keyword . ' '; - $list->items[] = $item; - } + $item = new CompletionItem($keyword, CompletionItemKind::KEYWORD); + $item->insertText = $keyword . ' '; + $list->items[] = $item; } } diff --git a/src/utils.php b/src/utils.php index c0c5bf7..8c854d3 100644 --- a/src/utils.php +++ b/src/utils.php @@ -189,3 +189,7 @@ function getVendorDir(\stdClass $composerJson = null): string { return $composerJson->config->{'vendor-dir'} ?? 'vendor'; } + +function strStartsWith(string $haystack, string $prefix): bool { + return empty($prefix) || strpos($haystack, $prefix) === 0; +} diff --git a/tests/Utils/UtilsTest.php b/tests/Utils/UtilsTest.php new file mode 100644 index 0000000..d225ff2 --- /dev/null +++ b/tests/Utils/UtilsTest.php @@ -0,0 +1,34 @@ +assertEquals(strStartsWith($haystack, $prefix), $expectedResult); + } +}