revert request changes & polish

This commit is contained in:
Martin Aeschlimann 2021-11-26 11:45:08 +01:00
parent bb89815cfb
commit f1455eabed
No known key found for this signature in database
GPG key ID: 2609A01E695523E3
3 changed files with 40 additions and 43 deletions

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode'; import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
import { resolvePath, joinPath, uriScheme } from './requests'; import { resolvePath, joinPath } from './requests';
export function getCustomDataSource(toDispose: Disposable[]) { export function getCustomDataSource(toDispose: Disposable[]) {
@ -44,6 +44,10 @@ export function getCustomDataSource(toDispose: Disposable[]) {
}; };
} }
function isURI(uriOrPath: string) {
return /^(?<scheme>\w[\w\d+.-]*):/.test(uriOrPath);
}
function getCustomDataPathsInAllWorkspaces(): Set<string> { function getCustomDataPathsInAllWorkspaces(): Set<string> {
const workspaceFolders = workspace.workspaceFolders; const workspaceFolders = workspace.workspaceFolders;
@ -54,16 +58,16 @@ function getCustomDataPathsInAllWorkspaces(): Set<string> {
return dataPaths; return dataPaths;
} }
const collect = (paths: string[] | undefined, rootFolder: Uri) => { const collect = (uriOrPaths: string[] | undefined, rootFolder: Uri) => {
if (Array.isArray(paths)) { if (Array.isArray(uriOrPaths)) {
for (const path of paths) { for (const uriOrPath of uriOrPaths) {
if (typeof path === 'string') { if (typeof uriOrPath === 'string') {
if (!uriScheme.test(path)) { if (!isURI(uriOrPath)) {
// only resolve file paths relative to extension // path in the workspace
dataPaths.add(resolvePath(rootFolder, path).toString()); dataPaths.add(resolvePath(rootFolder, uriOrPath).toString());
} else { } else {
// others schemes // external uri
dataPaths.add(path); dataPaths.add(uriOrPath);
} }
} }
} }
@ -93,13 +97,13 @@ function getCustomDataPathsFromAllExtensions(): Set<string> {
for (const extension of extensions.all) { for (const extension of extensions.all) {
const customData = extension.packageJSON?.contributes?.html?.customData; const customData = extension.packageJSON?.contributes?.html?.customData;
if (Array.isArray(customData)) { if (Array.isArray(customData)) {
for (const rp of customData) { for (const uriOrPath of customData) {
if (!uriScheme.test(rp)) { if (!isURI(uriOrPath)) {
// no schame -> resolve relative to extension // relative path in an extension
dataPaths.add(joinPath(extension.extensionUri, rp).toString()); dataPaths.add(joinPath(extension.extensionUri, uriOrPath).toString());
} else { } else {
// actual schemes // external uri
dataPaths.add(rp); dataPaths.add(uriOrPath);
} }
} }

View file

@ -120,7 +120,7 @@ export function startClient(context: ExtensionContext, newLanguageClient: Langua
toDispose.push(disposable); toDispose.push(disposable);
client.onReady().then(() => { client.onReady().then(() => {
serveFileSystemRequests(client, runtime, context.subscriptions); toDispose.push(serveFileSystemRequests(client, runtime));
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris); client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
customDataSource.onDidChange(() => { customDataSource.onDidChange(() => {

View file

@ -3,12 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { Uri, workspace } from 'vscode'; import { Uri, workspace, Disposable } from 'vscode';
import { RequestType, CommonLanguageClient } from 'vscode-languageclient'; import { RequestType, CommonLanguageClient } from 'vscode-languageclient';
import { Runtime } from './htmlClient'; import { Runtime } from './htmlClient';
export const uriScheme = /^(?<scheme>\w[\w\d+.-]*):/;
export namespace FsContentRequest { export namespace FsContentRequest {
export const type: RequestType<{ uri: string; encoding?: string; }, string, any> = new RequestType('fs/content'); export const type: RequestType<{ uri: string; encoding?: string; }, string, any> = new RequestType('fs/content');
} }
@ -20,37 +18,32 @@ export namespace FsReadDirRequest {
export const type: RequestType<string, [string, FileType][], any> = new RequestType('fs/readDir'); export const type: RequestType<string, [string, FileType][], any> = new RequestType('fs/readDir');
} }
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime, subscriptions: { dispose(): any }[]) { export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime): Disposable {
subscriptions.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => { const disposables = [];
const uri = param.uri.match(uriScheme); disposables.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
if (uri?.groups?.scheme === 'file') { const uri = Uri.parse(param.uri);
if (runtime.fs) { if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.getContent(param.uri); return runtime.fs.getContent(param.uri);
} else {
return workspace.fs.readFile(Uri.parse(param.uri)).then(buffer => {
return new runtime.TextDecoder(param.encoding).decode(buffer);
});
}
} else {
return workspace.openTextDocument(Uri.parse(param.uri)).then(doc => {
return doc.getText();
});
} }
return workspace.fs.readFile(uri).then(buffer => {
return new runtime.TextDecoder(param.encoding).decode(buffer);
});
})); }));
subscriptions.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => { disposables.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => {
const uri = uriString.match(uriScheme); const uri = Uri.parse(uriString);
if (uri?.groups?.scheme === 'file' && runtime.fs) { if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.readDirectory(uriString); return runtime.fs.readDirectory(uriString);
} }
return workspace.fs.readDirectory(Uri.parse(uriString)); return workspace.fs.readDirectory(uri);
})); }));
subscriptions.push(client.onRequest(FsStatRequest.type, (uriString: string) => { disposables.push(client.onRequest(FsStatRequest.type, (uriString: string) => {
const uri = uriString.match(uriScheme); const uri = Uri.parse(uriString);
if (uri?.groups?.scheme === 'file' && runtime.fs) { if (uri.scheme === 'file' && runtime.fs) {
return runtime.fs.stat(uriString); return runtime.fs.stat(uriString);
} }
return workspace.fs.stat(Uri.parse(uriString)); return workspace.fs.stat(uri);
})); }));
return Disposable.from(...disposables);
} }
export enum FileType { export enum FileType {