diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index d65595bc61e89d..b6de009493f4d6 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -395,11 +395,11 @@ pub struct FormatCommand { #[arg(long, help_heading = "Miscellaneous")] pub stdin_filename: Option, - /// Enable preview mode; checks will include unstable rules and fixes. + /// Enable preview mode; enables unstable formatting. /// Use `--no-preview` to disable. - #[arg(long, overrides_with("no_preview"), hide = true)] + #[arg(long, overrides_with("no_preview"))] preview: bool, - #[clap(long, overrides_with("preview"), hide = true)] + #[clap(long, overrides_with("preview"))] no_preview: bool, } @@ -668,6 +668,8 @@ impl ConfigurationTransformer for CliOverrides { } if let Some(preview) = &self.preview { config.preview = Some(*preview); + config.lint.preview = Some(*preview); + config.format.preview = Some(*preview); } if let Some(per_file_ignores) = &self.per_file_ignores { config.lint.per_file_ignores = Some(collect_per_file_ignores(per_file_ignores.clone())); diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 0dd882c7b78dec..3f2e9a9e692c5f 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -151,14 +151,14 @@ impl Configuration { } let target_version = self.target_version.unwrap_or_default(); - let preview = self.preview.unwrap_or_default(); + let global_preview = self.preview.unwrap_or_default(); let format = self.format; let format_defaults = FormatterSettings::default(); // TODO(micha): Support changing the tab-width but disallow changing the number of spaces let formatter = FormatterSettings { exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?, - preview: match format.preview.unwrap_or(preview) { + preview: match format.preview.unwrap_or(global_preview) { PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled, PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled, }, @@ -176,6 +176,7 @@ impl Configuration { }; let lint = self.lint; + let lint_preview = lint.preview.unwrap_or(global_preview); Ok(Settings { cache_dir: self @@ -204,8 +205,9 @@ impl Configuration { }, linter: LinterSettings { - rules: lint.as_rule_table(preview), + rules: lint.as_rule_table(lint_preview), exclude: FilePatternSet::try_from_iter(lint.exclude.unwrap_or_default())?, + preview: lint_preview, target_version, project_root: project_root.to_path_buf(), allowed_confusables: lint @@ -234,7 +236,7 @@ impl Configuration { .iter() .flat_map(|selector| { selector.rules(&PreviewOptions { - mode: preview, + mode: lint_preview, require_explicit: false, }) }) @@ -244,7 +246,7 @@ impl Configuration { .iter() .flat_map(|selector| { selector.rules(&PreviewOptions { - mode: preview, + mode: lint_preview, require_explicit: false, }) }) @@ -257,7 +259,6 @@ impl Configuration { .task_tags .unwrap_or_else(|| TASK_TAGS.iter().map(ToString::to_string).collect()), logger_objects: lint.logger_objects.unwrap_or_default(), - preview, typing_modules: lint.typing_modules.unwrap_or_default(), // Plugins flake8_annotations: lint @@ -511,6 +512,7 @@ impl Configuration { #[derive(Debug, Default)] pub struct LintConfiguration { pub exclude: Option>, + pub preview: Option, // Rule selection pub extend_per_file_ignores: Vec, @@ -570,6 +572,7 @@ impl LintConfiguration { }) .collect() }), + preview: options.preview.map(PreviewMode::from), rule_selections: vec![RuleSelection { select: options.common.select, @@ -887,6 +890,7 @@ impl LintConfiguration { pub fn combine(self, config: Self) -> Self { Self { exclude: self.exclude.or(config.exclude), + preview: self.preview.or(config.preview), rule_selections: config .rule_selections .into_iter() diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 1f91a22f78b7ea..3bc3165811a95a 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -141,7 +141,7 @@ pub struct Options { pub required_version: Option, /// Whether to enable preview mode. When preview mode is enabled, Ruff will - /// use unstable rules and fixes. + /// use unstable rules, fixes, and formatting. #[option( default = "false", value_type = "bool", @@ -412,6 +412,18 @@ pub struct LintOptions { "# )] pub exclude: Option>, + + /// Whether to enable preview mode. When preview mode is enabled, Ruff will + /// use unstable rules and fixes. + #[option( + default = "false", + value_type = "bool", + example = r#" + # Enable preview features. + preview = true + "# + )] + pub preview: Option, } // Note: This struct should be inlined into [`LintOptions`] once support for the top-level lint settings diff --git a/ruff.schema.json b/ruff.schema.json index f678b56d880c2b..7d676b8f99ea24 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -491,7 +491,7 @@ } }, "preview": { - "description": "Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules and fixes.", + "description": "Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules, fixes, and formatting.", "type": [ "boolean", "null" @@ -1941,6 +1941,13 @@ } } }, + "preview": { + "description": "Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules and fixes.", + "type": [ + "boolean", + "null" + ] + }, "pycodestyle": { "description": "Options for the `pycodestyle` plugin.", "anyOf": [