From 02b6067a980e44a38ec60424bf4a7f3b18c2c5ea Mon Sep 17 00:00:00 2001 From: chemzqm Date: Wed, 31 Oct 2018 17:24:41 +0800 Subject: [PATCH] add StatusBarItem support --- src/server/typescriptServiceClient.ts | 4 +++ src/server/utils/typingsStatus.ts | 9 +++-- src/server/utils/versionStatus.ts | 47 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/server/utils/versionStatus.ts diff --git a/src/server/typescriptServiceClient.ts b/src/server/typescriptServiceClient.ts index 7203def..28b4d64 100644 --- a/src/server/typescriptServiceClient.ts +++ b/src/server/typescriptServiceClient.ts @@ -20,6 +20,7 @@ import { fork, getTempFile, IForkOptions, makeRandomHexString } from './utils/pr import Tracer from './utils/tracer' import { inferredProjectConfig } from './utils/tsconfig' import { TypeScriptVersion, TypeScriptVersionProvider } from './utils/versionProvider' +import VersionStatus from './utils/versionStatus' import { ICallback, Reader } from './utils/wireProtocol' interface CallbackItem { @@ -152,6 +153,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient private cancellationPipeName: string | null = null private requestQueue: RequestQueue private callbacks: CallbackMap + private versionStatus: VersionStatus private readonly _onTsServerStarted = new Emitter() private readonly _onProjectLanguageServiceStateChanged = new Emitter() private readonly _onDidBeginInstallTypings = new Emitter() @@ -175,6 +177,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient this.versionProvider = new TypeScriptVersionProvider(this._configuration) this._apiVersion = API.defaultVersion this.tracer = new Tracer(this.logger) + this.versionStatus = new VersionStatus(this.normalizePath.bind(this)) } private _onDiagnosticsReceived = new Emitter() @@ -315,6 +318,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient } workspace.showMessage(`Using tsserver from: ${currentVersion.path}`) // tslint:disable-line this._apiVersion = currentVersion.version + this.versionStatus.onDidChangeTypeScriptVersion(currentVersion) this.requestQueue = new RequestQueue() this.callbacks = new CallbackMap() this.lastError = null diff --git a/src/server/utils/typingsStatus.ts b/src/server/utils/typingsStatus.ts index 60418dd..d204b40 100644 --- a/src/server/utils/typingsStatus.ts +++ b/src/server/utils/typingsStatus.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { Disposable } from 'vscode-languageserver-protocol' import { ITypeScriptServiceClient } from '../typescriptService' -import { workspace } from 'coc.nvim' +import { workspace, StatusBarItem } from 'coc.nvim' const typingsInstallTimeout = 30 * 1000 @@ -64,8 +64,10 @@ export class AtaProgressReporter { private _promises = new Map() private _disposable: Disposable private _invalid = false + private statusItem: StatusBarItem constructor(client: ITypeScriptServiceClient) { + this.statusItem = workspace.createStatusBarItem(10, { progress: true }) const disposables: Disposable[] = [] disposables.push(client.onDidBeginInstallTypings(e => this._onBegin(e.eventId))) disposables.push(client.onDidEndInstallTypings(e => this._onEndOrTimeout(e.eventId))) @@ -95,10 +97,12 @@ export class AtaProgressReporter { resolve() }) }) - workspace.showMessage('Fetching data for better TypeScript IntelliSense') + this.statusItem.text = 'Fetching data for better TypeScript IntelliSense' + this.statusItem.show() } private _onEndOrTimeout(eventId: number): void { + this.statusItem.hide() const resolve = this._promises.get(eventId) if (resolve) { this._promises.delete(eventId) @@ -107,6 +111,7 @@ export class AtaProgressReporter { } private onTypesInstallerInitializationFailed() { // tslint:disable-line + this.statusItem.hide() if (!this._invalid) { workspace.showMessage('Could not install typings files for JavaScript language features. Please ensure that NPM is installed', 'error') } diff --git a/src/server/utils/versionStatus.ts b/src/server/utils/versionStatus.ts new file mode 100644 index 0000000..646e401 --- /dev/null +++ b/src/server/utils/versionStatus.ts @@ -0,0 +1,47 @@ +import { StatusBarItem, workspace, events } from 'coc.nvim' +import { Disposable } from 'vscode-languageserver-protocol' +import Uri from 'vscode-uri' +import { TypeScriptVersion } from './versionProvider' + +export default class VersionStatus { + private readonly _onChangeEditorSub: Disposable + private readonly _versionBarEntry: StatusBarItem + + constructor( + private readonly _normalizePath: (resource: Uri) => string | null + ) { + this._versionBarEntry = workspace.createStatusBarItem(99) + this._onChangeEditorSub = events.on('BufEnter', this.showHideStatus, this) + this._versionBarEntry.show() + } + + public dispose(): void { + this._versionBarEntry.dispose() + this._onChangeEditorSub.dispose() + } + + public onDidChangeTypeScriptVersion(version: TypeScriptVersion): void { + this._versionBarEntry.text = `TSC ${version.versionString}` + this.showHideStatus().catch(_e => { + // noop + }) + } + + private async showHideStatus(): Promise { + let document = await workspace.document + if (!document) { + this._versionBarEntry.hide() + return + } + + if (['typescript', 'typescriptreact'].indexOf(document.filetype) !== -1) { + if (this._normalizePath(Uri.parse(document.uri))) { + this._versionBarEntry.show() + } else { + this._versionBarEntry.hide() + } + return + } + this._versionBarEntry.hide() + } +}