Skip to content

Commit

Permalink
fix #24: get rid of vscode-base-languageclient
Browse files Browse the repository at this point in the history
vscode compatiblity layer is introduced to make use of vscode-languageclient directly instead of vscode-base-languageclient.

Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Jul 25, 2018
1 parent b79d075 commit 4525080
Show file tree
Hide file tree
Showing 22 changed files with 5,361 additions and 1,158 deletions.
3,043 changes: 2,058 additions & 985 deletions example/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"rimraf": "^2.6.2",
"source-map-loader": "^0.2.3",
"style-loader": "^0.20.3",
"typescript": "^2.7.2",
"typescript": "^2.9.2",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "^3.11.0",
"webpack-merge": "^4.1.2"
Expand Down
15 changes: 8 additions & 7 deletions example/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* ------------------------------------------------------------------------------------------ */
import { listen, MessageConnection } from 'vscode-ws-jsonrpc';
import {
BaseLanguageClient, CloseAction, ErrorAction,
createMonacoServices, createConnection
MonacoLanguageClient, CloseAction, ErrorAction,
MonacoServices, createConnection
} from 'monaco-languageclient';
import normalizeUrl = require('normalize-url');
const ReconnectingWebSocket = require('reconnecting-websocket');
Expand All @@ -31,6 +31,9 @@ const editor = monaco.editor.create(document.getElementById("container")!, {
}
});

// install Monaco language client services
MonacoServices.install(editor);

// create the web socket
const url = createUrl('/sampleServer')
const webSocket = createWebSocket(url);
Expand All @@ -45,9 +48,8 @@ listen({
}
});

const services = createMonacoServices(editor);
function createLanguageClient(connection: MessageConnection): BaseLanguageClient {
return new BaseLanguageClient({
function createLanguageClient(connection: MessageConnection): MonacoLanguageClient {
return new MonacoLanguageClient({
name: "Sample Language Client",
clientOptions: {
// use a language id as a document selector
Expand All @@ -58,14 +60,13 @@ function createLanguageClient(connection: MessageConnection): BaseLanguageClient
closed: () => CloseAction.DoNotRestart
}
},
services,
// create a language client connection from the JSON RPC connection on demand
connectionProvider: {
get: (errorHandler, closeHandler) => {
return Promise.resolve(createConnection(connection, errorHandler, closeHandler))
}
}
})
});
}

function createUrl(path: string): string {
Expand Down
50 changes: 33 additions & 17 deletions example/src/json-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TextDocument, Diagnostic, Command, CompletionList, CompletionItem, Hover,
SymbolInformation, DocumentSymbolParams, TextEdit
} from "vscode-languageserver-types";
import { TextDocumentPositionParams, DocumentRangeFormattingParams, ExecuteCommandParams, CodeActionParams } from 'vscode-base-languageclient/lib/protocol';
import { TextDocumentPositionParams, DocumentRangeFormattingParams, ExecuteCommandParams, CodeActionParams } from 'vscode-languageserver-protocol';
import { getLanguageService, LanguageService, JSONDocument } from "vscode-json-languageservice";

export function start(reader: MessageReader, writer: MessageWriter): JsonServer {
Expand Down Expand Up @@ -69,7 +69,7 @@ export class JsonServer {
}
}
});
this.connection.onCodeAction(params =>
this.connection.onCodeAction(params =>
this.codeAction(params)
);
this.connection.onCompletion(params =>
Expand Down Expand Up @@ -97,48 +97,61 @@ export class JsonServer {
}

protected codeAction(params: CodeActionParams): Command[] {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return [{
title: "Upper Case Document",
command: "json.documentUpper",
// Send a VersionedTextDocumentIdentifier
arguments: [{
...params.textDocument,
version: this.documents.get(params.textDocument.uri).version
version: document.version
}]
}];
}

protected format(params: DocumentRangeFormattingParams): TextEdit[] {
const document = this.documents.get(params.textDocument.uri);
return this.jsonService.format(document, params.range, params.options)
return document ? this.jsonService.format(document, params.range, params.options) : [];
}

protected findDocumentSymbols(params: DocumentSymbolParams): SymbolInformation[] {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
return [];
}
const jsonDocument = this.getJSONDocument(document);
return this.jsonService.findDocumentSymbols(document, jsonDocument);
}

protected executeCommand(params: ExecuteCommandParams): any {
if (params.command === "json.documentUpper" && params.arguments) {
const versionedTextDocumentIdentifier = params.arguments[0];
this.connection.workspace.applyEdit({
documentChanges: [{
textDocument: versionedTextDocumentIdentifier,
edits: [{
range: {
start: {line: 0, character: 0},
end: {line: Number.MAX_SAFE_INTEGER, character: Number.MAX_SAFE_INTEGER}
},
newText: this.documents.get(versionedTextDocumentIdentifier.uri).getText().toUpperCase()
const document = this.documents.get(versionedTextDocumentIdentifier.uri);
if (document) {
this.connection.workspace.applyEdit({
documentChanges: [{
textDocument: versionedTextDocumentIdentifier,
edits: [{
range: {
start: { line: 0, character: 0 },
end: { line: Number.MAX_SAFE_INTEGER, character: Number.MAX_SAFE_INTEGER }
},
newText: document.getText().toUpperCase()
}]
}]
}]
});
});
}
}
}

protected hover(params: TextDocumentPositionParams): Thenable<Hover> {
protected hover(params: TextDocumentPositionParams): Thenable<Hover | null> {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
return Promise.resolve(null);
}
const jsonDocument = this.getJSONDocument(document);
return this.jsonService.doHover(document, params.position, jsonDocument);
}
Expand All @@ -163,8 +176,11 @@ export class JsonServer {
return this.jsonService.doResolve(item);
}

protected completion(params: TextDocumentPositionParams): Thenable<CompletionList> {
protected completion(params: TextDocumentPositionParams): Thenable<CompletionList | null> {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
return Promise.resolve(null);
}
const jsonDocument = this.getJSONDocument(document);
return this.jsonService.doComplete(document, params.position, jsonDocument);
}
Expand Down
5 changes: 5 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const common = {
child_process: 'empty',
net: 'empty',
crypto: 'empty'
},
resolve: {
alias: {
'vscode': require.resolve('monaco-languageclient/lib/vscode-compatibility')
}
}
};

Expand Down
Loading

0 comments on commit 4525080

Please sign in to comment.