|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { ActiveJsTsEditorTracker } from "./activeJsTsEditorTracker"; |
| 3 | +import { Client } from "./client"; |
| 4 | +import { |
| 5 | + isSupportedLanguageMode, |
| 6 | + jsTsLanguageModes, |
| 7 | +} from "./util"; |
| 8 | + |
| 9 | +namespace ProjectInfoState { |
| 10 | + export const enum Type { |
| 11 | + None, |
| 12 | + Pending, |
| 13 | + Resolved, |
| 14 | + } |
| 15 | + |
| 16 | + export const None = Object.freeze({ type: Type.None } as const); |
| 17 | + |
| 18 | + export class Pending { |
| 19 | + public readonly type = Type.Pending; |
| 20 | + public readonly cancellation = new vscode.CancellationTokenSource(); |
| 21 | + |
| 22 | + constructor( |
| 23 | + public readonly resource: vscode.Uri, |
| 24 | + ) {} |
| 25 | + } |
| 26 | + |
| 27 | + export class Resolved { |
| 28 | + public readonly type = Type.Resolved; |
| 29 | + |
| 30 | + constructor( |
| 31 | + public readonly resource: vscode.Uri, |
| 32 | + public readonly configFile: string, |
| 33 | + ) {} |
| 34 | + } |
| 35 | + |
| 36 | + export type State = typeof None | Pending | Resolved; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Shows which tsconfig/jsconfig the current file belongs to. |
| 41 | + */ |
| 42 | +export class ProjectStatus implements vscode.Disposable { |
| 43 | + private statusItem?: vscode.LanguageStatusItem; |
| 44 | + private state: ProjectInfoState.State = ProjectInfoState.None; |
| 45 | + private disposables: vscode.Disposable[] = []; |
| 46 | + private ready = false; |
| 47 | + |
| 48 | + constructor( |
| 49 | + private readonly client: Client, |
| 50 | + private readonly activeEditorTracker: ActiveJsTsEditorTracker, |
| 51 | + onReady: vscode.Event<void>, |
| 52 | + ) { |
| 53 | + this.disposables.push( |
| 54 | + activeEditorTracker.onDidChangeActiveJsTsEditor(() => this.updateStatus()), |
| 55 | + ); |
| 56 | + this.disposables.push( |
| 57 | + onReady(() => { |
| 58 | + this.ready = true; |
| 59 | + this.updateStatus(); |
| 60 | + }), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + private async updateStatus(): Promise<void> { |
| 65 | + const doc = this.activeEditorTracker.activeJsTsEditor?.document; |
| 66 | + if (!doc || !isSupportedLanguageMode(doc)) { |
| 67 | + this.updateState(ProjectInfoState.None); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (doc.uri.scheme !== "file" && doc.uri.scheme !== "untitled") { |
| 72 | + this.updateState(ProjectInfoState.None); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (!this.ready) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const pendingState = new ProjectInfoState.Pending(doc.uri); |
| 81 | + this.updateState(pendingState); |
| 82 | + |
| 83 | + try { |
| 84 | + const result = await this.client.getProjectInfo(doc.uri.toString(), pendingState.cancellation.token); |
| 85 | + if (this.state === pendingState) { |
| 86 | + this.updateState(new ProjectInfoState.Resolved(doc.uri, result.configFilePath)); |
| 87 | + } |
| 88 | + } |
| 89 | + catch { |
| 90 | + // If we fail to get project info, just go back to no status |
| 91 | + if (this.state === pendingState) { |
| 92 | + this.updateState(ProjectInfoState.None); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private updateState(newState: ProjectInfoState.State): void { |
| 98 | + if (this.state === newState) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (this.state.type === ProjectInfoState.Type.Pending) { |
| 103 | + this.state.cancellation.cancel(); |
| 104 | + this.state.cancellation.dispose(); |
| 105 | + } |
| 106 | + |
| 107 | + this.state = newState; |
| 108 | + |
| 109 | + switch (this.state.type) { |
| 110 | + case ProjectInfoState.Type.None: { |
| 111 | + this.statusItem?.dispose(); |
| 112 | + this.statusItem = undefined; |
| 113 | + break; |
| 114 | + } |
| 115 | + case ProjectInfoState.Type.Pending: { |
| 116 | + const statusItem = this.ensureStatusItem(); |
| 117 | + statusItem.severity = vscode.LanguageStatusSeverity.Information; |
| 118 | + statusItem.text = "Loading project info..."; |
| 119 | + statusItem.detail = undefined; |
| 120 | + statusItem.command = undefined; |
| 121 | + statusItem.busy = true; |
| 122 | + break; |
| 123 | + } |
| 124 | + case ProjectInfoState.Type.Resolved: { |
| 125 | + const currentLanguageId = this.activeEditorTracker.activeJsTsEditor?.document.languageId; |
| 126 | + const isTypeScript = currentLanguageId === "typescript" |
| 127 | + || currentLanguageId === "typescriptreact"; |
| 128 | + const noConfigFileText = isTypeScript ? "No tsconfig" : "No jsconfig"; |
| 129 | + |
| 130 | + const rootPath = this.getWorkspaceRootForResource(this.state.resource); |
| 131 | + if (!rootPath) { |
| 132 | + if (this.statusItem) { |
| 133 | + this.statusItem.text = noConfigFileText; |
| 134 | + this.statusItem.detail = !vscode.workspace.workspaceFolders |
| 135 | + ? "No opened folders" |
| 136 | + : "File is not part of opened folders"; |
| 137 | + this.statusItem.busy = false; |
| 138 | + } |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const statusItem = this.ensureStatusItem(); |
| 143 | + statusItem.busy = false; |
| 144 | + statusItem.detail = undefined; |
| 145 | + statusItem.severity = vscode.LanguageStatusSeverity.Information; |
| 146 | + |
| 147 | + if (this.state.configFile) { |
| 148 | + statusItem.text = vscode.workspace.asRelativePath(this.state.configFile); |
| 149 | + statusItem.command = { |
| 150 | + command: "vscode.open", |
| 151 | + title: "Open Config File", |
| 152 | + arguments: [vscode.Uri.file(this.state.configFile)], |
| 153 | + }; |
| 154 | + } |
| 155 | + else { |
| 156 | + statusItem.text = noConfigFileText; |
| 157 | + statusItem.command = undefined; |
| 158 | + } |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private ensureStatusItem(): vscode.LanguageStatusItem { |
| 165 | + if (!this.statusItem) { |
| 166 | + this.statusItem = vscode.languages.createLanguageStatusItem("typescript.native-preview.projectStatus", jsTsLanguageModes); |
| 167 | + this.statusItem.name = "TypeScript Native Preview Project Status"; |
| 168 | + } |
| 169 | + return this.statusItem; |
| 170 | + } |
| 171 | + |
| 172 | + private getWorkspaceRootForResource(resource: vscode.Uri): vscode.Uri | undefined { |
| 173 | + const folder = vscode.workspace.getWorkspaceFolder(resource); |
| 174 | + return folder?.uri; |
| 175 | + } |
| 176 | + |
| 177 | + dispose(): void { |
| 178 | + this.statusItem?.dispose(); |
| 179 | + if (this.state.type === ProjectInfoState.Type.Pending) { |
| 180 | + this.state.cancellation.cancel(); |
| 181 | + this.state.cancellation.dispose(); |
| 182 | + } |
| 183 | + this.disposables.forEach(d => d.dispose()); |
| 184 | + } |
| 185 | +} |
0 commit comments