Skip to content

Commit 9f495dd

Browse files
committed
plugins.harpoon: refactor & switch to harpoon2
1 parent af76696 commit 9f495dd

File tree

3 files changed

+168
-312
lines changed

3 files changed

+168
-312
lines changed

plugins/by-name/harpoon/default.nix

Lines changed: 23 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -1,264 +1,41 @@
11
{
22
lib,
3-
helpers,
43
config,
5-
pkgs,
64
...
75
}:
8-
with lib;
96
let
10-
cfg = config.plugins.harpoon;
11-
12-
projectConfigModule = types.submodule {
13-
options = {
14-
termCommands = helpers.mkNullOrOption (with types; listOf str) ''
15-
List of predefined terminal commands for this project.
16-
'';
17-
18-
marks = helpers.mkNullOrOption (with types; listOf str) ''
19-
List of predefined marks (filenames) for this project.
20-
'';
21-
};
22-
};
7+
inherit (lib) mkEnableOption;
238
in
24-
{
25-
options.plugins.harpoon = lib.nixvim.plugins.neovim.extraOptionsOptions // {
26-
enable = mkEnableOption "harpoon";
27-
28-
package = lib.mkPackageOption pkgs "harpoon" {
29-
default = [
30-
"vimPlugins"
31-
"harpoon"
32-
];
33-
};
34-
35-
enableTelescope = mkEnableOption "telescope integration";
36-
37-
keymapsSilent = mkOption {
38-
type = types.bool;
39-
description = "Whether harpoon keymaps should be silent.";
40-
default = false;
41-
};
42-
43-
keymaps = {
44-
addFile = helpers.mkNullOrOption types.str ''
45-
Keymap for marking the current file.";
46-
'';
47-
48-
toggleQuickMenu = helpers.mkNullOrOption types.str ''
49-
Keymap for toggling the quick menu.";
50-
'';
51-
52-
navFile = helpers.mkNullOrOption (with types; attrsOf str) ''
53-
Keymaps for navigating to marks.
54-
55-
Examples:
56-
navFile = {
57-
"1" = "<C-j>";
58-
"2" = "<C-k>";
59-
"3" = "<C-l>";
60-
"4" = "<C-m>";
61-
};
62-
'';
9+
lib.nixvim.plugins.mkNeovimPlugin {
10+
name = "harpoon";
11+
package = "harpoon2";
6312

64-
navNext = helpers.mkNullOrOption types.str ''
65-
Keymap for navigating to next mark.";
66-
'';
13+
maintainers = [ lib.maintainers.GaetanLepage ];
6714

68-
navPrev = helpers.mkNullOrOption types.str ''
69-
Keymap for navigating to previous mark.";
70-
'';
15+
setup = ":setup";
7116

72-
gotoTerminal = helpers.mkNullOrOption (with types; attrsOf str) ''
73-
Keymaps for navigating to terminals.
17+
# TODO: introduced 2025-04-03: remove after 25.11
18+
imports = [
19+
./deprecations.nix
20+
];
7421

75-
Examples:
76-
gotoTerminal = {
77-
"1" = "<C-j>";
78-
"2" = "<C-k>";
79-
"3" = "<C-l>";
80-
"4" = "<C-m>";
81-
};
82-
'';
83-
84-
cmdToggleQuickMenu = helpers.mkNullOrOption types.str ''
85-
Keymap for toggling the cmd quick menu.
86-
'';
87-
88-
tmuxGotoTerminal = helpers.mkNullOrOption (with types; attrsOf str) ''
89-
Keymaps for navigating to tmux windows/panes.
90-
Attributes can either be tmux window ids or pane identifiers.
22+
extraOptions = {
23+
enableTelescope = mkEnableOption "telescope integration";
24+
};
9125

92-
Examples:
93-
tmuxGotoTerminal = {
94-
"1" = "<C-1>";
95-
"2" = "<C-2>";
96-
"{down-of}" = "<leader>g";
97-
};
98-
'';
26+
settingsExample = {
27+
settings = {
28+
save_on_toggle = true;
29+
sync_on_ui_close = false;
9930
};
31+
};
10032

101-
saveOnToggle = helpers.defaultNullOpts.mkBool false ''
102-
Sets the marks upon calling `toggle` on the ui, instead of require `:w`.
103-
'';
104-
105-
saveOnChange = helpers.defaultNullOpts.mkBool true ''
106-
Saves the harpoon file upon every change. disabling is unrecommended.
107-
'';
108-
109-
enterOnSendcmd = helpers.defaultNullOpts.mkBool false ''
110-
Sets harpoon to run the command immediately as it's passed to the terminal when calling `sendCommand`.
111-
'';
112-
113-
tmuxAutocloseWindows = helpers.defaultNullOpts.mkBool false ''
114-
Closes any tmux windows harpoon that harpoon creates when you close Neovim.
115-
'';
116-
117-
excludedFiletypes = helpers.defaultNullOpts.mkListOf types.str [ "harpoon" ] ''
118-
Filetypes that you want to prevent from adding to the harpoon list menu.
119-
'';
120-
121-
markBranch = helpers.defaultNullOpts.mkBool false ''
122-
Set marks specific to each git branch inside git repository.
123-
'';
124-
125-
projects = mkOption {
126-
default = { };
127-
description = ''
128-
Predefined projetcs. The keys of this attrs should be the path to the project.
129-
$HOME is working.
130-
'';
131-
example = ''
132-
projects = {
133-
"$HOME/personal/vim-with-me/server" = {
134-
termCommands = [
135-
"./env && npx ts-node src/index.ts"
136-
];
137-
};
138-
};
139-
'';
140-
type = types.attrsOf projectConfigModule;
33+
extraConfig = cfg: {
34+
assertions = lib.nixvim.mkAssertions "plugins.harpoon" {
35+
assertion = cfg.enableTelescope -> config.plugins.telescope.enable;
36+
message = "The harpoon telescope integration needs telescope to function as intended.";
14137
};
14238

143-
menu = {
144-
width = helpers.defaultNullOpts.mkInt 60 ''
145-
Menu window width
146-
'';
147-
148-
height = helpers.defaultNullOpts.mkInt 10 ''
149-
Menu window height
150-
'';
151-
152-
borderChars = helpers.defaultNullOpts.mkListOf types.str [
153-
"─"
154-
"│"
155-
"─"
156-
"│"
157-
"╭"
158-
"╮"
159-
"╯"
160-
"╰"
161-
] "Border characters";
162-
};
39+
plugins.telescope.enabledExtensions = lib.mkIf cfg.enableTelescope [ "harpoon" ];
16340
};
164-
165-
config =
166-
let
167-
projects = builtins.mapAttrs (name: value: {
168-
term.cmds = value.termCommands;
169-
mark.marks = helpers.ifNonNull' value.marks (map (mark: { filename = mark; }) value.marks);
170-
}) cfg.projects;
171-
172-
setupOptions =
173-
with cfg;
174-
{
175-
global_settings = {
176-
save_on_toggle = saveOnToggle;
177-
save_on_change = saveOnChange;
178-
enter_on_sendcmd = enterOnSendcmd;
179-
tmux_autoclose_windows = tmuxAutocloseWindows;
180-
excluded_filetypes = excludedFiletypes;
181-
mark_branch = markBranch;
182-
};
183-
184-
inherit projects;
185-
186-
menu = {
187-
inherit (menu) width height;
188-
borderchars = menu.borderChars;
189-
};
190-
}
191-
// cfg.extraOptions;
192-
in
193-
mkIf cfg.enable {
194-
assertions = lib.nixvim.mkAssertions "plugins.harpoon" [
195-
{
196-
assertion = cfg.enableTelescope -> config.plugins.telescope.enable;
197-
message = "The harpoon telescope integration needs telescope to function as intended.";
198-
}
199-
];
200-
201-
extraPlugins = [ cfg.package ];
202-
203-
extraConfigLua =
204-
let
205-
telescopeCfg = ''require("telescope").load_extension("harpoon")'';
206-
in
207-
''
208-
require('harpoon').setup(${lib.nixvim.toLuaObject setupOptions})
209-
${if cfg.enableTelescope then telescopeCfg else ""}
210-
'';
211-
212-
keymaps =
213-
let
214-
km = cfg.keymaps;
215-
216-
simpleMappings = flatten (
217-
mapAttrsToList
218-
(
219-
optionName: luaFunc:
220-
let
221-
key = km.${optionName};
222-
in
223-
optional (key != null) {
224-
inherit key;
225-
action.__raw = luaFunc;
226-
}
227-
)
228-
{
229-
addFile = "require('harpoon.mark').add_file";
230-
toggleQuickMenu = "require('harpoon.ui').toggle_quick_menu";
231-
navNext = "require('harpoon.ui').nav_next";
232-
navPrev = "require('harpoon.ui').nav_prev";
233-
cmdToggleQuickMenu = "require('harpoon.cmd-ui').toggle_quick_menu";
234-
}
235-
);
236-
237-
mkNavMappings =
238-
name: genLuaFunc:
239-
let
240-
mappingsAttrs = km.${name};
241-
in
242-
flatten (
243-
optionals (mappingsAttrs != null) (
244-
mapAttrsToList (id: key: {
245-
inherit key;
246-
action.__raw = genLuaFunc id;
247-
}) mappingsAttrs
248-
)
249-
);
250-
251-
allMappings =
252-
simpleMappings
253-
++ (mkNavMappings "navFile" (id: "function() require('harpoon.ui').nav_file(${id}) end"))
254-
++ (mkNavMappings "gotoTerminal" (id: "function() require('harpoon.term').gotoTerminal(${id}) end"))
255-
++ (mkNavMappings "tmuxGotoTerminal" (
256-
id: "function() require('harpoon.tmux').gotoTerminal(${id}) end"
257-
));
258-
in
259-
helpers.keymaps.mkKeymaps {
260-
mode = "n";
261-
options.silent = cfg.keymapsSilent;
262-
} allMappings;
263-
};
26441
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{ lib, ... }:
2+
{
3+
imports =
4+
let
5+
basePluginPath = [
6+
"plugins"
7+
"harpoon"
8+
];
9+
10+
commonWarning = ''
11+
/!\ `plugins.harpoon` has been refactored to now use harpoon2 (https://github.com/ThePrimeagen/harpoon/tree/harpoon2).
12+
'';
13+
14+
keymapsWarning = ''
15+
${commonWarning}
16+
17+
The `plugins.harpoon` module no longer allows you to define your keymaps.
18+
Please, manually define your keymaps using the top-level `keymaps` option.
19+
20+
For example,
21+
```
22+
plugins.harpoon.keymaps = {
23+
addFile = "<leader>a";
24+
toggleQuickMenu = "<C-e>";
25+
navFile = {
26+
"1" = "<C-j>";
27+
"2" = "<C-k>";
28+
"3" = "<C-l>";
29+
"4" = "<C-m>";
30+
};
31+
};
32+
```
33+
34+
would become:
35+
```
36+
keymaps = [
37+
{ mode = "n"; key = "<leader>a"; action.__raw = "function() require'harpoon':list():add() end"; }
38+
{ mode = "n"; key = "<C-e>"; action.__raw = "function() require'harpoon'.ui:toggle_quick_menu(require'harpoon':list()) end"; }
39+
{ mode = "n"; key = "<C-j>"; action.__raw = "function() require'harpoon':list():select(1) end"; }
40+
{ mode = "n"; key = "<C-k>"; action.__raw = "function() require'harpoon':list():select(2) end"; }
41+
{ mode = "n"; key = "<C-l>"; action.__raw = "function() require'harpoon':list():select(3) end"; }
42+
{ mode = "n"; key = "<C-m>"; action.__raw = "function() require'harpoon':list():select(4) end"; }
43+
];
44+
```
45+
'';
46+
47+
optionNames = [
48+
"saveOnToggle"
49+
"saveOnChange"
50+
"enterOnSendcmd"
51+
"tmuxAutocloseWindows"
52+
"excludedFiletypes"
53+
"markBranch"
54+
"projects"
55+
"menu"
56+
];
57+
in
58+
(map (
59+
optionName:
60+
lib.mkRemovedOptionModule (basePluginPath ++ [ optionName ]) ''
61+
${commonWarning}
62+
63+
You may now use `plugins.harpoon.settings` option to forward any value to the `require("harpoon"):setup()` call.
64+
''
65+
) optionNames)
66+
++ [
67+
(lib.mkRemovedOptionModule (basePluginPath ++ [ "keymaps" ]) keymapsWarning)
68+
(lib.mkRemovedOptionModule (basePluginPath ++ [ "keymapsSilent" ]) keymapsWarning)
69+
];
70+
71+
}

0 commit comments

Comments
 (0)