Skip to content

Commit

Permalink
Improvements to validation plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaul-stripe committed Jun 1, 2023
1 parent a3c9f26 commit f4cfaa3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ It is possible to have multiple Markdoc configurations for the same workspace by

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.

# Extending the language server with custom functionality

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.

# Contributing

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).
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.2",
"version": "0.0.3",
"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.2",
"version": "0.0.3",
"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.2",
"version": "0.0.3",
"description": "A Markdoc language server",
"main": "dist/index.js",
"author": "Ryan Paul",
Expand Down
19 changes: 14 additions & 5 deletions server/plugins/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default class ValidationProvider {
protected connection: LSP.Connection,
protected services: ServiceInstances
) {
services.Documents.onDidSave(this.onValidate, this);
services.Documents.onDidSave(this.onDidSave, this);
services.Documents.onDidClose(this.onDidClose, this);
services.Documents.onDidChangeContent(this.onValidate, this);
services.Documents.onDidChangeContent(this.onDidChangeContent, this);
}

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

onValidate({ document: { uri } }: TextChangeEvent) {
validate(uri: string) {
const doc = this.services.Documents.ast(uri);
const schema = this.services.Schema.get();

if (!schema || !doc) return;

const config = this.configuration(uri);
const errors = Markdoc.validate(doc, config);
const diagnostics = errors.map((err) => this.diagnostic(err));
this.connection.sendDiagnostics({ uri, diagnostics });
return errors.map((err) => this.diagnostic(err));
}

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

onDidSave({ document: { uri } }: TextChangeEvent) {
const diagnostics = this.validate(uri);
if (diagnostics) this.connection.sendDiagnostics({ uri, diagnostics });
}

onDidChangeContent({ document: { uri } }: TextChangeEvent) {
const diagnostics = this.validate(uri);
if (diagnostics) this.connection.sendDiagnostics({ uri, diagnostics });
}

onDidClose({ document: { uri } }: TextChangeEvent) {
this.connection.sendDiagnostics({ uri, diagnostics: [] });
}
Expand Down

0 comments on commit f4cfaa3

Please sign in to comment.