1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00

ability to debounce onchange events for large projects

This commit is contained in:
Andrew Nagy 2022-03-04 18:04:19 +00:00
parent ceb7b95169
commit e509aad263
3 changed files with 70 additions and 8 deletions

View File

@ -85,6 +85,7 @@ final class LanguageServer
'enable-provide-signature-help::',
'enable-provide-definition::',
'show-diagnostic-warnings::',
'on-change-debounce-ms::',
'use-extended-diagnostic-codes',
'verbose'
];
@ -209,6 +210,9 @@ Options:
--use-extended-diagnostic-codes (DEPRECATED)
Enables sending help uri links with the code in diagnostic messages.
--on-change-debounce-ms=[INT]
The number of milliseconds to debounce onChange events.
--verbose
Will send log messages to the client with information.
@ -308,6 +312,10 @@ HELP;
$clientConfiguration->onchangeLineLimit = (int) $options['disable-on-change'];
}
if (isset($options['on-change-debounce-ms'])) {
$clientConfiguration->onChangeDebounceMs = (int) $options['on-change-debounce-ms'];
}
$clientConfiguration->provideDefinition = !isset($options['enable-provide-definition'])
|| !is_string($options['enable-provide-definition'])
|| strtolower($options['enable-provide-definition']) !== 'false';

View File

@ -106,6 +106,29 @@ class ClientConfiguration
*/
public $onchangeLineLimit;
/**
* Debounce time in milliseconds for onChange events
*
* @var int|null
*
*/
public $onChangeDebounceMs;
/**
* Undocumented function
*
* @param boolean $hideWarnings
* @param boolean|null $provideCompletion
* @param boolean|null $provideDefinition
* @param boolean|null $provideHover
* @param boolean|null $provideSignatureHelp
* @param boolean|null $provideCodeActions
* @param boolean|null $provideDiagnostics
* @param boolean|null $findUnusedVariables
* @param 'always'|'auto'|null $findUnusedCode
* @param integer|null $logLevel
* @param integer|null $onchangeLineLimit
*/
public function __construct(
bool $hideWarnings = true,
bool $provideCompletion = null,
@ -113,6 +136,11 @@ class ClientConfiguration
bool $provideHover = null,
bool $provideSignatureHelp = null,
bool $provideCodeActions = null,
bool $provideDiagnostics = null,
bool $findUnusedVariables = null,
string $findUnusedCode = null,
int $logLevel = null,
int $onchangeLineLimit = null,
) {
$this->hideWarnings = $hideWarnings;
$this->provideCompletion = $provideCompletion;
@ -120,5 +148,10 @@ class ClientConfiguration
$this->provideHover = $provideHover;
$this->provideSignatureHelp = $provideSignatureHelp;
$this->provideCodeActions = $provideCodeActions;
$this->provideDiagnostics = $provideDiagnostics;
$this->findUnusedVariables = $findUnusedVariables;
$this->findUnusedCode = $findUnusedCode;
$this->logLevel = $logLevel;
$this->onchangeLineLimit = $onchangeLineLimit;
}
}

View File

@ -141,6 +141,12 @@ class LanguageServer extends Dispatcher
*/
protected $codebase;
/**
* The AMP Delay token
* @var string
*/
protected $versionedAnalysisDelayToken = '';
public function __construct(
ProtocolReader $reader,
ProtocolWriter $writer,
@ -627,7 +633,7 @@ class LanguageServer extends Dispatcher
*/
public function queueChangeFileAnalysis(string $file_path, string $uri, ?int $version = null): void
{
$this->doVersionedAnalysis([$file_path => $uri], $version);
$this->doVersionedAnalysisDebounce([$file_path => $uri], $version);
}
/**
@ -681,6 +687,25 @@ class LanguageServer extends Dispatcher
$this->doVersionedAnalysis($opened);
}
/**
* Debounced Queue File Analysis with optional version
*
* @param array<string, string> $files
* @param int|null $version
*/
public function doVersionedAnalysisDebounce(array $files, ?int $version = null): void
{
Loop::cancel($this->versionedAnalysisDelayToken);
if ($this->client->clientConfiguration->onChangeDebounceMs === null) {
$this->doVersionedAnalysis($files, $version);
} else {
$this->versionedAnalysisDelayToken = Loop::delay(
$this->client->clientConfiguration->onChangeDebounceMs,
fn() => $this->doVersionedAnalysis($files, $version)
);
}
}
/**
* Queue File Analysis with optional version
*
@ -689,13 +714,9 @@ class LanguageServer extends Dispatcher
*/
public function doVersionedAnalysis(array $files, ?int $version = null): void
{
Loop::cancel($this->versionedAnalysisDelayToken);
try {
if (empty($files)) {
$this->logWarning("No versioned analysis to do.");
return;
}
$this->logDebug("Reloading Files");
$this->logDebug("Doing Analysis from version: $version");
$this->codebase->reloadFiles(
$this->project_analyzer,
array_keys($files)
@ -705,7 +726,7 @@ class LanguageServer extends Dispatcher
array_combine(array_keys($files), array_keys($files))
);
$this->logDebug("Analyze Files");
$this->logDebug("Reloading Files");
$this->codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
$this->emitVersionedIssues($files, $version);