Skip to content
Merged
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
1 change: 1 addition & 0 deletions editors/vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Following configuration are supported via `settings.json` and can be changed for
| `oxc.typeAware` | `false` | `false` \| `true` | Enable type aware linting. |
| `oxc.flags` | - | `Record<string, string>` | Custom flags passed to the language server. |
| `oxc.fmt.experimental` | `false` | `false` \| `true` | Enable experimental formatting support. This feature is experimental and might not work as expected. |
| `oxc.fmt.configPath` | `null` | `<string>` \| `null` | Path to an oxfmt configuration file. When `null`, the server will use `.oxfmtrc.json` at the workspace root. |

#### Flags

Expand Down
21 changes: 21 additions & 0 deletions editors/vscode/client/WorkspaceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface WorkspaceConfigInterface {
* @default false
*/
['fmt.experimental']: boolean;

/**
* Path to an oxfmt configuration file
* `oxc.fmt.configPath`
*/
['fmt.configPath']?: string | null;
}

export class WorkspaceConfig {
Expand All @@ -79,6 +85,7 @@ export class WorkspaceConfig {
private _typeAware: boolean = false;
private _flags: Record<string, string> = {};
private _formattingExperimental: boolean = false;
private _formattingConfigPath: string | null = null;

constructor(private readonly workspace: WorkspaceFolder) {
this.refresh();
Expand All @@ -101,6 +108,7 @@ export class WorkspaceConfig {
this._typeAware = this.configuration.get<boolean>('typeAware') ?? false;
this._flags = flags;
this._formattingExperimental = this.configuration.get<boolean>('fmt.experimental') ?? false;
this._formattingConfigPath = this.configuration.get<string | null>('fmt.configPath') ?? null;
}

public effectsConfigChange(event: ConfigurationChangeEvent): boolean {
Expand All @@ -125,6 +133,9 @@ export class WorkspaceConfig {
if (event.affectsConfiguration(`${ConfigService.namespace}.fmt.experimental`, this.workspace)) {
return true;
}
if (event.affectsConfiguration(`${ConfigService.namespace}.fmt.configPath`, this.workspace)) {
return true;
}
return false;
}

Expand Down Expand Up @@ -195,6 +206,15 @@ export class WorkspaceConfig {
return this.configuration.update('fmt.experimental', value, ConfigurationTarget.WorkspaceFolder);
}

get formattingConfigPath(): string | null {
return this._formattingConfigPath;
}

updateFormattingConfigPath(value: string | null): PromiseLike<void> {
this._formattingConfigPath = value;
return this.configuration.update('fmt.configPath', value, ConfigurationTarget.WorkspaceFolder);
}

public toLanguageServerConfig(): WorkspaceConfigInterface {
return {
run: this.runTrigger,
Expand All @@ -204,6 +224,7 @@ export class WorkspaceConfig {
typeAware: this.typeAware,
flags: this.flags,
['fmt.experimental']: this.formattingExperimental,
['fmt.configPath']: this.formattingConfigPath ?? null,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class X { foo() { return 42; } }
9 changes: 9 additions & 0 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@
"scope": "resource",
"default": false,
"description": "Enable experimental formatting support. This feature is experimental and might not work as expected."
},
"oxc.fmt.configPath": {
"type": [
"string",
"null"
],
"scope": "resource",
"default": null,
"description": "Path to an oxfmt configuration file"
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion editors/vscode/tests/WorkspaceConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigurationTarget, workspace } from 'vscode';
import { WorkspaceConfig } from '../client/WorkspaceConfig.js';
import { WORKSPACE_FOLDER } from './test-helpers.js';

const keys = ['lint.run', 'configPath', 'tsConfigPath', 'flags', 'unusedDisableDirectives', 'typeAware', 'fmt.experimental'];
const keys = ['lint.run', 'configPath', 'tsConfigPath', 'flags', 'unusedDisableDirectives', 'typeAware', 'fmt.experimental', 'fmt.configPath'];

suite('WorkspaceConfig', () => {
setup(async () => {
Expand Down Expand Up @@ -36,6 +36,7 @@ suite('WorkspaceConfig', () => {
strictEqual(config.typeAware, false);
deepStrictEqual(config.flags, {});
strictEqual(config.formattingExperimental, false);
strictEqual(config.formattingConfigPath, null);
});

test('configPath defaults to null when using nested configs and configPath is empty', async () => {
Expand Down Expand Up @@ -71,6 +72,7 @@ suite('WorkspaceConfig', () => {
config.updateTypeAware(true),
config.updateFlags({ test: 'value' }),
config.updateFormattingExperimental(true),
config.updateFormattingConfigPath('./oxfmt.json'),
]);

const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER);
Expand All @@ -82,5 +84,6 @@ suite('WorkspaceConfig', () => {
strictEqual(wsConfig.get('typeAware'), true);
deepStrictEqual(wsConfig.get('flags'), { test: 'value' });
strictEqual(wsConfig.get('fmt.experimental'), true);
strictEqual(wsConfig.get('fmt.configPath'), './oxfmt.json');
});
});
20 changes: 20 additions & 0 deletions editors/vscode/tests/e2e_server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ teardown(async () => {
await workspace.getConfiguration('oxc').update('tsConfigPath', undefined);
await workspace.getConfiguration('oxc').update('typeAware', undefined);
await workspace.getConfiguration('oxc').update('fmt.experimental', undefined);
await workspace.getConfiguration('oxc').update('fmt.configPath', undefined);
await workspace.getConfiguration('editor').update('defaultFormatter', undefined);
await workspace.saveAll();
});
Expand Down Expand Up @@ -288,4 +289,23 @@ suite('E2E Diagnostics', () => {

strictEqual(content.toString(), "class X {\n foo() {\n return 42;\n }\n}\n");
});

test('formats code with `oxc.fmt.configPath`', async () => {
await loadFixture('formatting_with_config');

await workspace.getConfiguration('oxc').update('fmt.experimental', true);
await workspace.getConfiguration('oxc').update('fmt.configPath', './fixtures/formatter.json');
await workspace.getConfiguration('editor').update('defaultFormatter', 'oxc.oxc-vscode');

await sleep(500); // wait for the server to pick up the new config
const fileUri = Uri.joinPath(fixturesWorkspaceUri(), 'fixtures', 'formatting.ts');

const document = await workspace.openTextDocument(fileUri);
await window.showTextDocument(document);
await commands.executeCommand('editor.action.formatDocument');
await workspace.saveAll();
const content = await workspace.fs.readFile(fileUri);

strictEqual(content.toString(), "class X {\n foo() {\n return 42\n }\n}\n");
});
});
Loading