zed-editor: support tasks (#7493)

Just like other features, they can merge with preexisting JSON5
settings.
This commit is contained in:
Jairo Llopis
2025-07-21 01:14:57 +01:00
committed by GitHub
parent 7834432ca5
commit 13a83d1b65
4 changed files with 185 additions and 0 deletions

View File

@@ -93,6 +93,26 @@ in
'';
};
userTasks = mkOption {
type = jsonFormat.type;
default = [ ];
example = literalExpression ''
[
{
label = "Format Code";
command = "nix";
args = [ "fmt" "$ZED_WORKTREE_ROOT" ];
}
]
'';
description = ''
Configuration written to Zed's {file}`tasks.json`.
[List of tasks](https://zed.dev/docs/tasks) that can be run from the
command palette.
'';
};
extensions = mkOption {
type = types.listOf types.str;
default = [ ];
@@ -186,6 +206,14 @@ in
(jsonFormat.generate "zed-user-keymaps" cfg.userKeymaps)
);
})
(mkIf (cfg.userTasks != [ ]) {
zedTasksActivation = lib.hm.dag.entryAfter [ "linkGeneration" ] (
impureConfigMerger "[]"
"$dynamic + $static | group_by(.label) | map(reduce .[] as $item ({}; . * $item))"
"${config.xdg.configHome}/zed/tasks.json"
(jsonFormat.generate "zed-user-tasks" cfg.userTasks)
);
})
];
xdg.configFile = lib.mapAttrs' (

View File

@@ -5,5 +5,7 @@
zed-keymap-empty = ./keymap-empty.nix;
zed-settings = ./settings.nix;
zed-settings-empty = ./settings-empty.nix;
zed-tasks = ./tasks.nix;
zed-tasks-empty = ./tasks-empty.nix;
zed-themes = ./themes;
}

View File

@@ -0,0 +1,69 @@
{
config,
lib,
pkgs,
...
}:
{
programs.zed-editor = {
enable = true;
package = config.lib.test.mkStubPackage { };
userTasks = [
{
label = "Format Code";
command = "nix";
args = [
"fmt"
"$ZED_WORKTREE_ROOT"
];
allow_concurrent_runs = false;
}
];
};
home.homeDirectory = lib.mkForce "/@TMPDIR@/hm-user";
nmt.script =
let
preexistingTasks = builtins.toFile "preexisting.json" "";
expectedContent = builtins.toFile "expected.json" ''
[
{
"allow_concurrent_runs": false,
"args": [
"fmt",
"$ZED_WORKTREE_ROOT"
],
"command": "nix",
"label": "Format Code"
}
]
'';
taskPath = ".config/zed/tasks.json";
activationScript = pkgs.writeScript "activation" config.home.activation.zedTasksActivation.data;
in
''
export HOME=$TMPDIR/hm-user
# Simulate preexisting tasks
mkdir -p $HOME/.config/zed
cat ${preexistingTasks} > $HOME/${taskPath}
# Run the activation script
substitute ${activationScript} $TMPDIR/activate --subst-var TMPDIR
chmod +x $TMPDIR/activate
$TMPDIR/activate
# Validate the merged tasks
assertFileExists "$HOME/${taskPath}"
assertFileContent "$HOME/${taskPath}" "${expectedContent}"
# Test idempotency
$TMPDIR/activate
assertFileExists "$HOME/${taskPath}"
assertFileContent "$HOME/${taskPath}" "${expectedContent}"
'';
}

View File

@@ -0,0 +1,86 @@
{
config,
lib,
pkgs,
...
}:
{
programs.zed-editor = {
enable = true;
package = config.lib.test.mkStubPackage { };
userTasks = [
{
label = "Format Code";
command = "nix";
args = [
"fmt"
"$ZED_WORKTREE_ROOT"
];
allow_concurrent_runs = false;
}
];
};
home.homeDirectory = lib.mkForce "/@TMPDIR@/hm-user";
nmt.script =
let
preexistingTasks = builtins.toFile "preexisting.json" ''
[
// Label is the same
{
"label": "Format Code",
"allow_concurrent_runs": true,
},
// Different label, created by hand
{
"label": "Compile",
"command": "make",
},
]
'';
expectedContent = builtins.toFile "expected.json" ''
[
{
"label": "Compile",
"command": "make"
},
{
"label": "Format Code",
"allow_concurrent_runs": false,
"args": [
"fmt",
"$ZED_WORKTREE_ROOT"
],
"command": "nix"
}
]
'';
taskPath = ".config/zed/tasks.json";
activationScript = pkgs.writeScript "activation" config.home.activation.zedTasksActivation.data;
in
''
export HOME=$TMPDIR/hm-user
# Simulate preexisting tasks
mkdir -p $HOME/.config/zed
cat ${preexistingTasks} > $HOME/${taskPath}
# Run the activation script
substitute ${activationScript} $TMPDIR/activate --subst-var TMPDIR
chmod +x $TMPDIR/activate
$TMPDIR/activate
# Validate the merged tasks
assertFileExists "$HOME/${taskPath}"
assertFileContent "$HOME/${taskPath}" "${expectedContent}"
# Test idempotency
$TMPDIR/activate
assertFileExists "$HOME/${taskPath}"
assertFileContent "$HOME/${taskPath}" "${expectedContent}"
'';
}