From be5eb27f16e63ee6c5753e4fd669c14a5d3471f0 Mon Sep 17 00:00:00 2001 From: Stephen Jennings Date: Thu, 5 Sep 2024 16:26:28 -0700 Subject: [PATCH] fix: Add `enabled` config for fix tools Adds an optional `fix.tools.TOOL.enabled` config that disables use of a fix tool (if omitted, the tool is enabled). This is useful for defining tools in the user's configuration without enabling them for all repositories: ```toml # ~/.jjconfig.toml [fix.tools.rustfmt] enabled = false command = ["rustfmt", "--emit", "stdout"] patterns = ["glob:'**/*.rs'"] ``` Then to enable it in a repository: ```shell $ jj config set --repo fix.tools.rustfmt.enabled true ``` --- CHANGELOG.md | 4 ++ cli/src/commands/fix.rs | 22 ++++++++++- cli/src/config-schema.json | 5 +++ cli/tests/cli-reference@.md.snap | 3 ++ cli/tests/test_fix_command.rs | 67 ++++++++++++++++++++++++++++++++ docs/config.md | 21 ++++++++++ 6 files changed, 120 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57747bebf..d8b193c27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). * New `git.sign-on-push` config option to automatically sign commits which are being pushed to a Git remote. +* New `fix.tools.TOOL.enabled` config option to enable/disable tools. This is + useful for defining disabled tools in user configuration that can be enabled + in individual repositories with one config setting. + ### Fixed bugs * Fixed diff selection by external tools with `jj split`/`commit -i FILESETS`. diff --git a/cli/src/commands/fix.rs b/cli/src/commands/fix.rs index 7de74a5e7..a4ccb808c 100644 --- a/cli/src/commands/fix.rs +++ b/cli/src/commands/fix.rs @@ -85,6 +85,9 @@ use crate::ui::Ui; /// empty, no files will be affected by the tool. If there are multiple /// patterns, the tool is applied only once to each file in the union of the /// patterns. +/// - `enabled`: Enables or disables the tool. If omitted, the tool is enabled. +/// This is useful for defining disabled tools in user configuration that can +/// be enabled in individual repositories with one config setting. /// /// For example, the following configuration defines how two code formatters /// (`clang-format` and `black`) will apply to three different file extensions @@ -407,6 +410,8 @@ struct ToolConfig { command: CommandNameAndArgs, /// The matcher that determines if this tool matches a file. matcher: Box, + /// Whether the tool is enabled + enabled: bool, // TODO: Store the `name` field here and print it with the command's stderr, to clearly // associate any errors/warnings with the tool and its configuration entry. } @@ -424,6 +429,12 @@ struct ToolsConfig { struct RawToolConfig { command: CommandNameAndArgs, patterns: Vec, + #[serde(default = "default_tool_enabled")] + enabled: bool, +} + +fn default_tool_enabled() -> bool { + true } /// Parses the `fix.tools` config table. @@ -432,7 +443,7 @@ struct RawToolConfig { /// not check for issues that might still occur later like missing executables. /// This is a place where we could fail earlier in some cases, though. fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result { - let tools: Vec = settings + let mut tools: Vec = settings .table_keys("fix.tools") // Sort keys early so errors are deterministic. .sorted() @@ -458,11 +469,18 @@ fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result