-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ examples/c/example | |
examples/zig/zig-cache | ||
logs/ | ||
release/ | ||
src/tools/vscode/extension.js | ||
*.vsix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }] | ||
}] | ||
} | ||
} |