Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URI.joinPath() API #9422

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions packages/plugin-ext/src/common/rpc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ export class RPCProtocolImpl implements RPCProtocol {
private readonly pendingRPCReplies = new Map<string, Deferred<any>>();
private readonly multiplexer: RPCMultiplexer;

private replacer: (key: string | undefined, value: any) => any;
private reviver: (key: string | undefined, value: any) => any;

private readonly toDispose = new DisposableCollection(
Disposable.create(() => { /* mark as no disposed */ })
);

constructor(connection: MessageConnection) {
constructor(connection: MessageConnection, transformations?: {
replacer?: (key: string | undefined, value: any) => any,
reviver?: (key: string | undefined, value: any) => any
}) {
this.toDispose.push(
this.multiplexer = new RPCMultiplexer(connection)
);
Expand All @@ -101,6 +107,9 @@ export class RPCProtocolImpl implements RPCProtocol {
}
this.pendingRPCReplies.clear();
}));

this.reviver = transformations?.reviver || ObjectsTransferrer.reviver;
this.replacer = transformations?.replacer || ObjectsTransferrer.replacer;
}

private get isDisposed(): boolean {
Expand Down Expand Up @@ -163,12 +172,12 @@ export class RPCProtocolImpl implements RPCProtocol {
if (cancellationToken) {
args.push('add.cancellation.token');
cancellationToken.onCancellationRequested(() =>
this.multiplexer.send(MessageFactory.cancel(callId))
this.multiplexer.send(this.cancel(callId))
);
}

this.pendingRPCReplies.set(callId, result);
this.multiplexer.send(MessageFactory.request(callId, proxyId, methodName, args));
this.multiplexer.send(this.request(callId, proxyId, methodName, args));
return result.promise;
}

Expand All @@ -177,7 +186,7 @@ export class RPCProtocolImpl implements RPCProtocol {
return;
}
try {
const msg = <RPCMessage>JSON.parse(rawmsg, ObjectsTransferrer.reviver);
const msg = <RPCMessage>JSON.parse(rawmsg, this.reviver);

switch (msg.type) {
case MessageType.Request:
Expand Down Expand Up @@ -224,10 +233,10 @@ export class RPCProtocolImpl implements RPCProtocol {

invocation.then(result => {
this.cancellationTokenSources.delete(callId);
this.multiplexer.send(MessageFactory.replyOK(callId, result));
this.multiplexer.send(this.replyOK(callId, result));
}, error => {
this.cancellationTokenSources.delete(callId);
this.multiplexer.send(MessageFactory.replyErr(callId, error));
this.multiplexer.send(this.replyErr(callId, error));
});
}

Expand Down Expand Up @@ -278,6 +287,29 @@ export class RPCProtocolImpl implements RPCProtocol {
}
return method.apply(actor, args);
}

private cancel(req: string): string {
return `{"type":${MessageType.Cancel},"id":"${req}"}`;
}

private request(req: string, rpcId: string, method: string, args: any[]): string {
return `{"type":${MessageType.Request},"id":"${req}","proxyId":"${rpcId}","method":"${method}","args":${JSON.stringify(args, this.replacer)}}`;
}

private replyOK(req: string, res: any): string {
if (typeof res === 'undefined') {
return `{"type":${MessageType.Reply},"id":"${req}"}`;
}
return `{"type":${MessageType.Reply},"id":"${req}","res":${safeStringify(res, this.replacer)}}`;
}

private replyErr(req: string, err: any): string {
err = typeof err === 'string' ? new Error(err) : err;
if (err instanceof Error) {
return `{"type":${MessageType.ReplyErr},"id":"${req}","err":${safeStringify(transformErrorForSerialization(err))}}`;
}
return `{"type":${MessageType.ReplyErr},"id":"${req}","err":null}`;
}
}

function canceled(): Error {
Expand Down Expand Up @@ -346,32 +378,6 @@ class RPCMultiplexer implements Disposable, MessageConnection {
}
}

class MessageFactory {

static cancel(req: string): string {
return `{"type":${MessageType.Cancel},"id":"${req}"}`;
}

public static request(req: string, rpcId: string, method: string, args: any[]): string {
return `{"type":${MessageType.Request},"id":"${req}","proxyId":"${rpcId}","method":"${method}","args":${JSON.stringify(args, ObjectsTransferrer.replacer)}}`;
}

public static replyOK(req: string, res: any): string {
if (typeof res === 'undefined') {
return `{"type":${MessageType.Reply},"id":"${req}"}`;
}
return `{"type":${MessageType.Reply},"id":"${req}","res":${safeStringify(res, ObjectsTransferrer.replacer)}}`;
}

public static replyErr(req: string, err: any): string {
err = typeof err === 'string' ? new Error(err) : err;
if (err instanceof Error) {
return `{"type":${MessageType.ReplyErr},"id":"${req}","err":${safeStringify(transformErrorForSerialization(err))}}`;
}
return `{"type":${MessageType.ReplyErr},"id":"${req}","err":null}`;
}
}

/**
* These functions are responsible for correct transferring objects via rpc channel.
*
Expand All @@ -382,7 +388,7 @@ class MessageFactory {
* To distinguish between regular and altered objects, field $type is added to altered ones.
* Also value of that field specifies kind of the object.
*/
namespace ObjectsTransferrer {
export namespace ObjectsTransferrer {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function replacer(key: string | undefined, value: any): any {
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-ext/src/hosted/browser/worker/worker-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { KeyValueStorageProxy } from '../../../plugin/plugin-storage';
import { WebviewsExtImpl } from '../../../plugin/webviews';
import { loadManifest } from './plugin-manifest-loader';
import { TerminalServiceExtImpl } from '../../../plugin/terminal-ext';
import { reviver } from '../../../plugin/types-impl';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ctx = self as any;
Expand All @@ -45,8 +46,12 @@ const rpc = new RPCProtocolImpl({
onMessage: emitter.event,
send: (m: string) => {
ctx.postMessage(m);
}
},
},
{
reviver: reviver
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
addEventListener('message', (message: any) => {
emitter.fire(message.data);
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-ext/src/hosted/node/plugin-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import { Emitter } from '@theia/core/lib/common/event';
import { RPCProtocolImpl, MessageType, ConnectionClosedError } from '../../common/rpc-protocol';
import { PluginHostRPC } from './plugin-host-rpc';
import { reviver } from '../../plugin/types-impl';

console.log('PLUGIN_HOST(' + process.pid + ') starting instance');

// override exit() function, to do not allow plugin kill this node
Expand Down Expand Up @@ -80,6 +82,9 @@ const rpc = new RPCProtocolImpl({
process.send(m);
}
}
},
{
reviver: reviver
});

process.on('message', async (message: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as theia from '@theia/plugin';
import { RPCProtocol } from '../common/rpc-protocol';
import { CommandRegistryImpl } from './command-registry';
import { UriComponents } from '../common/uri-components';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import {
Range,
Comment,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/custom-editors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { CustomEditorsExt, CustomEditorsMain, PLUGIN_RPC_CONTEXT } from '../comm
import * as theia from '@theia/plugin';
import { RPCProtocol } from '../common/rpc-protocol';
import { Plugin } from '../common/plugin-api-rpc';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { UriComponents } from '../common/uri-components';
import { DocumentsExtImpl } from './documents';
import { WebviewImpl, WebviewsExtImpl } from './webviews';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../common/plugin-api-rpc';
import { Event } from '@theia/core/lib/common/event';
import { RPCProtocol } from '../common/rpc-protocol';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { Disposable } from './types-impl';

export class DecorationsExtImpl implements DecorationsExt {
Expand Down
14 changes: 7 additions & 7 deletions packages/plugin-ext/src/plugin/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { PLUGIN_RPC_CONTEXT as Ext, OpenDialogOptionsMain, DialogsMain, SaveDialogOptionsMain, UploadDialogOptionsMain } from '../common/plugin-api-rpc';
import { OpenDialogOptions, SaveDialogOptions, UploadDialogOptions } from '@theia/plugin';
import { RPCProtocol } from '../common/rpc-protocol';
import { URI as Uri } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';

export class DialogsExtImpl {
private proxy: DialogsMain;
Expand All @@ -25,7 +25,7 @@ export class DialogsExtImpl {
this.proxy = rpc.getProxy(Ext.DIALOGS_MAIN);
}

showOpenDialog(options: OpenDialogOptions): PromiseLike<Uri[] | undefined> {
showOpenDialog(options: OpenDialogOptions): PromiseLike<URI[] | undefined> {
const optionsMain = {
title: options.title,
openLabel: options.openLabel,
Expand All @@ -41,7 +41,7 @@ export class DialogsExtImpl {
if (result) {
const uris = [];
for (let i = 0; i < result.length; i++) {
const uri = Uri.parse('file://' + result[i]);
const uri = URI.parse('file://' + result[i]);
uris.push(uri);
}
resolve(uris);
Expand All @@ -54,7 +54,7 @@ export class DialogsExtImpl {
});
}

showSaveDialog(options: SaveDialogOptions): PromiseLike<Uri | undefined> {
showSaveDialog(options: SaveDialogOptions): PromiseLike<URI | undefined> {
const optionsMain = {
title: options.title,
saveLabel: options.saveLabel,
Expand All @@ -65,7 +65,7 @@ export class DialogsExtImpl {
return new Promise((resolve, reject) => {
this.proxy.$showSaveDialog(optionsMain).then(result => {
if (result) {
resolve(Uri.parse('file://' + result));
resolve(URI.parse('file://' + result));
} else {
resolve(undefined);
}
Expand All @@ -75,15 +75,15 @@ export class DialogsExtImpl {
});
}

showUploadDialog(options: UploadDialogOptions): PromiseLike<Uri[] | undefined> {
showUploadDialog(options: UploadDialogOptions): PromiseLike<URI[] | undefined> {
const optionsMain = {
defaultUri: options.defaultUri ? options.defaultUri.path : undefined
} as UploadDialogOptionsMain;

return new Promise((resolve, reject) => {
this.proxy.$showUploadDialog(optionsMain).then(result => {
if (result) {
resolve(result.map(uri => Uri.parse(uri)));
resolve(result.map(uri => URI.parse(uri)));
} else {
resolve(undefined);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/document-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as theia from '@theia/plugin';
import { ModelChangedEvent, DocumentsMain } from '../common/plugin-api-rpc';
import { Range as ARange } from '../common/plugin-api-rpc-model';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { ok } from '../common/assert';
import { Range, Position, EndOfLine } from './types-impl';
import { PrefixSumComputer } from './prefix-sum-computer';
Expand Down
14 changes: 7 additions & 7 deletions packages/plugin-ext/src/plugin/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* based on https://github.com/Microsoft/vscode/blob/bf9a27ec01f2ef82fc45f69e0c946c7d74a57d3e/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts
*/
import { DocumentsExt, ModelChangedEvent, PLUGIN_RPC_CONTEXT, DocumentsMain, SingleEditOperation } from '../common/plugin-api-rpc';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { UriComponents } from '../common/uri-components';
import { RPCProtocol } from '../common/rpc-protocol';
import { Emitter, Event } from '@theia/core/lib/common/event';
Expand Down Expand Up @@ -179,12 +179,12 @@ export class DocumentsExtImpl implements DocumentsExt {
this._onDidChangeDocument.fire({
document: data.document,
contentChanges: e.changes.map(change =>
({
range: Converter.toRange(change.range),
rangeOffset: change.rangeOffset,
rangeLength: change.rangeLength,
text: change.text
}))
({
range: Converter.toRange(change.range),
rangeOffset: change.rangeOffset,
rangeLength: change.rangeLength,
text: change.text
}))
});
}
getAllDocumentData(): DocumentDataExt[] {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/editors-and-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DocumentDataExt } from './document-data';
import { ok } from '../common/assert';
import * as Converter from './type-converters';
import { dispose } from '../common/disposable-util';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';

export class EditorsAndDocumentsExtImpl implements EditorsAndDocumentsExt {
private activeEditorId: string | null = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

import { Emitter, WaitUntilEvent, AsyncEmitter } from '@theia/core/lib/common/event';
import { IRelativePattern, parse } from '@theia/callhierarchy/lib/common/glob';
import { URI, UriComponents } from '@theia/core/shared/vscode-uri';
import { UriComponents } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { EditorsAndDocumentsExtImpl as ExtHostDocumentsAndEditors } from './editors-and-documents';
import type * as vscode from '@theia/plugin';
import * as typeConverter from './type-converters';
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/plugin/file-system-ext-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
/* eslint-disable @typescript-eslint/tslint/config */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { URI, UriComponents } from '@theia/core/shared/vscode-uri';
import { UriComponents } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { RPCProtocol } from '../common/rpc-protocol';
import { PLUGIN_RPC_CONTEXT, FileSystemExt, FileSystemMain, IFileChangeDto } from '../common/plugin-api-rpc';
import * as vscode from '@theia/plugin';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/known-commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as assert from 'assert';
import { KnownCommands } from './known-commands';
import { URI } from '@theia/core/shared/vscode-uri';
import { URI } from './types-impl';
import { Position } from './types-impl';
import { fromPosition } from './type-converters';

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-ext/src/plugin/known-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
********************************************************************************/

import { Range as R, Position as P, Location as L } from '@theia/core/shared/vscode-languageserver-types';
import { URI } from '@theia/core/shared/vscode-uri';
import * as theia from '@theia/plugin';
import { cloneAndChange } from '../common/objects';
import { Position, Range, Location, CallHierarchyItem } from './types-impl';
import { Position, Range, Location, CallHierarchyItem, URI } from './types-impl';
import {
fromPosition, fromRange, fromLocation,
isModelLocation, toLocation,
Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-ext/src/plugin/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import { RPCProtocol } from '../common/rpc-protocol';
import * as theia from '@theia/plugin';
import { DocumentsExtImpl } from './documents';
import { PluginModel } from '../common/plugin-protocol';
import { Disposable } from './types-impl';
import { URI } from '@theia/core/shared/vscode-uri';
import { Disposable, URI } from './types-impl';
import { UriComponents } from '../common/uri-components';
import {
CompletionContext,
Expand Down Expand Up @@ -640,11 +639,11 @@ function serializeEnterRules(rules?: theia.OnEnterRule[]): SerializedOnEnterRule
}

return rules.map(r =>
({
action: r.action,
beforeText: serializeRegExp(r.beforeText),
afterText: serializeRegExp(r.afterText)
} as SerializedOnEnterRule));
({
action: r.action,
beforeText: serializeRegExp(r.beforeText),
afterText: serializeRegExp(r.afterText)
} as SerializedOnEnterRule));
}

function serializeRegExp(regexp?: RegExp): SerializedRegExp | undefined {
Expand Down
Loading