From 5987cf8d7dd7a2302197bec4f383073ccf6f5a28 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Fri, 10 May 2024 07:25:26 +0800 Subject: [PATCH] feat(typescript): capture editor formatting options from the last formatting request close #30 --- .../lib/configs/getFormatCodeSettings.ts | 2 +- packages/typescript/lib/plugins/semantic.ts | 25 ++++++++++++++----- .../lib/semanticFeatures/codeAction.ts | 4 +-- .../lib/semanticFeatures/codeActionResolve.ts | 4 +-- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/typescript/lib/configs/getFormatCodeSettings.ts b/packages/typescript/lib/configs/getFormatCodeSettings.ts index 763705e4..c01363b4 100644 --- a/packages/typescript/lib/configs/getFormatCodeSettings.ts +++ b/packages/typescript/lib/configs/getFormatCodeSettings.ts @@ -6,7 +6,7 @@ import { getConfigTitle } from '../shared'; export async function getFormatCodeSettings( ctx: ServiceContext, document: TextDocument, - options?: FormattingOptions, + options: FormattingOptions | undefined, ): Promise { const config = await ctx.env.getConfiguration?.(getConfigTitle(document) + '.format') ?? {}; return { diff --git a/packages/typescript/lib/plugins/semantic.ts b/packages/typescript/lib/plugins/semantic.ts index 6b520695..24ed9605 100644 --- a/packages/typescript/lib/plugins/semantic.ts +++ b/packages/typescript/lib/plugins/semantic.ts @@ -13,7 +13,8 @@ import type { SignatureHelpTriggerKind, SignatureInformation, VirtualCode, - WorkspaceEdit + WorkspaceEdit, + FormattingOptions } from '@volar/language-service'; import * as path from 'path-browserify'; import * as semver from 'semver'; @@ -217,6 +218,8 @@ export function create( /* typescript-language-features is hardcode true */ const renameInfoOptions = { allowRenameOfImportPath: true }; + let formattingOptions: FormattingOptions | undefined; + return { provide: { @@ -228,6 +231,16 @@ export function create( languageService.dispose(); }, + provideDocumentFormattingEdits(_document, _range, options) { + formattingOptions = options; + return undefined; + }, + + provideOnTypeFormattingEdits(_document, _position, _key, options) { + formattingOptions = options; + return undefined; + }, + async provideCompletionItems(document, position, completeContext, token) { if (!isSemanticDocument(document)) { @@ -279,7 +292,7 @@ export function create( const { fileName, offset } = data; const document = ctx.getTextDocument(data.uri)!; const [formatOptions, preferences] = await Promise.all([ - getFormatCodeSettings(ctx, document), + getFormatCodeSettings(ctx, document, formattingOptions), getUserPreferences(ctx, document), ]); const details = safeCall(() => ctx.languageService.getCompletionEntryDetails(fileName, offset, data.originalItem.name, formatOptions, data.originalItem.source, preferences, data.originalItem.data)); @@ -402,7 +415,7 @@ export function create( } if (renameInfo.fileToRename) { const [formatOptions, preferences] = await Promise.all([ - getFormatCodeSettings(ctx, document), + getFormatCodeSettings(ctx, document, formattingOptions), getUserPreferences(ctx, document), ]); return renameFile(renameInfo.fileToRename, newName, formatOptions, preferences); @@ -452,13 +465,13 @@ export function create( } return worker(token, () => { - return getCodeActions(document, range, context); + return getCodeActions(document, range, context, formattingOptions); }); }, async resolveCodeAction(codeAction, token) { return await worker(token, () => { - return doCodeActionResolve(codeAction); + return doCodeActionResolve(codeAction, formattingOptions); }) ?? codeAction; }, @@ -706,7 +719,7 @@ export function create( return worker(token, async () => { const document = ctx.getTextDocument(oldUri)!; const [formatOptions, preferences] = await Promise.all([ - getFormatCodeSettings(ctx, document), + getFormatCodeSettings(ctx, document, formattingOptions), getUserPreferences(ctx, document), ]); diff --git a/packages/typescript/lib/semanticFeatures/codeAction.ts b/packages/typescript/lib/semanticFeatures/codeAction.ts index 30bfb066..925f2d92 100644 --- a/packages/typescript/lib/semanticFeatures/codeAction.ts +++ b/packages/typescript/lib/semanticFeatures/codeAction.ts @@ -43,9 +43,9 @@ export function register(ctx: SharedContext) { resolveEditSupport = true; } - return async (document: TextDocument, range: vscode.Range, context: vscode.CodeActionContext) => { + return async (document: TextDocument, range: vscode.Range, context: vscode.CodeActionContext, formattingOptions: vscode.FormattingOptions | undefined) => { const [formatOptions, preferences] = await Promise.all([ - getFormatCodeSettings(ctx, document), + getFormatCodeSettings(ctx, document, formattingOptions), getUserPreferences(ctx, document), ]); diff --git a/packages/typescript/lib/semanticFeatures/codeActionResolve.ts b/packages/typescript/lib/semanticFeatures/codeActionResolve.ts index 8b50d2e6..71eee898 100644 --- a/packages/typescript/lib/semanticFeatures/codeActionResolve.ts +++ b/packages/typescript/lib/semanticFeatures/codeActionResolve.ts @@ -9,12 +9,12 @@ import type { Data, FixAllData, RefactorData } from './codeAction'; import { convertFileTextChanges } from '../utils/lspConverters'; export function register(ctx: SharedContext) { - return async (codeAction: vscode.CodeAction) => { + return async (codeAction: vscode.CodeAction, formattingOptions: vscode.FormattingOptions | undefined) => { const data: Data = codeAction.data; const document = ctx.getTextDocument(data.uri)!; const [formatOptions, preferences] = await Promise.all([ - getFormatCodeSettings(ctx, document), + getFormatCodeSettings(ctx, document, formattingOptions), getUserPreferences(ctx, document), ]);