-
Notifications
You must be signed in to change notification settings - Fork 28k
/
versionStatus.ts
220 lines (176 loc) · 6.56 KB
/
versionStatus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { ITypeScriptServiceClient } from '../typescriptService';
import { coalesce } from '../utils/arrays';
import { Command, CommandManager } from '../utils/commandManager';
import { isTypeScriptDocument } from '../utils/languageModeIds';
import { isImplicitProjectConfigFile, openOrCreateConfig, openProjectConfigOrPromptToCreate, openProjectConfigForFile, ProjectType } from '../utils/tsconfig';
import { Disposable } from './dispose';
import { TypeScriptVersion } from './versionProvider';
const localize = nls.loadMessageBundle();
namespace ProjectInfoState {
export const enum Type { None, Pending, Resolved }
export const None = Object.freeze({ type: Type.None } as const);
export class Pending {
public readonly type = Type.Pending;
public readonly cancellation = new vscode.CancellationTokenSource();
constructor(
public readonly resource: vscode.Uri,
) { }
}
export class Resolved {
public readonly type = Type.Resolved;
constructor(
public readonly resource: vscode.Uri,
public readonly configFile: string,
) { }
}
export type State = typeof None | Pending | Resolved;
}
interface QuickPickItem extends vscode.QuickPickItem {
run(): void;
}
class ProjectStatusCommand implements Command {
public readonly id = '_typescript.projectStatus';
public constructor(
private readonly _client: ITypeScriptServiceClient,
private readonly _delegate: () => ProjectInfoState.State,
) { }
public async execute(): Promise<void> {
const info = this._delegate();
const result = await vscode.window.showQuickPick<QuickPickItem>(coalesce([
this.getProjectItem(info),
this.getVersionItem(),
this.getHelpItem(),
]), {
placeHolder: localize('projectQuickPick.placeholder', "TypeScript Project Info"),
});
return result?.run();
}
private getVersionItem(): QuickPickItem {
return {
label: localize('projectQuickPick.version.label', "Select TypeScript Version..."),
description: this._client.apiVersion.displayName,
run: () => {
this._client.showVersionPicker();
}
};
}
private getProjectItem(info: ProjectInfoState.State): QuickPickItem | undefined {
const rootPath = info.type === ProjectInfoState.Type.Resolved ? this._client.getWorkspaceRootForResource(info.resource) : undefined;
if (!rootPath) {
return undefined;
}
if (info.type === ProjectInfoState.Type.Resolved) {
if (isImplicitProjectConfigFile(info.configFile)) {
return {
label: localize('projectQuickPick.project.create', "Create tsconfig"),
detail: localize('projectQuickPick.project.create.description', "This file is currently not part of a tsconfig/jsconfig project"),
run: () => {
openOrCreateConfig(ProjectType.TypeScript, rootPath, this._client.configuration);
}
};
}
}
return {
label: localize('projectQuickPick.version.goProjectConfig', "Open tsconfig"),
description: info.type === ProjectInfoState.Type.Resolved ? vscode.workspace.asRelativePath(info.configFile) : undefined,
run: () => {
if (info.type === ProjectInfoState.Type.Resolved) {
openProjectConfigOrPromptToCreate(ProjectType.TypeScript, this._client, rootPath, info.configFile);
} else if (info.type === ProjectInfoState.Type.Pending) {
openProjectConfigForFile(ProjectType.TypeScript, this._client, info.resource);
}
}
};
}
private getHelpItem(): QuickPickItem {
return {
label: localize('projectQuickPick.help', "TypeScript help"),
run: () => {
vscode.env.openExternal(vscode.Uri.parse('https://go.microsoft.com/fwlink/?linkid=839919')); // TODO:
}
};
}
}
export default class VersionStatus extends Disposable {
private readonly _statusBarEntry: vscode.StatusBarItem;
private _ready = false;
private _state: ProjectInfoState.State = ProjectInfoState.None;
constructor(
private readonly _client: ITypeScriptServiceClient,
commandManager: CommandManager,
) {
super();
this._statusBarEntry = this._register(vscode.window.createStatusBarItem({
id: 'status.typescript',
name: localize('projectInfo.name', "TypeScript: Project Info"),
alignment: vscode.StatusBarAlignment.Right,
priority: 99 /* to the right of editor status (100) */
}));
const command = new ProjectStatusCommand(this._client, () => this._state);
commandManager.register(command);
this._statusBarEntry.command = command.id;
vscode.window.onDidChangeActiveTextEditor(this.updateStatus, this, this._disposables);
this._client.onReady(() => {
this._ready = true;
this.updateStatus();
});
this._register(this._client.onTsServerStarted(({ version }) => this.onDidChangeTypeScriptVersion(version)));
}
private onDidChangeTypeScriptVersion(version: TypeScriptVersion) {
this._statusBarEntry.text = version.displayName;
this._statusBarEntry.tooltip = version.path;
this.updateStatus();
}
private async updateStatus() {
if (!vscode.window.activeTextEditor) {
this.hide();
return;
}
const doc = vscode.window.activeTextEditor.document;
if (isTypeScriptDocument(doc)) {
const file = this._client.normalizedPath(doc.uri);
if (file) {
this._statusBarEntry.show();
if (!this._ready) {
return;
}
const pendingState = new ProjectInfoState.Pending(doc.uri);
this.updateState(pendingState);
const response = await this._client.execute('projectInfo', { file, needFileNameList: false }, pendingState.cancellation.token);
if (response.type === 'response' && response.body) {
if (this._state === pendingState) {
this.updateState(new ProjectInfoState.Resolved(doc.uri, response.body.configFileName));
this._statusBarEntry.show();
}
}
return;
}
}
if (!vscode.window.activeTextEditor.viewColumn) {
// viewColumn is undefined for the debug/output panel, but we still want
// to show the version info in the existing editor
return;
}
this.hide();
}
private hide(): void {
this._statusBarEntry.hide();
this.updateState(ProjectInfoState.None);
}
private updateState(newState: ProjectInfoState.State): void {
if (this._state === newState) {
return;
}
if (this._state.type === ProjectInfoState.Type.Pending) {
this._state.cancellation.cancel();
this._state.cancellation.dispose();
}
this._state = newState;
}
}