Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(vscode): split ConfigService and Config #7376

Merged
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
64 changes: 19 additions & 45 deletions editors/vscode/client/config.ts → editors/vscode/client/Config.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}

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

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

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

Expand All @@ -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,
Expand All @@ -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`
Expand Down
37 changes: 37 additions & 0 deletions editors/vscode/client/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
18 changes: 10 additions & 8 deletions editors/vscode/client/config.spec.ts
Original file line number Diff line number Diff line change
@@ -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, '');
});
});
18 changes: 9 additions & 9 deletions editors/vscode/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 () => {
Expand Down Expand Up @@ -58,21 +58,21 @@ export async function activate(context: ExtensionContext) {
const toggleEnable = commands.registerCommand(
OxcCommands.ToggleEnable,
() => {
config.enable = !config.enable;
configService.config.enable = !configService.config.enable;
},
);

context.subscriptions.push(
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);
Expand Down Expand Up @@ -148,7 +148,7 @@ export async function activate(context: ExtensionContext) {
],
},
initializationOptions: {
settings: config.toLanguageServerConfig(),
settings: configService.config.toLanguageServerConfig(),
},
outputChannel,
traceOutputChannel: outputChannel,
Expand Down Expand Up @@ -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 });
};
Expand All @@ -213,7 +213,7 @@ export async function activate(context: ExtensionContext) {

myStatusBarItem.backgroundColor = bgColor;
}
updateStatsBar(config.enable);
updateStatsBar(configService.config.enable);
client.start();
}

Expand Down
Loading