Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Keymap Configurable #38

Closed
wants to merge 11 commits into from
248 changes: 148 additions & 100 deletions modules/completion/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,107 +85,155 @@ in {
};
};

config = mkIf cfg.enable {
vim.startPlugins = [
"nvim-cmp"
"cmp-buffer"
"cmp-vsnip"
"cmp-path"
];

vim.autocomplete.sources = {
"nvim-cmp" = null;
"vsnip" = "[VSnip]";
"buffer" = "[Buffer]";
"crates" = "[Crates]";
"path" = "[Path]";
config = let
mkCmpAction = nvim.keymap.mkAction "cmp";
actions = {
docsDown = mkCmpAction "cmp.mapping.scroll_docs(-4)";
docsUp = mkCmpAction "cmp.mapping.scroll_docs(4)";
complete = mkCmpAction "cmp.mapping.complete()";
disable = mkCmpAction "cmp.config.disable";
close = mkCmpAction "cmp.mapping.close()";
abort = mkCmpAction "cmp.mapping.abort()";
confirm = mkCmpAction "cmp.mapping.confirm({
select = true,
})";
next = mkCmpAction ''
function (fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end
'';
prev = mkCmpAction ''
function (fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end
'';

# TODO Working title... also should this be seperated somehow?
doTabAction = mkCmpAction ''
function (fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn['vsnip#available'](1) == 1 then
feedkey(" <Plug> vsnip-expand-or-jump ", " ")
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end'';
# TODO Working title... also should this be seperated somehow?
doShiftTabAction = mkCmpAction ''
function (fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn['vsnip#available'](-1) == 1 then
feedkeys(" <Plug> vsnip-jump-prev ", " ")
end
end'';
};

vim.luaConfigRC.completion = mkIf (cfg.type == "nvim-cmp") (dagPlacement ''
local nvim_cmp_menu_map = function(entry, vim_item)
-- name for each source
vim_item.menu = ({
${builtMaps}
})[entry.source.name]
print(vim_item.menu)
return vim_item
end

${optionalString lspkindEnabled ''
lspkind_opts.before = ${cfg.formatting.format}
''}

local has_words_before = function()
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

local feedkey = function(key, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
end

local cmp = require'cmp'
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
sources = {
${builtSources}
},
mapping = {
['<C-d>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c'}),
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c'}),
['<C-y>'] = cmp.config.disable,
['<C-e>'] = cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
}),
['<CR>'] = cmp.mapping.confirm({
select = true,
}),
['<Tab>'] = cmp.mapping(function (fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn['vsnip#available'](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function (fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn['vsnip#available'](-1) == 1 then
feedkeys("<Plug>(vsnip-jump-prev)", "")
end
end, { 'i', 's' })
},
completion = {
completeopt = 'menu,menuone,noinsert',
},
formatting = {
format =
${
if lspkindEnabled
then "lspkind.cmp_format(lspkind_opts)"
else cfg.formatting.format
},
# TODO Create library function to unify this with lsp/default.nix
keymapString = let
singleElement = list:
if length list == 1
then head list
else builtins.throw "Expected single element";
makeString = binding: atoms: ''
['${binding}'] = cmp.mapping({
${
strings.concatStringsSep ",\n" (mapAttrsToList (mode: atoms: "${nvim.keymap.modeChar mode} = ${(singleElement atoms).action}") (groupBy (atom: atom.mode) atoms))
}
})
${optionalString (config.vim.autopairs.enable && config.vim.autopairs.type == "nvim-autopairs") ''
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done({ map_char = { text = ""} }))
''}
'');

vim.snippets.vsnip.enable =
if (cfg.type == "nvim-cmp")
then true
else config.vim.snippets.vsnip.enable;
};
})
'';

atoms = nvim.keymap.keymappingsOfType "cmp" config.nvim-flake.keymappings;

bindingStringList = mapAttrsToList makeString (groupBy (atom: atom.binding) (traceSeq {atoms = atoms;} atoms));
in
strings.concatStringsSep ",\n" bindingStringList;
in
mkIf cfg.enable {
nvim-flake.keymapActions = {autocomplete = actions;};
vim.startPlugins = [
"nvim-cmp"
"cmp-buffer"
"cmp-vsnip"
"cmp-path"
];

vim.autocomplete.sources = {
"nvim-cmp" = null;
"vsnip" = "[VSnip]";
"buffer" = "[Buffer]";
"crates" = "[Crates]";
"path" = "[Path]";
};

vim.luaConfigRC.completion = mkIf (cfg.type == "nvim-cmp") (dagPlacement ''
local nvim_cmp_menu_map = function(entry, vim_item)
-- name for each source
vim_item.menu = ({
${builtMaps}
})[entry.source.name]
print(vim_item.menu)
return vim_item
end

${optionalString lspkindEnabled ''
lspkind_opts.before = ${cfg.formatting.format}
''}

local has_words_before = function()
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

local feedkey = function(key, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
end

local cmp = require'cmp'
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
sources = {
${builtSources}
},
mapping = {
${keymapString}
},
completion = {
completeopt = 'menu,menuone,noinsert',
},
formatting = {
format =
${
if lspkindEnabled
then "lspkind.cmp_format(lspkind_opts)"
else cfg.formatting.format
},
}
})
${optionalString (config.vim.autopairs.enable && config.vim.autopairs.type == "nvim-autopairs") ''
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done({ map_char = { text = ""} }))
''}
'');

vim.snippets.vsnip.enable =
if (cfg.type == "nvim-cmp")
then true
else config.vim.snippets.vsnip.enable;
};
}
107 changes: 56 additions & 51 deletions modules/filetree/nvimtreelua.nix
Original file line number Diff line number Diff line change
Expand Up @@ -128,61 +128,66 @@ in {
description = "The command used to open a file with the associated default program";
type = types.str;
};

keymap = nvim.keymap.mkKeymapOptions;
};

config = mkIf cfg.enable {
vim.startPlugins = ["nvim-tree-lua"];

vim.nnoremap = {
"<C-n>" = ":NvimTreeToggle<CR>";
"<leader>tr" = ":NvimTreeRefresh<CR>";
"<leader>tg" = ":NvimTreeFindFile<CR>";
"<leader>tf" = ":NvimTreeFocus<CR>";
};

vim.luaConfigRC.nvimtreelua = nvim.dag.entryAnywhere ''
require'nvim-tree'.setup({
disable_netrw = ${boolToString cfg.disableNetRW},
hijack_netrw = ${boolToString cfg.hijackNetRW},
system_open = {
cmd = ${"'" + cfg.systemOpenCmd + "'"},
},
diagnostics = {
enable = ${boolToString cfg.lspDiagnostics},
},
view = {
width = ${toString cfg.treeWidth},
side = ${"'" + cfg.treeSide + "'"},
},
tab = {
sync = {
open = ${boolToString cfg.openTreeOnNewTab}
config = let
actions = with nvim.keymap; {
toggle = mkVimAction ":NvimTreeToggle<CR>";
refresh = mkVimAction ":NvimTreeRefresh<CR>";
findFile = mkVimAction ":NvimTreeFindFile<CR>";
findFileToggle = mkVimAction ":NvimTreeFindFileToggle<CR>";
focus = mkVimAction ":NvimTreeFocus<CR>";
};
in
mkIf cfg.enable {
nvim-flake.keymapActions = {nvimTreeLua = actions;};
vim.startPlugins = ["nvim-tree-lua"];

vim.luaConfigRC.nvimtreelua = nvim.dag.entryAnywhere ''
require'nvim-tree'.setup({
disable_netrw = ${boolToString cfg.disableNetRW},
hijack_netrw = ${boolToString cfg.hijackNetRW},
system_open = {
cmd = ${"'" + cfg.systemOpenCmd + "'"},
},
},
renderer = {
indent_markers = {
enable = ${boolToString cfg.indentMarkers},
diagnostics = {
enable = ${boolToString cfg.lspDiagnostics},
},
add_trailing = ${boolToString cfg.trailingSlash},
group_empty = ${boolToString cfg.groupEmptyFolders},
},
actions = {
open_file = {
quit_on_open = ${boolToString cfg.closeOnFileOpen},
resize_window = ${boolToString cfg.resizeOnFileOpen},
view = {
width = ${toString cfg.treeWidth},
side = ${"'" + cfg.treeSide + "'"},
},
},
git = {
enable = true,
ignore = ${boolToString cfg.hideIgnoredGitFiles},
},
filters = {
dotfiles = ${boolToString cfg.hideDotFiles},
custom = {
${builtins.concatStringsSep "\n" (builtins.map (s: "\"" + s + "\",") cfg.hideFiles)}
tab = {
sync = {
open = ${boolToString cfg.openTreeOnNewTab}
},
},
},
})
'';
};
renderer = {
indent_markers = {
enable = ${boolToString cfg.indentMarkers},
},
add_trailing = ${boolToString cfg.trailingSlash},
group_empty = ${boolToString cfg.groupEmptyFolders},
},
actions = {
open_file = {
quit_on_open = ${boolToString cfg.closeOnFileOpen},
resize_window = ${boolToString cfg.resizeOnFileOpen},
},
},
git = {
enable = true,
ignore = ${boolToString cfg.hideIgnoredGitFiles},
},
filters = {
dotfiles = ${boolToString cfg.hideDotFiles},
custom = {
${builtins.concatStringsSep "\n" (builtins.map (s: "\"" + s + "\",") cfg.hideFiles)}
},
},
})
'';
};
}
Loading