sketch for virtual document support for

This commit is contained in:
Jan Kretschmer 2021-11-01 14:50:32 +01:00
parent c2f44d476b
commit b074018c3e
3 changed files with 40 additions and 10 deletions

View file

@ -26,6 +26,13 @@ export function getCustomDataSource(toDispose: Disposable[]) {
}
}));
toDispose.push(workspace.onDidChangeTextDocument(e => {
const path = e.document.uri.toString();
if (pathsInExtensions.indexOf(path) || pathsInWorkspace.indexOf(path)) {
onChange.fire();
}
}));
return {
get uris() {
return pathsInWorkspace.concat(pathsInExtensions);
@ -50,7 +57,14 @@ function getCustomDataPathsInAllWorkspaces(): string[] {
if (Array.isArray(paths)) {
for (const path of paths) {
if (typeof path === 'string') {
dataPaths.push(resolvePath(rootFolder, path).toString());
const uri = Uri.parse(path);
if (uri.scheme === 'file') {
// only resolve file paths relative to extension
dataPaths.push(resolvePath(rootFolder, path).toString());
} else {
// others schemes
dataPaths.push(path);
}
}
}
}
@ -80,7 +94,15 @@ function getCustomDataPathsFromAllExtensions(): string[] {
const customData = extension.packageJSON?.contributes?.html?.customData;
if (Array.isArray(customData)) {
for (const rp of customData) {
dataPaths.push(joinPath(extension.extensionUri, rp).toString());
const uri = Uri.parse(rp);
if (uri.scheme === 'file') {
// only resolve file paths relative to extension
dataPaths.push(joinPath(extension.extensionUri, rp).toString());
} else {
// others schemes
dataPaths.push(rp);
}
}
}
}

View file

@ -16,7 +16,7 @@ import {
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, TextDocumentIdentifier, RequestType0, Range as LspRange, NotificationType, CommonLanguageClient
} from 'vscode-languageclient';
import { activateTagClosing } from './tagClosing';
import { RequestService } from './requests';
import { RequestService, serveFileSystemRequests } from './requests';
import { getCustomDataSource } from './customData';
namespace CustomDataChangedNotification {
@ -120,6 +120,8 @@ export function startClient(context: ExtensionContext, newLanguageClient: Langua
toDispose.push(disposable);
client.onReady().then(() => {
serveFileSystemRequests(client, runtime, context.subscriptions);
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
customDataSource.onDidChange(() => {
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);

View file

@ -18,30 +18,36 @@ export namespace FsReadDirRequest {
export const type: RequestType<string, [string, FileType][], any> = new RequestType('fs/readDir');
}
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime) {
client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime, subscriptions: { dispose(): any }[]) {
subscriptions.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
const uri = Uri.parse(param.uri);
if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.getContent(param.uri);
}
return workspace.fs.readFile(uri).then(buffer => {
return new runtime.TextDecoder(param.encoding).decode(buffer);
}, () => {
// this path also considers TextDocumentContentProvider
return workspace.openTextDocument(uri).then(doc => {
return doc.getText();
});
});
});
client.onRequest(FsReadDirRequest.type, (uriString: string) => {
}));
subscriptions.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => {
const uri = Uri.parse(uriString);
if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.readDirectory(uriString);
}
return workspace.fs.readDirectory(uri);
});
client.onRequest(FsStatRequest.type, (uriString: string) => {
}));
subscriptions.push(client.onRequest(FsStatRequest.type, (uriString: string) => {
const uri = Uri.parse(uriString);
if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.stat(uriString);
}
return workspace.fs.stat(uri);
});
}));
}
export enum FileType {