Adding VSCode extension (#23)

This commit is contained in:
Mat Wood 2020-11-23 11:39:14 -08:00 committed by GitHub
parent dda8dfe6bc
commit d137ff06fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 2363 additions and 1 deletions

5
.gitignore vendored
View file

@ -8,3 +8,8 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# Extension local development files
/extension/node_modules/
/extension/out/

17
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,17 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "CargoPlay",
"command": "cargo",
"args": [
"play",
"${file}",
],
"problemMatcher": [
"$rustc"
]
}
]
}

View file

@ -106,10 +106,14 @@ With your code file open, running `:CargoPlay` will allow you to test your curre
### VSCode
Install the [VSCode Extension](./extension/README.md)
OR
Open Command Palette and select **Configure Task**
- *This will either create a new tasks.json or open your existing tasks.json*
Add the following task:
Add the following [task](./.vscode/tasks.json):
```json
{

19
extension/.eslintrc.json Normal file
View file

@ -0,0 +1,19 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/class-name-casing": "warn",
"@typescript-eslint/semi": "warn",
"curly": "warn",
"eqeqeq": "warn",
"no-throw-literal": "warn",
"semi": "off"
}
}

7
extension/.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"dbaeumer.vscode-eslint"
]
}

36
extension/.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,36 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}

11
extension/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,11 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
}

20
extension/.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

12
extension/DEVELOPMENT.md Normal file
View file

@ -0,0 +1,12 @@
# Development
## Testing/Debugging
Add this directory as a workspace folder in VSCode and you can use the debugger to run the extension in a VSCode window. [Debug Guide](https://code.visualstudio.com/api/get-started/your-first-extension)
## Bundling / Publishing
[Guide](https://code.visualstudio.com/api/working-with-extensions/publishing-extension)
```
(cargo-play/extension) $ vsce package
(cargo-play/extension) $ vsce publish
```

37
extension/README.md Normal file
View file

@ -0,0 +1,37 @@
# cargo-play VSCode Extension
![cargo-play demo](images/cargo_play_vscode.gif)
## Features
- CargoPlay: Run
- Run the current Rust file in a CargoPlay playground [Usage instructions](../README.md#usage)
- CargoPlay: Install
- Install the cargo-play Cargo extension (only needs to be done once)
## Requirements
- Installation of [Rustup](https://rustup.rs/) and `cargo` in your `$PATH`
## Installation
Install from Github:
```
$ git clone https://github.com/fanzeyi/cargo-play
$ code --install-extension cargo-play/extension/cargo-play.0.0.1.vsix
```
## Known Issues
- At least one folder must be added to the workspace
- The extension is not aware of `cargo play` being installed or not
## Release Notes
### 1.0.0
Initial release of Cargo-Play extension, supports:
- CargoPlay installation
- Quick access to CargoPlay playground feature of running Rust files

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 681 KiB

2003
extension/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

59
extension/package.json Normal file
View file

@ -0,0 +1,59 @@
{
"name": "cargo-play",
"displayName": "CargoPlay",
"description": "A local Rust playground",
"version": "0.0.1",
"publisher": "fanzeyi",
"repository": {
"type": "git",
"url": "https://github.com/fanzeyi/cargo-play"
},
"engines": {
"vscode": "^1.43.0"
},
"icon": "images/cargo_play_icon.png",
"keywords": [
"rust"
],
"categories": [
"Programming Languages"
],
"activationEvents": [
"onCommand:extension.cargoPlay",
"onCommand:extension.installCargoPlay"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "extension.cargoPlay",
"title": "CargoPlay: Run"
},
{
"command": "extension.installCargoPlay",
"title": "CargoPlay: Install"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"lint": "eslint src --ext ts",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/mocha": "^7.0.1",
"@types/node": "^12.11.7",
"@types/vscode": "^1.43.0",
"eslint": "^6.8.0",
"@typescript-eslint/parser": "^2.18.0",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"glob": "^7.1.6",
"mocha": "^7.0.1",
"typescript": "^3.7.5",
"vscode-test": "^1.3.0"
}
}

View file

@ -0,0 +1,35 @@
import * as vscode from 'vscode';
interface CargoPlayTaskDefinition extends vscode.TaskDefinition {
/* The Rust file to compile */
target_file: string;
}
export async function activate(context: vscode.ExtensionContext) {
let play = vscode.commands.registerCommand('extension.cargoPlay', async () => {
const filename = vscode.window.activeTextEditor?.document.fileName;
if (filename === undefined || !filename.endsWith(".rs")) {
vscode.window.showWarningMessage("CargoPlay: No active Rust file found");
return;
}
let def: CargoPlayTaskDefinition = {
type: 'cargo-play',
target_file: filename
};
let execution = new vscode.ShellExecution(`cargo play ${def.target_file}`);
let task = new vscode.Task(def, vscode.TaskScope.Global.toString(), 'cargo-play', execution, ["$rustc"]);
vscode.tasks.executeTask(task);
});
context.subscriptions.push(play);
let install = vscode.commands.registerCommand('extension.installCargoPlay', async () => {
let def: vscode.TaskDefinition = { type: 'cargo-play-install' };
let execution = new vscode.ShellExecution(`cargo install cargo-play`);
let task = new vscode.Task(def, vscode.TaskScope.Global.toString(), 'cargo-play-install', execution, ["$rustc"]);
await vscode.tasks.executeTask(task);
});
context.subscriptions.push(install);
}
export function deactivate() { }

View file

@ -0,0 +1,23 @@
import * as path from 'path';
import { runTests } from 'vscode-test';
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}
main();

View file

@ -0,0 +1,15 @@
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
// import * as myExtension from '../extension';
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
test('Sample test', () => {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
});
});

View file

@ -0,0 +1,38 @@
import * as path from 'path';
import * as Mocha from 'mocha';
import * as glob from 'glob';
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
});
mocha.useColors(true);
const testsRoot = path.resolve(__dirname, '..');
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
});
});
}

21
extension/tsconfig.json Normal file
View file

@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "src",
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
},
"exclude": [
"node_modules",
".vscode-test"
]
}