Skip to content

Commit 95fbe40

Browse files
authored
Use a resolver for table conversion (#12)
1 parent f8db5fe commit 95fbe40

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

server/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "Ryan Paul",
77
"license": "MIT",
88
"devDependencies": {
9-
"@markdoc/markdoc": "^0.3.2",
9+
"@markdoc/markdoc": "^0.3.3",
1010
"@types/picomatch": "^2.3.0",
1111
"picomatch": "^2.3.1",
1212
"typescript": "^5.0.4",

server/plugins/codeaction.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,38 @@ export default class CodeActionProvider {
1010
protected services: ServiceInstances
1111
) {
1212
connection.onCodeAction(this.onCodeAction.bind(this));
13+
connection.onCodeActionResolve(this.onCodeActionResolve.bind(this));
1314
}
1415

1516
register(registration: LSP.BulkRegistration) {
16-
registration.add(LSP.CodeActionRequest.type, {documentSelector: null});
17+
registration.add(LSP.CodeActionRequest.type, {documentSelector: null, resolveProvider: true});
1718
}
1819

19-
findTables(ast: Markdoc.Node, params: LSP.CodeActionParams): LSP.Command[] {
20-
const output: LSP.Command[] = [];
20+
convertTable(uri: string, line: number): LSP.WorkspaceEdit | void {
21+
const ast = this.services.Documents.ast(uri);
22+
if (!ast) return;
23+
24+
for (const node of ast.walk()) {
25+
if (node.type === 'table' && node.lines.includes(line)) {
26+
const content = new Markdoc.Ast.Node('tag', {}, [node], 'table');
27+
const newText = Markdoc.format(content);
28+
const [start, end] = node.lines;
29+
const range = LSP.Range.create(start, 0, end + 1, 0);
30+
return {changes: {[uri]: [{range, newText}]}};
31+
}
32+
}
33+
}
34+
35+
findTables(ast: Markdoc.Node, params: LSP.CodeActionParams): LSP.CodeAction[] {
36+
const output: LSP.CodeAction[] = [];
2137
const {line} = params.range.start;
2238
const {uri} = params.textDocument;
2339

2440
for (const node of ast.walk())
2541
if (node.type === 'table' && node.lines.includes(line))
2642
output.push({
27-
command: 'markdoc.convertTable',
43+
data: {type: 'convertTable', uri, line},
2844
title: 'Convert to Markdoc Table',
29-
arguments: [uri, line]
3045
});
3146

3247
return output;
@@ -36,4 +51,14 @@ export default class CodeActionProvider {
3651
const ast = this.services.Documents.ast(params.textDocument.uri);
3752
return ast ? this.findTables(ast, params) : [];
3853
}
54+
55+
onCodeActionResolve(action: LSP.CodeAction): LSP.CodeAction {
56+
if (action.data.type === 'convertTable') {
57+
const {uri, line} = action.data;
58+
const edit = this.convertTable(uri, line);
59+
if (edit) return {...action, edit};
60+
}
61+
62+
return action;
63+
}
3964
}

server/plugins/formatting.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default class FormattingProvider {
1515
this.tokenizer = new Markdoc.Tokenizer(config.markdoc ?? {});
1616
connection.onDocumentFormatting(this.onDocumentFormatting.bind(this));
1717
connection.onDocumentRangeFormatting(this.onRangeFormatting.bind(this));
18-
services.Commands.add('markdoc.convertTable', this.convertTable.bind(this));
1918
}
2019

2120
register(registration: LSP.BulkRegistration) {

server/plugins/validation.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ export default class ValidationProvider {
7878
if (Scanner.has(part.attributes.file))
7979
partials[part.attributes.file] = true;
8080

81-
return { ...Schema?.get(uri), partials };
81+
const schema = Schema?.get(uri);
82+
return {
83+
...schema,
84+
partials,
85+
validation: {
86+
environment: 'language-server',
87+
...schema?.validation
88+
}
89+
};
8290
}
8391

8492
onDidSave({ document: { uri } }: TextChangeEvent) {

server/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type MarkdocConfig = {
5353
typographer?: boolean;
5454
allowIndentation?: boolean;
5555
allowComments?: boolean;
56+
validateFunctions?: boolean;
5657
};
5758

5859
export type RoutingConfig = {

0 commit comments

Comments
 (0)