mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
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:
parent
8cebdc8129
commit
466f395816
4 changed files with 75 additions and 62 deletions
|
|
@ -1,38 +1,26 @@
|
||||||
import { ConfigurationChangeEvent, workspace, WorkspaceConfiguration } from 'vscode';
|
import { workspace } from 'vscode';
|
||||||
import { IDisposable } from './types';
|
|
||||||
|
|
||||||
export class ConfigService implements Config, IDisposable {
|
export class Config implements ConfigInterface {
|
||||||
private static readonly _namespace = 'oxc';
|
private static readonly _namespace = 'oxc';
|
||||||
private readonly _disposables: IDisposable[] = [];
|
|
||||||
private _inner!: WorkspaceConfiguration;
|
|
||||||
private _runTrigger!: Trigger;
|
private _runTrigger!: Trigger;
|
||||||
private _enable!: boolean;
|
private _enable!: boolean;
|
||||||
private _trace!: TraceLevel;
|
private _trace!: TraceLevel;
|
||||||
private _configPath!: string;
|
private _configPath!: string;
|
||||||
private _binPath: string | undefined;
|
private _binPath: string | undefined;
|
||||||
|
|
||||||
public onConfigChange:
|
|
||||||
| ((this: ConfigService, config: ConfigurationChangeEvent) => void)
|
|
||||||
| undefined;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.setSettingsFromWorkspace();
|
this.refresh();
|
||||||
this.onConfigChange = undefined;
|
|
||||||
|
|
||||||
const disposeChangeListener = workspace.onDidChangeConfiguration(
|
|
||||||
this.onVscodeConfigChange.bind(this),
|
|
||||||
);
|
|
||||||
this._disposables.push(disposeChangeListener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private setSettingsFromWorkspace(): void {
|
public refresh(): void {
|
||||||
this._inner = workspace.getConfiguration(ConfigService._namespace);
|
const conf = workspace.getConfiguration(Config._namespace);
|
||||||
|
|
||||||
this._runTrigger = this._inner.get<Trigger>('lint.run') || 'onType';
|
this._runTrigger = conf.get<Trigger>('lint.run') || 'onType';
|
||||||
this._enable = this._inner.get<boolean>('enable') ?? true;
|
this._enable = conf.get<boolean>('enable') ?? true;
|
||||||
this._trace = this._inner.get<TraceLevel>('trace.server') || 'off';
|
this._trace = conf.get<TraceLevel>('trace.server') || 'off';
|
||||||
this._configPath = this._inner.get<string>('configPath') || '.eslintrc';
|
this._configPath = conf.get<string>('configPath') || '.eslintrc';
|
||||||
this._binPath = this._inner.get<string>('path.server');
|
this._binPath = conf.get<string>('path.server');
|
||||||
}
|
}
|
||||||
|
|
||||||
get runTrigger(): Trigger {
|
get runTrigger(): Trigger {
|
||||||
|
|
@ -42,7 +30,7 @@ export class ConfigService implements Config, IDisposable {
|
||||||
set runTrigger(value: Trigger) {
|
set runTrigger(value: Trigger) {
|
||||||
this._runTrigger = value;
|
this._runTrigger = value;
|
||||||
workspace
|
workspace
|
||||||
.getConfiguration(ConfigService._namespace)
|
.getConfiguration(Config._namespace)
|
||||||
.update('lint.run', value);
|
.update('lint.run', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,7 +41,7 @@ export class ConfigService implements Config, IDisposable {
|
||||||
set enable(value: boolean) {
|
set enable(value: boolean) {
|
||||||
this._enable = value;
|
this._enable = value;
|
||||||
workspace
|
workspace
|
||||||
.getConfiguration(ConfigService._namespace)
|
.getConfiguration(Config._namespace)
|
||||||
.update('enable', value);
|
.update('enable', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,7 +52,7 @@ export class ConfigService implements Config, IDisposable {
|
||||||
set trace(value: TraceLevel) {
|
set trace(value: TraceLevel) {
|
||||||
this._trace = value;
|
this._trace = value;
|
||||||
workspace
|
workspace
|
||||||
.getConfiguration(ConfigService._namespace)
|
.getConfiguration(Config._namespace)
|
||||||
.update('trace.server', value);
|
.update('trace.server', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +63,7 @@ export class ConfigService implements Config, IDisposable {
|
||||||
set configPath(value: string) {
|
set configPath(value: string) {
|
||||||
this._configPath = value;
|
this._configPath = value;
|
||||||
workspace
|
workspace
|
||||||
.getConfiguration(ConfigService._namespace)
|
.getConfiguration(Config._namespace)
|
||||||
.update('configPath', value);
|
.update('configPath', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,23 +74,10 @@ export class ConfigService implements Config, IDisposable {
|
||||||
set binPath(value: string | undefined) {
|
set binPath(value: string | undefined) {
|
||||||
this._binPath = value;
|
this._binPath = value;
|
||||||
workspace
|
workspace
|
||||||
.getConfiguration(ConfigService._namespace)
|
.getConfiguration(Config._namespace)
|
||||||
.update('path.server', value);
|
.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 {
|
public toLanguageServerConfig(): LanguageServerConfig {
|
||||||
return {
|
return {
|
||||||
run: this.runTrigger,
|
run: this.runTrigger,
|
||||||
|
|
@ -112,19 +87,18 @@ export class ConfigService implements Config, IDisposable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Trigger = 'onSave' | 'onType';
|
|
||||||
type TraceLevel = 'off' | 'messages' | 'verbose';
|
|
||||||
|
|
||||||
interface LanguageServerConfig {
|
interface LanguageServerConfig {
|
||||||
configPath: string;
|
configPath: string;
|
||||||
enable: boolean;
|
enable: boolean;
|
||||||
run: Trigger;
|
run: Trigger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Trigger = 'onSave' | 'onType';
|
||||||
|
type TraceLevel = 'off' | 'messages' | 'verbose';
|
||||||
/**
|
/**
|
||||||
* See `"contributes.configuration"` in `package.json`
|
* See `"contributes.configuration"` in `package.json`
|
||||||
*/
|
*/
|
||||||
interface Config {
|
interface ConfigInterface {
|
||||||
/**
|
/**
|
||||||
* When to run the linter and generate diagnostics
|
* When to run the linter and generate diagnostics
|
||||||
* `oxc.lint.run`
|
* `oxc.lint.run`
|
||||||
37
editors/vscode/client/ConfigService.ts
Normal file
37
editors/vscode/client/ConfigService.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
import { strictEqual } from 'assert';
|
import { strictEqual } from 'assert';
|
||||||
import { ConfigService } from './config.js';
|
import { Config } from './Config.js';
|
||||||
|
|
||||||
suite('default values on initialization', () => {
|
suite('Config', () => {
|
||||||
const service = new ConfigService();
|
test('default values on initialization', () => {
|
||||||
|
const config = new Config();
|
||||||
|
|
||||||
strictEqual(service.runTrigger, 'onType');
|
strictEqual(config.runTrigger, 'onType');
|
||||||
strictEqual(service.enable, true);
|
strictEqual(config.enable, true);
|
||||||
strictEqual(service.trace, 'off');
|
strictEqual(config.trace, 'off');
|
||||||
strictEqual(service.configPath, '.eslintrc');
|
strictEqual(config.configPath, '.eslintrc');
|
||||||
strictEqual(service.binPath, '');
|
strictEqual(config.binPath, '');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import { MessageType, ShowMessageNotification } from 'vscode-languageclient';
|
||||||
import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';
|
import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';
|
||||||
|
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { ConfigService } from './config';
|
import { ConfigService } from './ConfigService';
|
||||||
|
|
||||||
const languageClientName = 'oxc';
|
const languageClientName = 'oxc';
|
||||||
const outputChannelName = 'Oxc';
|
const outputChannelName = 'Oxc';
|
||||||
|
|
@ -25,7 +25,7 @@ let client: LanguageClient;
|
||||||
let myStatusBarItem: StatusBarItem;
|
let myStatusBarItem: StatusBarItem;
|
||||||
|
|
||||||
export async function activate(context: ExtensionContext) {
|
export async function activate(context: ExtensionContext) {
|
||||||
const config = new ConfigService();
|
const configService = new ConfigService();
|
||||||
const restartCommand = commands.registerCommand(
|
const restartCommand = commands.registerCommand(
|
||||||
OxcCommands.RestartServer,
|
OxcCommands.RestartServer,
|
||||||
async () => {
|
async () => {
|
||||||
|
|
@ -58,7 +58,7 @@ export async function activate(context: ExtensionContext) {
|
||||||
const toggleEnable = commands.registerCommand(
|
const toggleEnable = commands.registerCommand(
|
||||||
OxcCommands.ToggleEnable,
|
OxcCommands.ToggleEnable,
|
||||||
() => {
|
() => {
|
||||||
config.enable = !config.enable;
|
configService.config.enable = !configService.config.enable;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -66,13 +66,13 @@ export async function activate(context: ExtensionContext) {
|
||||||
restartCommand,
|
restartCommand,
|
||||||
showOutputCommand,
|
showOutputCommand,
|
||||||
toggleEnable,
|
toggleEnable,
|
||||||
config,
|
configService,
|
||||||
);
|
);
|
||||||
|
|
||||||
const outputChannel = window.createOutputChannel(outputChannelName, { log: true });
|
const outputChannel = window.createOutputChannel(outputChannelName, { log: true });
|
||||||
|
|
||||||
async function findBinary(): Promise<string> {
|
async function findBinary(): Promise<string> {
|
||||||
let bin = config.binPath;
|
let bin = configService.config.binPath;
|
||||||
if (bin) {
|
if (bin) {
|
||||||
try {
|
try {
|
||||||
await fsPromises.access(bin);
|
await fsPromises.access(bin);
|
||||||
|
|
@ -148,7 +148,7 @@ export async function activate(context: ExtensionContext) {
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
initializationOptions: {
|
initializationOptions: {
|
||||||
settings: config.toLanguageServerConfig(),
|
settings: configService.config.toLanguageServerConfig(),
|
||||||
},
|
},
|
||||||
outputChannel,
|
outputChannel,
|
||||||
traceOutputChannel: outputChannel,
|
traceOutputChannel: outputChannel,
|
||||||
|
|
@ -188,8 +188,8 @@ export async function activate(context: ExtensionContext) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
config.onConfigChange = function onConfigChange() {
|
configService.onConfigChange = function onConfigChange() {
|
||||||
let settings = this.toLanguageServerConfig();
|
let settings = this.config.toLanguageServerConfig();
|
||||||
updateStatsBar(settings.enable);
|
updateStatsBar(settings.enable);
|
||||||
client.sendNotification('workspace/didChangeConfiguration', { settings });
|
client.sendNotification('workspace/didChangeConfiguration', { settings });
|
||||||
};
|
};
|
||||||
|
|
@ -213,7 +213,7 @@ export async function activate(context: ExtensionContext) {
|
||||||
|
|
||||||
myStatusBarItem.backgroundColor = bgColor;
|
myStatusBarItem.backgroundColor = bgColor;
|
||||||
}
|
}
|
||||||
updateStatsBar(config.enable);
|
updateStatsBar(configService.config.enable);
|
||||||
client.start();
|
client.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue