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
13 changes: 9 additions & 4 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getGlobalSettings,
getUserSetLegacyServerSettings,
ISettings,
LegacyServerSetting,
} from "./settings";
import {
supportsNativeServer,
Expand Down Expand Up @@ -288,14 +289,18 @@ const LSP_MIGRATION_URL = "https://docs.astral.sh/ruff/editors/migration/";
const LSP_DEPRECATION_DISCUSSION_MESSAGE =
"Feel free to comment on the [GitHub discussion](https://github.com/astral-sh/ruff/discussions/15991) to ask questions or share feedback.";

function formatLegacyServerSettings(settings: LegacyServerSetting[]): string {
return settings.map((s) => `'${s.key}' in ${s.location}`).join(", ");
}

async function resolveNativeServerSetting(
settings: ISettings,
workspace: vscode.WorkspaceFolder,
serverId: string,
): Promise<{ useNativeServer: boolean; executable: RuffExecutable | undefined }> {
let useNativeServer: boolean;
let executable: RuffExecutable | undefined;
let legacyServerSettings: string[];
let legacyServerSettings: LegacyServerSetting[];

switch (settings.nativeServer) {
case "on":
Expand All @@ -304,7 +309,7 @@ async function resolveNativeServerSetting(
if (legacyServerSettings.length > 0) {
// User has explicitly set the native server to 'on' but still has legacy server settings.
showWarningMessage(
`The following settings have been deprecated in the native server: ${JSON.stringify(
`The following settings have been deprecated in the native server: ${formatLegacyServerSettings(
legacyServerSettings,
)}. Please [migrate](${LSP_MIGRATION_URL}) to the new settings or remove them. ` +
LSP_DEPRECATION_DISCUSSION_MESSAGE,
Expand All @@ -329,7 +334,7 @@ async function resolveNativeServerSetting(
legacyServerSettings = getUserSetLegacyServerSettings(serverId, workspace);
if (legacyServerSettings.length > 0) {
// ... and update the message if they have legacy server settings.
message += `The following settings are not supported with the native server and have been deprecated: ${JSON.stringify(
message += `The following settings are not supported with the native server and have been deprecated: ${formatLegacyServerSettings(
legacyServerSettings,
)}. Please [migrate](${LSP_MIGRATION_URL}) to the new settings or remove them. `;
}
Expand Down Expand Up @@ -372,7 +377,7 @@ async function resolveNativeServerSetting(
if (!useNativeServer) {
let message = `The legacy server ([ruff-lsp](${RUFF_LSP_URL})) has been deprecated. `;
if (legacyServerSettings.length > 0) {
message += `The following settings were only supported by the legacy server and has been deprecated: ${JSON.stringify(
message += `The following settings were only supported by the legacy server and has been deprecated: ${formatLegacyServerSettings(
legacyServerSettings,
)}. Please [migrate](${LSP_MIGRATION_URL}) to the new settings or remove them. `;
}
Expand Down
49 changes: 39 additions & 10 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,31 @@ function getPreferredGlobalSetting<T>(
return newSettings?.defaultValue;
}

/**
* Represents the legacy server settings that were explicitly set by the user.
*/
export type LegacyServerSetting = {
key: string;
location: SettingLocation;
};

/**
* Represents the location where a setting was explicitly set by the user.
*/
export enum SettingLocation {
global = "user settings",
workspace = "workspace settings",
workspaceFolder = "workspace folder settings",
}

/**
* Get the settings that were explicitly set by the user that are only relevant
* to the legacy server.
*/
export function getUserSetLegacyServerSettings(
namespace: string,
workspace: WorkspaceFolder,
): string[] {
): LegacyServerSetting[] {
const settings = [
"showNotifications",
"ignoreStandardLibrary",
Expand All @@ -349,18 +366,30 @@ export function getUserSetLegacyServerSettings(
];
const config = getConfiguration(namespace, workspace);
return settings
.filter((s) => isSettingExplicitlySetByUser(config, s))
.map((s) => `${namespace}.${s}`);
.map((setting) => {
const location = settingLocationExplicitlySetByUser(config, setting);
return location !== null ? { key: `${namespace}.${setting}`, location } : null;
})
.filter((setting): setting is LegacyServerSetting => setting !== null);
}

/**
* Check if a setting was explicitly set by the user.
* Return the location where a setting was explicitly set by the user or `null`
* if it was not explicitly set.
*/
function isSettingExplicitlySetByUser(config: WorkspaceConfiguration, section: string): boolean {
function settingLocationExplicitlySetByUser(
config: WorkspaceConfiguration,
section: string,
): SettingLocation | null {
const inspect = config.inspect(section);
return (
inspect?.globalValue !== undefined ||
inspect?.workspaceValue !== undefined ||
inspect?.workspaceFolderValue !== undefined
);
if (inspect?.workspaceFolderValue !== undefined) {
return SettingLocation.workspaceFolder;
}
if (inspect?.workspaceValue !== undefined) {
return SettingLocation.workspace;
}
if (inspect?.globalValue !== undefined) {
return SettingLocation.global;
}
return null;
}