From 3eb6c9b8d8348807a9573375d6afea21583b4671 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 17 Apr 2024 19:35:34 +0300 Subject: [PATCH] add nvim-tree-lua module --- flake.lock | 8 ++-- flake.nix | 61 +++++++++++++++++------------ modules/nvim-tree-lua.nix | 55 ++++++++++++++++++++++++++ neovim.nix | 82 ++++++++++++++++++--------------------- 4 files changed, 132 insertions(+), 74 deletions(-) create mode 100644 modules/nvim-tree-lua.nix diff --git a/flake.lock b/flake.lock index d0f7e68..4e3d4e3 100644 --- a/flake.lock +++ b/flake.lock @@ -197,11 +197,11 @@ }, "nix2lua": { "locked": { - "lastModified": 1709128456, - "narHash": "sha256-tRduS+XFI6VceWPQzjkuvlyn/Oe5NsQUS/wyEC69dvk=", + "lastModified": 1713368896, + "narHash": "sha256-ivvxVxSpuri8rYP3FVdcxSRrabejAz6iAQvXqdzEGLc=", "ref": "refs/heads/main", - "rev": "f152767145e61fea96eddb1b550ab3f25701e0ad", - "revCount": 18, + "rev": "98eaf7a9c2a7e608387debeca8f403885fbec0e4", + "revCount": 29, "type": "git", "url": "https://git.pleshevski.ru/mynix/nix2lua" }, diff --git a/flake.nix b/flake.nix index fe369bf..357c290 100644 --- a/flake.nix +++ b/flake.nix @@ -243,32 +243,42 @@ minimalNeovim = mkNeovim pkgs; - recommendedNeovim = (minimalNeovim.override { - enableDevIcons = true; - enableTabby = false; + recommendedNeovim = minimalNeovim.override { + modules = { + nvim-tree-lua = { + configs = { + renderer = { + group_empty = true; + full_name = true; + }; - plugins = with minimalNeovim.nix2lua; { - nvimTree.settings = { - renderer = { - group_empty = true; - full_name = true; - }; - }; - lualine.settings = { - options.ignore_focus = [ "NvimTree" ]; - sections = { - lualine_a = [ - [ "filename" (mkNamedField "path" 1) ] - ]; - lualine_b = [ "branch" "diff" "diagnostics" ]; - lualine_c = [ "lsp_progress" ]; - lualine_x = [ "filesize" "filetype" ]; - lualine_y = [ "progress" ]; - lualine_z = [ "location" "mode" ]; }; + keymaps = [ + { mode = "n"; bind = "nt"; command = "NvimTreeToggle"; } + { mode = "n"; bind = "nf"; command = "NvimTreeFindFile"; } + ]; }; }; - }); + + plugins = with minimalNeovim.nix2lua; { + lualine-nvim = pipe [ + (require "lualine") + (call "setup" [{ + options.ignore_focus = [ "NvimTree" ]; + sections = { + lualine_a = [ + [ "filename" (namedField "path" 1) ] + ]; + lualine_b = [ "branch" "diff" "diagnostics" ]; + lualine_c = [ "lsp_progress" ]; + lualine_x = [ "filesize" "filetype" ]; + lualine_y = [ "progress" ]; + lualine_z = [ "location" "mode" ]; + }; + }]) + ]; + }; + }; fullNeovim = recommendedNeovim.override { plugins = recommendedNeovim.plugins // (with minimalNeovim.nix2lua; { @@ -305,10 +315,11 @@ }; packages = { - default = recommendedNeovim; - recommended = recommendedNeovim; - full = fullNeovim; + # default = recommendedNeovim; + # full = fullNeovim; minimal = minimalNeovim; + + recommended = recommendedNeovim; }; in { diff --git a/modules/nvim-tree-lua.nix b/modules/nvim-tree-lua.nix new file mode 100644 index 0000000..96ddbde --- /dev/null +++ b/modules/nvim-tree-lua.nix @@ -0,0 +1,55 @@ +{ nix2lua }: + +{ configs ? { } +, keymaps ? [ + { mode = "n"; bind = "nt"; command = "NvimTreeToggle"; } + { mode = "n"; bind = "nf"; command = "NvimTreeFindFile"; } + ] +}: + +with nix2lua.lib; +let + vimKeymapSet = { mode, bind, command }: call "vim.keymap.set" [ mode bind command ]; + + isEmptyVar = name: eq "" (var name); +in +{ + nvim-tree-lua = ( + [ + (pipe [ (require "nvim-tree") (call "setup" [ configs ]) ]) + + (local (func "open_nvim_tree" [ "data" ] [ + # buffer is a [No Name] + (local (set "isNoNameBuffer" + (and + (isEmptyVar "data.file") + (isEmptyVar "vim.bo[data.buf].buftype") + ) + )) + + # buffer is a directory + (local (set "isDirectory" + (eq 1 (var "vim.fn.isdirectory(data.file)")) + )) + + (if' (not (and (var "isNoNameBuffer") (var "isDirectory"))) [ + return_void + ]) + + # change to the directory + (if' (var "isDirectory") [ + (call "vim.cmd.cd" [ (var "data.file") ]) + ]) + + # open the tree + (pipe [ (require "nvim-tree.api") (call "tree.open" [ ]) ]) + ])) + + (call "vim.api.nvim_create_autocmd" [ + [ "VimEnter" ] + { callback = var "open_nvim_tree"; } + ]) + ] + ++ (map vimKeymapSet keymaps) + ); +} diff --git a/neovim.nix b/neovim.nix index f6a516c..8a39831 100644 --- a/neovim.nix +++ b/neovim.nix @@ -1,24 +1,19 @@ -{ enableDevIcons ? false -, enableTabby ? false -, enableOrgMode ? false -, viAlias ? false +{ viAlias ? false , vimAlias ? false , extraConfig ? "" , extraLuaConfig ? "" -, extraPlugins ? [ ] -, theme ? { } -, plugins ? { } , wrapNeovim , neovim-unwrapped , tree-sitter , neovimPlugins , lib , nix2lua -, substituteAll -, callPackage +, plugins ? { } +, modules ? { } , ... }: + let plugins' = plugins; @@ -33,48 +28,45 @@ let in let plugins = mergeAttrs plugins'; - inherit (builtins) catAttrs readFile; - extLib = import ./lib.nix { inherit lib substituteAll; } // { - inherit (nix2lua.lib) toLua LuaNil; - }; + importModule = moduleName: import (./modules + "/${moduleName}.nix") { inherit nix2lua; }; + + allModules = mergeAttrs (lib.mapAttrsToList importModule modules); + + pluginsWithModules = mergeAttrs [ allModules plugins ]; - pluginParams = neovimPlugins // { - inherit tree-sitter plugins enableDevIcons enableTabby enableOrgMode; - themeCfg = theme; - lib = extLib; - }; + /* + Type: getPluginByName :: string -> derivation + */ + getPluginByName = lib.flip lib.getAttr neovimPlugins; + allPlugins = map getPluginByName (lib.attrNames pluginsWithModules); - callPlugin = plugin: callPackage plugin pluginParams; - callPlugins = list: map callPlugin list; + /* + Type: mkPluginLuaConfig :: string -> a -> string + */ + mkPluginLuaConfig = name: cfg: + with nix2lua.lib; + "-- Plugin: ${builtins.trace "Plugin: ${name}" name}\n" + + (if lib.isString cfg then cfg + else if lib.isAttrs cfg then toLua cfg + else if lib.isList cfg then toLua (concatLines cfg) + else abort "[neovim] mkPluginLuaConfig: unsupported type"); + pluginLuaConfigs = lib.mapAttrsToList mkPluginLuaConfig pluginsWithModules; - pluginsSettings = callPlugins [ - ./plugins/config - ./plugins/syntax - ./plugins/git - ./plugins/explorer - ./plugins/theme - ./plugins/lsp - ./plugins/formatter - ./plugins/ux - ]; + basicLuaConfigs = map builtins.readFile [ ./config/basic.lua ]; - basePlugins = [ neovimPlugins.plenary-nvim ]; - customPlugins = catAttrs "plugins" pluginsSettings; - allPlugins = basePlugins ++ customPlugins ++ extraPlugins; + allLuaConfigs = basicLuaConfigs ++ pluginLuaConfigs; - basicConfigs = map readFile [ ./config/basic.lua ]; - pluginConfigs = catAttrs "luaConfig" pluginsSettings; - allConfigs = basicConfigs ++ pluginConfigs ++ [ extraLuaConfig ]; + /* + Type: mkLuaHeredoc :: string -> string + */ + mkLuaHeredoc = content: lib.concatLines [ "lua << EOF" content "EOF" ]; - mkLuaHeredoc = content: '' - lua << EOF - ${content} - EOF - ''; - - mkLuaRc = lib.concatMapStrings mkLuaHeredoc; + /* + Type: mkLuaRc :: [string] -> string + */ + mkLuaRc = list: lib.concatLines (map mkLuaHeredoc list); in (wrapNeovim neovim-unwrapped { inherit viAlias; @@ -85,13 +77,13 @@ in withRuby = false; configure = { - customRC = extraConfig + mkLuaRc allConfigs; + customRC = extraConfig + mkLuaRc allLuaConfigs; packages.myVimPackages = { start = allPlugins; }; }; }).overrideAttrs (oldAttrs: { passthru = oldAttrs.passthru // { nix2lua = nix2lua.lib; - inherit plugins allConfigs; + inherit plugins; }; })