Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
Add support for sort text
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jan 2, 2021
1 parent 89eae53 commit 465edc1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"phly/phly-event-dispatcher": "^1.0",
"phpactor/code-transform": "^0.4.0",
"phpactor/code-transform-extension": "^0.2.1",
"phpactor/completion": "~0.4.2",
"phpactor/completion": "~0.4.3",
"phpactor/completion-extension": "~0.2.2",
"phpactor/completion-worse-extension": "^0.2.0",
"phpactor/console-extension": "~0.1",
Expand Down
14 changes: 12 additions & 2 deletions lib/LanguageServerCompletion/Handler/CompletionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public function completion(CompletionParams $params, CancellationToken $token):
: InsertTextFormat::PLAIN_TEXT
;
}

$items[] = CompletionItem::fromArray([
$items[] = $i = CompletionItem::fromArray([
'label' => $name,
'kind' => PhpactorToLspCompletionType::fromPhpactorType($suggestion->type()),
'detail' => $this->formatShortDescription($suggestion),
'documentation' => $suggestion->documentation(),
'insertText' => $insertText,
'sortText' => $this->sortText($suggestion),
'textEdit' => $this->textEdit($suggestion, $textDocument),
'command' => $this->command($textDocument->uri, $byteOffset, $suggestion),
'insertTextFormat' => $insertTextFormat
Expand Down Expand Up @@ -191,4 +192,13 @@ private function formatShortDescription(Suggestion $suggestion): string

return $prefix . $suggestion->shortDescription();
}

private function sortText(Suggestion $suggestion): ?string
{
if (null === $suggestion->priority()) {
return null;
}

return sprintf('%04s-%s', $suggestion->priority(), $suggestion->name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public static function fromPhpactorType(?string $suggestionType): ?int
case Suggestion::TYPE_REFERENCE:
return CompletionItemKind::REFERENCE;
case Suggestion::TYPE_CONSTANT:
return 21; // not currently in the LSP protocol library
return CompletionItemKind::CONSTANT;
case Suggestion::TYPE_FIELD:
return CompletionItemKind::FIELD;
default:
return null;
endswitch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private function updateDocument(TextDocument $textDocument): void

asyncCall(function () {
yield delay($this->updateInterval);

$this->waiting = false;

if (null === $this->documentToUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,44 @@ public function testHandleSuggestionsWithSnippetsWhenClientDoesNotSupportIt()
$this->assertFalse($response->result->isIncomplete);
}

public function testHandleSuggestionsWithProiority(): void
{
$tester = $this->create([
Suggestion::createWithOptions('hello', [
'type' => Suggestion::TYPE_METHOD,
'label' => 'hello',
'priority' => Suggestion::PRIORITY_HIGH
]),
Suggestion::createWithOptions('goodbye', [
'type' => Suggestion::TYPE_METHOD,
'snippet' => 'goodbye()',
'priority' => Suggestion::PRIORITY_LOW
]),
Suggestion::createWithOptions('$var', [
'type' => Suggestion::TYPE_VARIABLE,
]),
], false);

$response = $tester->requestAndWait(
'textDocument/completion',
[
'textDocument' => ProtocolFactory::textDocumentIdentifier(self::EXAMPLE_URI),
'position' => ProtocolFactory::position(0, 0)
]
);

$this->assertEquals([
self::completionItem('hello', 2, [
'sortText' => '0064-hello',
]),
self::completionItem('goodbye', 2, [
'sortText' => '0255-goodbye',
]),
self::completionItem('var', 6),
], $response->result->items);
$this->assertFalse($response->result->isIncomplete);
}

private static function completionItem(
string $label,
?int $type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ class PhpactorToLspCompletionTypeTest extends TestCase
public function testConverts()
{
$reflection = new ReflectionClass(Suggestion::class);
foreach ($reflection->getConstants() as $constantValue) {
$this->assertNotNull(PhpactorToLspCompletionType::fromPhpactorType($constantValue));
foreach ($reflection->getConstants() as $name => $constantValue) {
if (0 !== strpos($name, 'TYPE')) {
continue;
}
$this->assertNotNull(PhpactorToLspCompletionType::fromPhpactorType($constantValue), $constantValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Phpactor\Extension\LanguageServerWorseReflection\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\Executor;
use PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Phpactor\Extension\LanguageServerCompletion\Tests\IntegrationTestCase;
Expand All @@ -13,7 +12,6 @@
/**
* @BeforeMethods({"setUp"})
* @OutputTimeUnit("milliseconds")
* @Executor("local")
*/
class WorkspaceIndexBench extends IntegrationTestCase
{
Expand Down

0 comments on commit 465edc1

Please sign in to comment.