|
| 1 | +import { |
| 2 | + languages, |
| 3 | + type Disposable, |
| 4 | + window, |
| 5 | + CancellationTokenSource, |
| 6 | + workspace, |
| 7 | +} from 'vscode'; |
| 8 | +import { configResolveRequest, findFilesRequest } from '../lib/requestChannels'; |
| 9 | +import type { FindFilesRequestType } from '../../../shared/requestChannels'; |
| 10 | +import type { LanguageClient } from 'vscode-languageclient/node'; |
| 11 | +import { Commands } from '../../../shared/commands/defs'; |
| 12 | +import type { Command } from 'vscode'; |
| 13 | +import { Uri } from 'vscode'; |
| 14 | +import path from 'path'; |
| 15 | + |
| 16 | +export class ConfigResolveLanguageStatus implements Disposable { |
| 17 | + private _disposables: Disposable[] = []; |
| 18 | + private _languageStatus = languages.createLanguageStatusItem( |
| 19 | + 'phpstan.languageStatusItem', |
| 20 | + [{ language: 'php' }, { pattern: '**/*.neon' }] |
| 21 | + ); |
| 22 | + private _outstandingTokens = new Set<CancellationTokenSource>(); |
| 23 | + |
| 24 | + public constructor(private readonly _client: LanguageClient) { |
| 25 | + this._languageStatus.name = 'PHPStan'; |
| 26 | + this._disposables.push(this._languageStatus); |
| 27 | + |
| 28 | + this._disposables.push( |
| 29 | + _client.onRequest( |
| 30 | + findFilesRequest, |
| 31 | + async (params): Promise<FindFilesRequestType['response']> => { |
| 32 | + return { |
| 33 | + files: (await workspace.findFiles(params.pattern)).map( |
| 34 | + (file) => file.toString() |
| 35 | + ), |
| 36 | + }; |
| 37 | + } |
| 38 | + ) |
| 39 | + ); |
| 40 | + this._disposables.push( |
| 41 | + window.onDidChangeActiveTextEditor((editor) => { |
| 42 | + this._outstandingTokens.forEach((token) => token.cancel()); |
| 43 | + this._outstandingTokens.clear(); |
| 44 | + |
| 45 | + if (!editor) { |
| 46 | + // Should not be visible |
| 47 | + this._setStatus({ |
| 48 | + text: 'PHPStan resolving config...', |
| 49 | + command: undefined, |
| 50 | + busy: true, |
| 51 | + }); |
| 52 | + return; |
| 53 | + } |
| 54 | + void this._update(editor.document.uri); |
| 55 | + }) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + private _setStatus(config: { |
| 60 | + text: string; |
| 61 | + command: Command | undefined; |
| 62 | + busy: boolean; |
| 63 | + }): void { |
| 64 | + this._languageStatus.text = config.text; |
| 65 | + this._languageStatus.command = config.command; |
| 66 | + this._languageStatus.busy = config.busy ?? false; |
| 67 | + } |
| 68 | + |
| 69 | + private async _update(uri: Uri): Promise<void> { |
| 70 | + const cancelToken = new CancellationTokenSource(); |
| 71 | + this._outstandingTokens.add(cancelToken); |
| 72 | + |
| 73 | + this._setStatus({ |
| 74 | + text: 'PHPStan resolving config...', |
| 75 | + command: undefined, |
| 76 | + busy: true, |
| 77 | + }); |
| 78 | + const result = await this._client.sendRequest( |
| 79 | + configResolveRequest, |
| 80 | + { |
| 81 | + uri: uri.toString(), |
| 82 | + }, |
| 83 | + cancelToken.token |
| 84 | + ); |
| 85 | + |
| 86 | + this._languageStatus.busy = false; |
| 87 | + if (result.uri) { |
| 88 | + const configUri = Uri.parse(result.uri); |
| 89 | + this._setStatus({ |
| 90 | + text: path.basename(configUri.fsPath), |
| 91 | + busy: false, |
| 92 | + command: { |
| 93 | + title: 'Open config file', |
| 94 | + command: 'vscode.open', |
| 95 | + arguments: [configUri], |
| 96 | + }, |
| 97 | + }); |
| 98 | + } else { |
| 99 | + this._setStatus({ |
| 100 | + text: 'PHPStan (no config found)', |
| 101 | + busy: false, |
| 102 | + command: { |
| 103 | + title: 'Show output channel', |
| 104 | + command: Commands.SHOW_OUTPUT_CHANNEL, |
| 105 | + arguments: [], |
| 106 | + }, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public dispose(): void { |
| 112 | + this._disposables.forEach((disposable) => void disposable.dispose()); |
| 113 | + } |
| 114 | +} |
0 commit comments