Nix-Configs/configs/programs/neovim.nix
2023-12-22 19:37:27 -05:00

277 lines
5.6 KiB
Nix

with builtins;
{pkgs, lib}:
let
mkSettings = s:
concatStringsSep
"\n"
(attrValues
(mapAttrs
(k: v:
if typeOf v == "bool" then
if v then "set ${k}" else "set no${k}"
else
"set ${k}=${toString v}")
s
)
);
boolToString = b: if b then "true" else "false";
toLuaTable = as:
"{" +
( concatStringsSep
", "
( filter
(attr: !isNull attr)
( attrValues
( mapAttrs
(k: v: if v then "${k}=true" else null)
as
)
)
)
) +
"}";
mkMapping = {
mode,
binding,
command,
nowait?false,
silent?false,
unique?false,
expr?false
}:
"vim.api.nvim_set_keymap(${toJSON mode}, ${toJSON binding}, ${toJSON command}, ${toLuaTable {inherit nowait silent unique expr;}})";
mkMappings =
m:
"lua << END-OF-KEYBINDS\n" +
(concatStringsSep "\n" (map mkMapping m)) +
"\nEND-OF-KEYBINDS";
mkRuntimes = rs:
if rs == [] then "" else
"set runtimepath+=" +
( concatStringsSep
","
(map (rt: "${rt}") rs)
);
mkDigraph = {keys, code}: "dig " + keys + " " + (toString code) + "\n";
mkDigraphs = dgs: (concatStringsSep "" (map mkDigraph dgs));
mkVimHeader = h: "\"\"\"\"\"\"\"\" ${h} \"\"\"\"\"\"\"\"\"";
mkVimSection = section:
(concatStringsSep "\n\n\n"
(filter
(v: !isNull v)
(attrValues
(mapAttrs
(k: v: if "" == v then null else (mkVimHeader k) + "\n" + v)
section
)
)
)
);
mkVimSections = sections: concatStringsSep "\n\n\n" (map mkVimSection sections);
mkConfig = { settings?{}, mappings?[], digraphs?[], runtimes?[], extra?"" }:
mkVimSections [
{"ADDITIONAL RUNTIME DIRS" = mkRuntimes runtimes;}
{"SETTINGS" = mkSettings settings;}
{"DIGRAPHS" = mkDigraphs digraphs;}
{"KEY BINGINGS" = mkMappings mappings;}
{"MANUAL SETTINGS" = extra;}
];
# Via https://gist.github.com/nat-418/d76586da7a5d113ab90578ed56069509
fromGitHub = ref: repo: pkgs.vimUtils.buildVimPlugin {
pname = "${lib.strings.sanitizeDerivationName repo}";
version = ref;
src = builtins.fetchGit {
url = "https://github.com/${repo}.git";
ref = ref;
};
};
in
{
enable = true;
plugins = with pkgs.vimPlugins; [
editorconfig-nvim
vim-airline
vim-airline-themes
lsp_signature-nvim
nvim-colorizer-lua
nnn-vim
nvim-surround
hop-nvim
nvim-treesitter.withAllGrammars
nvim-ts-rainbow
nvim-lspconfig
(fromGitHub "HEAD" "whonore/Coqtail")
];
extraConfig = mkConfig {
settings = {
number = true;
expandtab = false;
linebreak = true;
display = "lastline";
comments = "s1:/*,mb:*,ex:*/,:///\\ #,:///,://!,://,b:#,:%,:XCOMM,n:>,b:-,b:*";
tgc = true;
tw = 90;
foldmethod = "expr";
foldexpr = "nvim_treesitter#foldexpr()";
foldminlines = 10;
foldnestmax = 2;
};
digraphs = [
{ # Append
keys = "<>";
code = 8853;
}
{ # Multi Map
keys = "-o";
code = 8888;
}
{ # Degree Symbol
keys = "'o";
code = 176;
}
{ # Ring Operator
keys = ".o";
code = 8728;
}
{ # Integers
keys = "ZZ";
code = 8484;
}
{ # Naturals
keys = "NN";
code = 8469;
}
{ # Reals
keys = "RR";
code = 8477;
}
{ # Rationals
keys = "QQ";
code = 8474;
}
];
mappings = [
{ # New tab
mode = "";
binding = "<C-t>";
command = "<Cmd>:tab split<CR>";
}
{ # File picker
mode = "n";
binding = "<C-n>";
command = "<cmd>call nnn#pick(expand('%:p:h'))<CR>";
}
{ # Jump
mode = "n";
binding = "<C-h>";
command = "<cmd>HopWord<CR>";
}
{ # Jump by characters
mode = "n";
binding = "<C-o>";
command = "<cmd>HopChar2<CR>";
}
{ # Clear search
mode = "";
binding = "<C-l>";
command = "<cmd>noh<CR>";
}
{
mode = "i";
binding = "<Tab>";
command = "coc#pum#visible() ? coc#pum#next(1) : CheckBackspace() ? \"\\<Tab>\" : coc#refresh()";
silent = true;
expr = true;
}
{
mode = "i";
binding = "<S-Tab>";
command = "coc#pum#visible() ? coc#pum#prev(1) : \"\\<C-h>\"";
expr = true;
}
{
mode = "n";
binding = "<C-Space>";
command = "coc#refresh()";
silent = true;
}
{
mode = "n";
binding = "[g";
command = "<Plug>(coc-diagnostic-prev)";
silent = true;
}
{
mode = "n";
binding = "]g";
command = "<Plug>(coc-diagnostic-next)";
silent = true;
}
{
mode = "n";
binding = "gd";
command = "<Plug>(coc-definition)";
silent = true;
}
{
mode = "n";
binding = "gy";
command = "<Plug>(coc-type-definition)";
silent = true;
}
{
mode = "n";
binding = "gi";
command = "<Plug>(coc-implementation)";
silent = true;
}
{
mode = "n";
binding = "gr";
command = "<Plug>(coc-references)";
silent = true;
}
{
mode = "n";
binding = "K";
command = ":call ShowDocumentation()<CR>";
silent = true;
}
{
mode = "n";
binding = "...";
command = "<Plug>(coc-codeaction-cursor)";
silent = true;
}
{
mode = "n";
binding = "<Space>rn";
command = "<Plug>(coc-rename)";
silent = true;
}
{
mode = "n";
binding = "<Space>cl";
command = "<Plug>(coc-codelens-action)";
silent = true;
}
];
runtimes = [neovim/laserwave];
extra = builtins.readFile neovim/init.vim;
};
coc.enable = true;
coc.settings = {
"diagnostic-languageserver.filetypes" = {
python = "mypy";
};
haskell = {
command = "haskell-language-server-wrapper";
args = ["--lsp"];
rootPatterns = ["*.cabal" "stack.yaml" "cabal.project" "package.yaml" "hie.yaml"];
filetypes = ["hs" "lhs" "haskell" "lhaskell"];
};
};
}