docs/hacking: use new keybind helper

This commit is contained in:
Ching Pei Yang 2024-11-17 19:49:27 +01:00
parent 8edc48c5ea
commit f085f5a047
No known key found for this signature in database
GPG key ID: B3841364253DC4C8

View file

@ -30,53 +30,12 @@ There are many settings available in the options. Please refer to the
[documentation](https://notashelf.github.io/nvf/options.html#opt-vim.keymaps) to [documentation](https://notashelf.github.io/nvf/options.html#opt-vim.keymaps) to
see a list of them. see a list of them.
**nvf** provides a list of helper commands, so that you don't have to write the **nvf** provides a helper function, so that you don't have to write the
mapping attribute sets every time: mapping attribute sets every time:
- `mkBinding = key: action: desc:` - makes a basic binding, with `silent` set to - `mkKeymap`, which mimics neovim's `vim.keymap.set` function
true.
- `mkExprBinding = key: action: desc:` - makes an expression binding, with
`lua`, `silent`, and `expr` set to true.
- `mkLuaBinding = key: action: desc:` - makes an expression binding, with `lua`,
and `silent` set to true.
Do note that the Lua in these bindings is actual Lua, and not pasted into a You can read the source code of some modules to see them in action, but the
`:lua` command. Therefore, you should either pass in a function like
`require('someplugin').some_function`, without actually calling it, or you
should define your own functions, for example
```lua
function()
require('someplugin').some_function()
end
```
Additionally, to not have to repeat the descriptions, there's another utility
function with its own set of functions: Utility function that takes two
attribute sets:
- `{ someKey = "some_value" }`
- `{ someKey = { description = "Some Description"; }; }`
and merges them into
`{ someKey = { value = "some_value"; description = "Some Description"; }; }`
```nix
addDescriptionsToMappings = actualMappings: mappingDefinitions:
```
This function can be used in combination with the same `mkBinding` functions as
above, except they only take two arguments - `binding` and `action`, and have
different names:
- `mkSetBinding = binding: action:` - makes a basic binding, with `silent` set
to true.
- `mkSetExprBinding = binding: action:` - makes an expression binding, with
`lua`, `silent`, and `expr` set to true.
- `mkSetLuaBinding = binding: action:` - makes an expression binding, with
`lua`, and `silent` set to true.
You can read the source code of some modules to see them in action, but their
usage should look something like this: usage should look something like this:
```nix ```nix
@ -90,19 +49,12 @@ in {
# Mappings should always be inside an attrset called mappings # Mappings should always be inside an attrset called mappings
mappings = { mappings = {
# mkMappingOption is a helper function from lib, workspaceDiagnostics = mkMappingOption "Workspace diagnostics [trouble]" "<leader>lwd";
# that takes a description (which will also appear in which-key), documentDiagnostics = mkMappingOption "Document diagnostics [trouble]" "<leader>ld";
# and a default mapping (which can be null) lspReferences = mkMappingOption "LSP References [trouble]" "<leader>lr";
toggleCurrentLine = mkMappingOption "Toggle current line comment" "gcc"; quickfix = mkMappingOption "QuickFix [trouble]" "<leader>xq";
toggleCurrentBlock = mkMappingOption "Toggle current block comment" "gbc"; locList = mkMappingOption "LOCList [trouble]" "<leader>xl";
symbols = mkMappingOption "Symbols [trouble]" "<leader>xs";
toggleOpLeaderLine = mkMappingOption "Toggle line comment" "gc";
toggleOpLeaderBlock = mkMappingOption "Toggle block comment" "gb";
toggleSelectedLine = mkMappingOption "Toggle selected comment" "gc";
toggleSelectedBlock = mkMappingOption "Toggle selected block" "gb";
};
}; };
} }
``` ```
@ -111,56 +63,27 @@ in {
# config.nix # config.nix
{ {
config, config,
pkgs,
lib, lib,
options,
... ...
}: let }: let
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf;
inherit (lib.nvim.binds) mkSetBinding; inherit (lib.nvim.binds) mkKeymap;
cfg = config.vim.plugin; cfg = config.vim.plugin;
self = import ./plugindefinition.nix {inherit lib;};
mappingDefinitions = self.options.vim.plugin;
# addDescriptionsToMappings is a helper function from lib, keys = cfg.mappings;
# that merges mapping values and their descriptions inherit (options.vim.lsp.trouble) mappings;
# into one nice attribute set
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
config = mkIf (cfg.enable) { config = mkIf cfg.enable {
# ... vim.keymaps = [
vim.maps.normal = mkMerge [ (mkKeymap "n" keys.workspaceDiagnostics "<cmd>Trouble toggle diagnostics<CR>" {desc = mappings.workspaceDiagnostics.description;})
# mkSetBinding is another helper function from lib, (mkKeymap "n" keys.documentDiagnostics "<cmd>Trouble toggle diagnostics filter.buf=0<CR>" {desc = mappings.documentDiagnostics.description;})
# that actually adds the mapping with a description. (mkKeymap "n" keys.lspReferences "<cmd>Trouble toggle lsp_references<CR>" {desc = mappings.lspReferences.description;})
(mkSetBinding mappings.findFiles "<cmd> Telescope find_files<CR>") (mkKeymap "n" keys.quickfix "<cmd>Trouble toggle quickfix<CR>" {desc = mappings.quickfix.description;})
(mkSetBinding mappings.liveGrep "<cmd> Telescope live_grep<CR>") (mkKeymap "n" keys.locList "<cmd>Trouble toggle loclist<CR>" {desc = mappings.locList.description;})
(mkSetBinding mappings.buffers "<cmd> Telescope buffers<CR>") (mkKeymap "n" keys.symbols "<cmd>Trouble toggle symbols<CR>" {desc = mappings.symbols.description;})
(mkSetBinding mappings.helpTags "<cmd> Telescope help_tags<CR>")
(mkSetBinding mappings.open "<cmd> Telescope<CR>")
(mkSetBinding mappings.gitCommits "<cmd> Telescope git_commits<CR>")
(mkSetBinding mappings.gitBufferCommits "<cmd> Telescope git_bcommits<CR>")
(mkSetBinding mappings.gitBranches "<cmd> Telescope git_branches<CR>")
(mkSetBinding mappings.gitStatus "<cmd> Telescope git_status<CR>")
(mkSetBinding mappings.gitStash "<cmd> Telescope git_stash<CR>")
(mkIf config.vim.lsp.enable (mkMerge [
(mkSetBinding mappings.lspDocumentSymbols "<cmd> Telescope lsp_document_symbols<CR>")
(mkSetBinding mappings.lspWorkspaceSymbols "<cmd> Telescope lsp_workspace_symbols<CR>")
(mkSetBinding mappings.lspReferences "<cmd> Telescope lsp_references<CR>")
(mkSetBinding mappings.lspImplementations "<cmd> Telescope lsp_implementations<CR>")
(mkSetBinding mappings.lspDefinitions "<cmd> Telescope lsp_definitions<CR>")
(mkSetBinding mappings.lspTypeDefinitions "<cmd> Telescope lsp_type_definitions<CR>")
(mkSetBinding mappings.diagnostics "<cmd> Telescope diagnostics<CR>")
]))
(
mkIf config.vim.treesitter.enable
(mkSetBinding mappings.treesitter "<cmd> Telescope treesitter<CR>")
)
]; ];
# ...
}; };
} }
``` ```