From 6c248db49a4242f8798125507906254722c5b5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Sun, 23 Jun 2024 14:32:47 +0200 Subject: [PATCH] [fix] nvim: I broke it again ma Major refactoring in order to fix it T-T --- flake.nix | 11 +- home/default.nix | 3 - nvim/augroups.nix | 164 ++++++++-------- nvim/default.nix | 13 +- nvim/mappings.nix | 294 ++++++++++++++--------------- nvim/nixvim.nix | 112 ----------- nvim/plugins.nix | 448 ++++++++++++++++++++++---------------------- nvim/standalone.nix | 92 +++++++++ 8 files changed, 561 insertions(+), 576 deletions(-) delete mode 100644 nvim/nixvim.nix create mode 100644 nvim/standalone.nix diff --git a/flake.nix b/flake.nix index ba74e54..a6ee94f 100644 --- a/flake.nix +++ b/flake.nix @@ -93,7 +93,7 @@ system: f { inherit system; - pkgs = import nixpkgs {inherit system;}; + pkgs = nixpkgs.legacyPackages.${system}; } ); overlays = builtins.attrValues self.overlays; @@ -110,7 +110,7 @@ in { nvim = nixvim.lib.${system}.check.mkTestDerivationFromNixvimModule { pkgs = import nixpkgs {inherit system overlays;}; - module = ./nvim/nixvim.nix; + module = ./nvim/standalone.nix; }; fmt = pkgs.callPackage ./fmt.nix {inherit src;}; lint = pkgs.callPackage ./lint.nix {inherit src;}; @@ -133,7 +133,7 @@ # Nvim standalone module nvim = nixvim.legacyPackages.${system}.makeNixvimWithModule { pkgs = import nixpkgs {inherit system overlays;}; - module = ./nvim/nixvim.nix; + module = ./nvim/standalone.nix; }; } ); @@ -204,9 +204,8 @@ nixvim.homeManagerModules.nixvim ./nvim ]; - overlays = builtins.attrValues self.overlays; - homeManagerModuleSandalone = import ./home {inherit overlays nvim-config stylix;}; - homeManagerModuleNixOS = import ./home {inherit overlays nvim-config;}; + homeManagerModuleSandalone = import ./home {inherit nvim-config stylix;}; + homeManagerModuleNixOS = import ./home {inherit nvim-config;}; nixosModule = { imports = [ diff --git a/home/default.nix b/home/default.nix index e4a7994..4e2e369 100644 --- a/home/default.nix +++ b/home/default.nix @@ -1,5 +1,4 @@ { - overlays, nvim-config, stylix ? null, }: { @@ -13,8 +12,6 @@ in { imports = [ - # Apply overlays - {nixpkgs = {inherit overlays;};} nvim-config ./options.nix ./gui diff --git a/nvim/augroups.nix b/nvim/augroups.nix index 50b857d..f810aaf 100644 --- a/nvim/augroups.nix +++ b/nvim/augroups.nix @@ -1,87 +1,89 @@ {helpers, ...}: let inherit (helpers) mkRaw; in { - autoGroups = { - "highlightOnYank" = {}; - "lspConfig" = {}; - "restoreCursorPosition" = {}; - }; - autoCmd = [ - { - group = "highlightOnYank"; - event = "TextYankPost"; - pattern = "*"; - callback = mkRaw '' - function() - vim.highlight.on_yank { - higroup = ( - vim.fn['hlexists'] 'HighlightedyankRegion' > 0 and 'HighlightedyankRegion' or 'IncSearch' - ), - timeout = 200, - } - end - ''; - } - { - group = "restoreCursorPosition"; - event = "BufReadPost"; - pattern = "*"; - callback = mkRaw '' - function() - if vim.fn.line '\'"' > 0 and vim.fn.line '\'"' <= vim.fn.line '$' then - vim.cmd [[execute "normal! g'\""]] - end - end - ''; - } - { - group = "lspConfig"; - event = "LspAttach"; - pattern = "*"; - callback = let - opts = "noremap = true, buffer = bufnr"; - in - mkRaw '' - function(opts) - local bufnr = opts.buf - local client = vim.lsp.get_client_by_id(opts.data.client_id) - local capabilities = client.server_capabilities - -- Set Omnifunc if supported - if capabilities.completionProvider then - vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc" - end - -- Enable inlay hints if supported - if capabilities.inlayHintProvider then - vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) - end - -- Some Lsp servers do not advertise inlay hints properly so enable this keybinding regardless - vim.keymap.set('n', 'ht', function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({bufnr = 0}), { bufnr = 0 }) - end, - { desc = '[H]ints [T]oggle', ${opts} } - ) - -- Enable hover if supported - if capabilities.hoverProvider then - vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = 'Hover Documentation', ${opts} }) - end - -- Enable rename if supported - if capabilities.renameProvider then - vim.keymap.set('n', 'r', vim.lsp.buf.rename, { desc = '[R]ename', ${opts} }) - end - -- Enable code actions if supported - if capabilities.codeActionProvider then - vim.keymap.set({ 'n', 'v' }, 'fa', vim.lsp.buf.code_action, { desc = '[F]ind Code [A]ctions', ${opts} }) - end - -- Enable formatting if supported - if capabilities.documentFormattingProvider then - vim.keymap.set('n', 'w', function() require("conform").format({ lsp_fallback = true }) end, { desc = 'Format Buffer', ${opts} }) - end - -- Other keybinds - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { desc = '[G]o to [D]efinition', ${opts} }) - vim.keymap.set('n', 'gt', vim.lsp.buf.type_definition, { desc = '[G]o to [T]ype Definition', ${opts} }) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { desc = '[G]o to [I]mplementation', ${opts} }) + config = { + autoGroups = { + "highlightOnYank" = {}; + "lspConfig" = {}; + "restoreCursorPosition" = {}; + }; + autoCmd = [ + { + group = "highlightOnYank"; + event = "TextYankPost"; + pattern = "*"; + callback = mkRaw '' + function() + vim.highlight.on_yank { + higroup = ( + vim.fn['hlexists'] 'HighlightedyankRegion' > 0 and 'HighlightedyankRegion' or 'IncSearch' + ), + timeout = 200, + } end ''; - } - ]; + } + { + group = "restoreCursorPosition"; + event = "BufReadPost"; + pattern = "*"; + callback = mkRaw '' + function() + if vim.fn.line '\'"' > 0 and vim.fn.line '\'"' <= vim.fn.line '$' then + vim.cmd [[execute "normal! g'\""]] + end + end + ''; + } + { + group = "lspConfig"; + event = "LspAttach"; + pattern = "*"; + callback = let + opts = "noremap = true, buffer = bufnr"; + in + mkRaw '' + function(opts) + local bufnr = opts.buf + local client = vim.lsp.get_client_by_id(opts.data.client_id) + local capabilities = client.server_capabilities + -- Set Omnifunc if supported + if capabilities.completionProvider then + vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc" + end + -- Enable inlay hints if supported + if capabilities.inlayHintProvider then + vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) + end + -- Some Lsp servers do not advertise inlay hints properly so enable this keybinding regardless + vim.keymap.set('n', 'ht', function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({bufnr = 0}), { bufnr = 0 }) + end, + { desc = '[H]ints [T]oggle', ${opts} } + ) + -- Enable hover if supported + if capabilities.hoverProvider then + vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = 'Hover Documentation', ${opts} }) + end + -- Enable rename if supported + if capabilities.renameProvider then + vim.keymap.set('n', 'r', vim.lsp.buf.rename, { desc = '[R]ename', ${opts} }) + end + -- Enable code actions if supported + if capabilities.codeActionProvider then + vim.keymap.set({ 'n', 'v' }, 'fa', vim.lsp.buf.code_action, { desc = '[F]ind Code [A]ctions', ${opts} }) + end + -- Enable formatting if supported + if capabilities.documentFormattingProvider then + vim.keymap.set('n', 'w', function() require("conform").format({ lsp_fallback = true }) end, { desc = 'Format Buffer', ${opts} }) + end + -- Other keybinds + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { desc = '[G]o to [D]efinition', ${opts} }) + vim.keymap.set('n', 'gt', vim.lsp.buf.type_definition, { desc = '[G]o to [T]ype Definition', ${opts} }) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { desc = '[G]o to [I]mplementation', ${opts} }) + end + ''; + } + ]; + }; } diff --git a/nvim/default.nix b/nvim/default.nix index 0cefbb5..d2762bc 100644 --- a/nvim/default.nix +++ b/nvim/default.nix @@ -1,14 +1,17 @@ { lib, - pkgs, config, - helpers, ... }: let - # Force inputs to be included - nixvim = import ./nixvim.nix {inherit lib pkgs config helpers;}; + cfg = config.jhome.nvim; in { imports = [./options.nix]; - config.programs.nixvim = nixvim.config; + config.programs.nixvim = lib.mkMerge [ + ./standalone.nix + (lib.mkIf cfg.enable { + enable = true; + defaultEditor = lib.mkDefault true; + }) + ]; } diff --git a/nvim/mappings.nix b/nvim/mappings.nix index ed8e1bb..693d0ce 100644 --- a/nvim/mappings.nix +++ b/nvim/mappings.nix @@ -1,148 +1,150 @@ {helpers, ...}: let inherit (helpers) mkRaw; -in [ - # Quickfix - { - mode = "n"; - key = "qo"; - action = "Copen"; - options.desc = "Quickfix Open"; - } - { - mode = "n"; - key = "qq"; - action = "cclose"; - options.desc = "Quickfix Quit"; - } - { - mode = "n"; - key = "qj"; - action = "cnext"; - options.desc = "Quickfix next [J]"; - } - { - mode = "n"; - key = "qk"; - action = "cprev"; - options.desc = "Quickfix previous [K]"; - } - # Open or create file - { - mode = "n"; - key = "gf"; - action = "e "; - options.desc = "Go to File"; - } - # Keep Selection when indenting - { - mode = "x"; - key = ">"; - action = ">gv"; - options.desc = "Indent Selection"; - } - { - mode = "x"; - key = "<"; - action = ""] = function(fallback) - if cmp.visible() then - cmp.select_next_item() - elseif require("luasnip").expand_or_jumpable() then - require("luasnip").expand_or_jump() - elseif has_words_before() then - cmp.complete() - else - fallback() - end - end, - [""] = function(fallback) - if cmp.visible() then - cmp.select_prev_item() - elseif require("luasnip").jumpable(-1) then - require("luasnip").jump(-1) - else - fallback() - end - end, - [""] = cmp.mapping(function(fallback) - if require("luasnip").choice_active() then - require("luasnip").next_choice() - else - fallback() - end - end), - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.complete { }, - [""] = cmp.mapping.close(), - [""] = cmp.mapping.confirm { select = true }, - }) - ''; - }; - }; - # FIXME: doesn't include formatters - conform-nvim = { - enable = true; - formattersByFt = { - "_" = ["trim_whitespace"]; - c = ["clang_format"]; - cpp = ["clang_format"]; - lua = ["stylua"]; - nix = ["alejandra"]; - rust = ["rustfmt"]; - sh = ["shfmt"]; - toml = ["taplo"]; - yaml = ["yamlfmt"]; - zig = ["zigfmt"]; - }; - }; - gitsigns.enable = true; - lint = { - enable = true; - lintersByFt = { - rust = ["typos"]; - latex = ["chktex" "typos"]; - markdown = ["typos"]; - nix = ["statix"]; - sh = ["dash"]; - zsh = ["zsh"]; - }; - }; - lsp = { - enable = true; - servers = { - bashls.enable = true; - bashls.package = pkgs.unstable.bash-language-server; - # clangd.enable = true; # Adds ~2GiB - html.enable = true; - jsonls.enable = true; - marksman.enable = true; - nixd.enable = true; - nil-ls = { - enable = true; - settings.nix.flake = { - autoArchive = true; - autoEvalInputs = true; + cmp = { + enable = true; + cmdline = { + "/" = { + mapping = mkRaw "cmp.mapping.preset.cmdline()"; + sources = [ + {name = "rg";} + {name = "buffer";} + ]; + }; + ":" = { + mapping = mkRaw "cmp.mapping.preset.cmdline()"; + sources = [ + {name = "path";} + {name = "cmdline";} + ]; }; }; - ruff-lsp.enable = true; - taplo.enable = true; - # texlab.enable = true; # Not writing TeX rn - typos-lsp.enable = true; - typst-lsp.enable = true; + settings = { + # Snippets + snippet.expand = "function(args) require('luasnip').lsp_expand(args.body) end"; + # Completion Sources + sources = [ + { + name = "buffer"; + groupIndex = 3; + } + { + name = "calc"; + groupIndex = 2; + } + { + name = "conventionalcommits"; + groupIndex = 1; + } + { + name = "crates"; + groupIndex = 1; + } + { + name = "luasnip"; + groupIndex = 1; + } + { + name = "nvim_lsp"; + groupIndex = 1; + } + { + name = "nvim_lsp_document_symbol"; + groupIndex = 1; + } + { + name = "nvim_lsp_signature_help"; + groupIndex = 1; + } + { + name = "path"; + groupIndex = 2; + } + { + name = "spell"; + groupIndex = 2; + } + { + name = "treesitter"; + groupIndex = 2; + } + { + name = "zsh"; + groupIndex = 1; + } + ]; + mapping = mkRaw '' + cmp.mapping.preset.insert({ + [""] = function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif require("luasnip").expand_or_jumpable() then + require("luasnip").expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, + [""] = function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif require("luasnip").jumpable(-1) then + require("luasnip").jump(-1) + else + fallback() + end + end, + [""] = cmp.mapping(function(fallback) + if require("luasnip").choice_active() then + require("luasnip").next_choice() + else + fallback() + end + end), + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete { }, + [""] = cmp.mapping.close(), + [""] = cmp.mapping.confirm { select = true }, + }) + ''; + }; }; - }; - lspkind = { - enable = true; - mode = "symbol"; - extraOptions.maxwidth = 50; - }; - lualine = { - enable = true; - theme = lib.mkForce "gruvbox"; - }; - luasnip = { - enable = true; - extraConfig = { - update_events = "TextChanged,TextChangedI"; + # FIXME: doesn't include formatters + conform-nvim = { + enable = true; + formattersByFt = { + "_" = ["trim_whitespace"]; + c = ["clang_format"]; + cpp = ["clang_format"]; + lua = ["stylua"]; + nix = ["alejandra"]; + rust = ["rustfmt"]; + sh = ["shfmt"]; + toml = ["taplo"]; + yaml = ["yamlfmt"]; + zig = ["zigfmt"]; + }; }; - }; - noice = { - enable = true; - lsp.override = { - "vim.lsp.util.convert_input_to_markdown_lines" = true; - "vim.lsp.util.stylize_markdown" = true; - "cmp.entry.get_documentation" = true; + gitsigns.enable = true; + lint = { + enable = true; + lintersByFt = { + rust = ["typos"]; + latex = ["chktex" "typos"]; + markdown = ["typos"]; + nix = ["statix"]; + sh = ["dash"]; + zsh = ["zsh"]; + }; }; - presets = { - # use a classic bottom cmdline for search - bottom_search = true; - # position the cmdline and popupmenu together - command_palette = false; - # long messages will be sent to a split - long_message_to_split = true; - # enables an input dialog for inc-rename.nvim - inc_rename = false; - # add a border to hover docs and signature help - lsp_doc_border = true; + lsp = { + enable = true; + servers = { + bashls.enable = true; + bashls.package = pkgs.unstable.bash-language-server; + # clangd.enable = true; # Adds ~2GiB + html.enable = true; + jsonls.enable = true; + marksman.enable = true; + nixd.enable = true; + nil-ls = { + enable = true; + settings.nix.flake = { + autoArchive = true; + autoEvalInputs = true; + }; + }; + ruff-lsp.enable = true; + taplo.enable = true; + # texlab.enable = true; # Not writing TeX rn + typos-lsp.enable = true; + typst-lsp.enable = true; + }; }; - }; - notify = { - enable = true; - backgroundColour = "#000000"; - }; - nvim-colorizer = { - enable = true; - userDefaultOptions = { - names = false; # disable named colors (i.e. red) - mode = "virtualtext"; + lspkind = { + enable = true; + mode = "symbol"; + extraOptions.maxwidth = 50; }; - }; - rustaceanvim = { - enable = true; - # Install through rustup - rustAnalyzerPackage = null; - }; - telescope = { - enable = true; - extensions = { - ui-select.enable = true; - fzy-native.enable = true; + lualine = { + enable = true; + theme = lib.mkForce "gruvbox"; + }; + luasnip = { + enable = true; + extraConfig = { + update_events = "TextChanged,TextChangedI"; + }; + }; + noice = { + enable = true; + lsp.override = { + "vim.lsp.util.convert_input_to_markdown_lines" = true; + "vim.lsp.util.stylize_markdown" = true; + "cmp.entry.get_documentation" = true; + }; + presets = { + # use a classic bottom cmdline for search + bottom_search = true; + # position the cmdline and popupmenu together + command_palette = false; + # long messages will be sent to a split + long_message_to_split = true; + # enables an input dialog for inc-rename.nvim + inc_rename = false; + # add a border to hover docs and signature help + lsp_doc_border = true; + }; + }; + notify = { + enable = true; + backgroundColour = "#000000"; + }; + nvim-colorizer = { + enable = true; + userDefaultOptions = { + names = false; # disable named colors (i.e. red) + mode = "virtualtext"; + }; + }; + rustaceanvim = { + enable = true; + # Install through rustup + rustAnalyzerPackage = null; + }; + telescope = { + enable = true; + extensions = { + ui-select.enable = true; + fzy-native.enable = true; + }; + }; + treesitter = { + enable = true; + indent = true; + incrementalSelection.enable = true; + }; + trouble = { + enable = true; + settings.auto_close = true; }; - }; - treesitter = { - enable = true; - indent = true; - incrementalSelection.enable = true; - }; - trouble = { - enable = true; - settings.auto_close = true; }; } diff --git a/nvim/standalone.nix b/nvim/standalone.nix new file mode 100644 index 0000000..91ea5ae --- /dev/null +++ b/nvim/standalone.nix @@ -0,0 +1,92 @@ +{pkgs, ...}: { + imports = [./options.nix ./plugins.nix ./mappings.nix ./augroups.nix]; + + config = { + package = pkgs.unstable.neovim-unwrapped; + globals.mapleader = " "; + # Appearance + colorschemes.gruvbox = { + enable = true; + settings = { + bold = true; + transparent_mode = true; + terminal_colors = true; + }; + }; + clipboard.providers.wl-copy.enable = true; + opts = { + number = true; + relativenumber = true; + colorcolumn = "+1"; + cursorline = true; + wrap = false; + splitright = true; + # Tabs & indentation + smarttab = true; + autoindent = true; + smartindent = true; + # Search path + path = ".,/usr/include,**"; + wildmenu = true; + hlsearch = true; + incsearch = true; + ignorecase = true; # Search ignores cases + smartcase = true; # Unless it has a capital letter + # Enable local configuration :h 'exrc' + exrc = true; # safe since nvim 0.9 + }; + extraPlugins = let + plugins = pkgs.unstable.vimPlugins; + extraPlugins = import ./extraPlugins {pkgs = pkgs.unstable;}; + in [ + plugins.nui-nvim + plugins.nvim-web-devicons + extraPlugins.vim-jjdescription + extraPlugins.nvim-silicon + ]; + # Formatting & linters + extraPackages = [ + pkgs.unstable.silicon + pkgs.unstable.alejandra + pkgs.unstable.luajitPackages.jsregexp + pkgs.unstable.statix + pkgs.unstable.stylua + pkgs.unstable.shfmt + pkgs.unstable.taplo + pkgs.unstable.typos + pkgs.unstable.yamlfmt + ]; + extraConfigLuaPre = '' + -- Lua Pre Config + if vim.fn.has 'termguicolors' then + -- Enable RGB colors + vim.g.termguicolors = true + end + + -- Useful function + local has_words_before = function() + -- unpack = unpack or table.unpack + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 + and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match '%s' == nil + end + -- END: Lua Pre Config + ''; + extraConfigLua = '' + -- Lua Config + require("nvim-silicon").setup { + theme = "gruvbox-dark", + pad_horiz = 16, + pad_vert = 16, + -- Current buffer name + window_title = function() + return vim.fn.fnamemodify( + vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()), + ":t" + ) + end, + } + -- END: Lua Config + ''; + }; +} -- 2.47.0