Skip to content

Commit

Permalink
Add support for CodeActions triggering commands on language servers
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Gates <ngates@palantir.com>
  • Loading branch information
gatesn committed Aug 11, 2017
1 parent 69d27ea commit 7471a0c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
4 changes: 2 additions & 2 deletions example/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const value = `{
"$schema": "http://json.schemastore.org/coffeelint",
"line_endings": "unix"
}`;
monaco.editor.create(document.getElementById("container")!, {
const editor = monaco.editor.create(document.getElementById("container")!, {
model: monaco.editor.createModel(value, 'json', monaco.Uri.parse('inmemory://model.json'))
});

Expand All @@ -40,7 +40,7 @@ listen({
}
});

const services = createMonacoServices();
const services = createMonacoServices(editor);
function createLanguageClient(connection: MessageConnection): BaseLanguageClient {
return new BaseLanguageClient({
name: "Sample Language Client",
Expand Down
19 changes: 19 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io). All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { Commands, Disposable } from 'vscode-base-languageclient/lib/services';

export class MonacoCommands implements Commands {

public constructor(private _editor: monaco.editor.IStandaloneCodeEditor) { }

public registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable {
return (this._editor as any)._commandService.addCommand(command, {
handler: (id: string, ...args: any[]) => {
console.log("Executing command", command, id, args);
callback(...args);
}
});
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
export * from './disposable';
export * from './commands';
export * from './console-window';
export * from './languages';
export * from './workspace';
Expand Down
8 changes: 5 additions & 3 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
* ------------------------------------------------------------------------------------------ */
import { BaseLanguageClient } from "vscode-base-languageclient/lib/base";
import { MonacoToProtocolConverter, ProtocolToMonacoConverter } from "./converter";
import { MonacoCommands } from './commands';
import { MonacoLanguages } from "./languages";
import { MonacoWorkspace } from "./workspace";
import { ConsoleWindow } from "./console-window";

export function createMonacoServices(): BaseLanguageClient.IServices {
export function createMonacoServices(editor: monaco.editor.IStandaloneCodeEditor): BaseLanguageClient.IServices {
const m2p = new MonacoToProtocolConverter();
const p2m = new ProtocolToMonacoConverter();
return {
commands: new MonacoCommands(editor),
languages: new MonacoLanguages(p2m, m2p),
workspace: new MonacoWorkspace(m2p),
window: new ConsoleWindow()
workspace: new MonacoWorkspace(p2m, m2p),
window: new ConsoleWindow(),
}
}
38 changes: 35 additions & 3 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io). All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { MonacoToProtocolConverter } from './converter';
import { MonacoToProtocolConverter, ProtocolToMonacoConverter } from './converter';
import { Workspace, TextDocumentDidChangeEvent, TextDocument, Event, Emitter } from "vscode-base-languageclient/lib/services";
import { WorkspaceEdit, TextEdit } from 'vscode-base-languageclient/lib/base';
import IModel = monaco.editor.IModel;

export class MonacoWorkspace implements Workspace {
Expand All @@ -15,8 +16,7 @@ export class MonacoWorkspace implements Workspace {
protected readonly onDidCloseTextDocumentEmitter = new Emitter<TextDocument>();
protected readonly onDidChangeTextDocumentEmitter = new Emitter<TextDocumentDidChangeEvent>();

constructor(
protected readonly m2p: MonacoToProtocolConverter) {
constructor(protected readonly p2m: ProtocolToMonacoConverter, protected readonly m2p: MonacoToProtocolConverter) {
for (const model of monaco.editor.getModels()) {
this.addModel(model);
}
Expand Down Expand Up @@ -83,4 +83,36 @@ export class MonacoWorkspace implements Workspace {
return this.onDidChangeTextDocumentEmitter.event;
}

public applyEdit(workspaceEdit: WorkspaceEdit): Thenable<boolean> {
let applied = true;
if (workspaceEdit.documentChanges) {
for (const change of workspaceEdit.documentChanges) {
if (change.textDocument.version && change.textDocument.version >= 0) {
const textDocument = this.documents.get(change.textDocument.uri);
if (textDocument && textDocument.version === change.textDocument.version) {
monaco.editor.getModel(monaco.Uri.parse(textDocument.uri)).pushEditOperations(
[], // Do not try and preserve editor selections.
change.edits.map((edit: TextEdit) => {
return {
identifier: {major: 1, minor: 0},
range: this.p2m.asRange(edit.range),
text: edit.newText,
forceMoveMarkers: true,
};
}),
() => [], // Do not try and preserve editor selections.
);
} else {
applied = false;
}
} else {
applied = false;
}
}
} else {
applied = false;
}
return Promise.resolve(applied);
}

}

0 comments on commit 7471a0c

Please sign in to comment.