Skip to content

Commit

Permalink
Rpaul markdoc 030 (#6)
Browse files Browse the repository at this point in the history
* Updates to Markdoc 0.3.0 and enables support for slots

* Make the formatter respect tokenizer settings
  • Loading branch information
rpaul-stripe authored Jun 29, 2023
1 parent f7b9a5f commit 0682ed3
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 16 deletions.
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.4",
"version": "0.0.5",
"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.4",
"version": "0.0.5",
"description": "A Markdoc language server and Visual Studio Code extension",
"repository": {
"type": "git",
Expand Down
12 changes: 6 additions & 6 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@markdoc/language-server",
"version": "0.0.4",
"version": "0.0.5",
"description": "A Markdoc language server",
"main": "dist/index.js",
"author": "Ryan Paul",
"license": "MIT",
"devDependencies": {
"@markdoc/markdoc": "^0.2.1",
"@markdoc/markdoc": "^0.3.0",
"@types/picomatch": "^2.3.0",
"picomatch": "^2.3.1",
"typescript": "^5.0.4",
Expand Down
1 change: 0 additions & 1 deletion server/plugins/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export default class CompletionProvider {
insertText: item.data.block ? text : text.replaceAll("\n", ""),
insertTextFormat: LSP.InsertTextFormat.Snippet,
kind: LSP.CompletionItemKind.Function,
// @ts-expect-error
documentation: config.description ?? "",
};
},
Expand Down
4 changes: 2 additions & 2 deletions server/plugins/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const example1 = `
const example1Formatted = `
# This is a test {% #foo %}
- This is an example [foo](/bar)
* This is an example [foo](/bar)
`;

const example1LastLine = `
- This is an example [foo](/bar)
* This is an example [foo](/bar)
`;

const mockConnection = {
Expand Down
6 changes: 5 additions & 1 deletion server/plugins/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import type { Config, ServiceInstances } from "../types";
import { TextDocument } from "vscode-languageserver-textdocument";

export default class FormattingProvider {
protected tokenizer: Markdoc.Tokenizer;

constructor(
protected config: Config,
protected connection: LSP.Connection,
protected services: ServiceInstances
) {
this.tokenizer = new Markdoc.Tokenizer(config.markdoc ?? {});
connection.onDocumentFormatting(this.onDocumentFormatting.bind(this));
connection.onDocumentRangeFormatting(this.onRangeFormatting.bind(this));
}
Expand All @@ -30,7 +33,8 @@ export default class FormattingProvider {
: LSP.Range.create(0, 0, doc.lineCount, 0);

const text = doc.getText(actualRange);
const ast = Markdoc.parse(text);
const tokens = this.tokenizer.tokenize(text);
const ast = Markdoc.parse(tokens, { slots: this.config.markdoc?.slots });
const output = Markdoc.format(ast);

return [LSP.TextEdit.replace(actualRange, output)];
Expand Down
2 changes: 1 addition & 1 deletion server/services/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class Documents<

parse(content: string, file: string) {
const tokens = this.tokenizer.tokenize(content);
return Markdoc.parse(tokens, file);
return Markdoc.parse(tokens, { file, slots: this.config.markdoc?.slots });
}

protected handleClose({ document }: TextChangeEvent) {
Expand Down
2 changes: 1 addition & 1 deletion server/services/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Scanner<

parse(content: string, file: string) {
const tokens = this.tokenizer.tokenize(content);
return Markdoc.parse(tokens, file);
return Markdoc.parse(tokens, { file, slots: this.config.markdoc?.slots });
}

async scan() {
Expand Down

0 comments on commit 0682ed3

Please sign in to comment.