diff --git a/client/client.ts b/client/client.ts index 9fe7b27..07bfbff 100644 --- a/client/client.ts +++ b/client/client.ts @@ -81,14 +81,24 @@ export default class MarkdocClient implements VSC.Disposable { this.client?.outputChannel.show(); } - private options() { + private async options() { const { scheme, fsPath } = this.root.uri; const config = { root: fsPath, ...this.config }; - const module = config.server?.path - ? pathutil.join(fsPath, config.server?.path) - : this.context.asAbsolutePath("dist/client/server.js"); + let serverPath = this.context.asAbsolutePath("dist/client/server.js"); - const run: LSP.NodeModule = { module, transport: LSP.TransportKind.ipc }; + if (config.server?.path) { + const path = pathutil.join(fsPath, config.server?.path); + + try { + await VSC.workspace.fs.stat(VSC.Uri.file(path)); + serverPath = path; + } + catch (err) { + console.log('Could not load server:', err); + } + } + + const run: LSP.NodeModule = { module: serverPath, transport: LSP.TransportKind.ipc }; const server: LSP.ServerOptions = { run, debug: { ...run, options: { execArgv: ["--nolazy", "--inspect=6009"] } }, @@ -105,7 +115,7 @@ export default class MarkdocClient implements VSC.Disposable { } private async createClient(resolve: () => void) { - const { server, client } = this.options(); + const { server, client } = await this.options(); const name = `Markdoc: ${this.id}`; this.client = new LSP.LanguageClient(this.id, name, server, client); diff --git a/client/extension.ts b/client/extension.ts index 8062f22..19ece4e 100644 --- a/client/extension.ts +++ b/client/extension.ts @@ -33,6 +33,7 @@ export default class Extension { ), VSC.workspace.onDidChangeConfiguration(this.onConfigChange.bind(this)), VSC.workspace.onDidChangeWorkspaceFolders(this.onFolderChange.bind(this)), + VSC.workspace.onDidSaveTextDocument(this.onSave.bind(this)), VSC.window.onDidChangeActiveTextEditor(this.onActive.bind(this)), VSC.window.registerTreeDataProvider("markdocPartials", this.partials) ); @@ -137,11 +138,7 @@ export default class Extension { const output = await client.getDependencies(uri); if (output) this.partials.update(output, client.uri); - - if (this.preview.exists() && client.canPreview()) { - const content = await client.renderPreview(uri); - if (content) this.preview.update(content); - } + this.updatePreview(client, uri); } async onNewFileFromTemplate() { @@ -181,9 +178,21 @@ export default class Extension { if (!client?.canPreview()) return; this.preview.display(); - const assetUri = this.preview.getAssetUri(client.root.uri); - const output = await client.renderPreview(uri, assetUri); - if (output) this.preview.update(output); + this.updatePreview(client, uri); + } + + async updatePreview(client: Client, uri: VSC.Uri) { + if (this.preview.exists() && client.canPreview()) { + const assetUri = this.preview.getAssetUri(client.root.uri); + const content = await client.renderPreview(uri, assetUri); + if (content) this.preview.update(content); + } + } + + onSave(doc: VSC.TextDocument) { + if (doc.languageId !== 'markdoc') return; + const client = this.findClient(doc.uri); + if (client) this.updatePreview(client, doc.uri); } onConfigChange(ev: VSC.ConfigurationChangeEvent) { diff --git a/client/package.json b/client/package.json index a7ea1fe..a000ddc 100644 --- a/client/package.json +++ b/client/package.json @@ -3,7 +3,7 @@ "description": "Markdoc Extension", "author": "Ryan Paul", "license": "MIT", - "version": "0.0.5", + "version": "0.0.6", "scripts": { "build": "esbuild index.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs", "build:server": "esbuild server.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs" diff --git a/package.json b/package.json index 76152e0..46df1d5 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "author": "Ryan Paul", "publisher": "stripe", "license": "MIT", - "version": "0.0.5", + "version": "0.0.6", "description": "A Markdoc language server and Visual Studio Code extension", "repository": { "type": "git", diff --git a/server/package.json b/server/package.json index 11aa2cf..f143fc6 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "@markdoc/language-server", - "version": "0.0.5", + "version": "0.0.6", "description": "A Markdoc language server", "main": "dist/index.js", "author": "Ryan Paul",