Skip to content

Commit

Permalink
Introduce common Ruff configuration options to extension settings (#456)
Browse files Browse the repository at this point in the history
## Summary

This is a follow-up to astral-sh/ruff#10984,
which implemented support for common Ruff configuration options in
client settings. This PR exposes these new settings in the extension
configuration.

These settings are different from other extension settings, because they
don't have default values. Instead, if the user does not set them, they
will be sent as `null`, and the server will use project configuration
instead.

At the moment, `ruff server` does not actually resolve these settings
yet - settings resolution is introduced in
astral-sh/ruff#11062, which means that you'll
need to work from that branch to test this PR (and vice-versa).

## Test Plan

See the test plan in astral-sh/ruff#11062.
  • Loading branch information
snowsignal committed Apr 25, 2024
1 parent eb3787f commit e14932d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@
"scope": "resource",
"type": "array"
},
"ruff.lint.preview": {
"default": null,
"markdownDescription": "Enable [preview mode](https://docs.astral.sh/ruff/settings/#lint_preview) for the linter; enables unstable rules and fixes.",
"scope": "resource",
"type": "boolean"
},
"ruff.lint.select": {
"default": null,
"markdownDescription": "Set rule codes to enable. Use `ALL` to enable all rules. See [the documentation](https://docs.astral.sh/ruff/settings/#lint_select) for more details.",
"examples": [
["E4", "E7", "E9", "F"]
],
"items": {
"type": "string"
},
"scope": "resource",
"type": "array"
},
"ruff.lint.extendSelect": {
"default": null,
"markdownDescription": "Enable additional rule codes on top of existing configuration, instead of overriding it. Use `ALL` to enable all rules.",
"items": {
"type": "string"
},
"scope": "resource",
"type": "array"
},
"ruff.lint.ignore": {
"default": null,
"markdownDescription": "Set rule codes to disable. See [the documentation](https://docs.astral.sh/ruff/settings/#lint_ignore) for more details.",
"items": {
"type": "string"
},
"scope": "resource",
"type": "array"
},
"ruff.run": {
"default": "onType",
"markdownDescription": "Run Ruff on every keystroke (`onType`) or on save (`onSave`).",
Expand Down Expand Up @@ -134,6 +170,12 @@
"scope": "resource",
"type": "array"
},
"ruff.format.preview": {
"default": null,
"markdownDescription": "Enable [preview mode](https://docs.astral.sh/ruff/settings/#format_preview) for the formatter; enables unstable formatting.",
"scope": "resource",
"type": "boolean"
},
"ruff.path": {
"default": [],
"markdownDescription": "Path to a custom `ruff` executable, e.g., `[\"/path/to/ruff\"]`.",
Expand Down Expand Up @@ -250,6 +292,23 @@
"scope": "window",
"type": "string"
},
"ruff.exclude": {
"default": null,
"items": {
"type": "string"
},
"markdownDescription": "Set paths for the linter and formatter to ignore. See [the documentation](https://docs.astral.sh/ruff/settings/#lint_exclude) for more details.",
"type": "array",
"scope": "resource"
},
"ruff.lineLength": {
"default": null,
"minimum": 1,
"maximum": 320,
"markdownDescription": "Set the [line length](https://docs.astral.sh/ruff/settings/#line-length) used by the formatter and linter. Must be greater than 0 and less than or equal to 320.",
"scope": "resource",
"type": ["integer", "null"]
},
"ruff.enableExperimentalFormatter": {
"default": false,
"markdownDescription": "Controls whether Ruff registers as capable of code formatting.",
Expand Down
33 changes: 33 additions & 0 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ type Lint = {
enable?: boolean;
args?: string[];
run?: Run;
preview?: boolean;
select?: string[];
extendSelect?: string[];
ignore?: string[];
};

type Format = {
args?: string[];
preview?: boolean;
};

export interface ISettings {
Expand All @@ -47,6 +52,8 @@ export interface ISettings {
fixAll: boolean;
lint: Lint;
format: Format;
exclude?: string[];
lineLength?: number;
}

export function getExtensionSettings(namespace: string): Promise<ISettings[]> {
Expand Down Expand Up @@ -111,14 +118,21 @@ export async function getWorkspaceSettings(
getPreferredWorkspaceSetting<string[]>("lint.args", "args", config) ?? [],
workspace,
),
preview: config.get<boolean>("lint.preview"),
select: config.get<string[]>("lint.select"),
extendSelect: config.get<string[]>("lint.extendSelect"),
ignore: config.get<string[]>("lint.ignore"),
},
format: {
args: resolveVariables(config.get<string[]>("format.args") ?? [], workspace),
preview: config.get<boolean>("format.preview"),
},
enable: config.get<boolean>("enable") ?? true,
organizeImports: config.get<boolean>("organizeImports") ?? true,
fixAll: config.get<boolean>("fixAll") ?? true,
showNotifications: config.get<string>("showNotifications") ?? "off",
exclude: config.get<string[]>("exclude"),
lineLength: config.get<number>("lineLength"),
};
}

Expand All @@ -127,6 +141,11 @@ function getGlobalValue<T>(config: WorkspaceConfiguration, key: string, defaultV
return inspect?.globalValue ?? inspect?.defaultValue ?? defaultValue;
}

function getOptionalGlobalValue<T>(config: WorkspaceConfiguration, key: string): T | undefined {
const inspect = config.inspect<T>(key);
return inspect?.globalValue;
}

export async function getGlobalSettings(namespace: string): Promise<ISettings> {
const config = getConfiguration(namespace);
return {
Expand All @@ -142,14 +161,21 @@ export async function getGlobalSettings(namespace: string): Promise<ISettings> {
enable: getPreferredGlobalSetting<boolean>("lint.enable", "enable", config) ?? true,
run: getPreferredGlobalSetting<Run>("lint.run", "run", config) ?? "onType",
args: getPreferredGlobalSetting<string[]>("lint.args", "args", config) ?? [],
preview: getOptionalGlobalValue<boolean>(config, "lint.preview"),
select: getOptionalGlobalValue<string[]>(config, "lint.select"),
extendSelect: getOptionalGlobalValue<string[]>(config, "lint.extendSelect"),
ignore: getOptionalGlobalValue<string[]>(config, "lint.ignore"),
},
format: {
args: getGlobalValue<string[]>(config, "format.args", []),
preview: getOptionalGlobalValue<boolean>(config, "format.preview"),
},
enable: getGlobalValue<boolean>(config, "enable", true),
organizeImports: getGlobalValue<boolean>(config, "organizeImports", true),
fixAll: getGlobalValue<boolean>(config, "fixAll", true),
showNotifications: getGlobalValue<string>(config, "showNotifications", "off"),
exclude: getOptionalGlobalValue<string[]>(config, "exclude"),
lineLength: getOptionalGlobalValue<number>(config, "lineLength"),
};
}

Expand All @@ -167,9 +193,16 @@ export function checkIfConfigurationChanged(
`${namespace}.interpreter`,
`${namespace}.lint.enable`,
`${namespace}.lint.run`,
`${namespace}.lint.preview`,
`${namespace}.lint.select`,
`${namespace}.lint.extendSelect`,
`${namespace}.lint.ignore`,
`${namespace}.organizeImports`,
`${namespace}.path`,
`${namespace}.showNotifications`,
`${namespace}.format.preview`,
`${namespace}.exclude`,
`${namespace}.lineLength`,
// Deprecated settings (prefer `lint.args`, etc.).
`${namespace}.args`,
`${namespace}.run`,
Expand Down

0 comments on commit e14932d

Please sign in to comment.