Skip to content

Commit

Permalink
fix: fixes source.fixAll.xo code action on save
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Feb 5, 2025
1 parent 74eeece commit 4560501
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
3 changes: 1 addition & 2 deletions client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import {
LanguageClient,
TransportKind,
type DocumentSelector,
type LanguageClientOptions,
type ServerOptions
} from 'vscode-languageclient/node';
Expand Down Expand Up @@ -95,7 +94,7 @@ export async function activate(context: ExtensionContext) {
// that may possibly affect the options xo should be using
workspace.createFileSystemWatcher('**/.eslintignore'),
workspace.createFileSystemWatcher('**/.xo-confi{g.cjs,g.json,g.js,g}'),
workspace.createFileSystemWatcher('**/xo.confi{g.cjs,g.js,g}'),
workspace.createFileSystemWatcher('**/xo.confi{g.cjs,g.js,g.ts,g.cts,g.mts}'),
workspace.createFileSystemWatcher('**/package.json')
].map((watcher) => watcher.onDidChange(restart)),
/**
Expand Down
19 changes: 12 additions & 7 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as process from 'node:process';
import * as path from 'node:path';
import {
type CodeAction,
createConnection,
ProposedFeatures,
TextDocuments,
Expand All @@ -17,7 +18,6 @@ import {
type TextDocumentIdentifier,
type DidChangeConfigurationParams,
type TextDocumentChangeEvent,
type CodeAction,
type DocumentRangeFormattingParams
} from 'vscode-languageserver/node';
import {TextDocument} from 'vscode-languageserver-textdocument';
Expand Down Expand Up @@ -202,7 +202,11 @@ class LintServer {
documentFormattingProvider: true,
documentRangeFormattingProvider: true,
codeActionProvider: {
codeActionKinds: [CodeActionKind.QuickFix, CodeActionKind.SourceFixAll]
codeActionKinds: [
CodeActionKind.QuickFix,
CodeActionKind.SourceFixAll,
`${CodeActionKind.SourceFixAll}.xo`
]
}
}
};
Expand Down Expand Up @@ -345,14 +349,15 @@ class LintServer {
}

const codeActions: CodeAction[] = [];
if (
context.only?.includes(CodeActionKind.SourceFixAll) || // eslint-disable-line @typescript-eslint/prefer-nullish-coalescing
context.only?.includes(`${CodeActionKind.SourceFixAll}.xo`)
) {

const isFixAll = context.only?.includes(CodeActionKind.SourceFixAll);
const isFixAllXo = context.only?.includes(`${CodeActionKind.SourceFixAll}.xo`);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (isFixAll || isFixAllXo) {
const fixes = await this.getDocumentFormatting(params.textDocument.uri);
const codeAction: CodeAction = {
title: 'Fix all XO auto-fixable problems',
kind: CodeActionKind.SourceFixAll,
kind: isFixAllXo ? `${CodeActionKind.SourceFixAll}.xo` : CodeActionKind.SourceFixAll,
edit: {
changes: {
[document.uri]: fixes.edits
Expand Down
2 changes: 1 addition & 1 deletion test/lsp/code-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('Server code actions', async () => {
assert.deepEqual(codeActions, [
{
title: 'Fix all XO auto-fixable problems',
kind: 'source.fixAll',
kind: 'source.fixAll.xo',
edit: {changes: {uri: []}}
}
]);
Expand Down
2 changes: 1 addition & 1 deletion test/lsp/initialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Server.handleInitialize', async () => {
textDocumentSync: {openClose: true, change: 2},
documentFormattingProvider: true,
documentRangeFormattingProvider: true,
codeActionProvider: {codeActionKinds: ['quickfix', 'source.fixAll']}
codeActionProvider: {codeActionKinds: ['quickfix', 'source.fixAll', 'source.fixAll.xo']}
}
});
});
Expand Down

0 comments on commit 4560501

Please sign in to comment.