Skip to content

Commit

Permalink
feat(typescript): capture editor formatting options from the last for…
Browse files Browse the repository at this point in the history
…matting request

close #30
  • Loading branch information
johnsoncodehk committed May 9, 2024
1 parent b26285a commit 5987cf8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/typescript/lib/configs/getFormatCodeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getConfigTitle } from '../shared';
export async function getFormatCodeSettings(
ctx: ServiceContext,
document: TextDocument,
options?: FormattingOptions,
options: FormattingOptions | undefined,
): Promise<ts.FormatCodeSettings> {
const config = await ctx.env.getConfiguration?.<any>(getConfigTitle(document) + '.format') ?? {};
return {
Expand Down
25 changes: 19 additions & 6 deletions packages/typescript/lib/plugins/semantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -217,6 +218,8 @@ export function create(
/* typescript-language-features is hardcode true */
const renameInfoOptions = { allowRenameOfImportPath: true };

let formattingOptions: FormattingOptions | undefined;

return {

provide: {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
},

Expand Down Expand Up @@ -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),
]);

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/lib/semanticFeatures/codeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/lib/semanticFeatures/codeActionResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);

Expand Down

0 comments on commit 5987cf8

Please sign in to comment.