diff --git a/editors/vscode/client/ConfigService.ts b/editors/vscode/client/ConfigService.ts index 3a9280fa5ec37..c60a687a065c4 100644 --- a/editors/vscode/client/ConfigService.ts +++ b/editors/vscode/client/ConfigService.ts @@ -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"; @@ -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, diff --git a/editors/vscode/client/WorkspaceConfig.ts b/editors/vscode/client/WorkspaceConfig.ts index 49cae2baeccde..54feaa978dd49 100644 --- a/editors/vscode/client/WorkspaceConfig.ts +++ b/editors/vscode/client/WorkspaceConfig.ts @@ -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 { @@ -42,7 +41,7 @@ interface WorkspaceConfigInterface { * * @default 'onType' */ - run: Trigger; + run: DiagnosticPullMode; /** * Define how directive comments like `// oxlint-disable-line` should be reported, @@ -99,7 +98,7 @@ export type OxfmtWorkspaceConfigInterface = Pick("lint.run") || "onType"; + this._runTrigger = + this.configuration.get("lint.run") || DiagnosticPullMode.onType; this._configPath = this.configuration.get("configPath") ?? null; this._tsConfigPath = this.configuration.get("tsConfigPath") ?? null; this._unusedDisableDirectives = @@ -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 { + updateRunTrigger(value: DiagnosticPullMode): PromiseLike { this._runTrigger = value; return this.configuration.update("lint.run", value, ConfigurationTarget.WorkspaceFolder); } @@ -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", diff --git a/editors/vscode/client/tools/linter.ts b/editors/vscode/client/tools/linter.ts index 2db0bbbd0aefa..71ac282bc4910 100644 --- a/editors/vscode/client/tools/linter.ts +++ b/editors/vscode/client/tools/linter.ts @@ -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) { diff --git a/editors/vscode/tests/WorkspaceConfig.spec.ts b/editors/vscode/tests/WorkspaceConfig.spec.ts index d8b02aa0c5368..59bd7740a0579 100644 --- a/editors/vscode/tests/WorkspaceConfig.spec.ts +++ b/editors/vscode/tests/WorkspaceConfig.spec.ts @@ -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'; @@ -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'), @@ -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'),