From edcc3acea595d3045253c3c2fe2462599c1c54e0 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Thu, 7 Sep 2023 16:53:41 +0200 Subject: Reorganize everything --- home/neovim/default.nix | 27 ++++++++++++ home/neovim/filetype.nix | 20 +++++++++ home/neovim/lsp.nix | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 home/neovim/default.nix create mode 100644 home/neovim/filetype.nix create mode 100644 home/neovim/lsp.nix (limited to 'home/neovim') diff --git a/home/neovim/default.nix b/home/neovim/default.nix new file mode 100644 index 0000000..883c170 --- /dev/null +++ b/home/neovim/default.nix @@ -0,0 +1,27 @@ +# This file contains the HM configuration options for Neovim. + +{ pkgs, lib, ... }: + +{ + imports = + [ + ./lsp.nix + ./filetype.nix + ]; + + programs.neovim = { + enable = true; + + # Typing `vi`, `vim`, or `vimdiff` will also run neovim. + viAlias = true; + vimAlias = true; + vimdiffAlias = true; + }; + + # Set Neovim as the default editor. + home.sessionVariables.EDITOR = "nvim"; + home.sessionVariables.VISUAL = "nvim"; + + # Use neovim as man pager. + home.sessionVariables.MANPAGER = "nvim +Man!"; +} diff --git a/home/neovim/filetype.nix b/home/neovim/filetype.nix new file mode 100644 index 0000000..66d5e68 --- /dev/null +++ b/home/neovim/filetype.nix @@ -0,0 +1,20 @@ +# This module configures various syntax/filetype plugins for Neovim. + +{ pkgs, ... }: + +let + vim-noweb = pkgs.vimUtils.buildVimPlugin { + pname = "vim-noweb"; + version = "26-08-2023"; # day of retrieval + src = pkgs.fetchzip { + url = "https://metaed.com/papers/vim-noweb/vim-noweb.tgz"; + hash = "sha256-c5eUZiKIjAfjJ33l821h5DjozMpMf0CaK03QIkSUfxg="; + }; + }; +in +{ + programs.neovim.plugins = with pkgs.vimPlugins; [ + vim-nix + vim-noweb + ]; +} diff --git a/home/neovim/lsp.nix b/home/neovim/lsp.nix new file mode 100644 index 0000000..588fb8b --- /dev/null +++ b/home/neovim/lsp.nix @@ -0,0 +1,106 @@ +# This module sets up LSP server configurations for Neovim. It is waaay +# overcomplicated as it kind of turned into an experiment in generating Lua +# code from a Nix attrset. + +{ pkgs, lib, ... }: + +{ + programs.neovim.plugins = [ + { + plugin = pkgs.vimPlugins.nvim-lspconfig; + type = "lua"; + config = '' + local lspconfig = require("lspconfig") + local util = require("lspconfig.util") + + -- Mappings. + -- See `:help vim.diagnostic.*` for documentation on any of the below functions + local opts = { noremap=true, silent=true } + vim.keymap.set('n', 'e', vim.diagnostic.open_float, opts) + vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts) + vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts) + vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) + + -- Use an on_attach function to only map the following keys + -- after the language server attaches to the current buffer + local on_attach = function(client, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + + -- Mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local bufopts = { noremap=true, silent=true, buffer=bufnr } + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) + vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) + vim.keymap.set('n', '', vim.lsp.buf.signature_help, bufopts) + vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, bufopts) + vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, bufopts) + vim.keymap.set('n', 'wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, bufopts) + vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, bufopts) + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, bufopts) + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, bufopts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) + vim.keymap.set('n', 'f', function() vim.lsp.buf.format { async = true } end, bufopts) + vim.keymap.set('n', 's', function() vim.cmd[[ClangdSwitchSourceHeader]] end, bufopts) + end + + -- Use a loop to conveniently call 'setup' on multiple servers and + -- map buffer local keybindings when the language server attaches + local servers = { + pyright = { cmd = { "${pkgs.pyright}/bin/pyright-langserver" } }, + rnix = { cmd = { "${pkgs.rnix-lsp}/bin/rnix-lsp" } }, + denols = { + init_options = { + enable = true, + unstable = true, + lint = true, + }, + cmd = { "${pkgs.deno}/bin/deno", "lsp", "--unstable" }, + root_dir = function(startpath) + if util.find_package_json_ancestor(startpath) then + -- This is a Node project; let tsserver handle this one. + return nil + else + -- Otherwise, we try to find the root or + -- default to the current directory. + return util.root_pattern("deno.json", "deno.jsonc", ".git")(startpath) + or util.path.dirname(startpath) + end + end, + }, + }; + for server, config in pairs(servers) do + # set common options + config.on_attach = on_attach; + config.debounce_text_changes = 150; + + lspconfig[server].setup(config) + end + ''; + } + ]; +} + +# I spent like an hour writing this, only to find it was a pretty bad idea. +# +# nixToLua = s: +# if builtins.isAttrs s then +# let +# renderAttr = name: value: "[ [==========[" + name + "]==========] ] = " + (nixToLua value); +# attrsList = map (name: renderAttr name s.${name}) (lib.attrNames s); +# attrsListStr = lib.concatStringsSep ", " attrsList; +# in +# "{ ${attrsListStr} }" +# else if builtins.isList s then +# "{ " + (lib.concatStringsSep ", " (map nixToLua s)) + " }" +# else if builtins.isString s then +# # Oh boy I sure hope `s` doesn't contain "]==========]". +# "[==========[" + s + "]==========]" +# else if builtins.isInt s || builtins.isFloat s then +# toString s +# else +# throw "Cannot convert ${builtins.typeOf s} to Lua value!"; -- cgit v1.2.3