Skip to content

Commit

Permalink
Update JSONRPC
Browse files Browse the repository at this point in the history
Updates to latest version of vscode-languageserver-protocol
Removes vscode-monaco-jsonrpc dependency
Replaces references to vscode-languageserver-types with
    references to vscode-languageserver-protocol
Refactors existing code to match new interfaces

Co-authored-by: Paul Maréchal <[email protected]
Co-authored-by: Colin Grant <[email protected]>
  • Loading branch information
Colin Grant committed Nov 30, 2021
1 parent 6b9e099 commit 96a7521
Show file tree
Hide file tree
Showing 106 changed files with 746 additions and 551 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject, named, postConstruct } from '@theia/core/shared/inversify';
import { Position, DocumentUri } from '@theia/core/shared/vscode-languageserver-types';
import { Position, DocumentUri } from '@theia/core/shared/vscode-languageserver-protocol';
import { Definition, Caller, Callee } from './callhierarchy';
import { ContributionProvider, Disposable } from '@theia/core/lib/common';
import { LanguageSelector, score } from '../common/language-selector';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { injectable, inject } from '@theia/core/shared/inversify';
import { CompositeTreeNode, TreeModelImpl, TreeNode } from '@theia/core/lib/browser';
import { CallHierarchyTree, DefinitionNode } from './callhierarchy-tree';
import { CallHierarchyServiceProvider } from '../callhierarchy-service';
import { Position } from '@theia/core/shared/vscode-languageserver-types';
import { Position } from '@theia/core/shared/vscode-languageserver-protocol';
import URI from '@theia/core/lib/common/uri';
import { CancellationTokenSource } from '@theia/core/lib/common/cancellation';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { DefinitionNode, CallerNode } from './callhierarchy-tree';
import { CallHierarchyTreeModel } from './callhierarchy-tree-model';
import { CALLHIERARCHY_ID, Definition, Caller } from '../callhierarchy';
import URI from '@theia/core/lib/common/uri';
import { Location, Range, SymbolKind, DocumentUri, SymbolTag } from '@theia/core/shared/vscode-languageserver-types';
import { Location, Range, SymbolKind, DocumentUri, SymbolTag } from '@theia/core/shared/vscode-languageserver-protocol';
import { EditorManager } from '@theia/editor/lib/browser';
import * as React from '@theia/core/shared/react';

Expand Down
2 changes: 1 addition & 1 deletion packages/callhierarchy/src/browser/callhierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Range, SymbolKind, Location, SymbolTag } from '@theia/core/shared/vscode-languageserver-types';
import { Range, SymbolKind, Location, SymbolTag } from '@theia/core/shared/vscode-languageserver-protocol';

export const CALLHIERARCHY_ID = 'callhierarchy';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { injectable, inject } from '@theia/core/shared/inversify';
import { EditorManager, TextEditor } from '@theia/editor/lib/browser';
import { Location } from '@theia/core/shared/vscode-languageserver-types';
import { Location } from '@theia/core/shared/vscode-languageserver-protocol';

/**
* @deprecated since 1.15.0. Import from `@theia/editor` instead.
Expand Down
2 changes: 1 addition & 1 deletion packages/callhierarchy/src/browser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Location, Range, Position } from '@theia/core/shared/vscode-languageserver-types';
import { Location, Range, Position } from '@theia/core/shared/vscode-languageserver-protocol';

/**
* Test if `otherRange` is in `range`. If the ranges are equal, will return true.
Expand Down
4 changes: 1 addition & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ export class SomeClass {
- [`react@^16.8.0`](https://www.npmjs.com/package/react)
- [`react-dom@^16.8.0`](https://www.npmjs.com/package/react-dom)
- [`react-virtualized@^9.20.0`](https://www.npmjs.com/package/react-virtualized)
- [`vscode-languageserver-protocol@~3.15.3`](https://www.npmjs.com/package/vscode-languageserver-protocol)
- [`vscode-languageserver-types@^3.15.1`](https://www.npmjs.com/package/vscode-languageserver-types)
- [`vscode-languageserver-protocol@^3.16.0`](https://www.npmjs.com/package/vscode-languageserver-protocol)
- [`vscode-uri@^2.1.1`](https://www.npmjs.com/package/vscode-uri)
- [`vscode-ws-jsonrpc@^0.2.0`](https://www.npmjs.com/package/vscode-ws-jsonrpc)
- [`ws@^7.1.2`](https://www.npmjs.com/package/ws)
- [`yargs@^15.3.1`](https://www.npmjs.com/package/yargs)

Expand Down
8 changes: 2 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@
"route-parser": "^0.0.5",
"safer-buffer": "^2.1.2",
"uuid": "^8.3.2",
"vscode-languageserver-protocol": "~3.15.3",
"vscode-languageserver-types": "^3.15.1",
"vscode-languageserver-protocol": "^3.16.0",
"vscode-uri": "^2.1.1",
"vscode-ws-jsonrpc": "^0.2.0",
"ws": "^7.1.2",
"yargs": "^15.3.1"
},
Expand Down Expand Up @@ -101,9 +99,7 @@
"react-dom",
"react-virtualized",
"vscode-languageserver-protocol",
"vscode-languageserver-types",
"vscode-uri",
"vscode-ws-jsonrpc"
"vscode-uri"
],
"export =": [
"dompurify as DOMPurify",
Expand Down
1 change: 0 additions & 1 deletion packages/core/shared/vscode-languageserver-types.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/shared/vscode-languageserver-types.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/shared/vscode-ws-jsonrpc.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/shared/vscode-ws-jsonrpc.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/browser/progress-status-bar-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject } from 'inversify';
import { CancellationToken } from 'vscode-ws-jsonrpc';
import { CancellationToken } from '../common/cancellation';
import { ProgressClient, ProgressMessage, ProgressUpdate } from '../common';
import { StatusBar, StatusBarAlignment } from './status-bar';
import { Deferred } from '../common/promise-util';
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/common/cancellation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export namespace CancellationToken {
isCancellationRequested: true,
onCancellationRequested: shortcutEvent
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function Is(what: any): what is CancellationToken {
return typeof what === 'object'
// eslint-disable-next-line no-null/no-null
&& what !== null
&& typeof what.onCancellationRequested === 'function'
&& typeof what.isCancellationRequested === 'boolean';
}
}

export class CancellationError extends Error {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/common/logger-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { injectable } from 'inversify';
import { Logger } from 'vscode-languageserver-protocol';
import { JsonRpcServer } from './messaging/proxy-factory';

export const ILoggerServer = Symbol('ILoggerServer');
Expand Down Expand Up @@ -86,6 +87,26 @@ export namespace LogLevel {
}
}

export class ConsoleLogger implements Logger {

error(message: string): void {
console.log(message);
}

warn(message: string): void {
console.log(message);
}

info(message: string): void {
console.log(message);
}

log(message: string): void {
console.log(message);
}

}

/* eslint-disable @typescript-eslint/no-explicit-any */
export namespace ConsoleLogger {
type Console = (message?: any, ...optionalParams: any[]) => void;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/lsp-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Range } from 'vscode-languageserver-types';
import { Range } from 'vscode-languageserver-protocol';

export interface TextDocumentContentChangeDelta {
readonly range: Range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
********************************************************************************/

import { injectable, interfaces } from 'inversify';
import { ConsoleLogger, createWebSocketConnection, Logger } from 'vscode-ws-jsonrpc';
import { Logger } from 'vscode-languageserver-protocol';
import { Emitter, Event } from '../event';
import { ConsoleLogger } from '../logger-protocol';
import { ConnectionHandler } from './handler';
import { JsonRpcProxy, JsonRpcProxyFactory } from './proxy-factory';
import { WebSocketChannel } from './web-socket-channel';
import { Channel } from './channel';

/**
* Factor common logic according to `ElectronIpcConnectionProvider` and
Expand Down Expand Up @@ -80,7 +82,7 @@ export abstract class AbstractConnectionProvider<AbstractOptions extends object>
*/
listen(handler: ConnectionHandler, options?: AbstractOptions): void {
this.openChannel(handler.path, channel => {
const connection = createWebSocketConnection(channel, this.createLogger());
const connection = Channel.createMessageConnection(channel, this.createLogger());
connection.onDispose(() => channel.close());
handler.onConnection(connection);
}, options);
Expand Down
72 changes: 72 additions & 0 deletions packages/core/src/common/messaging/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/********************************************************************************
* Copyright (C) 2021 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/* eslint-disable @typescript-eslint/no-explicit-any */

import * as rpc from 'vscode-languageserver-protocol';
import { RpcMessageRelay } from './rpc-message-relay';
import { Disposable } from '../disposable';

/**
* A `Channel` represents a bidirectional logical connection to a remote.
*/
export interface Channel<T = unknown> extends Disposable {
send(content: T): void;
onMessage(cb: (data: T) => void): void;
onError(cb: (reason: any) => void): void;
onClose(cb: (code: number, reason: string) => void): void;
}

export namespace Channel {

export class MessageReader extends rpc.AbstractMessageReader implements rpc.MessageReader {
constructor(protected channel: Channel<string>) {
super();
channel.onError(error => this.fireError(error));
channel.onClose(() => this.fireClose());
}
listen(callback: rpc.DataCallback): rpc.Disposable {
this.channel.onMessage(data => callback(JSON.parse(data)));
return rpc.Disposable.create(() => {
throw new Error('not supported');
});
}
}

export class MessageWriter extends rpc.AbstractMessageWriter implements rpc.MessageWriter {
constructor(protected channel: Channel<string>) {
super();
channel.onError(error => this.fireError(error));
channel.onClose(() => this.fireClose());
}
async write(message: rpc.Message): Promise<void> {
this.channel.send(JSON.stringify(message));
}
end(): void { }
}

export function createMessageConnection(channel: Channel<string>, logger?: rpc.Logger, options?: rpc.ConnectionOptions): rpc.MessageConnection {
const reader = new MessageReader(channel);
const writer = new MessageWriter(channel);
return rpc.createMessageConnection(reader, writer, logger, options);
}

export function createRpcMessageRelay(channel: Channel<string>, onDispose: () => void = () => { }): RpcMessageRelay {
const reader = new MessageReader(channel);
const writer = new MessageWriter(channel);
return RpcMessageRelay.create(reader, writer, onDispose);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Message } from 'vscode-ws-jsonrpc';
import { Message } from 'vscode-languageserver-protocol';
import { ILogger } from '../../common';

export interface ResolvedConnectionErrorHandlerOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/messaging/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MessageConnection } from 'vscode-ws-jsonrpc';
import { MessageConnection } from 'vscode-languageserver-protocol';

export const ConnectionHandler = Symbol('ConnectionHandler');

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/common/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
export * from './handler';
export * from './proxy-factory';
export * from './connection-error-handler';
export * from './rpc-message-relay';
export * from './channel';
36 changes: 36 additions & 0 deletions packages/core/src/common/messaging/json-rpc-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/********************************************************************************
* Copyright (C) 2021 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MaybePromise, Unpromise } from '../types';

export type UndefinedOrNull<T> = T extends object
? { [K in keyof T]: UndefinedOrNull<T[K]> }
: T extends undefined ? undefined | null : T;

export interface JsonRpcMethods {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[method: string]: (...args: any[]) => MaybePromise<any>
}

/**
* Use this type to wrap your JSON-RPC service APIs to ensure you only use types that can be serialized through JSON-RPC.
*
* It replaces any occurence of `undefined` in the signature with `undefined | null` because when passing `undefined` it
* becomes `null` when serialized in JSON. The implementations must be able to handle both cases.
*/
export type JsonRpcService<T extends JsonRpcMethods> = {
[K in keyof T]: (...args: UndefinedOrNull<Parameters<T[K]>>) => Promise<UndefinedOrNull<Unpromise<ReturnType<T[K]>>>>
};
24 changes: 9 additions & 15 deletions packages/core/src/common/messaging/proxy-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as chai from 'chai';
import { ConsoleLogger } from '../../node/messaging/logger';
import { JsonRpcProxyFactory, JsonRpcProxy } from './proxy-factory';
import { createMessageConnection } from 'vscode-jsonrpc/lib/main';
import { createMessageConnection } from 'vscode-languageserver-protocol/node';
import * as stream from 'stream';

const expect = chai.expect;
Expand Down Expand Up @@ -55,22 +55,16 @@ class TestClient {

describe('Proxy-Factory', () => {

it('Should correctly send notifications and requests.', done => {
it('Should correctly send notifications', async () => {
const it = getSetup();
it.clientProxy.notifyThat('hello');
function check(): void {
if (it.client.notifications.length === 0) {
console.log('waiting another 50 ms');
setTimeout(check, 50);
} else {
expect(it.client.notifications[0]).eq('hello');
it.serverProxy.doStuff('foo').then(result => {
expect(result).to.be.eq('done: foo');
done();
});
}
}
check();
await new Promise(resolve => setTimeout(resolve, 50));
expect(it.client.notifications[0]).eq('hello');
});
it('Should correctly handle requests', async () => {
const it = getSetup();
const result = await it.serverProxy.doStuff('foo');
expect(result).to.be.eq('done: foo');
});
it('Rejected Promise should result in rejected Promise.', done => {
const it = getSetup();
Expand Down
Loading

0 comments on commit 96a7521

Please sign in to comment.