Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ To enable it as your default formatter, use a VS Code `settings.json` like:
}
```

To run Oxc formatting through VS Code code actions on save, configure `editor.codeActionsOnSave`:
To run Oxc formatting through VS Code code actions on save, configure `editor.codeActionsOnSave`. No need to set `editor.defaultFormatter`:

```json
{
Expand All @@ -52,12 +52,12 @@ To run Oxc formatting through VS Code code actions on save, configure `editor.co
}
```

Running formatting as a code action on save, allows to define the order of changes when both formatting and lint fixes are applied on save. For example, the below configuration will run the formatter first, and then apply lint fixes:
Running formatting as a code action on save allows to define the order of changes when both formatting and lint fixes are applied on save. For example, the below configuration will run the formatter first, and then apply lint fixes:

```json
{
"editor.defaultFormatter": "oxc.oxc-vscode",
"editor.formatOnSave": false, // disable default behavior
"editor.defaultFormatter": "oxc.oxc-vscode", // optional, recommended for manual formatting
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.format.oxc": "always", // run formatter first
"source.fixAll.oxc": "always" // run lint fixes after
Expand Down
1 change: 1 addition & 0 deletions client/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const enum OxcCommands {
// only for formatter.ts usage
RestartServerFmt = `${commandPrefix}.restartServerFormatter`,
ToggleEnableFmt = `${commandPrefix}.toggleEnableFormatter`,
FormatDocument = `${commandPrefix}.formatDocument`,

// always available
CopyDebugInfo = `${commandPrefix}.copyDebugInfo`,
Expand Down
41 changes: 34 additions & 7 deletions client/tools/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
ConfigurationChangeEvent,
languages,
LogOutputChannel,
TextEdit,
Uri,
window,
workspace,
WorkspaceEdit,
} from "vscode";

import {
ConfigurationParams,
DocumentFormattingRequest,
DocumentSelector,
ShowMessageNotification,
} from "vscode-languageclient";
Expand All @@ -37,9 +41,9 @@ const formatCodeActionKind = CodeActionKind.Source.append("format.oxc");

const formatCodeAction = new CodeAction("Format Document", formatCodeActionKind);
formatCodeAction.command = {
command: "editor.action.formatDocument",
command: OxcCommands.FormatDocument,
title: "Format Document",
tooltip: "Format the document using the default formatter",
tooltip: "Format the document with oxfmt",
};

// This list is not used as-is for implementation to determine whether formatting processing is possible.
Expand Down Expand Up @@ -314,11 +318,8 @@ export default class FormatterTool implements ToolInterface {
const formatAction = languages.registerCodeActionsProvider(
this.documentSelectors,
{
provideCodeActions: (doc) => {
if (
configService.vsCodeConfig.enableOxfmt === false ||
workspace.getConfiguration("editor", doc).get("defaultFormatter") !== "oxc.oxc-vscode"
) {
provideCodeActions: (_doc) => {
if (configService.vsCodeConfig.enableOxfmt === false) {
return [];
}
return [formatCodeAction];
Expand Down Expand Up @@ -374,6 +375,31 @@ export default class FormatterTool implements ToolInterface {
// Create the language client and start the client.
this.client = new LanguageClient(languageClientName, serverOptions, clientOptions);

const formatCommand = commands.registerCommand(OxcCommands.FormatDocument, async () => {
const editor = window.activeTextEditor;
if (!editor || !this.client) return;

const doc = editor.document;
const editorConfig = workspace.getConfiguration("editor", doc);
const tabSize = editorConfig.get<number>("tabSize") ?? 4;
const insertSpaces = editorConfig.get<boolean>("insertSpaces") ?? true;

try {
const edits = await this.client.sendRequest(DocumentFormattingRequest.type, {
textDocument: { uri: doc.uri.toString() },
options: { tabSize, insertSpaces },
});

if (edits && edits.length > 0) {
const edit = new WorkspaceEdit();
edit.set(doc.uri, edits as TextEdit[]);
await workspace.applyEdit(edit);
}
} catch {
// Server might not support formatting for this document type
}
});

const onNotificationDispose = this.client.onNotification(
ShowMessageNotification.type,
(params) => {
Expand All @@ -386,6 +412,7 @@ export default class FormatterTool implements ToolInterface {
restartCommand.dispose();
toggleEnable.dispose();
formatAction.dispose();
formatCommand.dispose();
onNotificationDispose.dispose();
};

Expand Down