Skip to content

Commit b730e8d

Browse files
authored
Automatically update preview on save (#7)
1 parent 0682ed3 commit b730e8d

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

client/client.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,24 @@ export default class MarkdocClient implements VSC.Disposable {
8181
this.client?.outputChannel.show();
8282
}
8383

84-
private options() {
84+
private async options() {
8585
const { scheme, fsPath } = this.root.uri;
8686
const config = { root: fsPath, ...this.config };
87-
const module = config.server?.path
88-
? pathutil.join(fsPath, config.server?.path)
89-
: this.context.asAbsolutePath("dist/client/server.js");
87+
let serverPath = this.context.asAbsolutePath("dist/client/server.js");
9088

91-
const run: LSP.NodeModule = { module, transport: LSP.TransportKind.ipc };
89+
if (config.server?.path) {
90+
const path = pathutil.join(fsPath, config.server?.path);
91+
92+
try {
93+
await VSC.workspace.fs.stat(VSC.Uri.file(path));
94+
serverPath = path;
95+
}
96+
catch (err) {
97+
console.log('Could not load server:', err);
98+
}
99+
}
100+
101+
const run: LSP.NodeModule = { module: serverPath, transport: LSP.TransportKind.ipc };
92102
const server: LSP.ServerOptions = {
93103
run,
94104
debug: { ...run, options: { execArgv: ["--nolazy", "--inspect=6009"] } },
@@ -105,7 +115,7 @@ export default class MarkdocClient implements VSC.Disposable {
105115
}
106116

107117
private async createClient(resolve: () => void) {
108-
const { server, client } = this.options();
118+
const { server, client } = await this.options();
109119
const name = `Markdoc: ${this.id}`;
110120

111121
this.client = new LSP.LanguageClient(this.id, name, server, client);

client/extension.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default class Extension {
3333
),
3434
VSC.workspace.onDidChangeConfiguration(this.onConfigChange.bind(this)),
3535
VSC.workspace.onDidChangeWorkspaceFolders(this.onFolderChange.bind(this)),
36+
VSC.workspace.onDidSaveTextDocument(this.onSave.bind(this)),
3637
VSC.window.onDidChangeActiveTextEditor(this.onActive.bind(this)),
3738
VSC.window.registerTreeDataProvider("markdocPartials", this.partials)
3839
);
@@ -137,11 +138,7 @@ export default class Extension {
137138

138139
const output = await client.getDependencies(uri);
139140
if (output) this.partials.update(output, client.uri);
140-
141-
if (this.preview.exists() && client.canPreview()) {
142-
const content = await client.renderPreview(uri);
143-
if (content) this.preview.update(content);
144-
}
141+
this.updatePreview(client, uri);
145142
}
146143

147144
async onNewFileFromTemplate() {
@@ -181,9 +178,21 @@ export default class Extension {
181178
if (!client?.canPreview()) return;
182179

183180
this.preview.display();
184-
const assetUri = this.preview.getAssetUri(client.root.uri);
185-
const output = await client.renderPreview(uri, assetUri);
186-
if (output) this.preview.update(output);
181+
this.updatePreview(client, uri);
182+
}
183+
184+
async updatePreview(client: Client, uri: VSC.Uri) {
185+
if (this.preview.exists() && client.canPreview()) {
186+
const assetUri = this.preview.getAssetUri(client.root.uri);
187+
const content = await client.renderPreview(uri, assetUri);
188+
if (content) this.preview.update(content);
189+
}
190+
}
191+
192+
onSave(doc: VSC.TextDocument) {
193+
if (doc.languageId !== 'markdoc') return;
194+
const client = this.findClient(doc.uri);
195+
if (client) this.updatePreview(client, doc.uri);
187196
}
188197

189198
onConfigChange(ev: VSC.ConfigurationChangeEvent) {

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Markdoc Extension",
44
"author": "Ryan Paul",
55
"license": "MIT",
6-
"version": "0.0.5",
6+
"version": "0.0.6",
77
"scripts": {
88
"build": "esbuild index.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs",
99
"build:server": "esbuild server.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"author": "Ryan Paul",
88
"publisher": "stripe",
99
"license": "MIT",
10-
"version": "0.0.5",
10+
"version": "0.0.6",
1111
"description": "A Markdoc language server and Visual Studio Code extension",
1212
"repository": {
1313
"type": "git",

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@markdoc/language-server",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "A Markdoc language server",
55
"main": "dist/index.js",
66
"author": "Ryan Paul",

0 commit comments

Comments
 (0)