feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import * as vscode from "vscode";
|
||||
import { LanguageClient } from "vscode-languageclient/node";
|
||||
|
||||
export const externalScheme = "prometeu-source";
|
||||
export type ExternalContent = {
|
||||
descriptor: { uri: string; revision: string; origin: string; name: string; languageId: string; readOnly: true };
|
||||
text: string;
|
||||
};
|
||||
|
||||
/** Immutable virtual documents. No filesystem provider and no content-change events. */
|
||||
export class ExternalSources implements vscode.TextDocumentContentProvider, vscode.Disposable {
|
||||
private client?: LanguageClient;
|
||||
private consumerId?: string;
|
||||
private readonly documents = new Map<string, ExternalContent>();
|
||||
private readonly available = new Set<string>();
|
||||
private readonly closed = new Set<string>();
|
||||
private readonly status = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 10);
|
||||
|
||||
refreshStatus(): void {
|
||||
const document = vscode.window.activeTextEditor?.document;
|
||||
if (document?.uri.scheme === externalScheme && !this.canQuery(document)) {
|
||||
this.status.text = "Prometeu: external source unavailable";
|
||||
this.status.tooltip = "Reconnect to Studio or navigate again to obtain the current source revision.";
|
||||
this.status.show();
|
||||
} else this.status.hide();
|
||||
}
|
||||
|
||||
get initializationOptions(): object {
|
||||
return this.consumerId ? { prometeuExternalConsumerId: this.consumerId } : {};
|
||||
}
|
||||
|
||||
canQuery(document: vscode.TextDocument): boolean {
|
||||
const uri = document.uri;
|
||||
if (uri.scheme !== externalScheme) return true;
|
||||
const content = this.documents.get(uri.toString());
|
||||
return !!this.client && this.available.has(uri.toString()) && !!content
|
||||
&& document.getText().replace(/\r\n?/g, "\n") === content.text.replace(/\r\n?/g, "\n");
|
||||
}
|
||||
|
||||
async connected(client: LanguageClient): Promise<void> {
|
||||
const experimental = client.initializeResult?.capabilities.experimental;
|
||||
if (!experimental?.prometeuExternalSources) return;
|
||||
this.client = client;
|
||||
this.consumerId = experimental.prometeuExternalConsumerId;
|
||||
for (const uri of this.closed) await this.release(uri);
|
||||
this.closed.clear();
|
||||
for (const [uri, old] of this.documents) {
|
||||
try {
|
||||
const current = await this.request("revalidateExternalSource", uri);
|
||||
if (current.descriptor.revision === old.descriptor.revision && current.text === old.text) this.available.add(uri);
|
||||
} catch { /* Keep displayed text; fresh navigation is required. */ }
|
||||
}
|
||||
this.refreshStatus();
|
||||
}
|
||||
|
||||
disconnected(): void { this.client = undefined; this.available.clear(); this.refreshStatus(); }
|
||||
resetSession(): void { this.disconnected(); this.consumerId = undefined; }
|
||||
|
||||
private request(operation: string, uri: string): Promise<ExternalContent> {
|
||||
if (!this.client || !this.consumerId) return Promise.reject(new Error("External source unavailable: reconnect to Studio."));
|
||||
return this.client.sendRequest<ExternalContent>(`prometeu/${operation}`, { consumerId: this.consumerId, uri });
|
||||
}
|
||||
|
||||
async provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): Promise<string> {
|
||||
const key = uri.toString();
|
||||
const old = this.documents.get(key);
|
||||
if (old) return old.text;
|
||||
const content = await this.request("readExternalSource", key);
|
||||
if (token.isCancellationRequested) { await this.release(key); throw new Error("External source read cancelled."); }
|
||||
if (!content.descriptor.readOnly || content.descriptor.uri !== key) throw new Error("Invalid external source descriptor.");
|
||||
this.documents.set(key, content);
|
||||
this.available.add(key);
|
||||
this.refreshStatus();
|
||||
return content.text;
|
||||
}
|
||||
|
||||
async opened(document: vscode.TextDocument): Promise<void> {
|
||||
if (document.uri.scheme !== externalScheme) return;
|
||||
const content = this.documents.get(document.uri.toString());
|
||||
if (content && content.descriptor.languageId !== document.languageId)
|
||||
await vscode.languages.setTextDocumentLanguage(document, content.descriptor.languageId);
|
||||
}
|
||||
|
||||
async closeDocument(document: vscode.TextDocument): Promise<void> {
|
||||
const uri = document.uri.toString();
|
||||
if (!this.documents.has(uri)) return;
|
||||
// Changing language also fires close/open; retain while VS Code still owns the document.
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
if (vscode.workspace.textDocuments.some(open => open.uri.toString() === uri && !open.isClosed)) return;
|
||||
this.documents.delete(uri);
|
||||
this.available.delete(uri);
|
||||
if (this.client) await this.release(uri); else this.closed.add(uri);
|
||||
}
|
||||
|
||||
private async release(uri: string): Promise<void> {
|
||||
if (this.client && this.consumerId)
|
||||
await this.client.sendRequest("prometeu/releaseExternalSource", { consumerId: this.consumerId, uri });
|
||||
}
|
||||
async shutdown(): Promise<void> {
|
||||
if (this.client && this.consumerId)
|
||||
await this.client.sendRequest("prometeu/releaseExternalSource", { consumerId: this.consumerId, uri: null });
|
||||
this.resetSession();
|
||||
this.documents.clear();
|
||||
}
|
||||
dispose(): void { this.disconnected(); this.documents.clear(); this.status.dispose(); }
|
||||
}
|
||||
Reference in New Issue
Block a user