forked from nix-community/nixvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeymap-helpers.nix
149 lines (131 loc) · 3.92 KB
/
keymap-helpers.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
lib,
nixvimOptions,
nixvimTypes,
}:
with lib; rec {
# These are the configuration options that change the behavior of each mapping.
mapConfigOptions = {
silent =
nixvimOptions.defaultNullOpts.mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
nixvimOptions.defaultNullOpts.mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
nixvimOptions.defaultNullOpts.mkBool false
"Equivalent to adding <script> to a map.";
expr =
nixvimOptions.defaultNullOpts.mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
nixvimOptions.defaultNullOpts.mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap =
nixvimOptions.defaultNullOpts.mkBool true
"Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
remap =
nixvimOptions.defaultNullOpts.mkBool false
"Make the mapping recursive. Inverses \"noremap\"";
desc =
nixvimOptions.mkNullOrOption types.str
"A textual description of this keybind, to be shown in which-key, if you have it.";
};
modes = {
normal.short = "n";
insert.short = "i";
visual = {
desc = "visual and select";
short = "v";
};
visualOnly = {
desc = "visual only";
short = "x";
};
select.short = "s";
terminal.short = "t";
normalVisualOp = {
desc = "normal, visual, select and operator-pending (same as plain 'map')";
short = "";
};
operator.short = "o";
lang = {
desc = "normal, visual, select and operator-pending (same as plain 'map')";
short = "l";
};
insertCommand = {
desc = "insert and command-line";
short = "!";
};
command.short = "c";
};
modeEnum =
types.enum
# ["" "n" "v" ...]
(
map
(
{short, ...}: short
)
(attrValues modes)
);
mapOptionSubmodule = mkMapOptionSubmodule {};
mkModeOption = default:
mkOption {
type = with types;
either
modeEnum
(listOf modeEnum);
description = ''
One or several modes.
Use the short-names (`"n"`, `"v"`, ...).
See `:h map-modes` to learn more.
'';
inherit default;
example = ["n" "v"];
};
mkMapOptionSubmodule = defaults: (with types;
submodule {
options = {
key = mkOption ({
type = str;
description = "The key to map.";
example = "<C-m>";
}
// (
optionalAttrs
(defaults ? key)
{default = defaults.key;}
));
mode = mkModeOption defaults.mode or "";
action = mkOption ({
type = nixvimTypes.maybeRaw str;
description = "The action to execute.";
}
// (
optionalAttrs
(defaults ? action)
{default = defaults.action;}
));
lua = mkOption {
type = bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
'';
default = defaults.lua or false;
};
options = mapConfigOptions;
};
});
# Correctly merge two attrs (partially) representing a mapping.
mergeKeymap = defaults: keymap: let
# First, merge the `options` attrs of both options.
mergedOpts = (defaults.options or {}) // (keymap.options or {});
in
# Then, merge the root attrs together and add the previously merged `options` attrs.
(defaults // keymap) // {options = mergedOpts;};
mkKeymaps = defaults:
map
(mergeKeymap defaults);
}