diff --git a/editors/vscode/client/config.ts b/editors/vscode/client/Config.ts similarity index 54% rename from editors/vscode/client/config.ts rename to editors/vscode/client/Config.ts index d2eabe94ec753..2f19fdf75ac20 100644 --- a/editors/vscode/client/config.ts +++ b/editors/vscode/client/Config.ts @@ -1,38 +1,26 @@ -import { ConfigurationChangeEvent, workspace, WorkspaceConfiguration } from 'vscode'; -import { IDisposable } from './types'; +import { workspace } from 'vscode'; -export class ConfigService implements Config, IDisposable { +export class Config implements ConfigInterface { private static readonly _namespace = 'oxc'; - private readonly _disposables: IDisposable[] = []; - private _inner!: WorkspaceConfiguration; + private _runTrigger!: Trigger; private _enable!: boolean; private _trace!: TraceLevel; private _configPath!: string; private _binPath: string | undefined; - public onConfigChange: - | ((this: ConfigService, config: ConfigurationChangeEvent) => void) - | undefined; - constructor() { - this.setSettingsFromWorkspace(); - this.onConfigChange = undefined; - - const disposeChangeListener = workspace.onDidChangeConfiguration( - this.onVscodeConfigChange.bind(this), - ); - this._disposables.push(disposeChangeListener); + this.refresh(); } - private setSettingsFromWorkspace(): void { - this._inner = workspace.getConfiguration(ConfigService._namespace); + public refresh(): void { + const conf = workspace.getConfiguration(Config._namespace); - this._runTrigger = this._inner.get('lint.run') || 'onType'; - this._enable = this._inner.get('enable') ?? true; - this._trace = this._inner.get('trace.server') || 'off'; - this._configPath = this._inner.get('configPath') || '.eslintrc'; - this._binPath = this._inner.get('path.server'); + this._runTrigger = conf.get('lint.run') || 'onType'; + this._enable = conf.get('enable') ?? true; + this._trace = conf.get('trace.server') || 'off'; + this._configPath = conf.get('configPath') || '.eslintrc'; + this._binPath = conf.get('path.server'); } get runTrigger(): Trigger { @@ -42,7 +30,7 @@ export class ConfigService implements Config, IDisposable { set runTrigger(value: Trigger) { this._runTrigger = value; workspace - .getConfiguration(ConfigService._namespace) + .getConfiguration(Config._namespace) .update('lint.run', value); } @@ -53,7 +41,7 @@ export class ConfigService implements Config, IDisposable { set enable(value: boolean) { this._enable = value; workspace - .getConfiguration(ConfigService._namespace) + .getConfiguration(Config._namespace) .update('enable', value); } @@ -64,7 +52,7 @@ export class ConfigService implements Config, IDisposable { set trace(value: TraceLevel) { this._trace = value; workspace - .getConfiguration(ConfigService._namespace) + .getConfiguration(Config._namespace) .update('trace.server', value); } @@ -75,7 +63,7 @@ export class ConfigService implements Config, IDisposable { set configPath(value: string) { this._configPath = value; workspace - .getConfiguration(ConfigService._namespace) + .getConfiguration(Config._namespace) .update('configPath', value); } @@ -86,23 +74,10 @@ export class ConfigService implements Config, IDisposable { set binPath(value: string | undefined) { this._binPath = value; workspace - .getConfiguration(ConfigService._namespace) + .getConfiguration(Config._namespace) .update('path.server', value); } - private onVscodeConfigChange(event: ConfigurationChangeEvent): void { - if (event.affectsConfiguration(ConfigService._namespace)) { - this.setSettingsFromWorkspace(); - this.onConfigChange?.call(this, event); - } - } - - dispose() { - for (const disposable of this._disposables) { - disposable.dispose(); - } - } - public toLanguageServerConfig(): LanguageServerConfig { return { run: this.runTrigger, @@ -112,19 +87,18 @@ export class ConfigService implements Config, IDisposable { } } -type Trigger = 'onSave' | 'onType'; -type TraceLevel = 'off' | 'messages' | 'verbose'; - interface LanguageServerConfig { configPath: string; enable: boolean; run: Trigger; } +export type Trigger = 'onSave' | 'onType'; +type TraceLevel = 'off' | 'messages' | 'verbose'; /** * See `"contributes.configuration"` in `package.json` */ -interface Config { +interface ConfigInterface { /** * When to run the linter and generate diagnostics * `oxc.lint.run` diff --git a/editors/vscode/client/ConfigService.ts b/editors/vscode/client/ConfigService.ts new file mode 100644 index 0000000000000..7d84f09708085 --- /dev/null +++ b/editors/vscode/client/ConfigService.ts @@ -0,0 +1,37 @@ +import { ConfigurationChangeEvent, workspace } from 'vscode'; +import { Config } from './Config'; +import { IDisposable } from './types'; + +export class ConfigService implements IDisposable { + private static readonly _namespace = 'oxc'; + private readonly _disposables: IDisposable[] = []; + + public config: Config; + + public onConfigChange: + | ((this: ConfigService, config: ConfigurationChangeEvent) => void) + | undefined; + + constructor() { + this.config = new Config(); + this.onConfigChange = undefined; + + const disposeChangeListener = workspace.onDidChangeConfiguration( + this.onVscodeConfigChange.bind(this), + ); + this._disposables.push(disposeChangeListener); + } + + private onVscodeConfigChange(event: ConfigurationChangeEvent): void { + if (event.affectsConfiguration(ConfigService._namespace)) { + this.config.refresh(); + this.onConfigChange?.call(this, event); + } + } + + dispose() { + for (const disposable of this._disposables) { + disposable.dispose(); + } + } +} diff --git a/editors/vscode/client/config.spec.ts b/editors/vscode/client/config.spec.ts index 5b71a5f6537c3..b2c97b22f4683 100644 --- a/editors/vscode/client/config.spec.ts +++ b/editors/vscode/client/config.spec.ts @@ -1,12 +1,14 @@ import { strictEqual } from 'assert'; -import { ConfigService } from './config.js'; +import { Config } from './Config.js'; -suite('default values on initialization', () => { - const service = new ConfigService(); +suite('Config', () => { + test('default values on initialization', () => { + const config = new Config(); - strictEqual(service.runTrigger, 'onType'); - strictEqual(service.enable, true); - strictEqual(service.trace, 'off'); - strictEqual(service.configPath, '.eslintrc'); - strictEqual(service.binPath, ''); + strictEqual(config.runTrigger, 'onType'); + strictEqual(config.enable, true); + strictEqual(config.trace, 'off'); + strictEqual(config.configPath, '.eslintrc'); + strictEqual(config.binPath, ''); + }); }); diff --git a/editors/vscode/client/extension.ts b/editors/vscode/client/extension.ts index 53a47dd1e4afc..8d5223494a8f7 100644 --- a/editors/vscode/client/extension.ts +++ b/editors/vscode/client/extension.ts @@ -7,7 +7,7 @@ import { MessageType, ShowMessageNotification } from 'vscode-languageclient'; import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node'; import { join } from 'node:path'; -import { ConfigService } from './config'; +import { ConfigService } from './ConfigService'; const languageClientName = 'oxc'; const outputChannelName = 'Oxc'; @@ -25,7 +25,7 @@ let client: LanguageClient; let myStatusBarItem: StatusBarItem; export async function activate(context: ExtensionContext) { - const config = new ConfigService(); + const configService = new ConfigService(); const restartCommand = commands.registerCommand( OxcCommands.RestartServer, async () => { @@ -58,7 +58,7 @@ export async function activate(context: ExtensionContext) { const toggleEnable = commands.registerCommand( OxcCommands.ToggleEnable, () => { - config.enable = !config.enable; + configService.config.enable = !configService.config.enable; }, ); @@ -66,13 +66,13 @@ export async function activate(context: ExtensionContext) { restartCommand, showOutputCommand, toggleEnable, - config, + configService, ); const outputChannel = window.createOutputChannel(outputChannelName, { log: true }); async function findBinary(): Promise { - let bin = config.binPath; + let bin = configService.config.binPath; if (bin) { try { await fsPromises.access(bin); @@ -148,7 +148,7 @@ export async function activate(context: ExtensionContext) { ], }, initializationOptions: { - settings: config.toLanguageServerConfig(), + settings: configService.config.toLanguageServerConfig(), }, outputChannel, traceOutputChannel: outputChannel, @@ -188,8 +188,8 @@ export async function activate(context: ExtensionContext) { }); }); - config.onConfigChange = function onConfigChange() { - let settings = this.toLanguageServerConfig(); + configService.onConfigChange = function onConfigChange() { + let settings = this.config.toLanguageServerConfig(); updateStatsBar(settings.enable); client.sendNotification('workspace/didChangeConfiguration', { settings }); }; @@ -213,7 +213,7 @@ export async function activate(context: ExtensionContext) { myStatusBarItem.backgroundColor = bgColor; } - updateStatsBar(config.enable); + updateStatsBar(configService.config.enable); client.start(); }