mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 08:35:43 +02:00
nvim-cmp: fix snippet not being able to be null
This commit is contained in:
parent
5cb317a5cb
commit
70dd3446fb
1 changed files with 192 additions and 166 deletions
|
@ -7,9 +7,11 @@ let
|
||||||
cmpLib = import ./cmp-helpers.nix args;
|
cmpLib = import ./cmp-helpers.nix args;
|
||||||
# functionName should be a string
|
# functionName should be a string
|
||||||
# parameters should be a list of strings
|
# parameters should be a list of strings
|
||||||
wrapWithFunction = functionName: parameters: let
|
wrapWithFunction = functionName: parameters:
|
||||||
parameterString = strings.concatStringsSep "," parameters;
|
let
|
||||||
in ''${functionName}(${parameterString})'';
|
parameterString = strings.concatStringsSep "," parameters;
|
||||||
|
in
|
||||||
|
''${functionName}(${parameterString})'';
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.programs.nixvim.plugins.nvim-cmp = {
|
options.programs.nixvim.plugins.nvim-cmp = {
|
||||||
|
@ -17,7 +19,7 @@ in
|
||||||
|
|
||||||
performance = mkOption {
|
performance = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
debounce = mkOption {
|
debounce = mkOption {
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
|
@ -39,7 +41,7 @@ in
|
||||||
|
|
||||||
snippet = mkOption {
|
snippet = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
expand = mkOption {
|
expand = mkOption {
|
||||||
type = types.nullOr types.str;
|
type = types.nullOr types.str;
|
||||||
|
@ -57,20 +59,21 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
mappingPresets = mkOption {
|
mappingPresets = mkOption {
|
||||||
default = [];
|
default = [ ];
|
||||||
type = types.listOf (types.enum [
|
type = types.listOf (types.enum [
|
||||||
"insert"
|
"insert"
|
||||||
"cmdline"
|
"cmdline"
|
||||||
# Not sure if there are more or if this should just be str
|
# Not sure if there are more or if this should just be str
|
||||||
]);
|
]);
|
||||||
description = "Mapping presets to use; cmp.mapping.preset.\${mappingPreset} will be called with the configured mappings";
|
description = "Mapping presets to use; cmp.mapping.preset.\${mappingPreset} will be called with the configured mappings";
|
||||||
example = ''
|
example = ''
|
||||||
[ "insert" "cmdline" ]
|
[ "insert" "cmdline" ]
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
mapping = mkOption {
|
mapping = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.attrsOf (types.either types.str (types.submodule ({...}: {
|
type = types.nullOr (types.attrsOf (types.either types.str (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
action = mkOption {
|
action = mkOption {
|
||||||
type = types.nonEmptyStr;
|
type = types.nonEmptyStr;
|
||||||
|
@ -111,7 +114,7 @@ in
|
||||||
|
|
||||||
completion = mkOption {
|
completion = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
keyword_length = mkOption {
|
keyword_length = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -140,7 +143,7 @@ in
|
||||||
|
|
||||||
confirmation = mkOption {
|
confirmation = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
get_commit_characters = mkOption {
|
get_commit_characters = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -153,7 +156,7 @@ in
|
||||||
|
|
||||||
formatting = mkOption {
|
formatting = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
fields = mkOption {
|
fields = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -171,7 +174,7 @@ in
|
||||||
|
|
||||||
matching = mkOption {
|
matching = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
disallow_fuzzy_matching = mkOption {
|
disallow_fuzzy_matching = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -191,7 +194,7 @@ in
|
||||||
|
|
||||||
sorting = mkOption {
|
sorting = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
priority_weight = mkOption {
|
priority_weight = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -212,73 +215,75 @@ in
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
sources = let
|
sources =
|
||||||
source_config = types.submodule ({...}: {
|
let
|
||||||
options = {
|
source_config = types.submodule ({ ... }: {
|
||||||
name = mkOption {
|
options = {
|
||||||
type = types.str;
|
name = mkOption {
|
||||||
description = "The name of the source.";
|
type = types.str;
|
||||||
example = ''"buffer"'';
|
description = "The name of the source.";
|
||||||
};
|
example = ''"buffer"'';
|
||||||
|
};
|
||||||
|
|
||||||
option = mkOption {
|
option = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = with types; nullOr (attrsOf anything);
|
type = with types; nullOr (attrsOf anything);
|
||||||
description = "If direct lua code is needed use helpers.mkRaw";
|
description = "If direct lua code is needed use helpers.mkRaw";
|
||||||
};
|
};
|
||||||
|
|
||||||
keyword_length = mkOption {
|
keyword_length = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
};
|
};
|
||||||
|
|
||||||
keyword_pattern = mkOption {
|
keyword_pattern = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
};
|
};
|
||||||
|
|
||||||
trigger_characters = mkOption {
|
trigger_characters = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = with types; nullOr (listOf str);
|
type = with types; nullOr (listOf str);
|
||||||
};
|
};
|
||||||
|
|
||||||
priority = mkOption {
|
priority = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
};
|
};
|
||||||
|
|
||||||
max_item_count = mkOption {
|
max_item_count = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
};
|
};
|
||||||
|
|
||||||
group_index = mkOption {
|
group_index = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr types.int;
|
type = types.nullOr types.int;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
});
|
||||||
});
|
in
|
||||||
in mkOption {
|
mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = with types; nullOr (either (listOf source_config) (listOf (listOf source_config)));
|
type = with types; nullOr (either (listOf source_config) (listOf (listOf source_config)));
|
||||||
description = ''
|
description = ''
|
||||||
The sources to use.
|
The sources to use.
|
||||||
Can either be a list of sourceConfigs which will be made directly to a Lua object.
|
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`.
|
Or it can be a list of lists, which will use the cmp built-in helper function `cmp.config.sources`.
|
||||||
'';
|
'';
|
||||||
example = ''
|
example = ''
|
||||||
[
|
[
|
||||||
{ name = "nvim_lsp"; }
|
{ name = "nvim_lsp"; }
|
||||||
{ name = "luasnip"; } #For luasnip users.
|
{ name = "luasnip"; } #For luasnip users.
|
||||||
{ name = "path"; }
|
{ name = "path"; }
|
||||||
{ name = "buffer"; }
|
{ name = "buffer"; }
|
||||||
]
|
]
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
view = mkOption {
|
view = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
entries = mkOption {
|
entries = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -288,110 +293,131 @@ in
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
window = let
|
window =
|
||||||
# Reusable options
|
let
|
||||||
border = with types; mkNullOrOption (either str (listOf str)) null;
|
# Reusable options
|
||||||
winhighlight = mkNullOrOption types.str null;
|
border = with types; mkNullOrOption (either str (listOf str)) null;
|
||||||
zindex = mkNullOrOption types.int null;
|
winhighlight = mkNullOrOption types.str null;
|
||||||
in mkOption {
|
zindex = mkNullOrOption types.int null;
|
||||||
default = null;
|
in
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
mkOption {
|
||||||
options = {
|
default = null;
|
||||||
completion = mkOption {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
default = null;
|
options = {
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
completion = mkOption {
|
||||||
options = {
|
default = null;
|
||||||
inherit border winhighlight zindex;
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
};
|
options = {
|
||||||
}));
|
inherit border winhighlight zindex;
|
||||||
};
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
documentation = mkOption {
|
documentation = mkOption {
|
||||||
default = null;
|
default = null;
|
||||||
type = types.nullOr (types.submodule ({...}: {
|
type = types.nullOr (types.submodule ({ ... }: {
|
||||||
options = {
|
options = {
|
||||||
inherit border winhighlight zindex;
|
inherit border winhighlight zindex;
|
||||||
max_width = mkNullOrOption types.int "Window's max width";
|
max_width = mkNullOrOption types.int "Window's max width";
|
||||||
max_height = mkNullOrOption types.int "Window's max height";
|
max_height = mkNullOrOption types.int "Window's max height";
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
}));
|
||||||
}));
|
};
|
||||||
};
|
|
||||||
|
|
||||||
# This can be kept as types.attrs since experimental features are often removed or completely changed after a while
|
# 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";
|
experimental = mkNullOrOption types.attrs "Experimental features";
|
||||||
};
|
};
|
||||||
|
|
||||||
config = let
|
config =
|
||||||
options = {
|
let
|
||||||
enabled = cfg.enable;
|
options = {
|
||||||
performance = cfg.performance;
|
enabled = cfg.enable;
|
||||||
preselect = if (isNull cfg.preselect) then null else helpers.mkRaw "cmp.PreselectMode.${cfg.preselect}";
|
performance = cfg.performance;
|
||||||
|
preselect = if (isNull cfg.preselect) then null else helpers.mkRaw "cmp.PreselectMode.${cfg.preselect}";
|
||||||
|
|
||||||
# Not very readable sorry
|
# Not very readable sorry
|
||||||
# If null then null
|
# If null then null
|
||||||
# If an attribute is a string, just treat it as lua code for that mapping
|
# 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.
|
# 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
|
mapping =
|
||||||
mappings = if (isNull cfg.mapping) then null
|
let
|
||||||
else mapAttrs (bind: mapping: helpers.mkRaw (if isString mapping then mapping
|
mappings =
|
||||||
else "cmp.mapping(${mapping.action}${optionalString (mapping.modes != null && length mapping.modes >= 1) ("," + (helpers.toLuaObject mapping.modes))})")) cfg.mapping;
|
if (isNull cfg.mapping) then null
|
||||||
luaMappings = (helpers.toLuaObject mappings);
|
else
|
||||||
wrapped = lists.fold (presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})'') luaMappings cfg.mappingPresets;
|
mapAttrs
|
||||||
in helpers.mkRaw wrapped;
|
(bind: mapping: helpers.mkRaw (if isString mapping then mapping
|
||||||
snippet = {
|
else "cmp.mapping(${mapping.action}${optionalString (mapping.modes != null && length mapping.modes >= 1) ("," + (helpers.toLuaObject mapping.modes))})"))
|
||||||
expand = if (isNull cfg.snippet.expand) then null else helpers.mkRaw cfg.snippet.expand;
|
cfg.mapping;
|
||||||
|
luaMappings = (helpers.toLuaObject mappings);
|
||||||
|
wrapped = lists.fold (presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})'') luaMappings cfg.mappingPresets;
|
||||||
|
in
|
||||||
|
helpers.mkRaw wrapped;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
keyword_length = cfg.completion.keyword_length;
|
||||||
|
keyword_pattern = cfg.completion.keyword_pattern;
|
||||||
|
autocomplete = if (isNull cfg.completion.autocomplete) then null else mkRaw cfg.completion.autocomplete;
|
||||||
|
completeopt = cfg.completion.completeopt;
|
||||||
|
};
|
||||||
|
|
||||||
|
confirmation = if (isNull cfg.confirmation) then null else {
|
||||||
|
get_commit_characters =
|
||||||
|
if (isString cfg.confirmation.get_commit_characters) then helpers.mkRaw cfg.confirmation.get_commit_characters
|
||||||
|
else cfg.confirmation.get_commit_characters;
|
||||||
|
};
|
||||||
|
|
||||||
|
formatting = if (isNull cfg.formatting) then null else {
|
||||||
|
fields = cfg.formatting.fields;
|
||||||
|
format = if (isNull cfg.formatting.format) then null else helpers.mkRaw cfg.formatting.format;
|
||||||
|
};
|
||||||
|
|
||||||
|
matching = cfg.matching;
|
||||||
|
|
||||||
|
sorting = if (isNull cfg.sorting) then null else {
|
||||||
|
priority_weight = cfg.sorting.priority_weight;
|
||||||
|
comparators = if (isNull cfg.sorting.comparators) then null else helpers.mkRaw cfg.sorting.comparators;
|
||||||
|
};
|
||||||
|
|
||||||
|
sources = cfg.sources;
|
||||||
|
view = cfg.view;
|
||||||
|
window = cfg.window;
|
||||||
|
experimental = cfg.experimental;
|
||||||
};
|
};
|
||||||
completion = if (isNull cfg.completion) then null else {
|
in
|
||||||
keyword_length = cfg.completion.keyword_length;
|
mkIf cfg.enable {
|
||||||
keyword_pattern = cfg.completion.keyword_pattern;
|
programs.nixvim = {
|
||||||
autocomplete = if (isNull cfg.completion.autocomplete) then null else mkRaw cfg.completion.autocomplete;
|
extraPlugins = [ pkgs.vimPlugins.nvim-cmp ];
|
||||||
completeopt = cfg.completion.completeopt;
|
|
||||||
|
extraConfigLua = ''
|
||||||
|
local cmp = require('cmp')
|
||||||
|
cmp.setup(${helpers.toLuaObject options})
|
||||||
|
'';
|
||||||
|
|
||||||
|
# If auto_enable_sources is set to true, figure out which are provided by the user
|
||||||
|
# and enable the corresponding plugins.
|
||||||
|
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
|
||||||
|
mkIf cfg.auto_enable_sources attrs_enabled;
|
||||||
};
|
};
|
||||||
confirmation = if (isNull cfg.confirmation) then null else {
|
|
||||||
get_commit_characters =
|
|
||||||
if (isString cfg.confirmation.get_commit_characters) then helpers.mkRaw cfg.confirmation.get_commit_characters
|
|
||||||
else cfg.confirmation.get_commit_characters;
|
|
||||||
};
|
|
||||||
formatting = if (isNull cfg.formatting) then null else {
|
|
||||||
fields = cfg.formatting.fields;
|
|
||||||
format = if (isNull cfg.formatting.format) then null else helpers.mkRaw cfg.formatting.format;
|
|
||||||
};
|
|
||||||
matching = cfg.matching;
|
|
||||||
sorting = if (isNull cfg.sorting) then null else {
|
|
||||||
priority_weight = cfg.sorting.priority_weight;
|
|
||||||
comparators = if (isNull cfg.sorting.comparators) then null else helpers.mkRaw cfg.sorting.comparators;
|
|
||||||
};
|
|
||||||
sources = cfg.sources;
|
|
||||||
view = cfg.view;
|
|
||||||
window = cfg.window;
|
|
||||||
experimental = cfg.experimental;
|
|
||||||
};
|
};
|
||||||
in mkIf cfg.enable {
|
|
||||||
programs.nixvim = {
|
|
||||||
extraPlugins = [ pkgs.vimPlugins.nvim-cmp ];
|
|
||||||
|
|
||||||
extraConfigLua = ''
|
|
||||||
local cmp = require('cmp')
|
|
||||||
cmp.setup(${helpers.toLuaObject options})
|
|
||||||
'';
|
|
||||||
|
|
||||||
# If auto_enable_sources is set to true, figure out which are provided by the user
|
|
||||||
# and enable the corresponding plugins.
|
|
||||||
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 mkIf cfg.auto_enable_sources attrs_enabled;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue