feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
node_modules/
|
||||
|
||||
# Build output
|
||||
out/
|
||||
out/*
|
||||
!out/extension.js
|
||||
!out/externalSources.js
|
||||
dist/
|
||||
|
||||
# VS Code package output
|
||||
@@ -35,4 +37,4 @@ Thumbs.db
|
||||
.vscode/*.log
|
||||
|
||||
# AI
|
||||
debug
|
||||
debug
|
||||
|
||||
@@ -153,3 +153,31 @@ The extension currently provides only the minimal LSP client bridge:
|
||||
* receives LSP messages from the Java/LSP4J server
|
||||
|
||||
The Studio remains the source of trut
|
||||
|
||||
## Reading SDK/core and libraries
|
||||
|
||||
F12 opens bundled SDK/core and admitted dependency declarations as read-only
|
||||
`prometeu-source:` documents. Hover, continued F12 and semantic highlighting work
|
||||
inside them. Files in the editable project keep normal `file:` editing. A library
|
||||
under `.workspace` is still external. SDK/core are never copied to an editorial
|
||||
folder or cache. Formatting, rename and code actions cannot change external sources;
|
||||
a proposal touching both project and dependency files is refused in full.
|
||||
|
||||
An open external document keeps its source revision. Disconnect leaves its text
|
||||
visible, with queries unavailable until reconnection and revalidation. If the
|
||||
source changed, navigate again from the project to obtain new text and positions.
|
||||
The status bar indicates when an active external source is unavailable.
|
||||
|
||||
Compile with `npm run compile` before running the extension. Development acceptance
|
||||
can launch an isolated real VS Code host against the test LSP classpath:
|
||||
|
||||
```sh
|
||||
PROMETEU_VSCODE_DEV_TEST=1 ./gradlew :prometeu-lsp:prometeu-lsp-v1:test --tests '*ExternalSourceDevelopmentHostTest' --console=plain
|
||||
```
|
||||
|
||||
Run that Gradle command from the repository root. Set `PROMETEU_VSCODE_EXECUTABLE`
|
||||
when VS Code is not installed at the default macOS application path. The test covers
|
||||
read-only typing, declaration ranges, hover/F12, highlighting, dependency sources
|
||||
with Unicode/CRLF, reconnection and normal project editing. It does not validate a
|
||||
packaged Studio distribution or installed JAR. Dependencies must already exist on
|
||||
disk; dependency management remains a separate product task.
|
||||
|
||||
@@ -37,6 +37,7 @@ exports.activate = activate;
|
||||
exports.deactivate = deactivate;
|
||||
const net = __importStar(require("node:net"));
|
||||
const vscode = __importStar(require("vscode"));
|
||||
const externalSources_1 = require("./externalSources");
|
||||
const node_1 = require("vscode-languageclient/node");
|
||||
let client;
|
||||
let output;
|
||||
@@ -44,12 +45,14 @@ let reconnectTimer;
|
||||
let reconnectContext;
|
||||
let stoppingClient = false;
|
||||
let lastAppliedThemeSignature;
|
||||
const externalSources = new externalSources_1.ExternalSources();
|
||||
const RECONNECT_DELAY_MS = 2000;
|
||||
const VSCODE_HOST_ID = "vscode";
|
||||
function activate(context) {
|
||||
reconnectContext = context;
|
||||
output = vscode.window.createOutputChannel("Prometeu LSP");
|
||||
output.appendLine("Prometeu VS Code extension activated.");
|
||||
context.subscriptions.push(externalSources, vscode.workspace.registerTextDocumentContentProvider(externalSources_1.externalScheme, externalSources), vscode.window.onDidChangeActiveTextEditor(() => externalSources.refreshStatus()), vscode.workspace.onDidOpenTextDocument(document => void externalSources.opened(document)), vscode.workspace.onDidCloseTextDocument(document => void externalSources.closeDocument(document)));
|
||||
const connectCommand = vscode.commands.registerCommand("prometeu.connectStudioLsp", async () => {
|
||||
await startClient(context);
|
||||
});
|
||||
@@ -61,7 +64,12 @@ function activate(context) {
|
||||
void startClient(context);
|
||||
}
|
||||
async function deactivate() {
|
||||
await stopClient();
|
||||
try {
|
||||
await externalSources.shutdown();
|
||||
}
|
||||
finally {
|
||||
await stopClient();
|
||||
}
|
||||
}
|
||||
async function startClient(context) {
|
||||
clearReconnectTimer();
|
||||
@@ -113,7 +121,7 @@ async function startClient(context) {
|
||||
{
|
||||
scheme: "file",
|
||||
language: "pbs"
|
||||
}
|
||||
}, { scheme: externalSources_1.externalScheme, language: "pbs" }
|
||||
],
|
||||
synchronize: {
|
||||
fileEvents: [
|
||||
@@ -121,10 +129,20 @@ async function startClient(context) {
|
||||
]
|
||||
},
|
||||
initializationOptions: {
|
||||
...externalSources.initializationOptions,
|
||||
clientKind: "vscode",
|
||||
prometeuClientVersion: context.extension.packageJSON.version
|
||||
},
|
||||
outputChannel: output,
|
||||
middleware: {
|
||||
prepareRename: (document, position, token, next) => document.uri.scheme === externalSources_1.externalScheme ? null : next(document, position, token),
|
||||
provideRenameEdits: (document, position, name, token, next) => document.uri.scheme === externalSources_1.externalScheme ? null : next(document, position, name, token),
|
||||
provideDocumentFormattingEdits: (document, options, token, next) => document.uri.scheme === externalSources_1.externalScheme ? [] : next(document, options, token),
|
||||
provideCodeActions: (document, range, context, token, next) => document.uri.scheme === externalSources_1.externalScheme ? [] : next(document, range, context, token),
|
||||
provideHover: (document, position, token, next) => externalSources.canQuery(document) ? next(document, position, token) : null,
|
||||
provideDefinition: (document, position, token, next) => externalSources.canQuery(document) ? next(document, position, token) : null,
|
||||
provideDocumentSemanticTokens: (document, token, next) => externalSources.canQuery(document) ? next(document, token) : null
|
||||
},
|
||||
errorHandler: {
|
||||
error: (error) => {
|
||||
output.appendLine(`Prometeu LSP transport error: ${error.message}`);
|
||||
@@ -138,11 +156,18 @@ async function startClient(context) {
|
||||
};
|
||||
const nextClient = new node_1.LanguageClient("prometeuStudioLsp", "Prometeu Studio LSP", serverOptions, clientOptions);
|
||||
client = nextClient;
|
||||
nextClient.registerFeature({
|
||||
fillClientCapabilities(capabilities) {
|
||||
capabilities.experimental = { ...capabilities.experimental, prometeuExternalSources: true };
|
||||
},
|
||||
initialize() { }, clear() { }, getState() { return { kind: "static" }; }
|
||||
});
|
||||
nextClient.onDidChangeState((event) => {
|
||||
output.appendLine(`Prometeu LSP state changed: ${node_1.State[event.oldState]} -> ${node_1.State[event.newState]}`);
|
||||
if (event.newState !== node_1.State.Stopped) {
|
||||
return;
|
||||
}
|
||||
externalSources.disconnected();
|
||||
if (client === nextClient) {
|
||||
client = undefined;
|
||||
}
|
||||
@@ -152,11 +177,13 @@ async function startClient(context) {
|
||||
});
|
||||
try {
|
||||
await nextClient.start();
|
||||
await externalSources.connected(nextClient);
|
||||
await applyPrometeuVisualTheme(nextClient.initializeResult);
|
||||
output.appendLine("Prometeu Studio LSP client started.");
|
||||
vscode.window.showInformationMessage("Connected to Prometeu Studio LSP.");
|
||||
}
|
||||
catch (error) {
|
||||
externalSources.resetSession();
|
||||
if (client === nextClient) {
|
||||
client = undefined;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || (function () {
|
||||
var ownKeys = function(o) {
|
||||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||||
var ar = [];
|
||||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||||
return ar;
|
||||
};
|
||||
return ownKeys(o);
|
||||
};
|
||||
return function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ExternalSources = exports.externalScheme = void 0;
|
||||
const vscode = __importStar(require("vscode"));
|
||||
exports.externalScheme = "prometeu-source";
|
||||
/** Immutable virtual documents. No filesystem provider and no content-change events. */
|
||||
class ExternalSources {
|
||||
client;
|
||||
consumerId;
|
||||
documents = new Map();
|
||||
available = new Set();
|
||||
closed = new Set();
|
||||
status = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 10);
|
||||
refreshStatus() {
|
||||
const document = vscode.window.activeTextEditor?.document;
|
||||
if (document?.uri.scheme === exports.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() {
|
||||
return this.consumerId ? { prometeuExternalConsumerId: this.consumerId } : {};
|
||||
}
|
||||
canQuery(document) {
|
||||
const uri = document.uri;
|
||||
if (uri.scheme !== exports.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) {
|
||||
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() { this.client = undefined; this.available.clear(); this.refreshStatus(); }
|
||||
resetSession() { this.disconnected(); this.consumerId = undefined; }
|
||||
request(operation, uri) {
|
||||
if (!this.client || !this.consumerId)
|
||||
return Promise.reject(new Error("External source unavailable: reconnect to Studio."));
|
||||
return this.client.sendRequest(`prometeu/${operation}`, { consumerId: this.consumerId, uri });
|
||||
}
|
||||
async provideTextDocumentContent(uri, token) {
|
||||
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) {
|
||||
if (document.uri.scheme !== exports.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) {
|
||||
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);
|
||||
}
|
||||
async release(uri) {
|
||||
if (this.client && this.consumerId)
|
||||
await this.client.sendRequest("prometeu/releaseExternalSource", { consumerId: this.consumerId, uri });
|
||||
}
|
||||
async shutdown() {
|
||||
if (this.client && this.consumerId)
|
||||
await this.client.sendRequest("prometeu/releaseExternalSource", { consumerId: this.consumerId, uri: null });
|
||||
this.resetSession();
|
||||
this.documents.clear();
|
||||
}
|
||||
dispose() { this.disconnected(); this.documents.clear(); this.status.dispose(); }
|
||||
}
|
||||
exports.ExternalSources = ExternalSources;
|
||||
//# sourceMappingURL=externalSources.js.map
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as net from "node:net";
|
||||
import * as vscode from "vscode";
|
||||
import { ExternalSources, externalScheme } from "./externalSources";
|
||||
|
||||
import {
|
||||
CloseAction,
|
||||
@@ -17,6 +18,7 @@ let reconnectTimer: NodeJS.Timeout | undefined;
|
||||
let reconnectContext: vscode.ExtensionContext | undefined;
|
||||
let stoppingClient = false;
|
||||
let lastAppliedThemeSignature: string | undefined;
|
||||
const externalSources = new ExternalSources();
|
||||
|
||||
const RECONNECT_DELAY_MS = 2000;
|
||||
|
||||
@@ -76,6 +78,11 @@ export function activate(context: vscode.ExtensionContext): void {
|
||||
reconnectContext = context;
|
||||
output = vscode.window.createOutputChannel("Prometeu LSP");
|
||||
output.appendLine("Prometeu VS Code extension activated.");
|
||||
context.subscriptions.push(externalSources,
|
||||
vscode.workspace.registerTextDocumentContentProvider(externalScheme, externalSources),
|
||||
vscode.window.onDidChangeActiveTextEditor(() => externalSources.refreshStatus()),
|
||||
vscode.workspace.onDidOpenTextDocument(document => void externalSources.opened(document)),
|
||||
vscode.workspace.onDidCloseTextDocument(document => void externalSources.closeDocument(document)));
|
||||
|
||||
const connectCommand = vscode.commands.registerCommand(
|
||||
"prometeu.connectStudioLsp",
|
||||
@@ -98,7 +105,8 @@ export function activate(context: vscode.ExtensionContext): void {
|
||||
}
|
||||
|
||||
export async function deactivate(): Promise<void> {
|
||||
await stopClient();
|
||||
try { await externalSources.shutdown(); }
|
||||
finally { await stopClient(); }
|
||||
}
|
||||
|
||||
async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
@@ -167,7 +175,7 @@ async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
{
|
||||
scheme: "file",
|
||||
language: "pbs"
|
||||
}
|
||||
}, { scheme: externalScheme, language: "pbs" }
|
||||
],
|
||||
synchronize: {
|
||||
fileEvents: [
|
||||
@@ -175,10 +183,20 @@ async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
]
|
||||
},
|
||||
initializationOptions: {
|
||||
...externalSources.initializationOptions,
|
||||
clientKind: "vscode",
|
||||
prometeuClientVersion: context.extension.packageJSON.version
|
||||
},
|
||||
outputChannel: output,
|
||||
middleware: {
|
||||
prepareRename: (document, position, token, next) => document.uri.scheme === externalScheme ? null : next(document, position, token),
|
||||
provideRenameEdits: (document, position, name, token, next) => document.uri.scheme === externalScheme ? null : next(document, position, name, token),
|
||||
provideDocumentFormattingEdits: (document, options, token, next) => document.uri.scheme === externalScheme ? [] : next(document, options, token),
|
||||
provideCodeActions: (document, range, context, token, next) => document.uri.scheme === externalScheme ? [] : next(document, range, context, token),
|
||||
provideHover: (document, position, token, next) => externalSources.canQuery(document) ? next(document, position, token) : null,
|
||||
provideDefinition: (document, position, token, next) => externalSources.canQuery(document) ? next(document, position, token) : null,
|
||||
provideDocumentSemanticTokens: (document, token, next) => externalSources.canQuery(document) ? next(document, token) : null
|
||||
},
|
||||
errorHandler: {
|
||||
error: (error) => {
|
||||
output.appendLine(`Prometeu LSP transport error: ${error.message}`);
|
||||
@@ -198,11 +216,18 @@ async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
clientOptions
|
||||
);
|
||||
client = nextClient;
|
||||
nextClient.registerFeature({
|
||||
fillClientCapabilities(capabilities) {
|
||||
capabilities.experimental = { ...capabilities.experimental, prometeuExternalSources: true };
|
||||
},
|
||||
initialize() {}, clear() {}, getState() { return { kind: "static" }; }
|
||||
});
|
||||
nextClient.onDidChangeState((event) => {
|
||||
output.appendLine(`Prometeu LSP state changed: ${State[event.oldState]} -> ${State[event.newState]}`);
|
||||
if (event.newState !== State.Stopped) {
|
||||
return;
|
||||
}
|
||||
externalSources.disconnected();
|
||||
if (client === nextClient) {
|
||||
client = undefined;
|
||||
}
|
||||
@@ -213,11 +238,13 @@ async function startClient(context: vscode.ExtensionContext): Promise<void> {
|
||||
|
||||
try {
|
||||
await nextClient.start();
|
||||
await externalSources.connected(nextClient);
|
||||
await applyPrometeuVisualTheme(nextClient.initializeResult);
|
||||
|
||||
output.appendLine("Prometeu Studio LSP client started.");
|
||||
vscode.window.showInformationMessage("Connected to Prometeu Studio LSP.");
|
||||
} catch (error) {
|
||||
externalSources.resetSession();
|
||||
if (client === nextClient) {
|
||||
client = undefined;
|
||||
}
|
||||
|
||||
@@ -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(); }
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import * as assert from "node:assert/strict";
|
||||
import * as vscode from "vscode";
|
||||
import { ExternalSources, ExternalContent } from "../externalSources";
|
||||
import type { LanguageClient } from "vscode-languageclient/node";
|
||||
|
||||
async function providerLifecycle(): Promise<void> {
|
||||
const provider = new ExternalSources();
|
||||
const uri = vscode.Uri.parse("prometeu-source://test/revision/main.pbs");
|
||||
let content: ExternalContent = { descriptor: { uri: uri.toString(), revision: "one", origin: "dependency", name: "main.pbs", languageId: "pbs", readOnly: true }, text: "// á 😀\r\nfn f() {}\r\n" };
|
||||
const requests: string[] = [];
|
||||
const fake = { initializeResult: { capabilities: { experimental: { prometeuExternalSources: true, prometeuExternalConsumerId: "consumer" } } },
|
||||
sendRequest: async (method: string) => { requests.push(method); return content; } } as unknown as LanguageClient;
|
||||
await provider.connected(fake);
|
||||
const cancellation = new vscode.CancellationTokenSource();
|
||||
const original = await provider.provideTextDocumentContent(uri, cancellation.token);
|
||||
const document = { uri, getText: () => original.replace(/\r\n/g, "\n") } as vscode.TextDocument;
|
||||
assert.equal(provider.canQuery(document), true);
|
||||
provider.disconnected();
|
||||
assert.equal(provider.canQuery(document), false);
|
||||
assert.equal(await provider.provideTextDocumentContent(uri, cancellation.token), original);
|
||||
await provider.connected(fake);
|
||||
assert.equal(provider.canQuery(document), true);
|
||||
provider.disconnected();
|
||||
content = { descriptor: { ...content.descriptor, revision: "two" }, text: "different" };
|
||||
await provider.connected(fake);
|
||||
assert.equal(provider.canQuery(document), false);
|
||||
assert.equal(await provider.provideTextDocumentContent(uri, cancellation.token), original);
|
||||
await provider.closeDocument(document);
|
||||
assert.ok(requests.includes("prometeu/releaseExternalSource"));
|
||||
cancellation.cancel();
|
||||
await assert.rejects(provider.provideTextDocumentContent(uri, cancellation.token), /cancelled/);
|
||||
await provider.shutdown();
|
||||
provider.dispose();
|
||||
cancellation.dispose();
|
||||
}
|
||||
|
||||
async function eventually<T>(query: () => PromiseLike<T>, accept: (value: T) => boolean): Promise<T> {
|
||||
const deadline = Date.now() + 20000;
|
||||
while (true) {
|
||||
const value = await query();
|
||||
if (accept(value)) return value;
|
||||
if (Date.now() >= deadline) throw new Error("Timed out waiting for development LSP");
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
}
|
||||
|
||||
/** Executed by the real Extension Development Host; no packaged Studio involved. */
|
||||
export async function run(): Promise<void> {
|
||||
await providerLifecycle();
|
||||
const root = vscode.workspace.workspaceFolders![0].uri;
|
||||
const project = await vscode.workspace.openTextDocument(vscode.Uri.joinPath(root, "src/main.pbs"));
|
||||
await vscode.window.showTextDocument(project);
|
||||
const use = project.positionAt(project.getText().indexOf("Gfx.clear") + 4);
|
||||
const definitions = await eventually(
|
||||
() => vscode.commands.executeCommand<vscode.Location[]>("vscode.executeDefinitionProvider", project.uri, use),
|
||||
result => !!result?.length);
|
||||
assert.equal(definitions[0].uri.scheme, "prometeu-source");
|
||||
const external = await vscode.workspace.openTextDocument(definitions[0].uri);
|
||||
const editor = await vscode.window.showTextDocument(external, { selection: definitions[0].range });
|
||||
await eventually(async () => external.languageId, language => language === "pbs");
|
||||
assert.equal(external.isUntitled, false);
|
||||
assert.equal(external.isDirty, false);
|
||||
const original = external.getText();
|
||||
assert.ok(external.getText(definitions[0].range).includes("clear"));
|
||||
await vscode.commands.executeCommand("type", { text: "changed" });
|
||||
assert.equal(external.getText(), original);
|
||||
assert.equal(external.isDirty, false);
|
||||
const call = external.positionAt(original.indexOf("LowGfx.clear") + 7);
|
||||
const hover = await eventually(
|
||||
() => vscode.commands.executeCommand<vscode.Hover[]>("vscode.executeHoverProvider", external.uri, call),
|
||||
result => !!result?.length);
|
||||
assert.ok(hover.flatMap(item => item.contents).some(content => (typeof content === "string" ? content : content.value).includes("clear")));
|
||||
const host = await vscode.commands.executeCommand<vscode.Location[]>("vscode.executeDefinitionProvider", external.uri, call);
|
||||
assert.ok(host.length);
|
||||
const primitive = await vscode.workspace.openTextDocument(host[0].uri);
|
||||
assert.ok(primitive.getText(host[0].range).includes("clear"));
|
||||
const tokens = await eventually(
|
||||
() => vscode.commands.executeCommand<vscode.SemanticTokens>("vscode.provideDocumentSemanticTokens", external.uri),
|
||||
result => !!result?.data.length);
|
||||
assert.ok(tokens.data.length > 0);
|
||||
const libraryUse = project.positionAt(project.getText().lastIndexOf("answer()"));
|
||||
const libraryDefinitions = await vscode.commands.executeCommand<vscode.Location[]>("vscode.executeDefinitionProvider", project.uri, libraryUse);
|
||||
assert.equal(libraryDefinitions[0].uri.scheme, "prometeu-source");
|
||||
const library = await vscode.workspace.openTextDocument(libraryDefinitions[0].uri);
|
||||
await vscode.window.showTextDocument(library, { selection: libraryDefinitions[0].range });
|
||||
assert.ok(library.getText(libraryDefinitions[0].range).includes("answer"));
|
||||
assert.equal(libraryDefinitions[0].range.start.line, 1);
|
||||
const nestedUse = library.positionAt(library.getText().indexOf("helper();"));
|
||||
const nested = await eventually(
|
||||
() => vscode.commands.executeCommand<vscode.Location[]>("vscode.executeDefinitionProvider", library.uri, nestedUse),
|
||||
result => !!result?.length);
|
||||
assert.equal(nested[0].range.start.line, 2);
|
||||
await vscode.commands.executeCommand("prometeu.restartStudioLsp");
|
||||
assert.equal(external.getText(), original);
|
||||
const afterReconnect = await eventually(
|
||||
() => vscode.commands.executeCommand<vscode.Hover[]>("vscode.executeHoverProvider", external.uri, call),
|
||||
result => !!result?.length);
|
||||
assert.ok(afterReconnect.flatMap(item => item.contents).some(content => (typeof content === "string" ? content : content.value).includes("clear")));
|
||||
// Project editing remains functional in the same editor session.
|
||||
const projectEditor = await vscode.window.showTextDocument(project);
|
||||
assert.equal(await projectEditor.edit(edit => edit.insert(new vscode.Position(0, 0), "// editable\n")), true);
|
||||
await vscode.commands.executeCommand("workbench.action.revertAndCloseActiveEditor");
|
||||
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
|
||||
console.log("PLN-0141 VS Code development acceptance passed: readonly, syntax, positions, hover, continued definition, project editing.");
|
||||
}
|
||||
Reference in New Issue
Block a user