|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { |
| 4 | + commands, |
| 5 | + languages, |
| 6 | + StatusBarAlignment, |
| 7 | + StatusBarItem, |
| 8 | + TextDocument, |
| 9 | + TextEditor, |
| 10 | + ThemeColor, |
| 11 | + window, |
| 12 | + workspace, |
| 13 | +} from 'vscode'; |
| 14 | + |
| 15 | +import { LanguageClient, State } from 'vscode-languageclient'; |
| 16 | + |
| 17 | +enum Status { |
| 18 | + init = 1, |
| 19 | + ok = 2, |
| 20 | + error = 3, |
| 21 | +} |
| 22 | + |
| 23 | +interface IStatusBarItemConfig { |
| 24 | + icon: string; |
| 25 | + tooltip: string; |
| 26 | + color: string; |
| 27 | +} |
| 28 | + |
| 29 | +const STATUS_BAR_ITEM_NAME = 'GQL'; |
| 30 | +const STATUS_BAR_UI = { |
| 31 | + [Status.init]: { |
| 32 | + icon: 'sync', |
| 33 | + color: 'progressBar.background', |
| 34 | + tooltip: 'Graphql language server is initializing.', |
| 35 | + }, |
| 36 | + [Status.ok]: { |
| 37 | + icon: 'plug', |
| 38 | + color: 'statusBar.foreground', |
| 39 | + tooltip: 'Graphql language server is running.', |
| 40 | + }, |
| 41 | + [Status.error]: { |
| 42 | + icon: 'stop', |
| 43 | + color: 'editorError.foreground', |
| 44 | + tooltip: 'Graphql language server is not running.', |
| 45 | + }, |
| 46 | +}; |
| 47 | +export default class ClientStatusBarItem { |
| 48 | + private _item: StatusBarItem; |
| 49 | + private _client: LanguageClient; |
| 50 | + private _disposables: Array<{ dispose: () => any }> = []; |
| 51 | + |
| 52 | + constructor(client: LanguageClient) { |
| 53 | + this._item = window.createStatusBarItem(StatusBarAlignment.Right, 0); |
| 54 | + this._client = client; |
| 55 | + |
| 56 | + this._disposables.push(this._item); |
| 57 | + this._disposables.push(this._addOnClickToShowOutputChannel()); |
| 58 | + |
| 59 | + // update status bar depending on client state |
| 60 | + this._setStatus(Status.init); |
| 61 | + this._registerStatusChangeListeners(); |
| 62 | + |
| 63 | + // update visibility of statusBarItem depending on current activeTextEditor |
| 64 | + this._updateVisibility(window.activeTextEditor); |
| 65 | + window.onDidChangeActiveTextEditor(this._updateVisibility); |
| 66 | + } |
| 67 | + |
| 68 | + public dispose() { |
| 69 | + this._disposables.forEach(item => { |
| 70 | + item.dispose(); |
| 71 | + }); |
| 72 | + this._item = null; |
| 73 | + this._client = null; |
| 74 | + } |
| 75 | + |
| 76 | + private _registerStatusChangeListeners() { |
| 77 | + this._client.onDidChangeState(({ oldState, newState }) => { |
| 78 | + if (newState === State.Running) { |
| 79 | + this._setStatus(Status.ok); |
| 80 | + } else if (newState === State.Stopped) { |
| 81 | + this._setStatus(Status.error); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + this._client.onReady().then( |
| 86 | + () => { |
| 87 | + this._setStatus(Status.ok); |
| 88 | + }, |
| 89 | + () => { |
| 90 | + this._setStatus(Status.error); |
| 91 | + }, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private _addOnClickToShowOutputChannel() { |
| 96 | + const commandName = `showOutputChannel-${this._client.outputChannel.name}`; |
| 97 | + const disposable = commands.registerCommand(commandName, () => { |
| 98 | + this._client.outputChannel.show(); |
| 99 | + }); |
| 100 | + this._item.command = commandName; |
| 101 | + return disposable; |
| 102 | + } |
| 103 | + |
| 104 | + private _updateVisibility = (textEditor: TextEditor) => { |
| 105 | + let hide = true; |
| 106 | + |
| 107 | + if (textEditor && this._checkDocumentInsideWorkspace(textEditor.document)) { |
| 108 | + if (this._client.initializeResult) { |
| 109 | + // if client is initialized then show only for file extensions |
| 110 | + // defined in .gqlconfig |
| 111 | + // @TODO: if possible, match against patterns defined in .gqlconfig |
| 112 | + // instead of extensions. |
| 113 | + const extensions = this._client.initializeResult.fileExtensions; |
| 114 | + const score = languages.match( |
| 115 | + { scheme: 'file', pattern: `**/*.{${extensions.join(',')}}` }, |
| 116 | + textEditor.document, |
| 117 | + ); |
| 118 | + hide = score === 0; |
| 119 | + } else { |
| 120 | + // while server is initializing show status bar item |
| 121 | + // for all files inside worspace |
| 122 | + hide = false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + hide ? this._hide() : this._show(); |
| 127 | + }; |
| 128 | + |
| 129 | + private _checkDocumentInsideWorkspace(document: TextDocument): boolean { |
| 130 | + const folder = workspace.getWorkspaceFolder(document.uri); |
| 131 | + return folder && folder.uri.toString() === this._getWorkspace(); |
| 132 | + } |
| 133 | + |
| 134 | + private _getWorkspace(): string { |
| 135 | + return this._client.clientOptions.workspaceFolder.uri.toString(); |
| 136 | + } |
| 137 | + |
| 138 | + private _show() { |
| 139 | + this._item.show(); |
| 140 | + } |
| 141 | + |
| 142 | + private _hide() { |
| 143 | + this._item.hide(); |
| 144 | + } |
| 145 | + |
| 146 | + private _setStatus(status: Status) { |
| 147 | + const ui: IStatusBarItemConfig = STATUS_BAR_UI[status]; |
| 148 | + this._item.text = `$(${ui.icon}) ${STATUS_BAR_ITEM_NAME}`; |
| 149 | + this._item.tooltip = ui.tooltip; |
| 150 | + this._item.color = new ThemeColor(ui.color); |
| 151 | + } |
| 152 | +} |
0 commit comments