diff --git a/package.json b/package.json index 99482c8e..cef783ef 100644 --- a/package.json +++ b/package.json @@ -102,9 +102,22 @@ }, "ruff.configuration": { "default": null, - "markdownDescription": "Path to a `ruff.toml` or `pyproject.toml` file to use for configuration. By default, Ruff will discover configuration for each project from the filesystem, mirroring the behavior of the Ruff CLI.\n\n**This setting is used only by the native server.**", + "markdownDescription": "Configuration overrides for Ruff. See [the documentation](https://docs.astral.sh/ruff/editors/settings/#configuration) for more details.\n\n**This setting is used only by the native server.**", "scope": "window", - "type": "string" + "type": [ + "string", + "object" + ], + "oneOf": [ + { + "type": "string", + "markdownDescription": "Path to a `ruff.toml` or `pyproject.toml` file to use for configuration." + }, + { + "type": "object", + "markdownDescription": "Inline JSON configuration for Ruff settings (e.g., `{ \"line-length\": 100 }`). *Added in Ruff 0.9.8.*" + } + ] }, "ruff.args": { "default": [], diff --git a/src/common/server.ts b/src/common/server.ts index 9b503b5a..d4213a35 100644 --- a/src/common/server.ts +++ b/src/common/server.ts @@ -21,6 +21,7 @@ import { import { logger } from "./logger"; import { getDebuggerPath } from "./python"; import { + checkInlineConfigSupport, getExtensionSettings, getGlobalSettings, getUserSetLegacyServerSettings, @@ -189,6 +190,8 @@ async function createNativeServer( vscode.window.showErrorMessage(message); return Promise.reject(); } + + checkInlineConfigSupport(ruffVersion, serverId); } let ruffServerArgs: string[]; diff --git a/src/common/settings.ts b/src/common/settings.ts index 08c352d2..cfbf66d6 100644 --- a/src/common/settings.ts +++ b/src/common/settings.ts @@ -8,6 +8,12 @@ import { import { getInterpreterDetails } from "./python"; import { getConfiguration, getWorkspaceFolders } from "./vscodeapi"; import { logger } from "./logger"; +import { + INLINE_CONFIGURATION_VERSION, + VersionInfo, + supportsInlineConfiguration, + versionToString, +} from "./version"; type ImportStrategy = "fromEnvironment" | "useBundled"; @@ -50,7 +56,7 @@ export interface ISettings { path: string[]; ignoreStandardLibrary: boolean; interpreter: string[]; - configuration: string | null; + configuration: string | object | null; importStrategy: ImportStrategy; codeAction: CodeAction; enable: boolean; @@ -135,8 +141,8 @@ export async function getWorkspaceSettings( interpreter = resolveVariables(interpreter, workspace); } - let configuration = config.get("configuration") ?? null; - if (configuration !== null) { + let configuration = config.get("configuration") ?? null; + if (configuration !== null && typeof configuration === "string") { configuration = resolveVariables(configuration, workspace); } @@ -199,7 +205,7 @@ export async function getGlobalSettings(namespace: string): Promise { path: getGlobalValue(config, "path", []), ignoreStandardLibrary: getGlobalValue(config, "ignoreStandardLibrary", true), interpreter: [], - configuration: getGlobalValue(config, "configuration", null), + configuration: getGlobalValue(config, "configuration", null), importStrategy: getGlobalValue(config, "importStrategy", "fromEnvironment"), codeAction: getGlobalValue(config, "codeAction", {}), lint: { @@ -333,6 +339,25 @@ function getPreferredGlobalSetting( return newSettings?.defaultValue; } +export function checkInlineConfigSupport(ruffVersion: VersionInfo, serverId: string) { + if (supportsInlineConfiguration(ruffVersion)) { + return; + } + + getWorkspaceFolders().forEach((workspace) => { + const config = getConfiguration(serverId, workspace.uri).get("configuration"); + if (typeof config === "object") { + const message = `Inline configuration support was added in Ruff ${versionToString( + INLINE_CONFIGURATION_VERSION, + )} (current version is ${versionToString( + ruffVersion, + )}). Please update your Ruff version to use this feature.`; + logger.warn(message); + vscode.window.showWarningMessage(message); + } + }); +} + /** * Check if the user have configured `notebook.codeActionsOnSave` with non-notebook prefixed code actions. */ diff --git a/src/common/version.ts b/src/common/version.ts index 30070abc..218a2cf3 100644 --- a/src/common/version.ts +++ b/src/common/version.ts @@ -58,3 +58,15 @@ export const NATIVE_SERVER_STABLE_VERSION: VersionInfo = { major: 0, minor: 5, p export function supportsStableNativeServer(version: VersionInfo): boolean { return versionGte(version, NATIVE_SERVER_STABLE_VERSION); } + +/** + * The minimum version of the Ruff executable that supports inline configuration. + */ +export const INLINE_CONFIGURATION_VERSION: VersionInfo = { major: 0, minor: 9, patch: 8 }; + +/** + * Check if the given version of the Ruff executable supports inline configuration. + */ +export function supportsInlineConfiguration(version: VersionInfo): boolean { + return versionGte(version, INLINE_CONFIGURATION_VERSION); +}