From c9716735290cef9e3ef44efda3a20cfeaa2c86fb Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 8 Nov 2024 17:55:18 +0100 Subject: [PATCH 1/5] lazy: wrap beforeAll in lua function --- modules/wrapper/lazy/config.nix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/wrapper/lazy/config.nix b/modules/wrapper/lazy/config.nix index 01deeb0..895adad 100644 --- a/modules/wrapper/lazy/config.nix +++ b/modules/wrapper/lazy/config.nix @@ -26,6 +26,15 @@ (removeAttrs spec ["package" "setupModule" "setupOpts" "keys"]) // { "@1" = name; + beforeAll = + if spec.beforeAll != null + then + mkLuaInline '' + function() + ${spec.beforeAll} + end + '' + else null; before = if spec.before != null then From 62b1e30e4987a453b5f4083853116ebc4cdff731 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 8 Nov 2024 17:55:52 +0100 Subject: [PATCH 2/5] neo-tree: add hijack netrw --- modules/plugins/filetree/neo-tree/config.nix | 22 +++++++++++++++++++ .../plugins/filetree/neo-tree/neo-tree.nix | 10 ++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/plugins/filetree/neo-tree/config.nix b/modules/plugins/filetree/neo-tree/config.nix index 13da035..53b8db2 100644 --- a/modules/plugins/filetree/neo-tree/config.nix +++ b/modules/plugins/filetree/neo-tree/config.nix @@ -4,6 +4,7 @@ ... }: let inherit (lib.modules) mkIf; + inherit (lib.strings) optionalString; cfg = config.vim.filetree.neo-tree; in { @@ -21,7 +22,28 @@ in { setupModule = "neo-tree"; inherit (cfg) setupOpts; + beforeAll = + optionalString (cfg.setupOpts.filesystem.hijack_netrw_behavior != "disabled") + # from https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/1326 + '' + vim.api.nvim_create_autocmd("BufEnter", { + group = vim.api.nvim_create_augroup("load_neo_tree", {}), + desc = "Loads neo-tree when openning a directory", + callback = function(args) + local stats = vim.uv.fs_stat(args.file) + + if not stats or stats.type ~= "directory" then + return + end + + require("lz.n").trigger_load("neo-tree-nvim") + + return true + end, + }) + ''; cmd = ["Neotree"]; + event = []; }; visuals.nvimWebDevicons.enable = true; diff --git a/modules/plugins/filetree/neo-tree/neo-tree.nix b/modules/plugins/filetree/neo-tree/neo-tree.nix index b1ec260..29199d4 100644 --- a/modules/plugins/filetree/neo-tree/neo-tree.nix +++ b/modules/plugins/filetree/neo-tree/neo-tree.nix @@ -1,5 +1,5 @@ {lib, ...}: let - inherit (lib.types) bool str int submodule enum either listOf; + inherit (lib.types) bool str enum either listOf; inherit (lib.options) mkOption mkEnableOption literalExpression; inherit (lib.nvim.types) mkPluginSetupOption; in { @@ -150,6 +150,14 @@ in { A list of filetypes that should not be replaced when opening a file ''; }; + + filesystem = { + hijack_netrw_behavior = mkOption { + type = enum ["disabled" "open_default" "open_current"]; + default = "open_default"; + description = "Hijack Netrw behavior"; + }; + }; }; }; } From 7f5c33605d73e448cd9572a69a078fe63a6ccbc2 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 8 Nov 2024 18:22:13 +0100 Subject: [PATCH 3/5] nvim-tree: hijack netrw --- modules/plugins/filetree/nvimtree/config.nix | 121 +++++++++++-------- 1 file changed, 70 insertions(+), 51 deletions(-) diff --git a/modules/plugins/filetree/nvimtree/config.nix b/modules/plugins/filetree/nvimtree/config.nix index 30cc680..a7f782f 100644 --- a/modules/plugins/filetree/nvimtree/config.nix +++ b/modules/plugins/filetree/nvimtree/config.nix @@ -24,6 +24,7 @@ in { package = "nvim-tree-lua"; setupModule = "nvim-tree"; inherit (cfg) setupOpts; + cmd = ["NvimTreeClipboard" "NvimTreeClose" "NvimTreeCollapse" "NvimTreeCollapseKeepBuffers" "NvimTreeFindFile" "NvimTreeFindFileToggle" "NvimTreeFocus" "NvimTreeHiTest" "NvimTreeOpen" "NvimTreeRefresh" "NvimTreeResize" "NvimTreeToggle"]; keys = [ (mkLznBinding ["n"] cfg.mappings.toggle ":NvimTreeToggle" mappings.toggle.description) @@ -31,58 +32,76 @@ in { (mkLznBinding ["n"] cfg.mappings.findFile ":NvimTreeFindFile" mappings.findFile.description) (mkLznBinding ["n"] cfg.mappings.focus ":NvimTreeFocus" mappings.focus.description) ]; + + beforeAll = '' + ${ + optionalString cfg.setupOpts.disable_netrw '' + -- disable netrew completely + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + '' + } + + ${optionalString (cfg.setupOpts.hijack_netrw && !cfg.openOnSetup) '' + vim.api.nvim_create_autocmd("BufEnter", { + group = vim.api.nvim_create_augroup("load_nvim_tree", {}), + desc = "Loads nvim-tree when openning a directory", + callback = function(args) + local stats = vim.uv.fs_stat(args.file) + + if not stats or stats.type ~= "directory" then + return + end + + require("lz.n").trigger_load("nvim-tree-lua") + + return true + end, + }) + ''} + + ${ + optionalString cfg.openOnSetup '' + ${optionalString config.vim.lazy.enable ''require('lz.n').trigger_load("nvim-tree-lua")''} + -- autostart behaviour + -- Open on startup has been deprecated + -- see https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup + + -- use a nix eval to dynamically insert the open on startup function + local function open_nvim_tree(data) + local IGNORED_FT = { + "markdown", + } + + -- buffer is a real file on the disk + local real_file = vim.fn.filereadable(data.file) == 1 + + -- buffer is a [No Name] + local no_name = data.file == "" and vim.bo[data.buf].buftype == "" + + -- &ft + local filetype = vim.bo[data.buf].ft + + -- only files please + if not real_file and not no_name then + return + end + + -- skip ignored filetypes + if vim.tbl_contains(IGNORED_FT, filetype) then + return + end + + -- open the tree but don't focus it + require("nvim-tree.api").tree.toggle({ focus = false }) + end + + -- function to automatically open the tree on VimEnter + vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) + '' + } + ''; }; - - pluginRC.nvimtreelua = entryAnywhere '' - ${ - optionalString cfg.setupOpts.disable_netrw '' - -- disable netrew completely - vim.g.loaded_netrw = 1 - vim.g.loaded_netrwPlugin = 1 - '' - } - - ${ - optionalString cfg.openOnSetup '' - ${optionalString config.vim.lazy.enable ''require('lz.n').trigger_load("nvim-tree-lua")''} - -- autostart behaviour - -- Open on startup has been deprecated - -- see https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup - - -- use a nix eval to dynamically insert the open on startup function - local function open_nvim_tree(data) - local IGNORED_FT = { - "markdown", - } - - -- buffer is a real file on the disk - local real_file = vim.fn.filereadable(data.file) == 1 - - -- buffer is a [No Name] - local no_name = data.file == "" and vim.bo[data.buf].buftype == "" - - -- &ft - local filetype = vim.bo[data.buf].ft - - -- only files please - if not real_file and not no_name then - return - end - - -- skip ignored filetypes - if vim.tbl_contains(IGNORED_FT, filetype) then - return - end - - -- open the tree but don't focus it - require("nvim-tree.api").tree.toggle({ focus = false }) - end - - -- function to automatically open the tree on VimEnter - vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) - '' - } - ''; }; }; } From c9f5dd1e220fb5b45489a86735abdbcd084a4704 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Tue, 19 Nov 2024 01:17:09 +0100 Subject: [PATCH 4/5] neo-tree: move beforeAll to pluginRC --- modules/plugins/filetree/neo-tree/config.nix | 43 ++++++++++---------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/modules/plugins/filetree/neo-tree/config.nix b/modules/plugins/filetree/neo-tree/config.nix index 53b8db2..e0ab65f 100644 --- a/modules/plugins/filetree/neo-tree/config.nix +++ b/modules/plugins/filetree/neo-tree/config.nix @@ -4,7 +4,7 @@ ... }: let inherit (lib.modules) mkIf; - inherit (lib.strings) optionalString; + inherit (lib.nvim.dag) entryAnywhere; cfg = config.vim.filetree.neo-tree; in { @@ -22,30 +22,31 @@ in { setupModule = "neo-tree"; inherit (cfg) setupOpts; - beforeAll = - optionalString (cfg.setupOpts.filesystem.hijack_netrw_behavior != "disabled") - # from https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/1326 - '' - vim.api.nvim_create_autocmd("BufEnter", { - group = vim.api.nvim_create_augroup("load_neo_tree", {}), - desc = "Loads neo-tree when openning a directory", - callback = function(args) - local stats = vim.uv.fs_stat(args.file) - - if not stats or stats.type ~= "directory" then - return - end - - require("lz.n").trigger_load("neo-tree-nvim") - - return true - end, - }) - ''; cmd = ["Neotree"]; event = []; }; + pluginRC = mkIf (cfg.setupOpts.filesystem.hijack_netrw_behavior != "disabled" && config.vim.lazy.enable) { + # from https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/1326 + neo-tree = entryAnywhere '' + vim.api.nvim_create_autocmd("BufEnter", { + group = vim.api.nvim_create_augroup("load_neo_tree", {}), + desc = "Loads neo-tree when openning a directory", + callback = function(args) + local stats = vim.uv.fs_stat(args.file) + + if not stats or stats.type ~= "directory" then + return + end + + require("lz.n").trigger_load("neo-tree-nvim") + + return true + end, + }) + ''; + }; + visuals.nvimWebDevicons.enable = true; }; }; From 590cd886f43f35e2938d799fa3bb65af69260988 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Tue, 19 Nov 2024 01:18:52 +0100 Subject: [PATCH 5/5] nvimtree: move beforeAll to pluginRC --- modules/plugins/filetree/nvimtree/config.nix | 130 +++++++++---------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/modules/plugins/filetree/nvimtree/config.nix b/modules/plugins/filetree/nvimtree/config.nix index a7f782f..40df74b 100644 --- a/modules/plugins/filetree/nvimtree/config.nix +++ b/modules/plugins/filetree/nvimtree/config.nix @@ -32,76 +32,76 @@ in { (mkLznBinding ["n"] cfg.mappings.findFile ":NvimTreeFindFile" mappings.findFile.description) (mkLznBinding ["n"] cfg.mappings.focus ":NvimTreeFocus" mappings.focus.description) ]; + }; - beforeAll = '' - ${ - optionalString cfg.setupOpts.disable_netrw '' - -- disable netrew completely - vim.g.loaded_netrw = 1 - vim.g.loaded_netrwPlugin = 1 - '' - } + pluginRC.nvim-tree = entryAnywhere '' + ${ + optionalString cfg.setupOpts.disable_netrw '' + -- disable netrew completely + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + '' + } - ${optionalString (cfg.setupOpts.hijack_netrw && !cfg.openOnSetup) '' - vim.api.nvim_create_autocmd("BufEnter", { - group = vim.api.nvim_create_augroup("load_nvim_tree", {}), - desc = "Loads nvim-tree when openning a directory", - callback = function(args) - local stats = vim.uv.fs_stat(args.file) + ${optionalString (config.vim.lazy.enable && cfg.setupOpts.hijack_netrw && !cfg.openOnSetup) '' + vim.api.nvim_create_autocmd("BufEnter", { + group = vim.api.nvim_create_augroup("load_nvim_tree", {}), + desc = "Loads nvim-tree when openning a directory", + callback = function(args) + local stats = vim.uv.fs_stat(args.file) - if not stats or stats.type ~= "directory" then - return - end - - require("lz.n").trigger_load("nvim-tree-lua") - - return true - end, - }) - ''} - - ${ - optionalString cfg.openOnSetup '' - ${optionalString config.vim.lazy.enable ''require('lz.n').trigger_load("nvim-tree-lua")''} - -- autostart behaviour - -- Open on startup has been deprecated - -- see https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup - - -- use a nix eval to dynamically insert the open on startup function - local function open_nvim_tree(data) - local IGNORED_FT = { - "markdown", - } - - -- buffer is a real file on the disk - local real_file = vim.fn.filereadable(data.file) == 1 - - -- buffer is a [No Name] - local no_name = data.file == "" and vim.bo[data.buf].buftype == "" - - -- &ft - local filetype = vim.bo[data.buf].ft - - -- only files please - if not real_file and not no_name then - return - end - - -- skip ignored filetypes - if vim.tbl_contains(IGNORED_FT, filetype) then - return - end - - -- open the tree but don't focus it - require("nvim-tree.api").tree.toggle({ focus = false }) + if not stats or stats.type ~= "directory" then + return end - -- function to automatically open the tree on VimEnter - vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) - '' - } - ''; - }; + require("lz.n").trigger_load("nvim-tree-lua") + + return true + end, + }) + ''} + + ${ + optionalString cfg.openOnSetup '' + ${optionalString config.vim.lazy.enable ''require('lz.n').trigger_load("nvim-tree-lua")''} + -- autostart behaviour + -- Open on startup has been deprecated + -- see https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup + + -- use a nix eval to dynamically insert the open on startup function + local function open_nvim_tree(data) + local IGNORED_FT = { + "markdown", + } + + -- buffer is a real file on the disk + local real_file = vim.fn.filereadable(data.file) == 1 + + -- buffer is a [No Name] + local no_name = data.file == "" and vim.bo[data.buf].buftype == "" + + -- &ft + local filetype = vim.bo[data.buf].ft + + -- only files please + if not real_file and not no_name then + return + end + + -- skip ignored filetypes + if vim.tbl_contains(IGNORED_FT, filetype) then + return + end + + -- open the tree but don't focus it + require("nvim-tree.api").tree.toggle({ focus = false }) + end + + -- function to automatically open the tree on VimEnter + vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) + '' + } + ''; }; }; }