Skip to content

feat(language-service): more reliable embedded code formatting#138

Merged
johnsoncodehk merged 1 commit intomasterfrom
embedded-code-formatting
Feb 20, 2024
Merged

feat(language-service): more reliable embedded code formatting#138
johnsoncodehk merged 1 commit intomasterfrom
embedded-code-formatting

Conversation

@johnsoncodehk
Copy link
Member

@johnsoncodehk johnsoncodehk commented Feb 20, 2024

  • Embedded code indentation is no longer handled by @volar/language-service, but instead calculated and passed as initialIndentLevel, which is then reliably handled by ServicePlugin for additional indentation in specific languages.

  • The provideDocumentFormattingEdits and provideOnTypeFormattingEdits APIs now accept the EmbeddedCodeFormattingOptions parameter, which includes initialIndentLevel.

  • Formatting ranges are now correctly mapped to embedded code formatting ranges.

  • Removed the no longer needed ServicePlugin.provideFormattingIndentSensitiveLines API.

  • No longer relies on the conventional volar.format.initialIndent editor setting.

  • Added ServicePlugin.resolveEmbeddedCodeFormattingOptions API, allowing downstream tools to modify the initialIndentLevel passed to ServicePlugin based on custom settings (replacing volar.format.initialIndent).

    Here's an example of its usage in Vue:

     async resolveEmbeddedCodeFormattingOptions(code, options) {
     	const sourceFile = context.language.files.getByVirtualCode(code);
     	if (sourceFile.generated?.code instanceof vue.VueGeneratedCode) {
     		if (code.id === 'scriptFormat' || code.id === 'scriptSetupFormat') {
     			if (await context.env.getConfiguration?.('vue.format.initialIndent.script') ?? false) {
     				options.initialIndentLevel++;
     			}
     		}
     		else if (code.id.startsWith('style_')) {
     			if (await context.env.getConfiguration?.('vue.format.initialIndent.style') ?? false) {
     				options.initialIndentLevel++;
     			}
     		}
     		else if (code.id === 'template') {
     			if (await context.env.getConfiguration?.('vue.format.initialIndent.template') ?? true) {
     				options.initialIndentLevel++;
     			}
     		}
     	}
     	return options;
     },

With these changes, all formatting tests in microsoft/vscode#171547 have passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant