From 8044a5496be6629209ee72261c3b75215d50a468 Mon Sep 17 00:00:00 2001 From: Jiasheng Date: Mon, 15 Apr 2024 11:25:51 +0800 Subject: [PATCH] feat: support format switch in VSCode extension (#1259) Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com> --- packages/schema/package.json | 12 ++++- .../src/language-server/zmodel-formatter.ts | 44 +++++++++++++++---- .../src/language-server/zmodel-module.ts | 2 +- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/schema/package.json b/packages/schema/package.json index f5af5c3f3..8b78cbcff 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -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" diff --git a/packages/schema/src/language-server/zmodel-formatter.ts b/packages/schema/src/language-server/zmodel-formatter.ts index 1121290ec..93dd1b704 100644 --- a/packages/schema/src/language-server/zmodel-formatter.ts +++ b/packages/schema/src/language-server/zmodel-formatter.ts @@ -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); @@ -55,13 +72,24 @@ export class ZModelFormatter extends AbstractFormatter { } } - protected override doDocumentFormat( + override formatDocument( document: LangiumDocument, - options: FormattingOptions, - range?: Range | undefined - ): TextEdit[] { - this.formatOptions = options; - return super.doDocumentFormat(document, options, range); + params: DocumentFormattingParams + ): MaybePromise { + 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 { diff --git a/packages/schema/src/language-server/zmodel-module.ts b/packages/schema/src/language-server/zmodel-module.ts index c0c66ce43..116d486da 100644 --- a/packages/schema/src/language-server/zmodel-module.ts +++ b/packages/schema/src/language-server/zmodel-module.ts @@ -66,7 +66,7 @@ export const ZModelModule: Module 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),