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

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

Merged
merged 1 commit into from
Feb 20, 2024

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.

@johnsoncodehk johnsoncodehk marked this pull request as ready for review February 20, 2024 15:59
@johnsoncodehk johnsoncodehk merged commit b1932f7 into master Feb 20, 2024
6 checks passed
@johnsoncodehk johnsoncodehk deleted the embedded-code-formatting branch February 20, 2024 16:05
johnsoncodehk added a commit to vuejs/language-tools that referenced this pull request Feb 22, 2024
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