plugins/nvim-cmp: refactoring

This commit is contained in:
Gaetan Lepage 2023-08-17 16:07:28 +02:00 committed by Gaétan Lepage
parent 24350879b1
commit 02a0093468
2 changed files with 537 additions and 528 deletions

View file

@ -16,7 +16,9 @@ with lib; let
"ultisnips" = ''vim.fn["UltiSnips#Anon"](args.body)''; "ultisnips" = ''vim.fn["UltiSnips#Anon"](args.body)'';
}; };
in { in {
options.plugins.nvim-cmp = { options.plugins.nvim-cmp =
helpers.extraOptionsOptions
// {
enable = mkEnableOption "nvim-cmp"; enable = mkEnableOption "nvim-cmp";
package = helpers.mkPackageOption "nvim-cmp" pkgs.vimPlugins.nvim-cmp; package = helpers.mkPackageOption "nvim-cmp" pkgs.vimPlugins.nvim-cmp;
@ -55,26 +57,35 @@ in {
mapping = mkOption { mapping = mkOption {
default = null; default = null;
type = with types; type = with types;
nullOr (attrsOf (either str (types.submodule (_: { nullOr
(
attrsOf
(
either
str
(
submodule {
options = { options = {
action = mkOption { action = mkOption {
type = types.nonEmptyStr; type = nonEmptyStr;
description = "The function the mapping should call"; description = "The function the mapping should call";
example = ''"cmp.mapping.scroll_docs(-4)"''; example = ''"cmp.mapping.scroll_docs(-4)"'';
}; };
modes = mkOption { modes = mkOption {
default = null; default = null;
type = types.nullOr (types.listOf types.str); type = nullOr (listOf str);
example = ''[ "i" "s" ]''; example = ''[ "i" "s" ]'';
}; };
}; };
})))); }
example = '' )
{ )
);
example = {
"<CR>" = "cmp.mapping.confirm({ select = true })"; "<CR>" = "cmp.mapping.confirm({ select = true })";
"<Tab>" = { "<Tab>" = {
modes = ["i" "s"]; modes = ["i" "s"];
action = '${""}' action = ''
function(fallback) function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_next_item() cmp.select_next_item()
@ -88,11 +99,10 @@ in {
fallback() fallback()
end end
end end
'${""}';
};
}
''; '';
}; };
};
};
mappingPresets = mkOption { mappingPresets = mkOption {
default = []; default = [];
@ -107,13 +117,14 @@ in {
example = ''[ "insert" "cmdline" ]''; example = ''[ "insert" "cmdline" ]'';
}; };
snippet = helpers.mkCompositeOption "Snippet options" { snippet = {
expand = expand =
helpers.mkNullOrOption helpers.mkNullOrOption
( (
types.either with types;
either
helpers.rawType helpers.rawType
(types.enum (attrNames snippetEngines)) (enum (attrNames snippetEngines))
) )
'' ''
The snippet expansion function. That's how nvim-cmp interacts with a The snippet expansion function. That's how nvim-cmp interacts with a
@ -141,7 +152,7 @@ in {
''; '';
}; };
completion = helpers.mkCompositeOption "Completion options" { 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.
''; '';
@ -175,7 +186,7 @@ in {
''; '';
}; };
confirmation = helpers.mkCompositeOption "Confirmation options" { confirmation = {
getCommitCharacters = getCommitCharacters =
helpers.defaultNullOpts.mkStr helpers.defaultNullOpts.mkStr
'' ''
@ -189,14 +200,14 @@ in {
''; '';
}; };
formatting = helpers.mkCompositeOption "Formatting options" { formatting = {
expandableIndicator = helpers.defaultNullOpts.mkBool true '' expandableIndicator = helpers.defaultNullOpts.mkBool true ''
Boolean to show the `~` expandable indicator in cmp's floating window. Boolean to show the `~` expandable indicator in cmp's floating window.
''; '';
fields = fields =
helpers.defaultNullOpts.mkNullable helpers.defaultNullOpts.mkNullable
(types.listOf types.str) (with types; listOf str)
''[ "kind" "abbr" "menu" ]'' ''[ "kind" "abbr" "menu" ]''
"An array of completion fields to specify their order."; "An array of completion fields to specify their order.";
@ -216,7 +227,7 @@ in {
''; '';
}; };
matching = helpers.mkCompositeOption "Matching options" { matching = {
disallowFuzzyMatching = helpers.defaultNullOpts.mkBool false '' disallowFuzzyMatching = helpers.defaultNullOpts.mkBool false ''
Whether to allow fuzzy matching. Whether to allow fuzzy matching.
''; '';
@ -238,7 +249,7 @@ in {
''; '';
}; };
sorting = helpers.mkCompositeOption "Sorting options" { sorting = {
priorityWeight = helpers.defaultNullOpts.mkInt 2 '' priorityWeight = helpers.defaultNullOpts.mkInt 2 ''
Each item's original priority (given by its corresponding source) will be Each item's original priority (given by its corresponding source) will be
increased by `#sources - (source_index - 1)` and multiplied by `priority_weight`. increased by `#sources - (source_index - 1)` and multiplied by `priority_weight`.
@ -272,7 +283,7 @@ in {
name = mkOption { name = mkOption {
type = types.str; type = types.str;
description = "The name of the source."; description = "The name of the source.";
example = ''"buffer"''; example = "buffer";
}; };
option = helpers.mkNullOrOption types.attrs '' option = helpers.mkNullOrOption types.attrs ''
@ -291,7 +302,7 @@ in {
Note: the provided pattern will be embedded as such: `[[PATTERN]]`. Note: the provided pattern will be embedded as such: `[[PATTERN]]`.
''; '';
triggerCharacters = helpers.mkNullOrOption (types.listOf types.str) '' triggerCharacters = helpers.mkNullOrOption (with types; listOf str) ''
A source-specific keyword pattern. A source-specific keyword pattern.
''; '';
@ -300,27 +311,20 @@ in {
groupIndex = helpers.mkNullOrOption types.int '' groupIndex = helpers.mkNullOrOption types.int ''
The source group index. The source group index.
For instance, you can set the `buffer`'s source `group_index` to a larger number For instance, you can set the `buffer`'s source `groupIndex` to a larger number
if you don't want to see `buffer` source items while `nvim-lsp` source is available: if you don't want to see `buffer` source items while `nvim-lsp` source is available:
``` ```nix
cmp.setup { sources = [
sources = { {
{ name = 'nvim_lsp', group_index = 1 }, name = "nvim_lsp";
{ name = 'buffer', group_index = 2 }, groupIndex = 1;
} }
{
name = "buffer";
groupIndex = 2;
} }
``` ];
You can also achieve this by using the built-in configuration helper like this:
```
cmp.setup {
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
}, {
{ name = 'buffer' },
})
}
``` ```
''; '';
@ -351,19 +355,11 @@ in {
in in
mkOption { mkOption {
default = null; default = null;
type = with types; type = with types; nullOr (listOf source_config);
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.
Or it can be a list of lists, which will use the cmp built-in helper function
`cmp.config.sources`.
Default: [ ] Default: `[]`
''; '';
example = '' example = ''
[ [
@ -375,7 +371,7 @@ in {
''; '';
}; };
view = helpers.mkCompositeOption "View options" { view = {
entries = entries =
helpers.defaultNullOpts.mkNullable (with types; either str attrs) helpers.defaultNullOpts.mkNullable (with types; either str attrs)
'' ''
@ -387,6 +383,12 @@ in {
'' ''
The view class used to customize nvim-cmp's appearance. The view class used to customize nvim-cmp's appearance.
''; '';
docs = {
autoOpen = helpers.defaultNullOpts.mkBool true ''
Specify whether to show the docs_view when selecting an item.
'';
};
}; };
window = let window = let
@ -403,10 +405,13 @@ in {
The completion window's zindex. The completion window's zindex.
See |nvim_open_win|. See |nvim_open_win|.
''; '';
in in {
helpers.mkCompositeOption "Windows options" { completion = {
completion = helpers.mkCompositeOption "Completion window options" { border =
border = helpers.defaultNullOpts.mkBorder ''[ "" "" "" "" "" "" "" "" ]'' "nvim-cmp window" ""; helpers.defaultNullOpts.mkBorder
''[ "" "" "" "" "" "" "" "" ]''
"nvim-cmp window"
"";
winhighlight = winhighlight =
mkWinhighlightOption mkWinhighlightOption
@ -432,8 +437,12 @@ in {
''; '';
}; };
documentation = helpers.mkCompositeOption "Documentation window options" { documentation = {
border = helpers.defaultNullOpts.mkBorder ''[ "" "" "" " " "" "" "" " " ]'' "nvim-cmp documentation window" ""; border =
helpers.defaultNullOpts.mkBorder
''[ "" "" "" " " "" "" "" " " ]''
"nvim-cmp documentation window"
"";
winhighlight = mkWinhighlightOption "FloatBorder:NormalFloat"; winhighlight = mkWinhighlightOption "FloatBorder:NormalFloat";
@ -441,13 +450,13 @@ in {
maxWidth = maxWidth =
helpers.defaultNullOpts.mkNullable helpers.defaultNullOpts.mkNullable
(types.either types.int types.str) (with types; either int str)
"math.floor((40 * 2) * (vim.o.columns / (40 * 2 * 16 / 9)))" "math.floor((40 * 2) * (vim.o.columns / (40 * 2 * 16 / 9)))"
"The documentation window's max width."; "The documentation window's max width.";
maxHeight = maxHeight =
helpers.defaultNullOpts.mkNullable helpers.defaultNullOpts.mkNullable
(types.either types.int types.str) (with types; either int str)
"math.floor(40 * (40 / vim.o.lines))" "math.floor(40 * (40 / vim.o.lines))"
"The documentation window's max height."; "The documentation window's max height.";
}; };
@ -459,23 +468,20 @@ in {
}; };
config = let config = let
options = { setupOptions = with cfg;
# The upstream default value (which is a function) should not be overwritten. {
# https://www.reddit.com/r/neovim/comments/vtw4vl/comment/if9zfdf/?utm_source=share&utm_medium=web2x&context=3 performance = with performance; {
enabled = inherit
if cfg.enable debounce
then null throttle
else false; ;
performance = with cfg.performance; {
inherit debounce throttle;
fetching_timeout = fetchingTimeout; fetching_timeout = fetchingTimeout;
async_budget = asyncBudget; async_budget = asyncBudget;
max_view_entries = maxViewEntries; max_view_entries = maxViewEntries;
}; };
preselect = preselect =
helpers.ifNonNull' cfg.preselect helpers.ifNonNull' preselect
(helpers.mkRaw "cmp.PreselectMode.${cfg.preselect}"); (helpers.mkRaw "cmp.PreselectMode.${preselect}");
# Not very readable sorry # Not very readable sorry
# If null then null # If null then null
@ -483,20 +489,26 @@ in {
# 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 = let
mappings = mappings =
helpers.ifNonNull' cfg.mapping helpers.ifNonNull' mapping
(mapAttrs (mapAttrs
(bind: mapping: (
key: action:
helpers.mkRaw ( helpers.mkRaw (
if isString mapping if isString action
then mapping then action
else let else let
inherit (mapping) modes; inherit (action) modes;
modesString = modesString =
optionalString (modes != null && ((length modes) >= 1)) optionalString
("," + (helpers.toLuaObject mapping.modes)); (
(modes != null)
&& ((length modes) >= 1)
)
("," + (helpers.toLuaObject modes));
in "cmp.mapping(${mapping.action}${modesString})" in "cmp.mapping(${mapping.action}${modesString})"
)) )
cfg.mapping); )
mapping);
luaMappings = helpers.toLuaObject mappings; luaMappings = helpers.toLuaObject mappings;
@ -506,14 +518,12 @@ in {
presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})'' presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})''
) )
luaMappings luaMappings
cfg.mappingPresets; mappingPresets;
in in
helpers.mkRaw wrapped; helpers.mkRaw wrapped;
snippet = helpers.ifNonNull' cfg.snippet { snippet = with snippet; {
expand = let expand =
inherit (cfg.snippet) expand;
in
if isString expand if isString expand
then then
helpers.mkRaw '' helpers.mkRaw ''
@ -524,16 +534,12 @@ in {
else expand; else expand;
}; };
completion = helpers.ifNonNull' cfg.completion { completion = with completion; {
keyword_length = cfg.completion.keywordLength; keyword_length = keywordLength;
keyword_pattern = let keyword_pattern =
inherit (cfg.completion) keywordPattern;
in
helpers.ifNonNull' keywordPattern helpers.ifNonNull' keywordPattern
(helpers.mkRaw "[[${keywordPattern}]]"); (helpers.mkRaw "[[${keywordPattern}]]");
autocomplete = let autocomplete =
inherit (cfg.completion) autocomplete;
in
if isList autocomplete if isList autocomplete
then then
map map
@ -542,37 +548,35 @@ in {
autocomplete autocomplete
# either null or false # either null or false
else autocomplete; else autocomplete;
inherit (cfg.completion) completeopt; inherit completeopt;
}; };
confirmation = helpers.ifNonNull' cfg.confirmation { confirmation = with confirmation; {
get_commit_characters = get_commit_characters =
if (isString cfg.confirmation.getCommitCharacters) if (isString getCommitCharacters)
then helpers.mkRaw cfg.confirmation.getCommitCharacters then helpers.mkRaw getCommitCharacters
else cfg.confirmation.getCommitCharacters; else getCommitCharacters;
}; };
formatting = helpers.ifNonNull' cfg.formatting { formatting = with formatting; {
expandable_indicator = cfg.formatting.expandableIndicator; expandable_indicator = expandableIndicator;
inherit (cfg.formatting) fields; inherit fields;
format = format =
helpers.ifNonNull' cfg.formatting.format helpers.ifNonNull' format
(helpers.mkRaw cfg.formatting.format); (helpers.mkRaw format);
}; };
matching = helpers.ifNonNull' cfg.matching { matching = with matching; {
disallow_fuzzy_matching = cfg.matching.disallowFuzzyMatching; disallow_fuzzy_matching = disallowFuzzyMatching;
disallow_fullfuzzy_matching = cfg.matching.disallowFullfuzzyMatching; disallow_fullfuzzy_matching = disallowFullfuzzyMatching;
disallow_partial_fuzzy_matching = cfg.matching.disallowPartialFuzzyMatching; disallow_partial_fuzzy_matching = disallowPartialFuzzyMatching;
disallow_partial_matching = cfg.matching.disallowPartialMatching; disallow_partial_matching = disallowPartialMatching;
disallow_prefix_unmatching = cfg.matching.disallowPrefixUnmatching; disallow_prefix_unmatching = disallowPrefixUnmatching;
}; };
sorting = helpers.ifNonNull' cfg.sorting { sorting = with sorting; {
priority_weight = cfg.sorting.priorityWeight; priority_weight = priorityWeight;
comparators = let comparators =
inherit (cfg.sorting) comparators;
in
helpers.ifNonNull' comparators helpers.ifNonNull' comparators
( (
map map
@ -586,41 +590,42 @@ in {
sources = helpers.ifNonNull' cfg.sources ( sources = helpers.ifNonNull' cfg.sources (
map map
(source: { (source:
inherit (source) name option; with source; {
keyword_length = source.keywordLength; inherit
name
option
;
keyword_length = keywordLength;
keywordPattern = keywordPattern =
helpers.ifNonNull' source.keywordPattern helpers.ifNonNull' keywordPattern (helpers.mkRaw "[[${keywordPattern}]]");
(helpers.mkRaw "[[${source.keywordPattern}]]"); trigger_characters = triggerCharacters;
inherit priority;
trigger_characters = source.triggerCharacters; group_index = groupIndex;
entry_filter = entryFilter;
inherit (source) priority;
group_index = source.groupIndex;
entry_filter = source.entryFilter;
}) })
cfg.sources cfg.sources
); );
inherit (cfg) view; view = with view; {
window = helpers.ifNonNull' cfg.window { inherit entries;
completion = with cfg.window.completion; docs = with docs; {
helpers.ifNonNull' cfg.window.completion { auto_open = autoOpen;
};
};
window = with window; {
completion = with completion; {
inherit inherit
border border
winhighlight winhighlight
zindex zindex
scrolloff scrolloff
scrollbar
; ;
col_offset = colOffset; col_offset = colOffset;
side_padding = sidePadding; side_padding = sidePadding;
inherit scrollbar;
}; };
documentation = with cfg.window.documentation; documentation = with documentation; {
helpers.ifNonNull' cfg.window.documentation {
inherit inherit
border border
winhighlight winhighlight
@ -636,39 +641,51 @@ in {
else helpers.ifNonNull' maxHeight (helpers.mkRaw maxHeight); else helpers.ifNonNull' maxHeight (helpers.mkRaw maxHeight);
}; };
}; };
inherit (cfg) experimental; inherit experimental;
}; }
// cfg.extraOptions;
in in
mkIf cfg.enable { mkIf cfg.enable {
extraPlugins = [cfg.package]; extraPlugins = [cfg.package];
extraConfigLua = helpers.wrapDo '' extraConfigLua = helpers.wrapDo ''
local cmp = require('cmp') local cmp = require('cmp')
cmp.setup(${helpers.toLuaObject options}) cmp.setup(${helpers.toLuaObject setupOptions})
''; '';
# If auto_enable_sources is set to true, figure out which are provided by the user # If autoEnableSources is set to true, figure out which are provided by the user
# and enable the corresponding plugins. # and enable the corresponding plugins.
plugins = let plugins = let
flattened_sources = sourcesList =
if (cfg.sources == null) if cfg.sources == null
then [] then []
else flatten cfg.sources; else 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 # Take only the names from the sources provided by the user
foundSources =
lists.unique
(
map
(source: source.name)
sourcesList
);
# A list of known source names
knownSourceNames = attrNames cmpLib.pluginAndSourceNames;
attrsEnabled = listToAttrs (map
(name: { (name: {
# Name of the corresponding plugin to enable
name = cmpLib.pluginAndSourceNames.${name}; name = cmpLib.pluginAndSourceNames.${name};
value.enable = mkIf (elem name found_sources) true;
# Whether or not we enable it
value.enable = mkIf (elem name foundSources) true;
}) })
known_source_names); knownSourceNames);
in in
mkMerge [ mkMerge [
(mkIf cfg.autoEnableSources attrs_enabled) (mkIf cfg.autoEnableSources attrsEnabled)
(mkIf (elem "nvim_lsp" found_sources) (mkIf (elem "nvim_lsp" foundSources)
{ {
lsp.capabilities = '' lsp.capabilities = ''
capabilities = require('cmp_nvim_lsp').default_capabilities() capabilities = require('cmp_nvim_lsp').default_capabilities()

View file

@ -23,9 +23,7 @@
asyncBudget = 1; asyncBudget = 1;
maxViewEntries = 200; maxViewEntries = 200;
}; };
preselect = "Item"; preselect = "Item";
snippet = { snippet = {
expand.__raw = '' expand.__raw = ''
function(_) function(_)
@ -33,14 +31,12 @@
end end
''; '';
}; };
completion = { completion = {
keywordLength = 1; keywordLength = 1;
keywordPattern = ''\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)''; keywordPattern = ''\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%(-\w*\)*\)'';
autocomplete = ["TextChanged"]; autocomplete = ["TextChanged"];
completeopt = "menu,menuone,noselect"; completeopt = "menu,menuone,noselect";
}; };
confirmation = { confirmation = {
getCommitCharacters = '' getCommitCharacters = ''
function(commit_characters) function(commit_characters)
@ -48,7 +44,6 @@
end end
''; '';
}; };
formatting = { formatting = {
expandableIndicator = true; expandableIndicator = true;
fields = ["abbr" "kind" "menu"]; fields = ["abbr" "kind" "menu"];
@ -58,7 +53,6 @@
end end
''; '';
}; };
matching = { matching = {
disallowFuzzyMatching = false; disallowFuzzyMatching = false;
disallowFullfuzzyMatching = false; disallowFullfuzzyMatching = false;
@ -66,7 +60,6 @@
disallowPartialMatching = false; disallowPartialMatching = false;
disallowPrefixUnmatching = false; disallowPrefixUnmatching = false;
}; };
sorting = { sorting = {
priorityWeight = 2; priorityWeight = 2;
comparators = [ comparators = [
@ -80,20 +73,19 @@
"order" "order"
]; ];
}; };
sources = []; sources = [];
experimental = { experimental = {
ghost_text = false; ghost_text = false;
}; };
view = { view = {
entries = { entries = {
name = "custom"; name = "custom";
selection_order = "top_down"; selection_order = "top_down";
}; };
docs = {
autoOpen = true;
};
}; };
window = { window = {
completion = { completion = {
border = ["" "" "" "" "" "" "" ""]; border = ["" "" "" "" "" "" "" ""];