Skip to content

Commit 32f880b

Browse files
Merge pull request #126 from Watts-Lab/expandtemplates
Expand templates
2 parents 125f35b + d98449b commit 32f880b

11 files changed

Lines changed: 1149 additions & 50 deletions

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
{
6666
"command": "deliberation-lab-tools.openStagePreview",
6767
"title": "Deliberation Lab Tools: Open Stage Preview"
68+
},
69+
{
70+
"command": "deliberation-lab-tools.previewExpandedTemplates",
71+
"title": "Preview: Expanded Treatment File"
6872
}
6973
],
7074
"menus": {
@@ -78,6 +82,11 @@
7882
"command": "deliberation-lab-tools.openStagePreview",
7983
"when": "editorLangId == treatmentsYaml",
8084
"group": "deliberation-lab-tools"
85+
},
86+
{
87+
"command": "deliberation-lab-tools.previewExpandedTemplates",
88+
"when": "editorLangId == treatmentsYaml",
89+
"group": "deliberation-lab-tools"
8190
}
8291
]
8392
}

src/commands.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { get } from "http";
22
import * as vscode from "vscode";
33
import { getExtensionUri } from "./contextStore";
4-
import { load as loadYaml } from "js-yaml";
4+
import { dump as dumpYaml, load as loadYaml } from "js-yaml";
5+
import { EXP_SCHEME } from "./fillTemplates";
6+
import { fillTemplates } from "./fillTemplates";
57

68
// Command to create default treatments YAML file
79
export const defaultYaml = vscode.commands.registerCommand("deliberation-lab-tools.defaultTreatmentsYaml", async () => {
@@ -312,4 +314,31 @@ function getNonce(): string {
312314
text += possible.charAt(Math.floor(Math.random() * possible.length));
313315
}
314316
return text;
315-
}
317+
}
318+
319+
export const expandedTemplatesPreview = vscode.commands.registerCommand("deliberation-lab-tools.previewExpandedTemplates", async (resource?: vscode.Uri) => {
320+
const src = resource ?? vscode.window.activeTextEditor?.document.uri;
321+
if (!src) return;
322+
323+
const doc = await vscode.workspace.openTextDocument(src);
324+
if (doc.languageId !== "treatmentsYaml") {
325+
vscode.window.showWarningMessage("This preview is only for treatments YAML files.");
326+
return;
327+
}
328+
329+
const previewUri = vscode.Uri.parse(
330+
`${EXP_SCHEME}:${src.path}.expanded.treatments.yaml?src=${encodeURIComponent(src.toString())}`
331+
);
332+
333+
const vdoc = await vscode.workspace.openTextDocument(previewUri);
334+
335+
if (vdoc.languageId !== "treatmentsYaml") {
336+
await vscode.languages.setTextDocumentLanguage(vdoc, "treatmentsYaml");
337+
}
338+
339+
await vscode.window.showTextDocument(vdoc, {
340+
preview: true,
341+
preserveFocus: false,
342+
viewColumn: vscode.ViewColumn.Beside,
343+
});
344+
})

src/extension.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { parseYaml } from "./parsers/parseYaml";
44
import { parseMarkdown } from "./parsers/parseMarkdown";
55
import { parseDlConfig } from "./parsers/parseDlConfig";
66
import { parseBatchConfig } from "./parsers/parseBatchConfig";
7-
import { defaultMarkdown, inlineSuggestion, defaultYaml, markdownPreview } from "./commands";
7+
import { defaultMarkdown, inlineSuggestion, defaultYaml, markdownPreview, expandedTemplatesPreview } from "./commands";
8+
import { ExpandedTemplatesProvider, EXP_SCHEME } from "./fillTemplates";
89
import { setExtensionContext } from "./contextStore";
910
import { FileFixCodeActionProvider } from "./codeActionProvider";
10-
11+
import * as yaml from "js-yaml";
1112
// should this be named yamlDiagnostics if also using markdown?
1213
export const diagnosticCollection = vscode.languages.createDiagnosticCollection("yamlDiagnostics");
1314

@@ -51,12 +52,12 @@ export async function activate(context: vscode.ExtensionContext) {
5152
await parseDocument(event);
5253
}
5354
}),
54-
// for changing document
55-
vscode.workspace.onDidChangeTextDocument(async (event) => {
56-
if (event?.document !== undefined) {
57-
parseDocument(event?.document);
58-
};
59-
}),
55+
// for changing document
56+
vscode.workspace.onDidChangeTextDocument(async (event) => {
57+
if (event?.document !== undefined) {
58+
parseDocument(event?.document);
59+
};
60+
}),
6061

6162
// When we switch to a document open in another tab
6263
vscode.window.onDidChangeActiveTextEditor(async (event) => {
@@ -90,4 +91,22 @@ export async function activate(context: vscode.ExtensionContext) {
9091

9192
// Open Markdown preview
9293
context.subscriptions.push(markdownPreview);
94+
95+
// Open expanded templates preview
96+
const expandedProvider = new ExpandedTemplatesProvider();
97+
context.subscriptions.push(
98+
vscode.workspace.registerTextDocumentContentProvider(EXP_SCHEME, expandedProvider)
99+
);
100+
101+
// Open preview command
102+
context.subscriptions.push(
103+
expandedTemplatesPreview
104+
);
105+
106+
// Auto-refresh the preview when the source changes
107+
context.subscriptions.push(
108+
vscode.workspace.onDidChangeTextDocument((e) => {
109+
expandedProvider.refreshForSource(e.document.uri);
110+
})
111+
);
93112
}

0 commit comments

Comments
 (0)