Skip to content

Commit 7f66b67

Browse files
committed
feat: velocity preview
1 parent dcdaed9 commit 7f66b67

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed

package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"Snippets"
1717
],
1818
"activationEvents": [
19-
"onLanguage:velocity"
19+
"onLanguage:velocity",
20+
"onCommand:appsyncVtl.showPreview"
2021
],
2122
"contributes": {
2223
"snippets": [
@@ -29,6 +30,21 @@
2930
"triggerCharacters": [
3031
"("
3132
]
33+
},
34+
"commands": [
35+
{
36+
"command": "appsyncVtl.showPreview",
37+
"title": "AppSync Resolver: Velocity Preview"
38+
}
39+
],
40+
"menus": {
41+
"editor/title": [
42+
{
43+
"command": "appsyncVtl.showPreview",
44+
"when": "resourceLangId == velocity",
45+
"group": "navigation"
46+
}
47+
]
3248
}
3349
},
3450
"main": "./out/extension.js",

src/extension.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import * as vscode from 'vscode';
3+
import * as vscode from "vscode";
44

55
import registerProviders from "./providers";
6+
import { VelocityPreviewProvider } from "./previewContentProvider";
67

78
// this method is called when your extension is activated
89
// your extension is activated the very first time the command is executed
910
export function activate(context: vscode.ExtensionContext) {
11+
console.info(`appsync resolver extension activated!`);
1012

11-
console.info(`appsync resolver extension activated!`);
12-
13-
registerProviders(context);
14-
13+
registerProviders(context);
14+
VelocityPreviewProvider.register(context);
1515
}
1616

1717
// this method is called when your extension is deactivated
18-
export function deactivate() { }
18+
export function deactivate() {}

src/previewContentProvider.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as vscode from "vscode";
2+
import {render as renderVelocity} from "velocityjs";
3+
4+
function isVelocityFile(document?: vscode.TextDocument): boolean {
5+
return document?.languageId === "velocity";
6+
}
7+
8+
export class VelocityPreviewProvider
9+
implements vscode.TextDocumentContentProvider {
10+
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
11+
onDidChange = this.onDidChangeEmitter.event;
12+
13+
private contentProviderRegistration: vscode.Disposable;
14+
15+
static SCHEME = `appsyncvtl`;
16+
static URI = vscode.Uri.parse(`${VelocityPreviewProvider.SCHEME}:preview.json`, true);
17+
18+
constructor() {
19+
this.contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(VelocityPreviewProvider.SCHEME, this);
20+
}
21+
22+
provideTextDocumentContent(
23+
uri: vscode.Uri,
24+
token: vscode.CancellationToken
25+
): vscode.ProviderResult<string> {
26+
const editor = vscode.window.activeTextEditor;
27+
28+
if(!isVelocityFile(editor?.document)) {
29+
console.error(`Wrong document type`);
30+
return `Unknown document type "${editor?.document?.languageId}"`;
31+
}
32+
33+
const content = editor?.document?.getText() ?? ``;
34+
35+
return renderVelocity(content);
36+
}
37+
38+
async updatePreview() {}
39+
40+
async onContentUpdated(
41+
event?: vscode.TextDocumentChangeEvent | vscode.TextEditor
42+
) {
43+
if (!event) {
44+
return;
45+
}
46+
47+
if (isVelocityFile(event.document)) {
48+
this.onDidChangeEmitter.fire(VelocityPreviewProvider.URI);
49+
}
50+
}
51+
52+
static register(context: vscode.ExtensionContext): VelocityPreviewProvider {
53+
const provider = new VelocityPreviewProvider();
54+
55+
context.subscriptions.push(
56+
vscode.workspace.onDidChangeTextDocument(provider.onContentUpdated.bind(provider))
57+
);
58+
context.subscriptions.push(
59+
vscode.window.onDidChangeActiveTextEditor(provider.onContentUpdated.bind(provider))
60+
);
61+
62+
vscode.commands.registerCommand('appsyncVtl.showPreview', async () => {
63+
let doc = await vscode.workspace.openTextDocument(this.URI);
64+
await vscode.window.showTextDocument(doc, {preview: false, viewColumn: vscode.ViewColumn.Beside, preserveFocus: true});
65+
});
66+
67+
return provider;
68+
}
69+
}

test_templates/dynamo.vtl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#set($range = [0..5])
2+
3+
{
4+
"test": true
5+
#foreach($i in $range)
6+
,"idx_$i": "The $i index"
7+
#end
8+
9+
}

0 commit comments

Comments
 (0)