refactor(vscode): split ConfigService and Config (#7376)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alexander S. 2024-11-21 02:57:09 +01:00 committed by GitHub
parent 8cebdc8129
commit 466f395816
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 75 additions and 62 deletions

View file

@ -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<Trigger>('lint.run') || 'onType';
this._enable = this._inner.get<boolean>('enable') ?? true;
this._trace = this._inner.get<TraceLevel>('trace.server') || 'off';
this._configPath = this._inner.get<string>('configPath') || '.eslintrc';
this._binPath = this._inner.get<string>('path.server');
this._runTrigger = conf.get<Trigger>('lint.run') || 'onType';
this._enable = conf.get<boolean>('enable') ?? true;
this._trace = conf.get<TraceLevel>('trace.server') || 'off';
this._configPath = conf.get<string>('configPath') || '.eslintrc';
this._binPath = conf.get<string>('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`

View file

@ -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();
}
}
}

View file

@ -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, '');
});
});

View file

@ -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<string> {
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();
}