2023-02-20 11:42:13 +01:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
} @ args:
|
|
|
|
with lib; let
|
2022-09-18 11:19:23 +01:00
|
|
|
cfg = config.plugins.nvim-cmp;
|
2023-02-20 11:42:13 +01:00
|
|
|
helpers = import ../../helpers.nix {inherit lib;};
|
2022-07-28 21:38:38 +02:00
|
|
|
mkNullOrOption = helpers.mkNullOrOption;
|
|
|
|
cmpLib = import ./cmp-helpers.nix args;
|
|
|
|
# functionName should be a string
|
|
|
|
# parameters should be a list of strings
|
2023-02-20 11:42:13 +01:00
|
|
|
wrapWithFunction = functionName: parameters: let
|
|
|
|
parameterString = strings.concatStringsSep "," parameters;
|
|
|
|
in ''${functionName}(${parameterString})'';
|
|
|
|
in {
|
2022-09-18 11:19:23 +01:00
|
|
|
options.plugins.nvim-cmp = {
|
2023-01-22 03:32:08 +00:00
|
|
|
enable = mkEnableOption "nvim-cmp";
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-01-25 19:46:49 +01:00
|
|
|
package = helpers.mkPackageOption "nvim-cmp" pkgs.vimPlugins.nvim-cmp;
|
2023-01-19 10:45:15 +00:00
|
|
|
|
2022-07-28 21:38:38 +02:00
|
|
|
performance = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
debounce = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
throttle = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
preselect = mkOption {
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.enum ["Item" "None"]);
|
2022-07-28 21:38:38 +02:00
|
|
|
default = null;
|
|
|
|
example = ''"Item"'';
|
|
|
|
};
|
|
|
|
|
|
|
|
snippet = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
expand = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
example = ''
|
|
|
|
function(args)
|
|
|
|
vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
|
|
|
|
-- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
|
|
|
|
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
|
|
|
|
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
|
|
|
|
end
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
mappingPresets = mkOption {
|
2023-02-20 11:42:13 +01:00
|
|
|
default = [];
|
2022-07-28 21:38:38 +02:00
|
|
|
type = types.listOf (types.enum [
|
2022-08-05 15:01:10 +01:00
|
|
|
"insert"
|
|
|
|
"cmdline"
|
|
|
|
# Not sure if there are more or if this should just be str
|
2022-07-28 21:38:38 +02:00
|
|
|
]);
|
|
|
|
description = "Mapping presets to use; cmp.mapping.preset.\${mappingPreset} will be called with the configured mappings";
|
|
|
|
example = ''
|
|
|
|
[ "insert" "cmdline" ]
|
|
|
|
'';
|
|
|
|
};
|
2022-08-05 15:01:10 +01:00
|
|
|
|
2022-07-28 21:38:38 +02:00
|
|
|
mapping = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.attrsOf (types.either types.str (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
action = mkOption {
|
|
|
|
type = types.nonEmptyStr;
|
|
|
|
description = "The function the mapping should call";
|
|
|
|
example = ''"cmp.mapping.scroll_docs(-4)"'';
|
|
|
|
};
|
|
|
|
modes = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
example = ''[ "i" "s" ]'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}))));
|
|
|
|
example = ''
|
|
|
|
{
|
|
|
|
"<CR>" = "cmp.mapping.confirm({ select = true })";
|
|
|
|
"<Tab>" = {
|
|
|
|
modes = [ "i" "s" ];
|
|
|
|
action = '${""}'
|
|
|
|
function(fallback)
|
|
|
|
if cmp.visible() then
|
|
|
|
cmp.select_next_item()
|
|
|
|
elseif luasnip.expandable() then
|
|
|
|
luasnip.expand()
|
|
|
|
elseif luasnip.expand_or_jumpable() then
|
|
|
|
luasnip.expand_or_jump()
|
|
|
|
elseif check_backspace() then
|
|
|
|
fallback()
|
|
|
|
else
|
|
|
|
fallback()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
'${""}';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
completion = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
keyword_length = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
|
|
|
|
|
|
|
keyword_pattern = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
autocomplete = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "Lua code for the event.";
|
|
|
|
example = ''"false"'';
|
|
|
|
};
|
|
|
|
|
|
|
|
completeopt = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
confirmation = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
get_commit_characters = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "Direct lua code as a string";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
formatting = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
fields = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
example = ''[ "kind" "abbr" "menu" ]'';
|
|
|
|
};
|
|
|
|
format = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "A lua function as a string";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
matching = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
disallow_fuzzy_matching = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.bool;
|
|
|
|
};
|
|
|
|
disallow_partial_matching = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.bool;
|
|
|
|
};
|
|
|
|
disallow_prefix_unmatching = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.bool;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
sorting = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
priority_weight = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
|
|
|
comparators = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
auto_enable_sources = mkOption {
|
2023-01-21 18:12:32 +01:00
|
|
|
type = types.bool;
|
2022-07-28 21:38:38 +02:00
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Scans the sources array and installs the plugins if they are known to nixvim.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
sources = let
|
|
|
|
source_config = types.submodule ({...}: {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The name of the source.";
|
|
|
|
example = ''"buffer"'';
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
option = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (attrsOf anything);
|
|
|
|
description = "If direct lua code is needed use helpers.mkRaw";
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
keyword_length = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
keyword_pattern = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
trigger_characters = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (listOf str);
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
priority = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
max_item_count = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
group_index = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.int;
|
2022-07-28 21:38:38 +02:00
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
in
|
2022-08-05 15:01:10 +01:00
|
|
|
mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (either (listOf source_config) (listOf (listOf source_config)));
|
|
|
|
description = ''
|
|
|
|
The sources to use.
|
|
|
|
Can either be a list of sourceConfigs which will be made directly to a Lua object.
|
|
|
|
Or it can be a list of lists, which will use the cmp built-in helper function `cmp.config.sources`.
|
|
|
|
'';
|
|
|
|
example = ''
|
|
|
|
[
|
|
|
|
{ name = "nvim_lsp"; }
|
|
|
|
{ name = "luasnip"; } #For luasnip users.
|
|
|
|
{ name = "path"; }
|
|
|
|
{ name = "buffer"; }
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
|
|
|
view = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-07-28 21:38:38 +02:00
|
|
|
options = {
|
|
|
|
entries = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr (either str attrs);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
window = let
|
|
|
|
# Reusable options
|
|
|
|
border = with types; mkNullOrOption (either str (listOf str)) null;
|
|
|
|
winhighlight = mkNullOrOption types.str null;
|
|
|
|
zindex = mkNullOrOption types.int null;
|
|
|
|
in
|
2022-08-05 15:01:10 +01:00
|
|
|
mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-08-05 15:01:10 +01:00
|
|
|
options = {
|
|
|
|
completion = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-08-05 15:01:10 +01:00
|
|
|
options = {
|
2022-11-13 14:29:15 +00:00
|
|
|
inherit border winhighlight zindex;
|
|
|
|
col_offset = mkNullOrOption types.int "Offsets the completion window relative to the cursor";
|
|
|
|
side_padding = mkNullOrOption types.int "The amount of padding to add on the completion window's sides";
|
2022-08-05 15:01:10 +01:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2022-08-05 15:01:10 +01:00
|
|
|
documentation = mkOption {
|
|
|
|
default = null;
|
2023-02-20 11:42:13 +01:00
|
|
|
type = types.nullOr (types.submodule ({...}: {
|
2022-08-05 15:01:10 +01:00
|
|
|
options = {
|
|
|
|
inherit border winhighlight zindex;
|
|
|
|
max_width = mkNullOrOption types.int "Window's max width";
|
|
|
|
max_height = mkNullOrOption types.int "Window's max height";
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
};
|
2022-08-05 15:01:10 +01:00
|
|
|
}));
|
|
|
|
};
|
2022-07-28 21:38:38 +02:00
|
|
|
|
|
|
|
# This can be kept as types.attrs since experimental features are often removed or completely changed after a while
|
|
|
|
experimental = mkNullOrOption types.attrs "Experimental features";
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
config = let
|
|
|
|
options = {
|
|
|
|
enabled = cfg.enable;
|
|
|
|
performance = cfg.performance;
|
|
|
|
preselect =
|
|
|
|
if (isNull cfg.preselect)
|
|
|
|
then null
|
|
|
|
else helpers.mkRaw "cmp.PreselectMode.${cfg.preselect}";
|
|
|
|
|
|
|
|
# Not very readable sorry
|
|
|
|
# If null then null
|
|
|
|
# If an attribute is a string, just treat it as lua code for that mapping
|
|
|
|
# If an attribute is a module, create a mapping with cmp.mapping() using the action as the first input and the modes as the second.
|
|
|
|
mapping = let
|
|
|
|
mappings =
|
|
|
|
if (isNull cfg.mapping)
|
|
|
|
then null
|
|
|
|
else
|
|
|
|
mapAttrs
|
|
|
|
(bind: mapping:
|
|
|
|
helpers.mkRaw (
|
|
|
|
if isString mapping
|
|
|
|
then mapping
|
|
|
|
else "cmp.mapping(${mapping.action}${optionalString (mapping.modes != null && length mapping.modes >= 1) ("," + (helpers.toLuaObject mapping.modes))})"
|
|
|
|
))
|
|
|
|
cfg.mapping;
|
|
|
|
luaMappings = helpers.toLuaObject mappings;
|
|
|
|
wrapped = lists.fold (presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})'') luaMappings cfg.mappingPresets;
|
|
|
|
in
|
|
|
|
helpers.mkRaw wrapped;
|
2022-08-05 15:01:10 +01:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
snippet = {
|
|
|
|
expand =
|
|
|
|
if (isNull cfg.snippet || isNull cfg.snippet.expand)
|
|
|
|
then null
|
|
|
|
else helpers.mkRaw cfg.snippet.expand;
|
|
|
|
};
|
|
|
|
|
|
|
|
completion =
|
|
|
|
if (isNull cfg.completion)
|
|
|
|
then null
|
|
|
|
else {
|
2022-08-05 15:01:10 +01:00
|
|
|
keyword_length = cfg.completion.keyword_length;
|
|
|
|
keyword_pattern = cfg.completion.keyword_pattern;
|
2023-02-20 11:42:13 +01:00
|
|
|
autocomplete =
|
|
|
|
if (isNull cfg.completion.autocomplete)
|
|
|
|
then null
|
|
|
|
else mkRaw cfg.completion.autocomplete;
|
2022-08-05 15:01:10 +01:00
|
|
|
completeopt = cfg.completion.completeopt;
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
confirmation =
|
|
|
|
if (isNull cfg.confirmation)
|
|
|
|
then null
|
|
|
|
else {
|
2022-08-05 15:01:10 +01:00
|
|
|
get_commit_characters =
|
2023-02-20 11:42:13 +01:00
|
|
|
if (isString cfg.confirmation.get_commit_characters)
|
|
|
|
then helpers.mkRaw cfg.confirmation.get_commit_characters
|
2022-08-05 15:01:10 +01:00
|
|
|
else cfg.confirmation.get_commit_characters;
|
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
formatting =
|
|
|
|
if (isNull cfg.formatting)
|
|
|
|
then null
|
|
|
|
else {
|
2022-08-05 15:01:10 +01:00
|
|
|
fields = cfg.formatting.fields;
|
2023-02-20 11:42:13 +01:00
|
|
|
format =
|
|
|
|
if (isNull cfg.formatting.format)
|
|
|
|
then null
|
|
|
|
else helpers.mkRaw cfg.formatting.format;
|
2022-08-05 15:01:10 +01:00
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
matching = cfg.matching;
|
2022-08-05 15:01:10 +01:00
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
sorting =
|
|
|
|
if (isNull cfg.sorting)
|
|
|
|
then null
|
|
|
|
else {
|
2022-08-05 15:01:10 +01:00
|
|
|
priority_weight = cfg.sorting.priority_weight;
|
2023-02-20 11:42:13 +01:00
|
|
|
comparators =
|
|
|
|
if (isNull cfg.sorting.comparators)
|
|
|
|
then null
|
|
|
|
else helpers.mkRaw cfg.sorting.comparators;
|
2022-08-05 15:01:10 +01:00
|
|
|
};
|
|
|
|
|
2023-02-20 11:42:13 +01:00
|
|
|
sources = cfg.sources;
|
|
|
|
view = cfg.view;
|
|
|
|
window = cfg.window;
|
|
|
|
experimental = cfg.experimental;
|
|
|
|
};
|
|
|
|
in
|
2022-08-05 15:01:10 +01:00
|
|
|
mkIf cfg.enable {
|
2023-02-20 11:42:13 +01:00
|
|
|
extraPlugins = [cfg.package];
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2022-10-25 01:17:02 +02:00
|
|
|
extraConfigLua = helpers.wrapDo ''
|
2022-09-18 11:19:23 +01:00
|
|
|
local cmp = require('cmp')
|
|
|
|
cmp.setup(${helpers.toLuaObject options})
|
|
|
|
'';
|
2022-07-28 21:38:38 +02:00
|
|
|
|
2022-09-18 11:19:23 +01:00
|
|
|
# If auto_enable_sources is set to true, figure out which are provided by the user
|
|
|
|
# and enable the corresponding plugins.
|
2023-02-20 11:42:13 +01:00
|
|
|
plugins = let
|
|
|
|
flattened_sources =
|
|
|
|
if (isNull cfg.sources)
|
|
|
|
then []
|
|
|
|
else flatten cfg.sources;
|
|
|
|
# Take only the names from the sources provided by the user
|
|
|
|
found_sources = lists.unique (lists.map (source: source.name) flattened_sources);
|
|
|
|
# A list of known source names
|
|
|
|
known_source_names = attrNames cmpLib.pluginAndSourceNames;
|
|
|
|
|
|
|
|
attrs_enabled = listToAttrs (map
|
|
|
|
(name: {
|
|
|
|
name = cmpLib.pluginAndSourceNames.${name};
|
|
|
|
value.enable = mkIf (elem name found_sources) true;
|
|
|
|
})
|
|
|
|
known_source_names);
|
|
|
|
in
|
2022-09-18 11:19:23 +01:00
|
|
|
mkIf cfg.auto_enable_sources attrs_enabled;
|
2022-07-28 21:38:38 +02:00
|
|
|
};
|
|
|
|
}
|