Skip to content

Commit f4cfaa3

Browse files
committed
Improvements to validation plugin
1 parent a3c9f26 commit f4cfaa3

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ It is possible to have multiple Markdoc configurations for the same workspace by
5555

5656
In [multi-root workspaces](https://code.visualstudio.com/docs/editor/multi-root-workspaces), a Markdoc configuration file is specific to an individual workspace root. You can have separate Markdoc configuration files for each root. If you need to override the location of the Markdoc language server configuration file in a multi-root workspace, you can use a [folder setting](https://code.visualstudio.com/docs/editor/multi-root-workspaces#_settings) to customize this behavior per root.
5757

58+
# Extending the language server with custom functionality
59+
60+
The language server is published as a [package on npm](https://www.npmjs.com/package/@markdoc/language-server) in order to support extensibility and customization. You can tailor the functionality to your needs by adding plugins or creating subclasses that substitute built-in plugins. Support for this is somewhat experimental and the APIs exposed by the package are still subject to change. We will not guarantee API stability or backwards compatibility for language server plugins until the 1.0 release.
61+
5862
# Contributing
5963

6064
Contributions and feedback are welcomed and encouraged. Feel free to open PRs here, or open issues and discussion threads in the [Markdoc core repo](https://github.com/markdoc/markdoc).

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.2",
6+
"version": "0.0.3",
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.2",
10+
"version": "0.0.3",
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.2",
3+
"version": "0.0.3",
44
"description": "A Markdoc language server",
55
"main": "dist/index.js",
66
"author": "Ryan Paul",

server/plugins/validation.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export default class ValidationProvider {
1010
protected connection: LSP.Connection,
1111
protected services: ServiceInstances
1212
) {
13-
services.Documents.onDidSave(this.onValidate, this);
13+
services.Documents.onDidSave(this.onDidSave, this);
1414
services.Documents.onDidClose(this.onDidClose, this);
15-
services.Documents.onDidChangeContent(this.onValidate, this);
15+
services.Documents.onDidChangeContent(this.onDidChangeContent, this);
1616
}
1717

1818
severity(level: string): LSP.DiagnosticSeverity {
@@ -56,16 +56,15 @@ export default class ValidationProvider {
5656
return LSP.Range.create(line, 0, line + 1, 0);
5757
}
5858

59-
onValidate({ document: { uri } }: TextChangeEvent) {
59+
validate(uri: string) {
6060
const doc = this.services.Documents.ast(uri);
6161
const schema = this.services.Schema.get();
6262

6363
if (!schema || !doc) return;
6464

6565
const config = this.configuration(uri);
6666
const errors = Markdoc.validate(doc, config);
67-
const diagnostics = errors.map((err) => this.diagnostic(err));
68-
this.connection.sendDiagnostics({ uri, diagnostics });
67+
return errors.map((err) => this.diagnostic(err));
6968
}
7069

7170
configuration(uri: string): Markdoc.Config {
@@ -81,6 +80,16 @@ export default class ValidationProvider {
8180
return { ...Schema?.get(), partials };
8281
}
8382

83+
onDidSave({ document: { uri } }: TextChangeEvent) {
84+
const diagnostics = this.validate(uri);
85+
if (diagnostics) this.connection.sendDiagnostics({ uri, diagnostics });
86+
}
87+
88+
onDidChangeContent({ document: { uri } }: TextChangeEvent) {
89+
const diagnostics = this.validate(uri);
90+
if (diagnostics) this.connection.sendDiagnostics({ uri, diagnostics });
91+
}
92+
8493
onDidClose({ document: { uri } }: TextChangeEvent) {
8594
this.connection.sendDiagnostics({ uri, diagnostics: [] });
8695
}

0 commit comments

Comments
 (0)