modules: add git module

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-20 23:05:33 +03:00
parent 2b92614d0e
commit 1d31c3d1d2
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
9 changed files with 131 additions and 87 deletions

View file

@ -101,11 +101,11 @@
"gitsigns-nvim": {
"flake": false,
"locked": {
"lastModified": 1695571272,
"narHash": "sha256-VJCtDnPSo5RgSC+czSIOvqGhpUT0dzvCzbLtup+Ctyo=",
"lastModified": 1713620636,
"narHash": "sha256-UK3DyvrQ0kLm9wrMQ6tLDoDunoThbY/Yfjn+eCZpuMw=",
"owner": "lewis6991",
"repo": "gitsigns.nvim",
"rev": "bdeba1cec3faddd89146690c10b9a87949c0ee66",
"rev": "035da036e68e509ed158414416c827d022d914bd",
"type": "github"
},
"original": {
@ -213,11 +213,11 @@
},
"nix2lua": {
"locked": {
"lastModified": 1713368896,
"narHash": "sha256-ivvxVxSpuri8rYP3FVdcxSRrabejAz6iAQvXqdzEGLc=",
"lastModified": 1713641247,
"narHash": "sha256-UDAxwO1vSkaPtmby04xjiHq5dxd03IBD3lkpFKYlV7Q=",
"ref": "refs/heads/main",
"rev": "98eaf7a9c2a7e608387debeca8f403885fbec0e4",
"revCount": 29,
"rev": "8c4f3ca834ef7c07c9e3be3e006d7395c69a176c",
"revCount": 36,
"type": "git",
"url": "https://git.pleshevski.ru/mynix/nix2lua"
},

View file

@ -242,7 +242,7 @@
minimalNeovim = mkNeovim pkgs;
recommendedNeovim = minimalNeovim.override (prev: {
allNeovim = minimalNeovim.override (prev: {
modules = {
catppuccin = { };
nvim-tree-lua = {
@ -256,16 +256,17 @@
];
};
nvim-treesitter = { };
git = { };
};
plugins = with prev.nix2lua.lib; {
lualine-nvim = pipe [
lualine-nvim = pipe1
(require "lualine")
(call "setup" [{
(call "setup" {
options.ignore_focus = [ "NvimTree" ];
sections = {
lualine_a = [
[ "filename" (namedField "path" 1) ]
[ "filename" (nf "path" 1) ]
];
lualine_b = [ "branch" "diff" "diagnostics" ];
lualine_c = [ "lsp_progress" ];
@ -273,51 +274,14 @@
lualine_y = [ "progress" ];
lualine_z = [ "location" "mode" ];
};
}])
];
});
};
});
fullNeovim = recommendedNeovim.override {
plugins = recommendedNeovim.plugins // (with minimalNeovim.nix2lua; {
lspSaga.settings = {
border_style = "rounded";
symbol_in_winbar.enable = false;
code_action_lightbulb.enable = false;
code_action_keys = { quit = "<Esc>"; };
definition_action_keys = { quit = "<Esc>"; };
rename_action_quit = "<C-c>";
};
lspConfig = {
servers = {
nil_ls = { };
tsserver = { };
eslint = { };
denols = {
root_dir = mkLuaRaw "root_pattern(\"deno.json\", \"deno.jsonc\")";
};
rust_analyzer = {
settings.rust-analyzer = {
"server.path" = "rust-analyzer";
"updates.prompt" = false;
"updates.checkOnStartup" = false;
"checkOnSave.enable" = true;
"checkOnSave.command" = "clippy";
"cargo.autoreload" = true;
};
};
};
};
tabbyml.enable = true;
});
};
packages = {
# default = recommendedNeovim;
# full = fullNeovim;
minimal = minimalNeovim;
recommended = recommendedNeovim;
all = allNeovim;
};
in
{

View file

@ -1,7 +0,0 @@
{ lib, substituteAll, ... }:
let
readSubFile = src: params: builtins.readFile
(substituteAll (params // { inherit src; }));
in
lib // { inherit readSubFile substituteAll; }

34
lib/vim.nix Normal file
View file

@ -0,0 +1,34 @@
{ nix2lua }:
with nix2lua.lib;
{
fn.isdirectory = file: call1 "vim.fn.isdirectory" (var file);
cmd.cd = file: call1 "vim.cmd.cd" (var file);
keymap.set = { mode, bind, command, opts ? { } }:
call "vim.keymap.set" [ mode bind command opts ];
/*
Parameters:
- {event} (string|array) Event(s) that will trigger the handler (callback or command).
- {opts} Options dict:
- group (string|integer) optional: autocommand group name or id to match against.
- pattern (string|array) optional: pattern(s) to match literally autocmd-pattern.
- buffer (integer) optional: buffer number for buffer-local autocommands autocmd-buflocal. Cannot be used with {pattern}.
- desc (string) optional: description (for documentation and troubleshooting).
- callback (function|string) optional: Lua function (or Vimscript function name, if string) called when the event(s) is triggered. Lua callback can return a truthy value (not false or nil) to delete the autocommand. Receives a table argument with these keys:
- id: (number) autocommand id
- event: (string) name of the triggered event autocmd-events
- group: (number|nil) autocommand group id, if any
- match: (string) expanded value of <amatch>
- buf: (number) expanded value of <abuf>
- file: (string) expanded value of <afile>
- data: (any) arbitrary data passed from nvim_exec_autocmds()
- command (string) optional: Vim command to execute on event. Cannot be used with {callback}
- once (boolean) optional: defaults to false. Run the autocommand only once autocmd-once.
- nested (boolean) optional: defaults to false. Run nested autocommands autocmd-nested.
*/
api.nvim_create_autocmd = event: opts:
call "vim.api.nvim_create_autocmd" [ event opts ];
}

View file

@ -7,7 +7,7 @@ with nix2lua.lib;
{
catppuccin = [
(set "vim.g.catppuccin_flavour" flavour)
(pipe [ (require "catppuccin") (call "setup" [ ]) ])
(call "vim.cmd" [ "colorscheme catppuccin" ])
(pipe1 (require "catppuccin") (call0 "setup"))
(call "vim.cmd" "colorscheme catppuccin")
];
}

53
modules/git.nix Normal file
View file

@ -0,0 +1,53 @@
{ nix2lua, vim, lib, ... }:
with nix2lua.lib;
{ configs ? { }
, keymaps ? [
{ mode = "n"; bind = "]h"; command = raw "next_hunk"; }
{ mode = "n"; bind = "[h"; command = raw "prev_hunk"; }
{ mode = "n"; bind = "<leader>gs"; command = raw "gs.stage_hunk"; }
{ mode = "n"; bind = "<leader>gr"; command = raw "gs.reset_hunk"; }
{ mode = "n"; bind = "<leader>gu"; command = raw "gs.undo_stage_hunk"; }
{ mode = "n"; bind = "<leader>gp"; command = raw "gs.preview_hunk"; }
{ mode = "n"; bind = "<leader>gb"; command = lambda0 (call "gs.blame_line" { full = true; }); }
{ mode = "n"; bind = "<leader>gd"; command = raw "gs.diffthis"; }
{ mode = "n"; bind = "<leader>gD"; command = lambda0 (call "gs.diffthis" "~"); }
{ mode = "n"; bind = "<leader>gtb"; command = raw "gs.toggle_current_line_blame"; }
{ mode = "n"; bind = "<leader>gtd"; command = raw "gs.toggle_deleted"; }
]
}:
let
bufferedKeymaps = map (args: lib.recursiveUpdate args { opts.buffer = var "bufnr"; }) keymaps;
in
{
gitsigns-nvim = [
(local (func "on_attach" [ "bufnr" ] (
[
(local (set "gs" (var "package.loaded.gitsigns")))
(local (func0 "next_hunk"
(ifelse (var "vim.wo.diff")
(call1 "vim.cmd.normal" [ "]h" (nf "bang" true) ])
(call1 "gs.nav_hunk" "next")
)
))
(local (func0 "prev_hunk"
(ifelse (var "vim.wo.diff")
(call1 "vim.cmd.normal" [ "[h" (nf "bang" true) ])
(call1 "gs.nav_hunk" "prev"))
))
]
++ map vim.keymap.set bufferedKeymaps
)))
(pipe1
(require "gitsigns")
(call "setup" (configs // {
on_attach = var "on_attach";
}))
)
];
}

View file

@ -1,4 +1,4 @@
{ nix2lua, ... }:
{ nix2lua, vim, ... }:
{ configs ? { }
, keymaps ? [
@ -8,17 +8,13 @@
}:
with nix2lua.lib;
let
vimKeymapSet = { mode, bind, command }: call "vim.keymap.set" [ mode bind command ];
isEmptyVar = name: eq "" (var name);
in
let isEmptyVar = name: eq "" (var name); in
{
nvim-web-devicons = true;
nvim-tree-lua = (
[
(pipe [ (require "nvim-tree") (call "setup" [ configs ]) ])
(pipe1 (require "nvim-tree") (call "setup" configs))
(local (func "open_nvim_tree" [ "data" ] [
# buffer is a [No Name]
@ -31,27 +27,24 @@ in
# buffer is a directory
(local (set "isDirectory"
(eq 1 (var "vim.fn.isdirectory(data.file)"))
(eq 1 (vim.fn.isdirectory "data.file"))
))
(if' (not (and (var "isNoNameBuffer") (var "isDirectory"))) [
(if' (not (and (var "isNoNameBuffer") (var "isDirectory")))
return_void
])
)
# change to the directory
(if' (var "isDirectory") [
(call "vim.cmd.cd" [ (var "data.file") ])
])
(if' (var "isDirectory")
(vim.cmd.cd "data.file")
)
# open the tree
(pipe [ (require "nvim-tree.api") (call "tree.open" [ ]) ])
(pipe1 (require "nvim-tree.api") (call0 "tree.open"))
]))
(call "vim.api.nvim_create_autocmd" [
[ "VimEnter" ]
{ callback = var "open_nvim_tree"; }
])
(vim.api.nvim_create_autocmd [ "VimEnter" ] { callback = var "open_nvim_tree"; })
]
++ (map vimKeymapSet keymaps)
++ (map vim.keymap.set keymaps)
);
}

View file

@ -1,4 +1,4 @@
{ nix2lua, pkgs }:
{ nix2lua, pkgs, ... }:
let
defaultGrammars = g: [
@ -191,18 +191,18 @@ in
nvim-treesitter = [
(local (set "parser_config"
(pipe [ (require "nvim-treesitter.parsers") (call "get_parser_configs" [ ]) ])
(pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs"))
))
(set "parser_config.d2" { })
(pipe [
(pipe1
(require "nvim-treesitter.configs")
(call "setup" [{
(call "setup" {
ensure_installed = { };
sync_install = false;
highlight.enable = true;
indent.enable = true;
}])
])
})
)
];
}

View file

@ -31,7 +31,10 @@ let
plugins = mergeAttrs plugins';
modules = mergeAttrs modules';
importModule = moduleName: import (./modules + "/${moduleName}.nix") { inherit nix2lua pkgs; };
vim = import ./lib/vim.nix { inherit nix2lua; };
importModule = moduleName:
import (./modules + "/${moduleName}.nix") { inherit nix2lua pkgs vim lib; };
allModules = mergeAttrs (lib.mapAttrsToList importModule modules);
# Type: excludeOverride :: AttrSet -> AttrSet
@ -56,7 +59,7 @@ let
"-- Plugin: ${builtins.trace "Plugin: ${name}" name}\n"
+ (if lib.isString cfg then cfg
else if lib.isAttrs cfg && cfg ? _type then toLua cfg
else if lib.isList cfg then toLua (concatLines cfg)
else if lib.isList cfg then toLua (spaceBetween cfg)
else if lib.isBool cfg then "" # Just skip lua config. Only enable the plugin.
else abort "[neovim] mkPluginLuaConfig: '${name}' has unsupported type '${builtins.typeOf cfg}'");
pluginLuaConfigs = lib.mapAttrsToList mkPluginLuaConfig pluginsWithModules;
@ -68,7 +71,11 @@ let
/*
Type: mkLuaHeredoc :: string -> string
*/
mkLuaHeredoc = content: lib.concatLines [ "lua << EOF" content "EOF" ];
mkLuaHeredoc = content: ''
lua << EOF
${content}
EOF
'';
/*
Type: mkLuaRc :: [string] -> string