-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10050 from weirdan/lsp-modernize-progress-reporting
Modernize LSP progress reporting
- Loading branch information
Showing
8 changed files
with
260 additions
and
27 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
use LanguageServerProtocol\LogMessage; | ||
use LanguageServerProtocol\MessageType; | ||
use LogicException; | ||
use Psalm\Internal\LanguageServer\ClientHandler; | ||
|
||
/** @internal */ | ||
final class LegacyProgress implements ProgressInterface | ||
{ | ||
private const STATUS_INACTIVE = 'inactive'; | ||
private const STATUS_ACTIVE = 'active'; | ||
private const STATUS_FINISHED = 'finished'; | ||
|
||
private string $status = self::STATUS_INACTIVE; | ||
|
||
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 = null): void | ||
{ | ||
|
||
if ($this->status === self::STATUS_ACTIVE) { | ||
throw new LogicException('Progress has already been started'); | ||
} | ||
|
||
if ($this->status === self::STATUS_FINISHED) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
$this->title = $title; | ||
|
||
$this->notify($message); | ||
|
||
$this->status = self::STATUS_ACTIVE; | ||
} | ||
|
||
public function update(?string $message = null, ?int $percentage = null): void | ||
{ | ||
if ($this->status === self::STATUS_FINISHED) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
if ($this->status === self::STATUS_INACTIVE) { | ||
throw new LogicException('Progress has not been started yet'); | ||
} | ||
|
||
$this->notify($message); | ||
} | ||
|
||
public function end(?string $message = null): void | ||
{ | ||
if ($this->status === self::STATUS_FINISHED) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
if ($this->status === self::STATUS_INACTIVE) { | ||
throw new LogicException('Progress has not been started yet'); | ||
} | ||
|
||
$this->notify($message); | ||
|
||
$this->status = self::STATUS_FINISHED; | ||
} | ||
|
||
private function notify(?string $message): void | ||
{ | ||
$this->handler->notify( | ||
'telemetry/event', | ||
new LogMessage( | ||
MessageType::INFO, | ||
$this->title . (empty($message) ? '' : (': ' . $message)), | ||
), | ||
); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
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,121 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
use LogicException; | ||
use Psalm\Internal\LanguageServer\ClientHandler; | ||
|
||
/** @internal */ | ||
final class Progress implements ProgressInterface | ||
{ | ||
private const STATUS_INACTIVE = 'inactive'; | ||
private const STATUS_ACTIVE = 'active'; | ||
private const STATUS_FINISHED = 'finished'; | ||
|
||
private string $status = self::STATUS_INACTIVE; | ||
|
||
private ClientHandler $handler; | ||
private string $token; | ||
private bool $withPercentage = false; | ||
|
||
public function __construct(ClientHandler $handler, string $token) | ||
{ | ||
$this->handler = $handler; | ||
$this->token = $token; | ||
} | ||
|
||
public function begin( | ||
string $title, | ||
?string $message = null, | ||
?int $percentage = null | ||
): void { | ||
if ($this->status === self::STATUS_ACTIVE) { | ||
throw new LogicException('Progress has already been started'); | ||
} | ||
|
||
if ($this->status === self::STATUS_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 ($percentage !== null) { | ||
$notification['value']['percentage'] = $percentage; | ||
$this->withPercentage = true; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
|
||
$this->status = self::STATUS_ACTIVE; | ||
} | ||
|
||
public function end(?string $message = null): void | ||
{ | ||
if ($this->status === self::STATUS_FINISHED) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
if ($this->status === self::STATUS_INACTIVE) { | ||
throw new LogicException('Progress has not been started yet'); | ||
} | ||
|
||
$notification = [ | ||
'token' => $this->token, | ||
'value' => [ | ||
'kind' => 'end', | ||
], | ||
]; | ||
|
||
if ($message !== null) { | ||
$notification['value']['message'] = $message; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
|
||
$this->status = self::STATUS_FINISHED; | ||
} | ||
|
||
public function update(?string $message = null, ?int $percentage = null): void | ||
{ | ||
if ($this->status === self::STATUS_FINISHED) { | ||
throw new LogicException('Progress has already been finished'); | ||
} | ||
|
||
if ($this->status === self::STATUS_INACTIVE) { | ||
throw new LogicException('Progress has not been started yet'); | ||
} | ||
|
||
$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 without percentage', | ||
); | ||
} | ||
$notification['value']['percentage'] = $percentage; | ||
} | ||
|
||
$this->handler->notify('$/progress', $notification); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\LanguageServer\Client\Progress; | ||
|
||
/** @internal */ | ||
interface ProgressInterface | ||
{ | ||
public function begin( | ||
string $title, | ||
?string $message = null, | ||
?int $percentage = null | ||
): void; | ||
|
||
public function update(?string $message = null, ?int $percentage = null): void; | ||
public function end(?string $message = null): 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
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
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