Skip to content

Commit

Permalink
[move] Add move-analyzer --version
Browse files Browse the repository at this point in the history
Add a `--version` option to the `move-analyzer` executable, and a command to
display the version to the Visual Studio extension. To do so:

* Add a method of configuring the location of the `move-analyzer`
  executable that the extension interfaces with.
* Add a programmatic entry point to the extension (previously the
  extension was just a static grammar file and programmatic tests).

Other small improvements include:

* Add a linter for TypeScript documentation.
* Update the versions of the Node dependencies used by the VS Code
  extension.
* Tweak linter settings -- in particular, ignore readonly parameter
  warnings since VS Code's API provides many mutable types.

Closes: #9405
  • Loading branch information
modocache authored and bors-libra committed Oct 13, 2021
1 parent 411b76b commit bf68a39
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 282 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}/language/move-analyzer/editors/code"
]
],
"preLaunchTask": "Pretest"
},
{
"name": "VS Code Tokenizer Tests",
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions language/move-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ homepage = "https://diem.com"
license = "Apache-2.0"
publish = false
edition = "2018"

[dependencies]
structopt = "0.3.21"

[dev-dependencies]
diem-workspace-hack = { path = "../../common/workspace-hack" }
18 changes: 14 additions & 4 deletions language/move-analyzer/editors/code/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

// Plug-in eslint rules from @typescript-eslint.
"plugins": [
"@typescript-eslint"
"@typescript-eslint",
"eslint-plugin-tsdoc"
],
// Use the core rules that eslint and @typescript-eslint recommend.
"extends": [
Expand Down Expand Up @@ -187,7 +188,6 @@
"@typescript-eslint/prefer-literal-enum-member": "warn",
"@typescript-eslint/prefer-nullish-coalescing": "warn",
"@typescript-eslint/prefer-optional-chain": "warn",
"@typescript-eslint/prefer-readonly-parameter-types": "warn",
"@typescript-eslint/prefer-readonly": "warn",
"@typescript-eslint/prefer-reduce-type-parameter": "warn",
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
Expand All @@ -196,10 +196,20 @@
"@typescript-eslint/require-array-sort-compare": "warn",
"@typescript-eslint/return-await": "warn",
"@typescript-eslint/semi": "warn",
"@typescript-eslint/space-before-function-paren": ["warn", "never"],
"@typescript-eslint/space-before-function-paren": [
"warn",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
],
"@typescript-eslint/strict-boolean-expressions": "warn",
"@typescript-eslint/switch-exhaustiveness-check": "warn",
"@typescript-eslint/type-annotation-spacing": "warn",
"@typescript-eslint/unified-signatures": "warn"
"@typescript-eslint/unified-signatures": "warn",

// The following are eslint-plugin-tsdoc rules:
"tsdoc/syntax": "warn"
}
}
1 change: 1 addition & 0 deletions language/move-analyzer/editors/code/.vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# https://code.visualstudio.com/api/working-with-extensions/publishing-extension#.vscodeignore

**/*
out/src/**/*
!language-configuration.json
!move.tmLanguage.json
!package-lock.json
Expand Down
543 changes: 279 additions & 264 deletions language/move-analyzer/editors/code/package-lock.json

Large diffs are not rendered by default.

56 changes: 43 additions & 13 deletions language/move-analyzer/editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,44 @@
"url": "https://github.com/diem/diem/issues"
},
"engines": {
"vscode": "^1.60.0"
"vscode": "^1.60.1"
},
"categories": [
"Programming Languages"
],
"keywords": [
"move"
],
"main": "./out/src/extension.js",
"activationEvents": [
"onLanguage:move"
],
"contributes": {
"commands": [
{
"command": "move-analyzer.serverVersion",
"title": "Show Server Version",
"category": "Move Analyzer"
}
],
"configuration": {
"type": "object",
"title": "Move Analyzer",
"properties": {
"move-analyzer.server.path": {
"type": "string",
"default": "move-analyzer",
"markdownDescription": "Path and filename of the move-analyzer executable, e.g. `/usr/bin/move-analyzer`."
}
}
},
"grammars": [
{
"language": "move",
"scopeName": "source.move",
"path": "./move.tmLanguage.json"
}
],
"languages": [
{
"id": "move",
Expand All @@ -37,13 +66,13 @@
"configuration": "./language-configuration.json"
}
],
"grammars": [
{
"language": "move",
"scopeName": "source.move",
"path": "./move.tmLanguage.json"
}
]
"menus": {
"commandPalette": [
{
"command": "move-analyzer.serverVersion"
}
]
}
},
"scripts": {
"compile": "tsc -p ./",
Expand All @@ -58,15 +87,16 @@
"devDependencies": {
"@types/glob": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "14.x",
"@types/vscode": "^1.60.0",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"@types/node": "^14.17.22",
"@types/vscode": "^1.60.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"@vscode/test-electron": "^1.6.1",
"eslint": "^7.32.0",
"eslint-plugin-tsdoc": "^0.2.14",
"glob": "^7.1.7",
"mocha": "^9.1.1",
"typescript": "^4.4.3",
"typescript": "^4.4.4",
"typescript-formatter": "^7.2.2",
"vsce": "^1.100.1",
"vscode-test": "^1.6.1"
Expand Down
28 changes: 28 additions & 0 deletions language/move-analyzer/editors/code/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

import * as os from 'os';
import * as vscode from 'vscode';

/**
* User-defined configuration values, such as those specified in VS Code settings.
*
* This provides a more strongly typed interface to the configuration values specified in this
* extension's `package.json`, under the key `"contributes.configuration.properties"`.
*/
export class Configuration {
private readonly configuration: vscode.WorkspaceConfiguration;

constructor() {
this.configuration = vscode.workspace.getConfiguration('move-analyzer');
}

/** The path to the move-analyzer executable. */
get serverPath(): string {
const path = this.configuration.get<string>('server.path', 'move-analyzer');
if (path.startsWith('~/')) {
return os.homedir() + path.slice('~'.length);
}
return path;
}
}
41 changes: 41 additions & 0 deletions language/move-analyzer/editors/code/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

import type { Configuration } from './configuration';
import * as fs from 'fs';
import * as vscode from 'vscode';

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

static create(
extension: 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);
}

/**
* Registers the given command with VS Code.
*
* "Registering" the function means that the VS Code machinery will execute it when the command
* with the given name is requested by the user. The command names themselves are specified in
* this extension's `package.json` file, under the key `"contributes.commands"`.
*/
registerCommand(
name: Readonly<string>,
command: (context: Readonly<Context>) => Promise<void>,
): void {
const disposable = vscode.commands.registerCommand(`move-analyzer.${name}`, async () => {
return command(this);
});
this.extension.subscriptions.push(disposable);
}
}
52 changes: 52 additions & 0 deletions language/move-analyzer/editors/code/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 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}'.`,
);
}
}

/**
* 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;
}

context.registerCommand('serverVersion', serverVersion);
}
7 changes: 7 additions & 0 deletions language/move-analyzer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use structopt::StructOpt;

#[derive(StructOpt)]
#[structopt(name = "move-analyzer", about = "A language server for Move")]
struct Options {}

fn main() {
Options::from_args();
todo!()
}

0 comments on commit bf68a39

Please sign in to comment.