Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a resolver for table conversion #12

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/package-lock.json

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

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Ryan Paul",
"license": "MIT",
"devDependencies": {
"@markdoc/markdoc": "^0.3.2",
"@markdoc/markdoc": "^0.3.3",
"@types/picomatch": "^2.3.0",
"picomatch": "^2.3.1",
"typescript": "^5.0.4",
Expand Down
35 changes: 30 additions & 5 deletions server/plugins/codeaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@ export default class CodeActionProvider {
protected services: ServiceInstances
) {
connection.onCodeAction(this.onCodeAction.bind(this));
connection.onCodeActionResolve(this.onCodeActionResolve.bind(this));
}

register(registration: LSP.BulkRegistration) {
registration.add(LSP.CodeActionRequest.type, {documentSelector: null});
registration.add(LSP.CodeActionRequest.type, {documentSelector: null, resolveProvider: true});
}

findTables(ast: Markdoc.Node, params: LSP.CodeActionParams): LSP.Command[] {
const output: LSP.Command[] = [];
convertTable(uri: string, line: number): LSP.WorkspaceEdit | void {
const ast = this.services.Documents.ast(uri);
if (!ast) return;

for (const node of ast.walk()) {
if (node.type === 'table' && node.lines.includes(line)) {
const content = new Markdoc.Ast.Node('tag', {}, [node], 'table');
const newText = Markdoc.format(content);
const [start, end] = node.lines;
const range = LSP.Range.create(start, 0, end + 1, 0);
return {changes: {[uri]: [{range, newText}]}};
}
}
}

findTables(ast: Markdoc.Node, params: LSP.CodeActionParams): LSP.CodeAction[] {
const output: LSP.CodeAction[] = [];
const {line} = params.range.start;
const {uri} = params.textDocument;

for (const node of ast.walk())
if (node.type === 'table' && node.lines.includes(line))
output.push({
command: 'markdoc.convertTable',
data: {type: 'convertTable', uri, line},
title: 'Convert to Markdoc Table',
arguments: [uri, line]
});

return output;
Expand All @@ -36,4 +51,14 @@ export default class CodeActionProvider {
const ast = this.services.Documents.ast(params.textDocument.uri);
return ast ? this.findTables(ast, params) : [];
}

onCodeActionResolve(action: LSP.CodeAction): LSP.CodeAction {
if (action.data.type === 'convertTable') {
const {uri, line} = action.data;
const edit = this.convertTable(uri, line);
if (edit) return {...action, edit};
}

return action;
}
}
1 change: 0 additions & 1 deletion server/plugins/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default class FormattingProvider {
this.tokenizer = new Markdoc.Tokenizer(config.markdoc ?? {});
connection.onDocumentFormatting(this.onDocumentFormatting.bind(this));
connection.onDocumentRangeFormatting(this.onRangeFormatting.bind(this));
services.Commands.add('markdoc.convertTable', this.convertTable.bind(this));
}

register(registration: LSP.BulkRegistration) {
Expand Down
10 changes: 9 additions & 1 deletion server/plugins/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ export default class ValidationProvider {
if (Scanner.has(part.attributes.file))
partials[part.attributes.file] = true;

return { ...Schema?.get(uri), partials };
const schema = Schema?.get(uri);
return {
...schema,
partials,
validation: {
environment: 'language-server',
...schema?.validation
}
};
}

onDidSave({ document: { uri } }: TextChangeEvent) {
Expand Down
1 change: 1 addition & 0 deletions server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type MarkdocConfig = {
typographer?: boolean;
allowIndentation?: boolean;
allowComments?: boolean;
validateFunctions?: boolean;
};

export type RoutingConfig = {
Expand Down