Skip to content

Commit

Permalink
Automatically update preview on save (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaul-stripe authored Jul 17, 2023
1 parent 0682ed3 commit b730e8d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
22 changes: 16 additions & 6 deletions client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] } },
Expand All @@ -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);
Expand Down
25 changes: 17 additions & 8 deletions client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit b730e8d

Please sign in to comment.