-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will use `$/progress` when available and fall back to old telemetry-based reporting otherwise
- Loading branch information
Showing
5 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Psalm/Internal/LanguageServer/Client/Progress/LegacyProgress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
use LanguageServerProtocol\LogMessage; | ||
use LanguageServerProtocol\MessageType; | ||
use LogicException; | ||
use Psalm\Internal\LanguageServer\ClientHandler; | ||
|
||
final class LegacyProgress implements ProgressInterface | ||
Check failure on line 10 in src/Psalm/Internal/LanguageServer/Client/Progress/LegacyProgress.php
|
||
{ | ||
private ClientHandler $handler; | ||
private ?string $title = null; | ||
|
||
public function __construct(ClientHandler $handler) | ||
{ | ||
$this->handler = $handler; | ||
} | ||
|
||
public function begin(string $title, ?string $message = null, ?int $percentage): void | ||
{ | ||
if ($this->title !== null) { | ||
throw new LogicException('Progress has already been started'); | ||
} | ||
|
||
$this->title = $title; | ||
|
||
$this->notify($message); | ||
} | ||
|
||
public function update(?string $message = null, ?int $percentage): void | ||
{ | ||
if ($this->title === null) { | ||
throw new LogicException('The progress has not been started yet'); | ||
} | ||
|
||
$this->notify($message); | ||
} | ||
|
||
public function end(?string $message): void | ||
{ | ||
if ($this->title === null) { | ||
throw new LogicException('The progress has not been started yet'); | ||
} | ||
|
||
$this->notify($message); | ||
} | ||
|
||
private function notify(?string $message): void | ||
{ | ||
$this->handler->notify( | ||
'telemetry/event', | ||
new LogMessage( | ||
$this->title . (empty($message) ? '' : (': ' . $message)), | ||
Check failure on line 54 in src/Psalm/Internal/LanguageServer/Client/Progress/LegacyProgress.php
|
||
MessageType::INFO, | ||
Check failure on line 55 in src/Psalm/Internal/LanguageServer/Client/Progress/LegacyProgress.php
|
||
), | ||
); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/Psalm/Internal/LanguageServer/Client/Progress/Progress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
use LogicException; | ||
use Psalm\Internal\LanguageServer\ClientHandler; | ||
|
||
/** @internal */ | ||
final class Progress implements ProgressInterface | ||
{ | ||
private ClientHandler $handler; | ||
private string $token; | ||
private bool $withPercentage = false; | ||
private bool $finished = false; | ||
|
||
public function __construct(ClientHandler $handler, string $token) | ||
{ | ||
$this->handler = $handler; | ||
$this->token = $token; | ||
} | ||
|
||
public function begin( | ||
string $title, | ||
?string $message = null, | ||
?int $percent = null | ||
Check failure on line 25 in src/Psalm/Internal/LanguageServer/Client/Progress/Progress.php
|
||
): void { | ||
if ($this->finished) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
$notification = [ | ||
'token' => $this->token, | ||
'value' => [ | ||
'kind' => 'begin', | ||
'title' => $title, | ||
], | ||
]; | ||
if ($message !== null) { | ||
$notification['value']['message'] = $message; | ||
} | ||
|
||
if ($percent !== null) { | ||
$notification['value']['percentage'] = $percent; | ||
$this->withPercentage = true; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
} | ||
|
||
public function end(?string $message = null): void | ||
{ | ||
if ($this->finished) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
$notification = [ | ||
'token' => $this->token, | ||
'value' => [ | ||
'kind' => 'end', | ||
], | ||
]; | ||
|
||
if ($message !== null) { | ||
$notification['value']['message'] = $message; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
|
||
$this->finished = true; | ||
} | ||
|
||
public function update(?string $message = null, ?int $percentage = null): void | ||
{ | ||
if ($this->finished) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
$notification = [ | ||
'token' => $this->token, | ||
'value' => [ | ||
'kind' => 'report', | ||
], | ||
]; | ||
|
||
if ($message !== null) { | ||
$notification['value']['message'] = $message; | ||
} | ||
|
||
if ($percentage !== null) { | ||
if ($this->withPercentage) { | ||
throw new LogicException( | ||
'Cannot update percentage for progress ' | ||
. 'that was started with percentage', | ||
); | ||
} | ||
$notification['value']['percentage'] = $percentage; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Psalm/Internal/LanguageServer/Client/Progress/ProgressInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
interface ProgressInterface | ||
{ | ||
public function begin( | ||
string $title, | ||
?string $message = null, | ||
?int $percentage | ||
): void; | ||
|
||
public function update(?string $message = null, ?int $percentage): void; | ||
public function end(?string $message): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters