Compare commits

..

39 commits

Author SHA1 Message Date
Ching Pei Yang
ff214b7aac
Merge 43e6e673af into 2fc7dc798b 2024-08-15 13:54:41 +00:00
Ching Pei Yang
43e6e673af add TODO 2024-08-15 15:53:45 +02:00
Ching Pei Yang
1b82b13dc7 lib: allow luaInline in lz.n map action 2024-08-15 15:53:45 +02:00
Ching Pei Yang
f5850432a8 flake: update lzn-auto-require 2024-08-15 15:53:45 +02:00
Ching Pei Yang
f672689023 fixup! wrapper: use lzn-auto-require loader 2024-08-15 15:53:45 +02:00
Ching Pei Yang
669088244d lib: add mkSetLuaBinding 2024-08-15 15:53:45 +02:00
Ching Pei Yang
769e07e7cc wrapper: use lzn-auto-require loader 2024-08-15 15:53:45 +02:00
Ching Pei Yang
0b9f69a20f flake: add plugin lzn-auto-require 2024-08-15 15:52:34 +02:00
Ching Pei Yang
9dd2a11e4d lz.n: generate less code 2024-08-15 15:52:34 +02:00
Ching Pei Yang
09e83a6cec lz.n: wrap lua code in function 2024-08-15 15:52:34 +02:00
Ching Pei Yang
c027a87527 lib: add mkLznBinding 2024-08-15 15:52:34 +02:00
Ching Pei Yang
abd76db843 nvim-tree: load nvim-tree if openOnSetup 2024-08-15 15:52:34 +02:00
Ching Pei Yang
85c0839105 nvim-tree: move to lz.n keymaps 2024-08-15 15:52:34 +02:00
Ching Pei Yang
6fc691a7c3 lib: add mkLznBinding 2024-08-15 15:52:34 +02:00
Ching Pei Yang
14bc100a5e lib: change lz.n spec "inlineLua" types to str 2024-08-15 15:50:16 +02:00
Ching Pei Yang
290de58ae9 lz.n: missing type check 2024-08-15 15:50:16 +02:00
Ching Pei Yang
1ee0a513cc lib: add lznKeySpec example 2024-08-15 15:50:16 +02:00
Ching Pei Yang
346f536737 lz.n: process key maps 2024-08-15 15:50:16 +02:00
Ching Pei Yang
ee5a157894 remove unused 2024-08-15 15:50:16 +02:00
Pei Yang Ching
805dd05b39 lib: fix lz.n map type 2024-08-15 15:50:16 +02:00
Ching Pei Yang
d3e39d10b0 lib: add lz.n KeySpec 2024-08-15 15:50:16 +02:00
Pei Yang Ching
14061c6db5 nvim-tree: use lazy 2024-08-15 15:50:16 +02:00
Pei Yang Ching
5c105b8182 lazy: add setupOpts support 2024-08-15 15:50:16 +02:00
Pei Yang Ching
b10c525ffe fix: broken optPlugins 2024-08-15 15:50:16 +02:00
Pei Yang Ching
9b4332ed77 switch to other hacky array-table syntax 2024-08-15 15:50:16 +02:00
Pei Yang Ching
f632fa1a92 wrap lazy init code in function 2024-08-15 15:50:16 +02:00
Ching Pei Yang
0faf4b923b flake: update lz.n 2024-08-15 15:50:16 +02:00
Ching Pei Yang
205582135d lib: add lznPluginTableType 2024-08-15 15:50:16 +02:00
Ching Pei Yang
1988203073 lib: export lznPluginType 2024-08-15 15:50:16 +02:00
Ching Pei Yang
d1d91ff8a5 lz.n: load lz.n 2024-08-15 15:50:16 +02:00
Ching Pei Yang
b552b33077 lz.n: add basic lazy.plugins option 2024-08-15 15:50:16 +02:00
Pei Yang Ching
920e0bad5e lib: add basic lz.n plugin spec type 2024-08-15 15:50:16 +02:00
Pei Yang Ching
f3ce59ed40 add lazy module skeleton 2024-08-15 15:50:16 +02:00
Pei Yang Ching
50efc3b557 flake: add lz.n plugin 2024-08-15 15:50:16 +02:00
Ching Pei Yang
2fc7dc798b
Re-add old maps API (#356)
* wrapper: fix wrong import source

* wrapper: fix typo

* Revert "lib/binds: improve code, adjust functions to new api"

This reverts commit 6cb57e1d26.

* mappings: re-add legacy vim.maps API

* mappings: fix wrong submodule syntax

* docs: fix missing section id
2024-08-13 00:27:05 +02:00
raf
3ca7e3b841
Merge branch 'main' into v0.7 2024-08-12 00:10:03 +00:00
Gerg-L
7d824d6711
modules: fix build (#353)
Some checks failed
Set up binary cache / cachix (default) (push) Waiting to run
Set up binary cache / cachix (maximal) (push) Waiting to run
Set up binary cache / cachix (nix) (push) Waiting to run
Validate flake & check formatting / Validate Flake (push) Waiting to run
Validate flake & check formatting / Formatting via Alejandra (push) Waiting to run
Validate flake & check documentation / Validate Flake Documentation (docs) (push) Has been cancelled
Validate flake & check documentation / Validate Flake Documentation (docs-html) (push) Has been cancelled
Validate flake & check documentation / Validate Flake Documentation (docs-json) (push) Has been cancelled
Validate flake & check documentation / Validate Flake Documentation (docs-manpages) (push) Has been cancelled
Build and deploy documentation / publish (push) Has been cancelled
* flake.lock: lock

* remove ussage of mapAttrsFlatten

* flake.lock: update mnw

* modules/default.nix makeNeovimConfig -> mnw

* modules/default.nix use buildVimPlugin (sad)

dont build when possible
2024-08-12 00:08:53 +00:00
diniamo
b71bf75dcd
vim.maps rewrite (#352)
* feat: rewrite vim.maps

* modules/mappings: enable silent by default

* docs: add entry for vim.maps rewrite

* lib/binds: improve code, adjust functions to new api
2024-08-12 00:07:45 +00:00
diniamo
de20ff2c51
style: improve modules/default.nix code, rename helper scripts (#351)
Co-authored-by: raf <raf@notashelf.dev>
2024-08-11 21:10:55 +00:00
6 changed files with 192 additions and 248 deletions

View file

@ -4,6 +4,8 @@ Release notes for release 0.7
## Breaking Changes and Migration Guide {#sec-breaking-changes-and-migration-guide-0-7} ## Breaking Changes and Migration Guide {#sec-breaking-changes-and-migration-guide-0-7}
### `vim.configRC` removed {#sec-vim-configrc-removed}
In v0.7 we are removing `vim.configRC` in favor of making `vim.luaConfigRC` the In v0.7 we are removing `vim.configRC` in favor of making `vim.luaConfigRC` the
top-level DAG, and thereby making the entire configuration Lua based. This top-level DAG, and thereby making the entire configuration Lua based. This
change introduces a few breaking changes: change introduces a few breaking changes:
@ -24,6 +26,28 @@ making good use of its extensive Lua API. Additionally, Vimscript is slow and
brings unnecessary performance overhead while working with different brings unnecessary performance overhead while working with different
configuration formats. configuration formats.
### `vim.maps` rewrite {#sec-vim-maps-rewrite}
Instead of specifying map modes using submodules (eg.: `vim.maps.normal`), a new
`mode` option has mode has been introduced. It can be either a string, or a list
of strings, where a string represents the short-name of the map mode(s), that
the mapping should be set for. See `:help map-modes` for more information.
For example:
```nix
vim.maps.normal."<leader>m" = { ... };
```
has to be replaced by
```nix
vim.maps."<leader>m" = {
mode = "n";
...
};
```
## Changelog {#sec-release-0.7-changelog} ## Changelog {#sec-release-0.7-changelog}
[ItsSorae](https://github.com/ItsSorae): [ItsSorae](https://github.com/ItsSorae):
@ -114,6 +138,8 @@ configuration formats.
has been introduced for setting up internal plugins. See the "DAG entries in has been introduced for setting up internal plugins. See the "DAG entries in
nvf" manual page for more information. nvf" manual page for more information.
- Rewrite `vim.maps`, see the breaking changes section above.
[NotAShelf](https://github.com/notashelf): [NotAShelf](https://github.com/notashelf):
[ts-error-translator.nvim]: https://github.com/dmmulroy/ts-error-translator.nvim [ts-error-translator.nvim]: https://github.com/dmmulroy/ts-error-translator.nvim
@ -143,10 +169,10 @@ configuration formats.
- Add [neo-tree.nvim] as an alternative file-tree plugin. It will be available - Add [neo-tree.nvim] as an alternative file-tree plugin. It will be available
under `vim.filetree.neo-tree`, similar to nvimtree. under `vim.filetree.neo-tree`, similar to nvimtree.
- Add `print-nvf-config` & `print-nvf-config-path` helper scripts to Neovim - Add `nvf-print-config` & `nvf-print-config-path` helper scripts to Neovim
closure. Both of those scripts have been automatically added to your PATH upon closure. Both of those scripts have been automatically added to your PATH upon
using neovimConfig or `programs.nvf.enable`. using neovimConfig or `programs.nvf.enable`.
- `print-nvf-config` will display your `init.lua`, in full. - `nvf-print-config` will display your `init.lua`, in full.
- `print-nvf-config-path` will display the path to _a clone_ of your - `nvf-print-config-path` will display the path to _a clone_ of your
`init.lua`. This is not the path used by the Neovim wrapper, but an `init.lua`. This is not the path used by the Neovim wrapper, but an
identical clone. identical clone.

View file

@ -69,11 +69,11 @@
}, },
"mnw": { "mnw": {
"locked": { "locked": {
"lastModified": 1722191188, "lastModified": 1723419050,
"narHash": "sha256-YF//iMALbrd2Ni9aju7w8NniH16Qz6RFTRD6md5UkDc=", "narHash": "sha256-Eb8jBUgHwpte+bGsqeXNbKMBfZaDB7RiPQwyb1vzJK8=",
"owner": "Gerg-L", "owner": "Gerg-L",
"repo": "mnw", "repo": "mnw",
"rev": "c7b289f3f5a31b6e744be37d83fc231816621231", "rev": "e625f5965567f16102bc52897c7600dcde53c6c3",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -971,6 +971,22 @@
"type": "github" "type": "github"
} }
}, },
"plugin-new-file-template-nvim": {
"flake": false,
"locked": {
"lastModified": 1721518222,
"narHash": "sha256-g0IjJrHRXw7U9goVLzVYUyHBSsDZGHMpi3YZPhg64zA=",
"owner": "otavioschwanck",
"repo": "new-file-template.nvim",
"rev": "6ac66669dbf2dc5cdee184a4fe76d22465ca67e8",
"type": "github"
},
"original": {
"owner": "otavioschwanck",
"repo": "new-file-template.nvim",
"type": "github"
}
},
"plugin-noice-nvim": { "plugin-noice-nvim": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -1896,6 +1912,7 @@
"plugin-neo-tree-nvim": "plugin-neo-tree-nvim", "plugin-neo-tree-nvim": "plugin-neo-tree-nvim",
"plugin-neocord": "plugin-neocord", "plugin-neocord": "plugin-neocord",
"plugin-neodev-nvim": "plugin-neodev-nvim", "plugin-neodev-nvim": "plugin-neodev-nvim",
"plugin-new-file-template-nvim": "plugin-new-file-template-nvim",
"plugin-noice-nvim": "plugin-noice-nvim", "plugin-noice-nvim": "plugin-noice-nvim",
"plugin-none-ls": "plugin-none-ls", "plugin-none-ls": "plugin-none-ls",
"plugin-nui-nvim": "plugin-nui-nvim", "plugin-nui-nvim": "plugin-nui-nvim",

View file

@ -7,91 +7,72 @@ inputs: {
extraModules ? [], extraModules ? [],
}: let }: let
inherit (pkgs) vimPlugins; inherit (pkgs) vimPlugins;
inherit (pkgs.vimUtils) buildVimPlugin;
inherit (lib.strings) isString toString; inherit (lib.strings) isString toString;
inherit (lib.lists) filter map concatLists; inherit (lib.lists) filter map concatLists;
inherit (lib.attrsets) recursiveUpdate getAttr;
inherit (lib.asserts) assertMsg;
# import modules.nix with `check`, `pkgs` and `lib` as arguments # import modules.nix with `check`, `pkgs` and `lib` as arguments
# check can be disabled while calling this file is called # check can be disabled while calling this file is called
# to avoid checking in all modules # to avoid checking in all modules
nvimModules = import ./modules.nix { nvimModules = import ./modules.nix {inherit pkgs check lib;};
inherit pkgs check lib;
};
# evaluate the extended library with the modules # evaluate the extended library with the modules
# optionally with any additional modules passed by the user # optionally with any additional modules passed by the user
module = lib.evalModules { module = lib.evalModules {
specialArgs = recursiveUpdate {modulesPath = toString ./.;} extraSpecialArgs; specialArgs = extraSpecialArgs // {modulesPath = toString ./.;};
modules = concatLists [[configuration] nvimModules extraModules]; modules = concatLists [[configuration] nvimModules extraModules];
}; };
# alias to the internal configuration # alias to the internal configuration
vimOptions = module.config.vim; vimOptions = module.config.vim;
# build a vim plugin with the given name and arguments
# if the plugin is nvim-treesitter, warn the user to use buildTreesitterPlug
# instead
buildPlug = {pname, ...} @ attrs: let
src = getAttr ("plugin-" + pname) inputs;
in
pkgs.stdenvNoCC.mkDerivation ({
inherit src;
version = src.shortRev or src.shortDirtyRev or "dirty";
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r . $out
runHook postInstall
'';
}
// attrs);
noBuildPlug = {pname, ...} @ attrs: let noBuildPlug = {pname, ...} @ attrs: let
input = getAttr ("plugin-" + pname) inputs; src = inputs."plugin-${attrs.pname}";
in in
{ {
version = input.shortRev or input.shortDirtyRev or "dirty"; version = src.shortRev or src.shortDirtyRev or "dirty";
outPath = getAttr ("plugin-" + pname) inputs; outPath = src;
passthru.vimPlugin = false;
} }
// attrs; // attrs;
# build a vim plugin with the given name and arguments
# if the plugin is nvim-treesitter, warn the user to use buildTreesitterPlug
# instead
buildPlug = attrs: let
src = inputs."plugin-${attrs.pname}";
in
pkgs.vimUtils.buildVimPlugin (
{
version = src.shortRev or src.shortDirtyRev or "dirty";
inherit src;
}
// attrs
);
buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars); buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars);
pluginBuilders = {
nvim-treesitter = buildTreesitterPlug vimOptions.treesitter.grammars;
flutter-tools-patched = buildPlug {
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
};
};
buildConfigPlugins = plugins: buildConfigPlugins = plugins:
map map (
(plug: ( plug:
if (isString plug) if (isString plug)
then then pluginBuilders.${plug} or (noBuildPlug {pname = plug;})
( else plug
if (plug == "nvim-treesitter") ) (filter (f: f != null) plugins);
then (buildTreesitterPlug vimOptions.treesitter.grammars)
else if (plug == "flutter-tools-patched")
then
(
buildPlug
{
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
}
)
else noBuildPlug {pname = plug;}
)
else plug
))
(filter
(f: f != null)
plugins);
# built (or "normalized") plugins that are modified # built (or "normalized") plugins that are modified
builtStartPlugins = buildConfigPlugins vimOptions.startPlugins; builtStartPlugins = buildConfigPlugins vimOptions.startPlugins;
builtOptPlugins = map (package: package // {optional = true;}) (buildConfigPlugins vimOptions.optPlugins); builtOptPlugins = map (package: package // {optional = true;}) (buildConfigPlugins vimOptions.optPlugins);
# additional Lua and Python3 packages, mapped to their respective functions # additional Lua and Python3 packages, mapped to their respective functions
# to conform to the format makeNeovimConfig expects. end user should # to conform to the format mnw expects. end user should
# only ever need to pass a list of packages, which are modified # only ever need to pass a list of packages, which are modified
# here # here
extraLuaPackages = ps: map (x: ps.${x}) vimOptions.luaPackages; extraLuaPackages = ps: map (x: ps.${x}) vimOptions.luaPackages;
@ -101,7 +82,7 @@ inputs: {
# generate a wrapped Neovim package. # generate a wrapped Neovim package.
neovim-wrapped = inputs.mnw.lib.wrap pkgs { neovim-wrapped = inputs.mnw.lib.wrap pkgs {
neovim = vimOptions.package; neovim = vimOptions.package;
plugins = concatLists [builtStartPlugins builtOptPlugins]; plugins = builtStartPlugins ++ builtOptPlugins;
appName = "nvf"; appName = "nvf";
extraBinPath = vimOptions.extraPackages; extraBinPath = vimOptions.extraPackages;
initLua = vimOptions.builtLuaConfigRC; initLua = vimOptions.builtLuaConfigRC;
@ -111,20 +92,11 @@ inputs: {
inherit extraLuaPackages extraPython3Packages; inherit extraLuaPackages extraPython3Packages;
}; };
dummyInit = pkgs.writeText "nvf-init.lua" vimOptions.builtLuaConfigRC;
# Additional helper scripts for printing and displaying nvf configuration # Additional helper scripts for printing and displaying nvf configuration
# in your commandline. # in your commandline.
printConfig = pkgs.writers.writeDashBin "print-nvf-config" '' printConfig = pkgs.writers.writeDashBin "nvf-print-config" "cat ${dummyInit}";
cat << EOF printConfigPath = pkgs.writers.writeDashBin "nvf-print-config-path" "echo -n ${dummyInit}";
${vimOptions.builtLuaConfigRC}
EOF
'';
printConfigPath = pkgs.writers.writeDashBin "print-nvf-config-path" ''
realpath ${pkgs.writeTextFile {
name = "nvf-init.lua";
text = vimOptions.builtLuaConfigRC;
}}
'';
in { in {
inherit (module) options config; inherit (module) options config;
inherit (module._module.args) pkgs; inherit (module._module.args) pkgs;
@ -134,7 +106,7 @@ in {
neovim = pkgs.symlinkJoin { neovim = pkgs.symlinkJoin {
name = "nvf-with-helpers"; name = "nvf-with-helpers";
paths = [neovim-wrapped printConfig printConfigPath]; paths = [neovim-wrapped printConfig printConfigPath];
postBuild = "echo helpers added"; postBuild = "echo Helpers added";
meta = { meta = {
description = "Wrapped version of Neovim with additional helper scripts"; description = "Wrapped version of Neovim with additional helper scripts";

View file

@ -8,45 +8,26 @@
cfg = config.vim; cfg = config.vim;
in { in {
config = { config = {
vim.maps = { vim.maps = mkIf cfg.disableArrows {
normal = mkIf cfg.disableArrows { "<up>" = {
"<up>" = { mode = ["n" "i"];
action = "<nop>"; action = "<nop>";
noremap = false;
noremap = false;
};
"<down>" = {
action = "<nop>";
noremap = false;
};
"<left>" = {
action = "<nop>";
noremap = false;
};
"<right>" = {
action = "<nop>";
noremap = false;
};
}; };
"<down>" = {
insert = mkIf cfg.disableArrows { mode = ["n" "i"];
"<up>" = { action = "<nop>";
action = "<nop>"; noremap = false;
noremap = false; };
}; "<left>" = {
"<down>" = { mode = ["n" "i"];
action = "<nop>"; action = "<nop>";
noremap = false; noremap = false;
}; };
"<left>" = { "<right>" = {
action = "<nop>"; mode = ["n" "i"];
noremap = false; action = "<nop>";
}; noremap = false;
"<right>" = {
action = "<nop>";
noremap = false;
};
}; };
}; };
}; };

View file

@ -1,41 +1,48 @@
{lib, ...}: let {lib, ...}: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) bool str attrsOf nullOr submodule; inherit (lib.types) either str listOf attrsOf nullOr submodule bool;
inherit (lib.nvim.config) mkBool; inherit (lib.nvim.config) mkBool;
# Most of the keybindings code is highly inspired by pta2002/nixvim.
# Thank you!
mapConfigOptions = { mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap =
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.";
desc = mkOption { desc = mkOption {
type = nullOr str; type = nullOr str;
default = null; default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled."; description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
}; };
action = mkOption {
type = str;
description = "The command to execute.";
};
lua = mkBool false ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
'';
silent = mkBool true "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait = mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script = mkBool false "Equivalent to adding <script> to a map.";
expr = mkBool false "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique = mkBool false "Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap = 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.";
}; };
mapType = submodule {
options =
mapConfigOptions
// {
mode = mkOption {
type = either str (listOf str);
description = ''
The short-name of the mode to set the keymapping for. Passing an empty string is the equivalent of `:map`.
See `:help map-modes` for a list of modes.
'';
};
};
};
# legacy stuff
mapOption = submodule { mapOption = submodule {
options = options =
mapConfigOptions mapConfigOptions
@ -66,6 +73,7 @@ in {
options.vim = { options.vim = {
maps = mkOption { maps = mkOption {
type = submodule { type = submodule {
freeformType = attrsOf mapType;
options = { options = {
normal = mapOptions "normal"; normal = mapOptions "normal";
insert = mapOptions "insert"; insert = mapOptions "insert";
@ -82,15 +90,11 @@ in {
}; };
}; };
default = {}; default = {};
description = '' description = "Custom keybindings.";
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use `maps.normalVisualOp`.
'';
example = '' example = ''
maps = { maps = {
normal."<leader>m" = { "<leader>m" = {
mode = "n";
silent = true; silent = true;
action = "<cmd>make<CR>"; action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR> }; # Same as nnoremap <leader>m <silent> <cmd>make<CR>

View file

@ -3,87 +3,20 @@
lib, lib,
... ...
}: let }: let
inherit (builtins) map mapAttrs filter; inherit (builtins) map mapAttrs filter removeAttrs attrNames;
inherit (lib.options) mkOption; inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList;
inherit (lib.attrsets) filterAttrs getAttrs attrValues attrNames;
inherit (lib.strings) concatLines concatMapStringsSep optionalString; inherit (lib.strings) concatLines concatMapStringsSep optionalString;
inherit (lib.misc) mapAttrsFlatten;
inherit (lib.trivial) showWarnings; inherit (lib.trivial) showWarnings;
inherit (lib.types) str nullOr;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere; inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.config) mkBool;
cfg = config.vim; cfg = config.vim;
# Most of the keybindings code is highly inspired by pta2002/nixvim.
# Thank you!
mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap =
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.";
desc = mkOption {
type = nullOr str;
default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
};
};
genMaps = mode: maps: let
/*
Take a user-defined action (string or attrs) and return the following attribute set:
{
action = (string) the actual action to map to this key
config = (attrs) the configuration options for this mapping (noremap, silent...)
}
*/
normalizeAction = action: {
# Extract the values of the config options that have been explicitly set by the user
config =
filterAttrs (_: v: v != null)
(getAttrs (attrNames mapConfigOptions) action);
action =
if action.lua
then mkLuaInline action.action
else action.action;
};
in
attrValues (mapAttrs
(key: action: let
normalizedAction = normalizeAction action;
in {
inherit (normalizedAction) action config;
inherit key;
inherit mode;
})
maps);
in { in {
config = let config = let
filterNonNull = attrs: filterAttrs (_: value: value != null) attrs; filterNonNull = filterAttrs (_: value: value != null);
globalsScript = globalsScript =
mapAttrsFlatten (name: value: "vim.g.${name} = ${toLuaObject value}") mapAttrsToList (name: value: "vim.g.${name} = ${toLuaObject value}")
(filterNonNull cfg.globals); (filterNonNull cfg.globals);
extraPluginConfigs = resolveDag { extraPluginConfigs = resolveDag {
@ -98,38 +31,49 @@ in {
mapResult = result: concatLines (map mkLuarcSection result); mapResult = result: concatLines (map mkLuarcSection result);
}; };
toLuaBindings = mode: maps: getAction = keymap:
map (value: '' if keymap.lua
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) then mkLuaInline keymap.action
'') (genMaps mode maps); else keymap.action;
# I'm not sure if every one of these will work. getOpts = keymap: {
allmap = toLuaBindings "" config.vim.maps.normalVisualOp; inherit (keymap) silent nowait script expr unique noremap;
nmap = toLuaBindings "n" config.vim.maps.normal; };
vmap = toLuaBindings "v" config.vim.maps.visual;
xmap = toLuaBindings "x" config.vim.maps.visualOnly;
smap = toLuaBindings "s" config.vim.maps.select;
imap = toLuaBindings "i" config.vim.maps.insert;
cmap = toLuaBindings "c" config.vim.maps.command;
tmap = toLuaBindings "t" config.vim.maps.terminal;
lmap = toLuaBindings "l" config.vim.maps.lang;
omap = toLuaBindings "o" config.vim.maps.operator;
icmap = toLuaBindings "ic" config.vim.maps.insertCommand;
maps = [ toLuaKeymap = {
nmap name,
imap value,
vmap }: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})";
xmap
smap namedModes = {
cmap "normal" = ["n"];
omap "insert" = ["i"];
tmap "select" = ["s"];
lmap "visual" = ["v"];
icmap "terminal" = ["t"];
allmap "normalVisualOp" = ["n" "v" "o"];
]; "visualOnly" = ["n" "x"];
mappings = concatLines (map concatLines maps); "operator" = ["o"];
"insertCommand" = ["i" "c"];
"lang" = ["l"];
"command" = ["c"];
};
maps =
removeAttrs cfg.maps (attrNames namedModes)
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normal;}) cfg.maps.normal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insert;}) cfg.maps.insert
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.select;}) cfg.maps.select
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visual;}) cfg.maps.visual
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.terminal;}) cfg.maps.terminal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normalVisualOp;}) cfg.maps.normalVisualOp
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visualOnly;}) cfg.maps.visualOnly
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.operator;}) cfg.maps.operator
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insertCommand;}) cfg.maps.insertCommand
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.lang;}) cfg.maps.lang
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.command;}) cfg.maps.command;
keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull maps)));
in { in {
vim = { vim = {
luaConfigRC = { luaConfigRC = {
@ -137,7 +81,7 @@ in {
# basic, theme # basic, theme
pluginConfigs = entryAfter ["theme"] pluginConfigs; pluginConfigs = entryAfter ["theme"] pluginConfigs;
extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs; extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
mappings = entryAfter ["extraPluginConfigs"] mappings; mappings = entryAfter ["extraPluginConfigs"] keymaps;
# FIXME: put this somewhere less stupid # FIXME: put this somewhere less stupid
footer = entryAfter ["mappings"] (optionalString config.vim.lazy.enable "require('lzn-auto-require.loader').register_loader()"); footer = entryAfter ["mappings"] (optionalString config.vim.lazy.enable "require('lzn-auto-require.loader').register_loader()");
}; };