Skip to content

Commit

Permalink
[move] Language server initialization
Browse files Browse the repository at this point in the history
Implement language server initialization and an event loop in the
move-analyzer language server executable. In tandem, implement a client
for the server in the move-analyzer VS Code extension.

For now, the server only advertises a single capability: "go to
definition." This capability causes VS Code to display a "go to
definition" command option when users have the move-analyzer extension
installed. However, for now, when that option is used, the server panics
(implementing a response is a "TODO").

However, despite the panic, several "features" are added as part of this
commit: users of the VS Code extension will see logs in their "Move
Analyzer Client" and "Move Language Server" output channels indicating
that the client and server have initialized and connected to one
another.

Closes: #9463
  • Loading branch information
modocache authored and bors-libra committed Oct 19, 2021
1 parent 1b54bf4 commit 084d391
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 60 deletions.
32 changes: 31 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions common/workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ tokio-util = { version = "0.6.7", features = ["codec", "compat", "default", "fut
toml = { version = "0.5.8", features = ["default"] }
tracing = { version = "0.1.26", features = ["attributes", "default", "log", "std", "tracing-attributes"] }
tracing-core = { version = "0.1.18", features = ["default", "lazy_static", "std"] }
url = { version = "2.2.2", default-features = false, features = ["serde"] }
warp = { version = "0.3.0", features = ["default", "multipart", "tls", "tokio-rustls", "tokio-tungstenite", "websocket"] }
zeroize = { version = "1.2.0", features = ["alloc", "default", "zeroize_derive"] }

Expand Down Expand Up @@ -124,6 +125,7 @@ tokio-util = { version = "0.6.7", features = ["codec", "compat", "default", "fut
toml = { version = "0.5.8", features = ["default"] }
tracing = { version = "0.1.26", features = ["attributes", "default", "log", "std", "tracing-attributes"] }
tracing-core = { version = "0.1.18", features = ["default", "lazy_static", "std"] }
url = { version = "2.2.2", default-features = false, features = ["serde"] }
warp = { version = "0.3.0", features = ["default", "multipart", "tls", "tokio-rustls", "tokio-tungstenite", "websocket"] }
zeroize = { version = "1.2.0", features = ["alloc", "default", "zeroize_derive"] }

Expand Down Expand Up @@ -180,6 +182,7 @@ tokio-util = { version = "0.6.7", features = ["codec", "compat", "default", "fut
toml = { version = "0.5.8", features = ["default"] }
tracing = { version = "0.1.26", features = ["attributes", "default", "log", "std", "tracing-attributes"] }
tracing-core = { version = "0.1.18", features = ["default", "lazy_static", "std"] }
url = { version = "2.2.2", default-features = false, features = ["serde"] }
warp = { version = "0.3.0", features = ["default", "multipart", "tls", "tokio-rustls", "tokio-tungstenite", "websocket"] }
zeroize = { version = "1.2.0", features = ["alloc", "default", "zeroize_derive"] }

Expand Down Expand Up @@ -241,6 +244,7 @@ tokio-util = { version = "0.6.7", features = ["codec", "compat", "default", "fut
toml = { version = "0.5.8", features = ["default"] }
tracing = { version = "0.1.26", features = ["attributes", "default", "log", "std", "tracing-attributes"] }
tracing-core = { version = "0.1.18", features = ["default", "lazy_static", "std"] }
url = { version = "2.2.2", default-features = false, features = ["serde"] }
warp = { version = "0.3.0", features = ["default", "multipart", "tls", "tokio-rustls", "tokio-tungstenite", "websocket"] }
zeroize = { version = "1.2.0", features = ["alloc", "default", "zeroize_derive"] }

Expand Down
3 changes: 3 additions & 0 deletions language/move-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ publish = false
edition = "2018"

[dependencies]
lsp-server = "0.5.1"
lsp-types = "0.90.1"
serde_json = "1.0.64"
structopt = "0.3.21"

[dev-dependencies]
Expand Down
75 changes: 65 additions & 10 deletions language/move-analyzer/editors/code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion language/move-analyzer/editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"keywords": [
"move"
],
"main": "./out/src/extension.js",
"main": "./out/src/main.js",
"activationEvents": [
"onLanguage:move"
],
Expand Down Expand Up @@ -85,6 +85,9 @@
"package": "vsce package -o move-analyzer.vsix",
"publish": "npm run pretest && npm run test && vsce publish"
},
"dependencies": {
"vscode-languageclient": "6.1.4"
},
"devDependencies": {
"@types/glob": "^7.1.4",
"@types/mocha": "^9.0.0",
Expand Down
5 changes: 5 additions & 0 deletions language/move-analyzer/editors/code/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class Configuration {
this.configuration = vscode.workspace.getConfiguration('move-analyzer');
}

/** A string representation of the configured values, for logging purposes. */
toString(): string {
return JSON.stringify(this.configuration);
}

/** The path to the move-analyzer executable. */
get serverPath(): string {
const path = this.configuration.get<string>('server.path', 'move-analyzer');
Expand Down
44 changes: 40 additions & 4 deletions language/move-analyzer/editors/code/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
import type { Configuration } from './configuration';
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { log } from './log';

/** Information passed along to each VS Code command defined by this extension. */
export class Context {
private constructor(
private readonly extension: Readonly<vscode.ExtensionContext>,
private readonly extensionContext: Readonly<vscode.ExtensionContext>,
readonly configuration: Readonly<Configuration>,
) { }

static create(
extension: Readonly<vscode.ExtensionContext>,
extensionContext: Readonly<vscode.ExtensionContext>,
configuration: Readonly<Configuration>,
): Context | Error {
if (!fs.existsSync(configuration.serverPath)) {
return new Error(`command '${configuration.serverPath}' could not be found.`);
}
return new Context(extension, configuration);
return new Context(extensionContext, configuration);
}

/**
Expand All @@ -36,6 +38,40 @@ export class Context {
const disposable = vscode.commands.registerCommand(`move-analyzer.${name}`, async () => {
return command(this);
});
this.extension.subscriptions.push(disposable);
this.extensionContext.subscriptions.push(disposable);
}

/**
* Configures and starts the client that interacts with the language server.
*
* The "client" is an object that sends messages to the language server, which in Move's case is
* the `move-analyzer` executable. Unlike registered extension commands such as
* `move-analyzer.serverVersion`, which are manually executed by a VS Code user via the command
* palette or menu, this client sends many of its messages on its own (for example, when it
* starts, it sends the "initialize" request).
*
* To read more about the messages sent and responses received by this client, such as
* "initialize," read [the Language Server Protocol specification](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize).
**/
startClient(): void {
const executable: lc.Executable = {
command: this.configuration.serverPath,
};
const serverOptions: lc.ServerOptions = {
run: executable,
debug: executable,
};
const clientOptions: lc.LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'move' }],
};
const client = new lc.LanguageClient(
'move-analyzer',
'Move Language Server',
serverOptions,
clientOptions,
);
log.info('Starting client...');
const disposable = client.start();
this.extensionContext.subscriptions.push(disposable);
}
}
63 changes: 20 additions & 43 deletions language/move-analyzer/editors/code/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

import { Configuration } from './configuration';
import { Context } from './context';
import * as child_process from 'child_process';
import * as assert from 'assert';
import * as vscode from 'vscode';

/**
* An extension command that displays the version of the server that this extension
* interfaces with.
*/
async function serverVersion(context: Readonly<Context>): Promise<void> {
const version = child_process.spawnSync(
context.configuration.serverPath, ['--version'], { encoding: 'utf8' },
);
if (version.stdout) {
await vscode.window.showInformationMessage(version.stdout);
} else if (version.error) {
await vscode.window.showErrorMessage(
`Could not execute move-analyzer: ${version.error.message}.`,
);
} else {
await vscode.window.showErrorMessage(
`A problem occurred when executing '${context.configuration.serverPath}'.`,
);
}
}
/** Information related to this extension itself, such as its identifier and version. */
export class Extension {
/** The string used to uniquely identify this particular extension to VS Code. */
readonly identifier = 'move.move-analyzer';

/**
* The entry point to this VS Code extension.
*
* As per [the VS Code documentation on activation
* events](https://code.visualstudio.com/api/references/activation-events), "an extension must
* export an `activate()` function from its main module and it will be invoked only once by
* VS Code when any of the specified activation events [are] emitted."
*
* Activation events for this extension are listed in its `package.json` file, under the key
* `"activationEvents"`.
*/
export function activate(extensionContext: Readonly<vscode.ExtensionContext>): void {
const configuration = new Configuration();
const context = Context.create(extensionContext, configuration);
if (context instanceof Error) {
void vscode.window.showErrorMessage(
`Could not activate move-analyzer: ${context.message}.`,
);
return;
private readonly extension: vscode.Extension<unknown>;

constructor() {
const extension = vscode.extensions.getExtension(this.identifier);
assert(extension !== undefined, `extension ${this.identifier} is not available`);
this.extension = extension;
}

context.registerCommand('serverVersion', serverVersion);
/** The version string. */
get version(): string {
for (const entry of Object.entries(this.extension.packageJSON)) {
if (entry[0] === 'version') {
return entry[1] as string;
}
}
return 'unknown';
}
}
Loading

0 comments on commit 084d391

Please sign in to comment.