Skip to content

Commit

Permalink
vscode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibo committed Sep 19, 2024
1 parent f93dd39 commit 41ee79b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ examples/c/example
examples/zig/zig-cache
logs/
release/
src/tools/vscode/extension.js
*.vsix
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
"@types/node": "^22.5.3",
"@types/react": "^18.2.45",
"@types/semver": "^7.5.8",
"@types/vscode": "^1.93.0",
"@vitest/coverage-v8": "^2.0.5",
"@vscode/vsce": "^3.1.0",
"binaryen": "^118.0.0",
"esbuild": "^0.23.1",
"eslint": "<=8",
Expand Down Expand Up @@ -132,11 +134,12 @@
"release": "npm run compile && node build/tools/release",
"benchmark": "npm run compile && node build/test/benchmark",
"integration": "npm run compile && node build/test/integration",
"extension": "npm -C src/tools/vscode run build",
"postinstall": "node src/bin/install-pkmn-engine"
},
"eslintConfig": {
"extends": "@pkmn",
"ignorePatterns": ["dist/", "node_modules/", "examples/zig", "build/"],
"ignorePatterns": ["dist/", "node_modules/", "examples/zig", "build/", "extension.js"],
"env": {"es6": true, "node": true, "browser": true},
"overrides": [{
"files": ["**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions src/tools/vscode/LICENSE
9 changes: 9 additions & 0 deletions src/tools/vscode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
In order to view the `.bin` files present in the regression test [fixtures](../../test/regression/fixtures/) in Visual Studio Code:

1. Add [`pkmn-debug`](../../bin/pkmn-debug) to the path `PATH`
2. Package the extension into a `.vsix`:
```sh
$ npm run extension
```
3. Install the extension in Visual Studio Code via "**Extensions: Install from VSIX...**"
4. Restart Visual Studio Code
66 changes: 66 additions & 0 deletions src/tools/vscode/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {execFile} from 'child_process';

import * as vscode from 'vscode';

type RenderedDocument = vscode.CustomDocument & {readonly preview: string};

class PkmnDebugEditorProvider implements vscode.CustomReadonlyEditorProvider {
async openCustomDocument(
uri: vscode.Uri,
context: vscode.CustomDocumentOpenContext,
token: vscode.CancellationToken
): Promise<RenderedDocument> {
return {uri, preview: await preview(uri, token), dispose() {}};
}

resolveCustomEditor(document: RenderedDocument, webviewPanel: vscode.WebviewPanel) {
try {
webviewPanel.webview.options = {enableScripts: true};
webviewPanel.webview.html = document.preview;
} catch (error: unknown) {
if (!(error instanceof Error)) throw error;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
vscode.window.showErrorMessage(`Error running pkmn-debug: ${error.message}`);
}
}
}

function preview(uri: vscode.Uri, token?: vscode.CancellationToken) {
return new Promise<string>((resolve, reject) => {
const child = execFile('pkmn-debug', [uri.fsPath]);
token?.onCancellationRequested(() => {
child.kill();
});
let stdout = '';
child.stdout?.on('data', (data: Buffer) => {
stdout += data.toString();
});
let stderr = '';
child.stderr?.on('data', (data: Buffer) => {
stderr += data.toString();
});
child.on('error', error => {
reject(error);
});
child.on('exit', code => {
if (code === 0) {
resolve(stdout);
} else {
reject(new Error(`pkmn-debug exited with code: ${code}\n${stderr}`));
}
});
});
}

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.window.registerCustomEditorProvider(
'pkmn-debug',
new PkmnDebugEditorProvider(),
{webviewOptions: {
enableFindWidget: true,
retainContextWhenHidden: true,
}},
));
}

export function deactivate() {}
20 changes: 20 additions & 0 deletions src/tools/vscode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "pkmn-debug",
"version": "0.1.0",
"publisher": "pkmn",
"repository": "github:pkmn/engine",
"main": "extension.js",
"files": ["extension.js", "LICENSE"],
"engines": {"vscode": "^1.93.0"},
"activationEvents": [],
"scripts": {
"build": "esbuild extension.ts --format=cjs --outfile=extension.js && vsce package"
},
"contributes": {
"customEditors": [{
"viewType": "pkmn-debug",
"displayName": "pkmn-debug",
"selector": [{ "filenamePattern": "*.bin" }]
}]
}
}

0 comments on commit 41ee79b

Please sign in to comment.