plugins/cmp: refactoring

This commit is contained in:
Gaetan Lepage 2024-02-01 16:36:09 +01:00 committed by Gaétan Lepage
parent 90fbde275c
commit c10e73fb65
2 changed files with 330 additions and 314 deletions

View file

@ -8,13 +8,6 @@
with lib; let with lib; let
cfg = config.plugins.nvim-cmp; cfg = config.plugins.nvim-cmp;
cmpLib = import ./cmp-helpers.nix args; cmpLib = import ./cmp-helpers.nix args;
snippetEngines = {
"vsnip" = ''vim.fn["vsnip#anonymous"](args.body)'';
"luasnip" = ''require('luasnip').lsp_expand(args.body)'';
"snippy" = ''require('snippy').expand_snippet(args.body)'';
"ultisnips" = ''vim.fn["UltiSnips#Anon"](args.body)'';
};
in { in {
options.plugins.nvim-cmp = options.plugins.nvim-cmp =
helpers.neovim-plugin.extraOptionsOptions helpers.neovim-plugin.extraOptionsOptions
@ -49,16 +42,21 @@ in {
''; '';
}; };
preselect = helpers.defaultNullOpts.mkEnumFirstDefault ["Item" "None"] '' preselect = mkOption {
type = with types; nullOr (enum ["Item" "None"]);
apply = v:
helpers.ifNonNull' v
(helpers.mkRaw "cmp.PreselectMode.${v}");
default = null;
description = ''
- "Item": nvim-cmp will preselect the item that the source specified. - "Item": nvim-cmp will preselect the item that the source specified.
- "None": nvim-cmp will not preselect any items. - "None": nvim-cmp will not preselect any items.
''; '';
};
mapping = mkOption { mapping = mkOption {
default = null; default = {};
type = with types; type = with types;
nullOr
(
attrsOf attrsOf
( (
either either
@ -79,7 +77,6 @@ in {
}; };
} }
) )
)
); );
example = { example = {
"<C-d>" = "cmp.mapping.scroll_docs(-4)"; "<C-d>" = "cmp.mapping.scroll_docs(-4)";
@ -100,10 +97,8 @@ in {
mappingPresets = mkOption { mappingPresets = mkOption {
default = []; default = [];
type = types.listOf (types.enum [ type = with types;
"insert" listOf (enum ["insert" "cmdline"]);
"cmdline"
]);
description = '' description = ''
Mapping presets to use; cmp.mapping.preset. Mapping presets to use; cmp.mapping.preset.
\$\{mappingPreset} will be called with the configured mappings. \$\{mappingPreset} will be called with the configured mappings.
@ -112,15 +107,33 @@ in {
}; };
snippet = { snippet = {
expand = expand = let
helpers.mkNullOrOption snippetEngines = {
"vsnip" = ''vim.fn["vsnip#anonymous"](args.body)'';
"luasnip" = ''require('luasnip').lsp_expand(args.body)'';
"snippy" = ''require('snippy').expand_snippet(args.body)'';
"ultisnips" = ''vim.fn["UltiSnips#Anon"](args.body)'';
};
in
mkOption {
default = null;
type = with types;
nullOr
( (
with types;
either either
helpers.nixvimTypes.rawLua helpers.nixvimTypes.rawLua
(enum (attrNames snippetEngines)) (enum (attrNames snippetEngines))
) );
apply = v:
if isString v
then
helpers.mkRaw ''
function(args)
${snippetEngines.${v}}
end
'' ''
else v;
description = ''
The snippet expansion function. That's how nvim-cmp interacts with a The snippet expansion function. That's how nvim-cmp interacts with a
particular snippet engine. particular snippet engine.
@ -145,35 +158,55 @@ in {
``` ```
''; '';
}; };
};
completion = { completion = {
keywordLength = helpers.defaultNullOpts.mkInt 1 '' keywordLength = helpers.defaultNullOpts.mkInt 1 ''
The number of characters needed to trigger auto-completion. The number of characters needed to trigger auto-completion.
''; '';
keywordPattern = keywordPattern = mkOption {
helpers.defaultNullOpts.mkStr type = with types; nullOr str;
''\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)'' default = null;
'' description = ''
The default keyword pattern. The default keyword pattern.
Note: the provided pattern will be embedded as such: `[[PATTERN]]`. Note: the provided pattern will be embedded as such: `[[PATTERN]]`.
'';
autocomplete = Default: "\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)"
helpers.defaultNullOpts.mkNullable '';
apply = v:
helpers.ifNonNull'
v
(helpers.mkRaw "[[${v}]]");
};
autocomplete = mkOption {
type = with types;
nullOr
( (
with types;
either either
(listOf str) (listOf str)
(types.enum [false]) (types.enum [false])
) );
''[ "TextChanged" ]'' default = null;
'' description = ''
The event to trigger autocompletion. The event to trigger autocompletion.
If set to `"false"`, then completion is only invoked manually If set to `"false"`, then completion is only invoked manually
(e.g. by calling `cmp.complete`). (e.g. by calling `cmp.complete`).
Default: ["TextChanged"]
''; '';
apply = v:
if isList v
then
map
(triggerEvent:
helpers.mkRaw "require('cmp.types').cmp.TriggerEvent.${triggerEvent}")
v
# either null or false
else v;
};
completeopt = helpers.defaultNullOpts.mkStr "menu,menuone,noselect" '' completeopt = helpers.defaultNullOpts.mkStr "menu,menuone,noselect" ''
Like vim's completeopt setting. In general, you don't need to change this. Like vim's completeopt setting. In general, you don't need to change this.
@ -182,7 +215,7 @@ in {
confirmation = { confirmation = {
getCommitCharacters = getCommitCharacters =
helpers.defaultNullOpts.mkStr helpers.defaultNullOpts.mkLuaFn
'' ''
function(commit_characters) function(commit_characters)
return commit_characters return commit_characters
@ -252,16 +285,28 @@ in {
`final_score = orig_score + ((#sources - (source_index - 1)) * sorting.priority_weight)` `final_score = orig_score + ((#sources - (source_index - 1)) * sorting.priority_weight)`
''; '';
comparators = comparators = mkOption {
helpers.defaultNullOpts.mkNullable (types.listOf types.str) type = with helpers.nixvimTypes; nullOr (listOf strLuaFn);
''[ "offset" "exact" "score" "recently_used" "locality" "kind" "length" "order" ]'' apply = v:
'' helpers.ifNonNull' v (
map
(
funcName:
helpers.mkRaw "require('cmp.config.compare').${funcName}"
)
v
);
default = null;
description = ''
The function to customize the sorting behavior. The function to customize the sorting behavior.
You can use built-in comparators via `cmp.config.compare.*`. You can use built-in comparators via `cmp.config.compare.*`.
Signature: `(fun(entry1: cmp.Entry, entry2: cmp.Entry): boolean | nil)[]` Signature: `(fun(entry1: cmp.Entry, entry2: cmp.Entry): boolean | nil)[]`
Default: ["offset" "exact" "score" "recently_used" "locality" "kind" "length" "order"]
''; '';
}; };
};
autoEnableSources = mkOption { autoEnableSources = mkOption {
type = types.bool; type = types.bool;
@ -455,16 +500,20 @@ in {
inherit zindex; inherit zindex;
maxWidth = maxWidth =
helpers.defaultNullOpts.mkNullable helpers.mkNullOrStrLuaOr types.ints.unsigned
(with types; either int str) ''
"math.floor((40 * 2) * (vim.o.columns / (40 * 2 * 16 / 9)))" The documentation window's max width.
"The documentation window's max width.";
Default: "math.floor((40 * 2) * (vim.o.columns / (40 * 2 * 16 / 9)))"
'';
maxHeight = maxHeight =
helpers.defaultNullOpts.mkNullable helpers.mkNullOrStrLuaOr types.ints.unsigned
(with types; either int str) ''
"math.floor(40 * (40 / vim.o.lines))" The documentation window's max height.
"The documentation window's max height.";
Default: "math.floor(40 * (40 / vim.o.lines))"
'';
}; };
}; };
@ -473,196 +522,15 @@ in {
experimental = helpers.mkNullOrOption types.attrs "Experimental features"; experimental = helpers.mkNullOrOption types.attrs "Experimental features";
}; };
config = let config = mkIf cfg.enable {
setupOptions = with cfg;
{
performance = with performance; {
inherit
debounce
throttle
;
fetching_timeout = fetchingTimeout;
async_budget = asyncBudget;
max_view_entries = maxViewEntries;
};
preselect =
helpers.ifNonNull' preselect
(helpers.mkRaw "cmp.PreselectMode.${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 =
helpers.ifNonNull' mapping
(mapAttrs
(
key: action:
helpers.mkRaw (
if isString action
then action
else let
inherit (action) modes;
modesString =
optionalString
(
(modes != null)
&& ((length modes) >= 1)
)
("," + (helpers.toLuaObject modes));
in "cmp.mapping(${action.action}${modesString})"
)
)
mapping);
luaMappings = helpers.toLuaObject mappings;
wrapped =
lists.fold
(
presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})''
)
luaMappings
mappingPresets;
in
helpers.mkRaw wrapped;
snippet = with snippet; {
expand =
if isString expand
then
helpers.mkRaw ''
function(args)
${snippetEngines.${expand}}
end
''
else expand;
};
completion = with completion; {
keyword_length = keywordLength;
keyword_pattern =
helpers.ifNonNull' keywordPattern
(helpers.mkRaw "[[${keywordPattern}]]");
autocomplete =
if isList autocomplete
then
map
(triggerEvent:
helpers.mkRaw "require('cmp.types').cmp.TriggerEvent.${triggerEvent}")
autocomplete
# either null or false
else autocomplete;
inherit completeopt;
};
confirmation = with confirmation; {
get_commit_characters =
if (isString getCommitCharacters)
then helpers.mkRaw getCommitCharacters
else getCommitCharacters;
};
formatting = with formatting; {
expandable_indicator = expandableIndicator;
inherit fields format;
};
matching = with matching; {
disallow_fuzzy_matching = disallowFuzzyMatching;
disallow_fullfuzzy_matching = disallowFullfuzzyMatching;
disallow_partial_fuzzy_matching = disallowPartialFuzzyMatching;
disallow_partial_matching = disallowPartialMatching;
disallow_prefix_unmatching = disallowPrefixUnmatching;
};
sorting = with sorting; {
priority_weight = priorityWeight;
comparators =
helpers.ifNonNull' comparators
(
map
(
funcName:
helpers.mkRaw "require('cmp.config.compare').${funcName}"
)
comparators
);
};
sources = let
convertSourceAttrs = source:
with source; {
inherit
name
option
;
keyword_length = keywordLength;
keywordPattern =
helpers.ifNonNull' keywordPattern (helpers.mkRaw "[[${keywordPattern}]]");
trigger_characters = triggerCharacters;
inherit priority;
group_index = groupIndex;
entry_filter = entryFilter;
};
in
if sources == null || sources == []
then null
# List of lists of sources -> we use the `cmp.config.sources` helper
else if isList (head sources)
then let
sourcesListofLists =
map
(map convertSourceAttrs)
sources;
in
helpers.mkRaw "cmp.config.sources(${helpers.toLuaObject sourcesListofLists})"
# List of sources
else map convertSourceAttrs sources;
view = with view; {
inherit entries;
docs = with docs; {
auto_open = autoOpen;
};
};
window = with window; {
completion = with completion; {
inherit
border
winhighlight
zindex
scrolloff
;
col_offset = colOffset;
side_padding = sidePadding;
inherit scrollbar;
};
documentation = with documentation; {
inherit
border
winhighlight
zindex
;
max_width =
if isInt maxWidth
then maxWidth
else helpers.mkRaw maxWidth;
max_height =
if isInt maxHeight
then maxHeight
else helpers.mkRaw maxHeight;
};
};
inherit experimental;
}
// cfg.extraOptions;
in
mkIf cfg.enable {
extraPlugins = [cfg.package]; extraPlugins = [cfg.package];
extraConfigLua = helpers.wrapDo '' extraConfigLua = let
setupOptions = import ./setup-options.nix {
inherit cfg lib helpers;
};
in
helpers.wrapDo ''
local cmp = require('cmp') local cmp = require('cmp')
cmp.setup(${helpers.toLuaObject setupOptions}) cmp.setup(${helpers.toLuaObject setupOptions})
''; '';

View file

@ -0,0 +1,148 @@
{
cfg,
lib,
helpers,
}:
with lib; let
# 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 =
mapAttrs
(
key: action:
helpers.mkRaw (
if isString action
then action
else let
inherit (action) modes;
modesString =
optionalString
(
(modes != null)
&& ((length modes) >= 1)
)
("," + (helpers.toLuaObject modes));
in "cmp.mapping(${action.action}${modesString})"
)
)
cfg.mapping;
luaMappings = helpers.toLuaObject mappings;
wrapped =
lists.fold
(
presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})''
)
luaMappings
cfg.mappingPresets;
in
helpers.mkRaw wrapped;
sources' = let
convertSourceAttrs = source:
with source; {
inherit
name
option
;
keyword_length = keywordLength;
keywordPattern =
helpers.ifNonNull' keywordPattern (helpers.mkRaw "[[${keywordPattern}]]");
trigger_characters = triggerCharacters;
inherit priority;
group_index = groupIndex;
entry_filter = entryFilter;
};
in
if cfg.sources == null || cfg.sources == []
then null
# List of lists of sources -> we use the `cmp.config.sources` helper
else if isList (head cfg.sources)
then let
sourcesListofLists =
map
(map convertSourceAttrs)
cfg.sources;
in
helpers.mkRaw "cmp.config.sources(${helpers.toLuaObject sourcesListofLists})"
# List of sources
else map convertSourceAttrs cfg.sources;
in
with cfg;
{
performance = with performance; {
inherit
debounce
throttle
;
fetching_timeout = fetchingTimeout;
async_budget = asyncBudget;
max_view_entries = maxViewEntries;
};
inherit preselect;
inherit mapping';
snippet = with snippet; {
inherit expand;
};
completion = with completion; {
keyword_length = keywordLength;
keyword_pattern = keywordPattern;
inherit
autocomplete
completeopt
;
};
confirmation = with confirmation; {
get_commit_characters = getCommitCharacters;
};
formatting = with formatting; {
expandable_indicator = expandableIndicator;
inherit fields format;
};
matching = with matching; {
disallow_fuzzy_matching = disallowFuzzyMatching;
disallow_fullfuzzy_matching = disallowFullfuzzyMatching;
disallow_partial_fuzzy_matching = disallowPartialFuzzyMatching;
disallow_partial_matching = disallowPartialMatching;
disallow_prefix_unmatching = disallowPrefixUnmatching;
};
sorting = with sorting; {
priority_weight = priorityWeight;
inherit comparators;
};
inherit sources';
view = with view; {
inherit entries;
docs = with docs; {
auto_open = autoOpen;
};
};
window = with window; {
completion = with completion; {
inherit
border
winhighlight
zindex
scrolloff
;
col_offset = colOffset;
side_padding = sidePadding;
inherit scrollbar;
};
documentation = with documentation; {
inherit
border
winhighlight
zindex
;
max_width = maxWidth;
max_height = maxHeight;
};
};
inherit experimental;
}
// cfg.extraOptions