Skip to content

Commit e903ba2

Browse files
committed
refactor(editor): split Config to VSCodeConfig and WorkspaceConfig (#10572)
1 parent f3eac51 commit e903ba2

File tree

8 files changed

+256
-188
lines changed

8 files changed

+256
-188
lines changed

editors/vscode/client/Config.ts

Lines changed: 0 additions & 153 deletions
This file was deleted.

editors/vscode/client/ConfigService.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { ConfigurationChangeEvent, workspace } from 'vscode';
2-
import { Config } from './Config';
32
import { IDisposable } from './types';
3+
import { VSCodeConfig } from './VSCodeConfig';
4+
import { WorkspaceConfig } from './WorkspaceConfig';
45

56
export class ConfigService implements IDisposable {
6-
private static readonly _namespace = 'oxc';
7+
public static readonly namespace = 'oxc';
78
private readonly _disposables: IDisposable[] = [];
89

9-
public config: Config;
10+
public vsCodeConfig: VSCodeConfig;
11+
12+
private _workspaceConfig: WorkspaceConfig;
1013

1114
public onConfigChange:
1215
| ((this: ConfigService, config: ConfigurationChangeEvent) => Promise<void>)
1316
| undefined;
1417

1518
constructor() {
16-
this.config = new Config();
19+
const conf = workspace.getConfiguration(ConfigService.namespace);
20+
this.vsCodeConfig = new VSCodeConfig(conf);
21+
this._workspaceConfig = new WorkspaceConfig(conf);
1722
this.onConfigChange = undefined;
1823

1924
const disposeChangeListener = workspace.onDidChangeConfiguration(
@@ -22,9 +27,19 @@ export class ConfigService implements IDisposable {
2227
this._disposables.push(disposeChangeListener);
2328
}
2429

30+
public get rootServerConfig(): WorkspaceConfig {
31+
return this._workspaceConfig;
32+
}
33+
34+
public refresh(): void {
35+
const conf = workspace.getConfiguration(ConfigService.namespace);
36+
this.vsCodeConfig.refresh(conf);
37+
this.rootServerConfig.refresh(conf);
38+
}
39+
2540
private async onVscodeConfigChange(event: ConfigurationChangeEvent): Promise<void> {
26-
if (event.affectsConfiguration(ConfigService._namespace)) {
27-
this.config.refresh();
41+
if (event.affectsConfiguration(ConfigService.namespace)) {
42+
this.refresh();
2843
await this.onConfigChange?.(event);
2944
}
3045
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { strictEqual } from 'assert';
2+
import { Uri, workspace, WorkspaceEdit } from 'vscode';
3+
import { VSCodeConfig } from './VSCodeConfig.js';
4+
5+
const conf = workspace.getConfiguration('oxc');
6+
7+
suite('Config', () => {
8+
setup(async () => {
9+
const keys = ['enable', 'trace.server', 'path.server'];
10+
11+
await Promise.all(keys.map(key => conf.update(key, undefined)));
12+
});
13+
14+
suiteTeardown(async () => {
15+
const WORKSPACE_DIR = workspace.workspaceFolders![0].uri.toString();
16+
const file = Uri.parse(WORKSPACE_DIR + '/.vscode/settings.json');
17+
const edit = new WorkspaceEdit();
18+
edit.deleteFile(file);
19+
await workspace.applyEdit(edit);
20+
});
21+
22+
test('default values on initialization', () => {
23+
const config = new VSCodeConfig(conf);
24+
25+
strictEqual(config.enable, true);
26+
strictEqual(config.trace, 'off');
27+
strictEqual(config.binPath, '');
28+
});
29+
30+
test('updating values updates the workspace configuration', async () => {
31+
const config = new VSCodeConfig(conf);
32+
33+
await Promise.all([
34+
config.updateEnable(false),
35+
config.updateTrace('messages'),
36+
config.updateBinPath('./binary'),
37+
]);
38+
39+
const wsConfig = workspace.getConfiguration('oxc');
40+
41+
strictEqual(wsConfig.get('enable'), false);
42+
strictEqual(wsConfig.get('trace.server'), 'messages');
43+
strictEqual(wsConfig.get('path.server'), './binary');
44+
});
45+
});

editors/vscode/client/VSCodeConfig.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { workspace, WorkspaceConfiguration } from 'vscode';
2+
import { ConfigService } from './ConfigService';
3+
4+
export const oxlintConfigFileName = '.oxlintrc.json';
5+
6+
export class VSCodeConfig implements VSCodeConfigInterface {
7+
private _enable!: boolean;
8+
private _trace!: TraceLevel;
9+
private _binPath: string | undefined;
10+
11+
constructor(configuration: WorkspaceConfiguration) {
12+
this.refresh(configuration);
13+
}
14+
15+
public refresh(configuration: WorkspaceConfiguration): void {
16+
this._enable = configuration.get<boolean>('enable') ?? true;
17+
this._trace = configuration.get<TraceLevel>('trace.server') || 'off';
18+
this._binPath = configuration.get<string>('path.server');
19+
}
20+
21+
get enable(): boolean {
22+
return this._enable;
23+
}
24+
25+
updateEnable(value: boolean): PromiseLike<void> {
26+
this._enable = value;
27+
return workspace
28+
.getConfiguration(ConfigService.namespace)
29+
.update('enable', value);
30+
}
31+
32+
get trace(): TraceLevel {
33+
return this._trace;
34+
}
35+
36+
updateTrace(value: TraceLevel): PromiseLike<void> {
37+
this._trace = value;
38+
return workspace
39+
.getConfiguration(ConfigService.namespace)
40+
.update('trace.server', value);
41+
}
42+
43+
get binPath(): string | undefined {
44+
return this._binPath;
45+
}
46+
47+
updateBinPath(value: string | undefined): PromiseLike<void> {
48+
this._binPath = value;
49+
return workspace
50+
.getConfiguration(ConfigService.namespace)
51+
.update('path.server', value);
52+
}
53+
}
54+
55+
type TraceLevel = 'off' | 'messages' | 'verbose';
56+
/**
57+
* See `"contributes.configuration"` in `package.json`
58+
*/
59+
interface VSCodeConfigInterface {
60+
/**
61+
* `oxc.enable`
62+
*
63+
* @default true
64+
*/
65+
enable: boolean;
66+
/**
67+
* Trace VSCode <-> Oxc Language Server communication
68+
* `oxc.trace.server`
69+
*
70+
* @default 'off'
71+
*/
72+
trace: TraceLevel;
73+
/**
74+
* Path to LSP binary
75+
* `oxc.path.server`
76+
* @default undefined
77+
*/
78+
binPath: string | undefined;
79+
}

0 commit comments

Comments
 (0)