From 6753c39ccd782f70aea22373002c3c1adb668783 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 8 May 2024 20:26:57 -0400 Subject: [PATCH] refactor(nvim): move stuff to lua, fix windows depending on size of win, etc --- common/home/neovim/coc.nix | 53 +++++++++-- common/home/neovim/default.nix | 96 +++++++++---------- common/home/neovim/git.nix | 35 +++++++ common/home/neovim/langs/bash.nix | 1 - common/home/neovim/langs/default.nix | 1 + common/home/neovim/langs/hyprlang.nix | 27 ++++++ common/home/neovim/neotree.lua | 64 +++++++++++++ common/home/neovim/plugins/autopairs.lua | 16 ---- common/home/neovim/plugins/gitsigns.lua | 13 --- common/home/neovim/plugins/indent.lua | 26 ------ common/home/neovim/plugins/mini.lua | 9 -- common/home/neovim/plugins/neotree.lua | 38 -------- common/home/neovim/plugins/neotree.vim | 10 -- common/home/neovim/plugins/snippets.vim | 13 --- common/home/neovim/theme.nix | 113 ++++++++++++++++++++--- common/home/neovim/treesitter.nix | 8 +- 16 files changed, 316 insertions(+), 207 deletions(-) create mode 100644 common/home/neovim/git.nix create mode 100644 common/home/neovim/langs/hyprlang.nix create mode 100644 common/home/neovim/neotree.lua delete mode 100644 common/home/neovim/plugins/autopairs.lua delete mode 100644 common/home/neovim/plugins/gitsigns.lua delete mode 100644 common/home/neovim/plugins/indent.lua delete mode 100644 common/home/neovim/plugins/mini.lua delete mode 100644 common/home/neovim/plugins/neotree.lua delete mode 100644 common/home/neovim/plugins/neotree.vim delete mode 100644 common/home/neovim/plugins/snippets.vim diff --git a/common/home/neovim/coc.nix b/common/home/neovim/coc.nix index 1196584..54df1c1 100644 --- a/common/home/neovim/coc.nix +++ b/common/home/neovim/coc.nix @@ -5,7 +5,6 @@ ... }: let inherit (config.vars) neovimIde; - inherit (lib) fileContents; inherit (pkgs) vimPlugins; in lib.mkIf neovimIde { @@ -26,11 +25,6 @@ in lua */ '' - vim.api.nvim_create_autocmd("FileType", { - pattern = 'hyprlang', - command = 'setlocal ts=4 sw=4 sts=0 expandtab', - }); - vim.api.nvim_create_user_command("Format", "call CocAction('format')", {}); -- Always show the signcolumn, otherwise it would shift the text each time @@ -42,7 +36,25 @@ in { plugin = vimPlugins.coc-snippets; type = "viml"; - config = fileContents ./plugins/snippets.vim; + config = + /* + vim + */ + '' + " use vscode keybinds for snippets completion + inoremap + \ coc#pum#visible() ? coc#_select_confirm() : + \ coc#expandableOrJumpable() ? "\=coc#rpc#request('doKeymap', ['snippets-expand-jump','''])\" : + \ CheckBackspace() ? "\" : + \ coc#refresh() + + function! CheckBackspace() abort + let col = col('.') - 1 + return !col || getline('.')[col - 1] =~# '\s' + endfunction + + let g:coc_snippet_next = '' + ''; } ## Fzf @@ -52,6 +64,33 @@ in vimPlugins.coc-json vimPlugins.coc-yaml vimPlugins.coc-toml + + { + plugin = vimPlugins.nvim-autopairs; + type = "lua"; + config = + /* + lua + */ + '' + -- Auto indent when pressing Enter between brackets + local remap = vim.api.nvim_set_keymap + local npairs = require('nvim-autopairs') + npairs.setup({map_cr=false}) + + _G.MUtils= {} + + MUtils.completion_confirm=function() + if vim.fn["coc#pum#visible"]() ~= 0 then + return vim.fn["coc#pum#confirm"]() + else + return npairs.autopairs_cr() + end + end + + remap('i' , '','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true}) + ''; + } ]; }; }; diff --git a/common/home/neovim/default.nix b/common/home/neovim/default.nix index b9a868b..afc393d 100644 --- a/common/home/neovim/default.nix +++ b/common/home/neovim/default.nix @@ -1,15 +1,13 @@ { config, pkgs, - lib, ... }: let - inherit (config.vars) neovimIde; - inherit (lib) fileContents optionals; inherit (pkgs) vimPlugins; in { imports = [ ./coc.nix + ./git.nix ./langs ./theme.nix ./treesitter.nix @@ -38,59 +36,49 @@ in { vim.opt.undodir = '${config.xdg.cacheHome}/nvim/'; -- remove highlight on words - vim.cmd[[nnoremap :noh]]; + vim.keymap.set('n', '', ':noh', { noremap = true, silent = true }); ''; - plugins = - [ - vimPlugins.fzfWrapper - vimPlugins.fzf-vim - vimPlugins.fugitive - { - plugin = vimPlugins.todo-comments-nvim; - type = "lua"; - config = - /* - lua - */ - '' - require('todo-comments').setup(); - ''; - } - { - plugin = vimPlugins.gitsigns-nvim; - type = "lua"; - config = fileContents ./plugins/gitsigns.lua; - } - { - plugin = vimPlugins.mini-nvim; - type = "lua"; - config = fileContents ./plugins/mini.lua; - } - { - plugin = vimPlugins.codewindow-nvim; - type = "lua"; - config = - /* - lua - */ - '' - require('codewindow').setup({ - auto_enable = true, - minimap_width = 8, - relative = 'editor', - window_border = 'none', - }); - ''; - } - ] - ++ optionals neovimIde [ - { - plugin = vimPlugins.nvim-autopairs; - type = "lua"; - config = fileContents ./plugins/autopairs.lua; - } - ]; + plugins = [ + vimPlugins.fzfWrapper + vimPlugins.fzf-vim + + { + plugin = vimPlugins.todo-comments-nvim; + type = "lua"; + config = + /* + lua + */ + '' + require('todo-comments').setup(); + ''; + } + { + plugin = vimPlugins.mini-nvim; + type = "lua"; + config = + /* + lua + */ + '' + -- TODO: see how this works + local ts_input = require('mini.surround').gen_spec.input.treesitter; + + require('mini.surround').setup({ + custom_surroundings = { + -- Use tree-sitter to search for function call + f = { + input = ts_input({ + outer = '@call.outer', + inner = '@call.inner', + }); + }, + }, + }); + ''; + } + ]; }; }; } diff --git a/common/home/neovim/git.nix b/common/home/neovim/git.nix new file mode 100644 index 0000000..9f5528a --- /dev/null +++ b/common/home/neovim/git.nix @@ -0,0 +1,35 @@ +{pkgs, ...}: let + inherit (pkgs) vimPlugins; +in { + programs = { + neovim = { + plugins = [ + vimPlugins.fugitive + + { + plugin = vimPlugins.gitsigns-nvim; + type = "lua"; + config = + /* + lua + */ + '' + local gitsigns = require("gitsigns"); + + local function visual_stage() + local first_line = vim.fn.line('v'); + local last_line = vim.fn.getpos('.')[2]; + gitsigns.stage_hunk({ first_line, last_line }); + end + + vim.keymap.set("v", "gs", function() + visual_stage() + end); + + gitsigns.setup(); + ''; + } + ]; + }; + }; +} diff --git a/common/home/neovim/langs/bash.nix b/common/home/neovim/langs/bash.nix index 4b547ea..eea239f 100644 --- a/common/home/neovim/langs/bash.nix +++ b/common/home/neovim/langs/bash.nix @@ -32,7 +32,6 @@ in ''; coc.settings = { - # Bash bashIde.shellcheckPath = "${pkgs.shellcheck}/bin/shellcheck"; }; diff --git a/common/home/neovim/langs/default.nix b/common/home/neovim/langs/default.nix index 3f7fc85..27595bd 100644 --- a/common/home/neovim/langs/default.nix +++ b/common/home/neovim/langs/default.nix @@ -2,6 +2,7 @@ imports = [ ./bash.nix ./clang.nix + ./hyprlang.nix ./java.nix ./lua.nix ./markdown.nix diff --git a/common/home/neovim/langs/hyprlang.nix b/common/home/neovim/langs/hyprlang.nix new file mode 100644 index 0000000..84792d9 --- /dev/null +++ b/common/home/neovim/langs/hyprlang.nix @@ -0,0 +1,27 @@ +{ + config, + lib, + ... +}: let + inherit (config.vars) neovimIde; +in + lib.mkIf neovimIde { + programs = { + neovim = { + extraLuaConfig = + /* + lua + */ + '' + vim.filetype.add({ + pattern = { [".*/hypr/.*%.conf"] = "hyprlang" }, + }); + + vim.api.nvim_create_autocmd("FileType", { + pattern = 'hyprlang', + command = 'setlocal ts=4 sw=4 sts=0 expandtab', + }); + ''; + }; + }; + } diff --git a/common/home/neovim/neotree.lua b/common/home/neovim/neotree.lua new file mode 100644 index 0000000..89dc1bf --- /dev/null +++ b/common/home/neovim/neotree.lua @@ -0,0 +1,64 @@ +-- Override netrw +vim.g.loaded_netrw = 0 +vim.g.loaded_netrwPlugin = 0 + +require('neo-tree').setup({ + close_if_last_window = true, + enable_refresh_on_write = true, + + window = { + width = 22, + }, + + filesystem = { + use_libuv_file_watcher = true, + group_empty_dirs = true, + + filtered_items = { + visible = false, + hide_dotfiles = false, + hide_gitignored = true, + hide_by_name = {}, + hide_by_pattern = {}, + always_show = {}, + never_show = {}, + never_show_by_pattern = {}, + }, + }, + + source_selector = { + winbar = true, + statusline = false + }, + + follow_current_file = { + enabled = true, + leave_dirs_open = true, + } +}) + +local function is_neotree_open() + local manager = require("neo-tree.sources.manager") + local renderer = require("neo-tree.ui.renderer") + local state = manager.get_state("filesystem") + local window_exists = renderer.window_exists(state) + return window_exists +end + +-- Auto open Neo-Tree on big enough window +vim.api.nvim_create_autocmd({ 'VimEnter', 'VimResized' }, { + pattern = '*', + callback = function() + if vim.api.nvim_eval([[&columns]]) > 100 then + if is_neotree_open() == false then + vim.cmd [[Neotree show]]; + vim.cmd [[Neotree close]]; + vim.cmd [[Neotree show]]; + end + else + if is_neotree_open() then + vim.cmd [[Neotree close]]; + end + end + end, +}); diff --git a/common/home/neovim/plugins/autopairs.lua b/common/home/neovim/plugins/autopairs.lua deleted file mode 100644 index 8960496..0000000 --- a/common/home/neovim/plugins/autopairs.lua +++ /dev/null @@ -1,16 +0,0 @@ --- Auto indent when pressing Enter between brackets -local remap = vim.api.nvim_set_keymap -local npairs = require('nvim-autopairs') -npairs.setup({map_cr=false}) - -_G.MUtils= {} - -MUtils.completion_confirm=function() - if vim.fn["coc#pum#visible"]() ~= 0 then - return vim.fn["coc#pum#confirm"]() - else - return npairs.autopairs_cr() - end -end - -remap('i' , '','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true}) diff --git a/common/home/neovim/plugins/gitsigns.lua b/common/home/neovim/plugins/gitsigns.lua deleted file mode 100644 index 762cbc6..0000000 --- a/common/home/neovim/plugins/gitsigns.lua +++ /dev/null @@ -1,13 +0,0 @@ -local gitsigns = require("gitsigns") - -local function visual_stage() - local first_line = vim.fn.line('v') - local last_line = vim.fn.getpos('.')[2] - gitsigns.stage_hunk({ first_line, last_line }) -end - -vim.keymap.set("v", "gs", function() - visual_stage() -end) - -gitsigns.setup(); diff --git a/common/home/neovim/plugins/indent.lua b/common/home/neovim/plugins/indent.lua deleted file mode 100644 index 61494fd..0000000 --- a/common/home/neovim/plugins/indent.lua +++ /dev/null @@ -1,26 +0,0 @@ -local highlight = { - "RainbowRed", - "RainbowYellow", - "RainbowBlue", - "RainbowOrange", - "RainbowGreen", - "RainbowViolet", - "RainbowCyan", -} -local hooks = require('ibl.hooks') -hooks.register(hooks.type.HIGHLIGHT_SETUP, function() - vim.api.nvim_set_hl(0, "RainbowRed", { fg = "#E06C75" }) - vim.api.nvim_set_hl(0, "RainbowYellow", { fg = "#E5C07B" }) - vim.api.nvim_set_hl(0, "RainbowBlue", { fg = "#61AFEF" }) - vim.api.nvim_set_hl(0, "RainbowOrange", { fg = "#D19A66" }) - vim.api.nvim_set_hl(0, "RainbowGreen", { fg = "#98C379" }) - vim.api.nvim_set_hl(0, "RainbowViolet", { fg = "#C678DD" }) - vim.api.nvim_set_hl(0, "RainbowCyan", { fg = "#56B6C2" }) -end) - -require('ibl').setup({ - indent = { - highlight = highlight, - char = "▏", - }, -}) diff --git a/common/home/neovim/plugins/mini.lua b/common/home/neovim/plugins/mini.lua deleted file mode 100644 index b088462..0000000 --- a/common/home/neovim/plugins/mini.lua +++ /dev/null @@ -1,9 +0,0 @@ -local ts_input = require('mini.surround').gen_spec.input.treesitter -require('mini.surround').setup({ - custom_surroundings = { - -- Use tree-sitter to search for function call - f = { - input = ts_input({ outer = '@call.outer', inner = '@call.inner' }) - }, - } -}) diff --git a/common/home/neovim/plugins/neotree.lua b/common/home/neovim/plugins/neotree.lua deleted file mode 100644 index ad420ba..0000000 --- a/common/home/neovim/plugins/neotree.lua +++ /dev/null @@ -1,38 +0,0 @@ --- Override netrw -vim.g.loaded_netrw = 0 -vim.g.loaded_netrwPlugin = 0 - -require('neo-tree').setup({ - close_if_last_window = true, - enable_refresh_on_write = true, - - window = { - width = 22, - }, - - filesystem = { - use_libuv_file_watcher = true, - group_empty_dirs = true, - - filtered_items = { - visible = false, - hide_dotfiles = false, - hide_gitignored = true, - hide_by_name = {}, - hide_by_pattern = {}, - always_show = {}, - never_show = {}, - never_show_by_pattern = {}, - }, - }, - - source_selector = { - winbar = true, - statusline = false - }, - - follow_current_file = { - enabled = true, - leave_dirs_open = true, - } -}) diff --git a/common/home/neovim/plugins/neotree.vim b/common/home/neovim/plugins/neotree.vim deleted file mode 100644 index 49b34ef..0000000 --- a/common/home/neovim/plugins/neotree.vim +++ /dev/null @@ -1,10 +0,0 @@ -" Auto open Neo-Tree on big enough window -function! OpenTree() abort - if &columns > 100 - Neotree show - Neotree close - Neotree show - endif -endfunction - -autocmd VimEnter * call OpenTree() diff --git a/common/home/neovim/plugins/snippets.vim b/common/home/neovim/plugins/snippets.vim deleted file mode 100644 index 3df084b..0000000 --- a/common/home/neovim/plugins/snippets.vim +++ /dev/null @@ -1,13 +0,0 @@ -" use vscode keybinds for snippets completion -inoremap - \ coc#pum#visible() ? coc#_select_confirm() : - \ coc#expandableOrJumpable() ? "\=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\" : - \ CheckBackspace() ? "\" : - \ coc#refresh() - -function! CheckBackspace() abort - let col = col('.') - 1 - return !col || getline('.')[col - 1] =~# '\s' -endfunction - -let g:coc_snippet_next = '' diff --git a/common/home/neovim/theme.nix b/common/home/neovim/theme.nix index 17e418b..194d3db 100644 --- a/common/home/neovim/theme.nix +++ b/common/home/neovim/theme.nix @@ -28,21 +28,59 @@ in { */ '' -- set dot icon in place of trailing whitespaces - vim.cmd[[set list listchars=tab:\ \ ,nbsp:␣,trail:•,extends:⟩,precedes:⟨]] + vim.opt.listchars = { + tab = '→ ', + trail = '•', + extends = '⟩', + precedes = '⟨', + nbsp = '␣', + }; + vim.opt.list = true; -- Add visual indicator for trailing whitespaces - vim.opt.fillchars = {eob = " "} + vim.opt.fillchars = { eob = " " }; + vim.fn.matchadd('errorMsg', [[\s\+$]]); - vim.cmd[[colorscheme dracula]] + vim.cmd.colorscheme('dracula'); ''; } { plugin = vimPlugins.indent-blankline-nvim; type = "lua"; - config = fileContents ./plugins/indent.lua; + config = + /* + lua + */ + '' + local highlight = { + "RainbowRed", + "RainbowYellow", + "RainbowBlue", + "RainbowOrange", + "RainbowGreen", + "RainbowViolet", + "RainbowCyan", + }; + + local hooks = require('ibl.hooks'); + hooks.register(hooks.type.HIGHLIGHT_SETUP, function() + vim.api.nvim_set_hl(0, "RainbowRed", { fg = "#E06C75" }) + vim.api.nvim_set_hl(0, "RainbowYellow", { fg = "#E5C07B" }) + vim.api.nvim_set_hl(0, "RainbowBlue", { fg = "#61AFEF" }) + vim.api.nvim_set_hl(0, "RainbowOrange", { fg = "#D19A66" }) + vim.api.nvim_set_hl(0, "RainbowGreen", { fg = "#98C379" }) + vim.api.nvim_set_hl(0, "RainbowViolet", { fg = "#C678DD" }) + vim.api.nvim_set_hl(0, "RainbowCyan", { fg = "#56B6C2" }) + end); + + require('ibl').setup({ + indent = { + highlight = highlight, + char = "▏", + }, + }); + ''; } - ] - ++ optionals neovimIde [ { plugin = vimPlugins.lualine-nvim; type = "lua"; @@ -62,16 +100,65 @@ in { }); ''; } + ] + ++ optionals neovimIde [ { plugin = vimPlugins.neo-tree-nvim; - type = "viml"; - config = '' - ${fileContents ./plugins/neotree.vim} + type = "lua"; + config = fileContents ./neotree.lua; + } + { + plugin = vimPlugins.codewindow-nvim; + type = "lua"; + config = + /* + lua + */ + '' + local codewindow = require('codewindow'); - lua << EOF - ${fileContents ./plugins/neotree.lua} - EOF - ''; + codewindow.setup({ + auto_enable = false, + minimap_width = 8, + relative = 'editor', + window_border = 'none', + exclude_filetypes = { 'help' }, + }); + + vim.api.nvim_create_autocmd({ 'VimEnter', 'VimResized' }, { + pattern = '*', + callback = function() + if vim.api.nvim_win_get_width(0) < 88 then + codewindow.close_minimap(); + else + codewindow.open_minimap(); + end + end, + }); + ''; + } + { + plugin = vimPlugins.transparent-nvim; + type = "lua"; + config = + /* + lua + */ + '' + require("transparent").setup({ + groups = { + 'Normal', 'NormalNC', 'Comment', 'Constant', + 'Special', 'Identifier', 'Statement', 'PreProc', + 'Type', 'Underlined', 'Todo', 'String', 'Function', + 'Conditional', 'Repeat', 'Operator', 'Structure', + 'LineNr', 'NonText', 'SignColumn', 'CursorLine', + 'CursorLineNr', 'StatusLine', 'StatusLineNC', + 'EndOfBuffer', + }, + extra_groups = {}, + exclude_groups = {}, + }); + ''; } ]; }; diff --git a/common/home/neovim/treesitter.nix b/common/home/neovim/treesitter.nix index e4d29ff..2b8f4d1 100644 --- a/common/home/neovim/treesitter.nix +++ b/common/home/neovim/treesitter.nix @@ -18,9 +18,7 @@ in { min_window_height = 20, }); - vim.cmd( - 'hi TreesitterContextBottom gui=underline guisp=Grey' - ); + vim.cmd.hi('TreesitterContextBottom', 'gui=underline guisp=Grey'); ''; } @@ -37,10 +35,6 @@ in { highlight = { enable = true }, indent = { enable = true }, }); - - vim.filetype.add({ - pattern = { [".*/hypr/.*%.conf"] = "hyprlang" }, - }); ''; plugin = vimPlugins.nvim-treesitter.withPlugins (p: [ p.awk