Skip to content
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
19 changes: 19 additions & 0 deletions editors/vscode/client/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "node:path";
import { ConfigurationChangeEvent, RelativePattern, Uri, workspace, WorkspaceFolder } from "vscode";
import { DiagnosticPullMode } from "vscode-languageclient";
import { validateSafeBinaryPath } from "./PathValidator";
import { IDisposable } from "./types";
import { VSCodeConfig } from "./VSCodeConfig";
Expand Down Expand Up @@ -90,6 +91,24 @@ export class ConfigService implements IDisposable {
return this.searchBinaryPath(this.vsCodeConfig.binPathOxfmt, "oxfmt");
}

public shouldRequestDiagnostics(
textDocumentUri: Uri,
diagnosticPullMode: DiagnosticPullMode,
): boolean {
if (!this.vsCodeConfig.enable) {
return false;
}

const textDocumentPath = textDocumentUri.path;

for (const [workspaceUri, workspaceConfig] of this.workspaceConfigs.entries()) {
if (textDocumentPath.startsWith(workspaceUri)) {
return workspaceConfig.shouldRequestDiagnostics(diagnosticPullMode);
}
}
return false;
}

private async searchBinaryPath(
settingsBinary: string | undefined,
defaultPattern: string,
Expand Down
21 changes: 13 additions & 8 deletions editors/vscode/client/WorkspaceConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ConfigurationChangeEvent, ConfigurationTarget, workspace, WorkspaceFolder } from "vscode";
import { DiagnosticPullMode } from "vscode-languageclient";
import { ConfigService } from "./ConfigService";

export const oxlintConfigFileName = ".oxlintrc.json";

export type Trigger = "onSave" | "onType";

type UnusedDisableDirectives = "allow" | "warn" | "deny";

export enum FixKind {
Expand Down Expand Up @@ -42,7 +41,7 @@ interface WorkspaceConfigInterface {
*
* @default 'onType'
*/
run: Trigger;
run: DiagnosticPullMode;

/**
* Define how directive comments like `// oxlint-disable-line` should be reported,
Expand Down Expand Up @@ -99,7 +98,7 @@ export type OxfmtWorkspaceConfigInterface = Pick<WorkspaceConfigInterface, "fmt.
export class WorkspaceConfig {
private _configPath: string | null = null;
private _tsConfigPath: string | null = null;
private _runTrigger: Trigger = "onType";
private _runTrigger: DiagnosticPullMode = DiagnosticPullMode.onType;
private _unusedDisableDirectives: UnusedDisableDirectives = "allow";
private _typeAware: boolean = false;
private _disableNestedConfig: boolean = false;
Expand Down Expand Up @@ -134,7 +133,8 @@ export class WorkspaceConfig {
disableNestedConfig = true;
}

this._runTrigger = this.configuration.get<Trigger>("lint.run") || "onType";
this._runTrigger =
this.configuration.get<DiagnosticPullMode>("lint.run") || DiagnosticPullMode.onType;
this._configPath = this.configuration.get<string | null>("configPath") ?? null;
this._tsConfigPath = this.configuration.get<string | null>("tsConfigPath") ?? null;
this._unusedDisableDirectives =
Expand Down Expand Up @@ -188,11 +188,11 @@ export class WorkspaceConfig {
return this.configPath !== null && this.configPath !== oxlintConfigFileName;
}

get runTrigger(): Trigger {
get runTrigger(): DiagnosticPullMode {
return this._runTrigger;
}

updateRunTrigger(value: Trigger): PromiseLike<void> {
updateRunTrigger(value: DiagnosticPullMode): PromiseLike<void> {
this._runTrigger = value;
return this.configuration.update("lint.run", value, ConfigurationTarget.WorkspaceFolder);
}
Expand Down Expand Up @@ -268,15 +268,20 @@ export class WorkspaceConfig {
return this.configuration.update("fmt.configPath", value, ConfigurationTarget.WorkspaceFolder);
}

public shouldRequestDiagnostics(diagnosticPullMode: DiagnosticPullMode): boolean {
return diagnosticPullMode === this.runTrigger;
}

public toOxlintConfig(): OxlintWorkspaceConfigInterface {
return {
run: this.runTrigger,
configPath: this.configPath ?? null,
tsConfigPath: this.tsConfigPath ?? null,
unusedDisableDirectives: this.unusedDisableDirectives,
typeAware: this.typeAware,
disableNestedConfig: this.disableNestedConfig,
fixKind: this.fixKind,
// keep for backward compatibility
run: this.runTrigger,
// deprecated, kept for backward compatibility
flags: {
disable_nested_config: this.disableNestedConfig ? "true" : "false",
Expand Down
6 changes: 6 additions & 0 deletions editors/vscode/client/tools/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ export default class LinterTool implements ToolInterface {
initializationOptions: configService.oxlintServerConfig,
outputChannel,
traceOutputChannel: outputChannel,
diagnosticPullOptions: {
onChange: true,
onSave: true,
onTabs: false,
filter: (document, mode) => !configService.shouldRequestDiagnostics(document.uri, mode),
},
middleware: {
handleDiagnostics: (uri, diagnostics, next) => {
for (const diag of diagnostics) {
Expand Down
5 changes: 3 additions & 2 deletions editors/vscode/tests/WorkspaceConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strictEqual } from 'assert';
import { ConfigurationTarget, workspace } from 'vscode';
import { DiagnosticPullMode } from 'vscode-languageclient';
import { FixKind, WorkspaceConfig } from '../client/WorkspaceConfig.js';
import { WORKSPACE_FOLDER } from './test-helpers.js';

Expand Down Expand Up @@ -65,7 +66,7 @@ suite('WorkspaceConfig', () => {
const config = new WorkspaceConfig(WORKSPACE_FOLDER);

await Promise.all([
config.updateRunTrigger('onSave'),
config.updateRunTrigger(DiagnosticPullMode.onSave),
config.updateConfigPath('./somewhere'),
config.updateTsConfigPath('./tsconfig.json'),
config.updateUnusedDisableDirectives('deny'),
Expand All @@ -91,7 +92,7 @@ suite('WorkspaceConfig', () => {
const config = new WorkspaceConfig(WORKSPACE_FOLDER);

await Promise.all([
config.updateRunTrigger('onSave'),
config.updateRunTrigger(DiagnosticPullMode.onSave),
config.updateConfigPath('./somewhere'),
config.updateTsConfigPath('./tsconfig.json'),
config.updateUnusedDisableDirectives('deny'),
Expand Down
Loading