Skip to content

Commit

Permalink
feat: support format switch in VSCode extension (#1259)
Browse files Browse the repository at this point in the history
Co-authored-by: ymc9 <[email protected]>
  • Loading branch information
jiashengguo and ymc9 authored Apr 15, 2024
1 parent b0f5d3b commit 8044a54
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
12 changes: 11 additions & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@
"scopeName": "source.zmodel",
"path": "./bundle/syntaxes/zmodel.tmLanguage.json"
}
]
],
"configuration": {
"title": "ZenStack",
"properties": {
"zmodel.format.usePrismaStyle": {
"type": "boolean",
"default": true,
"description": "Use Prisma style indentation."
}
}
}
},
"activationEvents": [
"onLanguage:zmodel"
Expand Down
44 changes: 36 additions & 8 deletions packages/schema/src/language-server/zmodel-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import { AbstractFormatter, AstNode, Formatting, LangiumDocument } from 'langium';
import {
AbstractFormatter,
AstNode,
ConfigurationProvider,
Formatting,
LangiumDocument,
LangiumServices,
MaybePromise,
} from 'langium';

import * as ast from '@zenstackhq/language/ast';
import { FormattingOptions, Range, TextEdit } from 'vscode-languageserver';
import { DocumentFormattingParams, FormattingOptions, TextEdit } from 'vscode-languageserver';
import { ZModelLanguageMetaData } from '@zenstackhq/language/generated/module';

export class ZModelFormatter extends AbstractFormatter {
private formatOptions?: FormattingOptions;
private isPrismaStyle = true;

protected readonly configurationProvider: ConfigurationProvider;

constructor(services: LangiumServices) {
super();
this.configurationProvider = services.shared.workspace.ConfigurationProvider;
}

protected format(node: AstNode): void {
const formatter = this.getNodeFormatter(node);

Expand Down Expand Up @@ -55,13 +72,24 @@ export class ZModelFormatter extends AbstractFormatter {
}
}

protected override doDocumentFormat(
override formatDocument(
document: LangiumDocument<AstNode>,
options: FormattingOptions,
range?: Range | undefined
): TextEdit[] {
this.formatOptions = options;
return super.doDocumentFormat(document, options, range);
params: DocumentFormattingParams
): MaybePromise<TextEdit[]> {
this.formatOptions = params.options;

this.configurationProvider.getConfiguration(ZModelLanguageMetaData.languageId, 'format').then((config) => {
// in the CLI case, the config is undefined
if (config) {
if (config.usePrismaStyle === false) {
this.setPrismaStyle(false);
} else {
this.setPrismaStyle(true);
}
}
});

return super.formatDocument(document, params);
}

public getFormatOptions(): FormattingOptions | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/language-server/zmodel-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const ZModelModule: Module<ZModelServices, PartialLangiumServices & ZMode
ZModelValidator: (services) => new ZModelValidator(services),
},
lsp: {
Formatter: () => new ZModelFormatter(),
Formatter: (services) => new ZModelFormatter(services),
CodeActionProvider: (services) => new ZModelCodeActionProvider(services),
DefinitionProvider: (services) => new ZModelDefinitionProvider(services),
SemanticTokenProvider: (services) => new ZModelSemanticTokenProvider(services),
Expand Down

0 comments on commit 8044a54

Please sign in to comment.