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)'';
};
in {
options.plugins.nvim-cmp = {
options.plugins.nvim-cmp =
helpers.extraOptionsOptions
// {
enable = mkEnableOption "nvim-cmp";
package = helpers.mkPackageOption "nvim-cmp" pkgs.vimPlugins.nvim-cmp;
@ -55,26 +57,35 @@ in {
mapping = mkOption {
default = null;
type = with types;
nullOr (attrsOf (either str (types.submodule (_: {
nullOr
(
attrsOf
(
either
str
(
submodule {
options = {
action = mkOption {
type = types.nonEmptyStr;
type = 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);
type = nullOr (listOf str);
example = ''[ "i" "s" ]'';
};
};
}))));
example = ''
{
}
)
)
);
example = {
"<CR>" = "cmp.mapping.confirm({ select = true })";
"<Tab>" = {
modes = ["i" "s"];
action = '${""}'
action = ''
function(fallback)
if cmp.visible() then
cmp.select_next_item()
@ -88,11 +99,10 @@ in {
fallback()
end
end
'${""}';
};
}
'';
};
};
};
mappingPresets = mkOption {
default = [];
@ -107,13 +117,14 @@ in {
example = ''[ "insert" "cmdline" ]'';
};
snippet = helpers.mkCompositeOption "Snippet options" {
snippet = {
expand =
helpers.mkNullOrOption
(
types.either
with types;
either
helpers.rawType
(types.enum (attrNames snippetEngines))
(enum (attrNames snippetEngines))
)
''
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 ''
The number of characters needed to trigger auto-completion.
'';
@ -175,7 +186,7 @@ in {
'';
};
confirmation = helpers.mkCompositeOption "Confirmation options" {
confirmation = {
getCommitCharacters =
helpers.defaultNullOpts.mkStr
''
@ -189,14 +200,14 @@ in {
'';
};
formatting = helpers.mkCompositeOption "Formatting options" {
formatting = {
expandableIndicator = helpers.defaultNullOpts.mkBool true ''
Boolean to show the `~` expandable indicator in cmp's floating window.
'';
fields =
helpers.defaultNullOpts.mkNullable
(types.listOf types.str)
(with types; listOf str)
''[ "kind" "abbr" "menu" ]''
"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 ''
Whether to allow fuzzy matching.
'';
@ -238,7 +249,7 @@ in {
'';
};
sorting = helpers.mkCompositeOption "Sorting options" {
sorting = {
priorityWeight = helpers.defaultNullOpts.mkInt 2 ''
Each item's original priority (given by its corresponding source) will be
increased by `#sources - (source_index - 1)` and multiplied by `priority_weight`.
@ -272,7 +283,7 @@ in {
name = mkOption {
type = types.str;
description = "The name of the source.";
example = ''"buffer"'';
example = "buffer";
};
option = helpers.mkNullOrOption types.attrs ''
@ -291,7 +302,7 @@ in {
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.
'';
@ -300,27 +311,20 @@ in {
groupIndex = helpers.mkNullOrOption types.int ''
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:
```
cmp.setup {
sources = {
{ name = 'nvim_lsp', group_index = 1 },
{ name = 'buffer', group_index = 2 },
```nix
sources = [
{
name = "nvim_lsp";
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
mkOption {
default = null;
type = with types;
nullOr (
either
(listOf source_config)
(listOf (listOf source_config))
);
type = with types; nullOr (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`.
Default: [ ]
Default: `[]`
'';
example = ''
[
@ -375,7 +371,7 @@ in {
'';
};
view = helpers.mkCompositeOption "View options" {
view = {
entries =
helpers.defaultNullOpts.mkNullable (with types; either str attrs)
''
@ -387,6 +383,12 @@ in {
''
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
@ -403,10 +405,13 @@ in {
The completion window's zindex.
See |nvim_open_win|.
'';
in
helpers.mkCompositeOption "Windows options" {
completion = helpers.mkCompositeOption "Completion window options" {
border = helpers.defaultNullOpts.mkBorder ''[ "" "" "" "" "" "" "" "" ]'' "nvim-cmp window" "";
in {
completion = {
border =
helpers.defaultNullOpts.mkBorder
''[ "" "" "" "" "" "" "" "" ]''
"nvim-cmp window"
"";
winhighlight =
mkWinhighlightOption
@ -432,8 +437,12 @@ in {
'';
};
documentation = helpers.mkCompositeOption "Documentation window options" {
border = helpers.defaultNullOpts.mkBorder ''[ "" "" "" " " "" "" "" " " ]'' "nvim-cmp documentation window" "";
documentation = {
border =
helpers.defaultNullOpts.mkBorder
''[ "" "" "" " " "" "" "" " " ]''
"nvim-cmp documentation window"
"";
winhighlight = mkWinhighlightOption "FloatBorder:NormalFloat";
@ -441,13 +450,13 @@ in {
maxWidth =
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)))"
"The documentation window's max width.";
maxHeight =
helpers.defaultNullOpts.mkNullable
(types.either types.int types.str)
(with types; either int str)
"math.floor(40 * (40 / vim.o.lines))"
"The documentation window's max height.";
};
@ -459,23 +468,20 @@ in {
};
config = let
options = {
# 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
enabled =
if cfg.enable
then null
else false;
performance = with cfg.performance; {
inherit debounce throttle;
setupOptions = with cfg;
{
performance = with performance; {
inherit
debounce
throttle
;
fetching_timeout = fetchingTimeout;
async_budget = asyncBudget;
max_view_entries = maxViewEntries;
};
preselect =
helpers.ifNonNull' cfg.preselect
(helpers.mkRaw "cmp.PreselectMode.${cfg.preselect}");
helpers.ifNonNull' preselect
(helpers.mkRaw "cmp.PreselectMode.${preselect}");
# Not very readable sorry
# 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.
mapping = let
mappings =
helpers.ifNonNull' cfg.mapping
helpers.ifNonNull' mapping
(mapAttrs
(bind: mapping:
(
key: action:
helpers.mkRaw (
if isString mapping
then mapping
if isString action
then action
else let
inherit (mapping) modes;
inherit (action) modes;
modesString =
optionalString (modes != null && ((length modes) >= 1))
("," + (helpers.toLuaObject mapping.modes));
optionalString
(
(modes != null)
&& ((length modes) >= 1)
)
("," + (helpers.toLuaObject modes));
in "cmp.mapping(${mapping.action}${modesString})"
))
cfg.mapping);
)
)
mapping);
luaMappings = helpers.toLuaObject mappings;
@ -506,14 +518,12 @@ in {
presetName: prevString: ''cmp.mapping.preset.${presetName}(${prevString})''
)
luaMappings
cfg.mappingPresets;
mappingPresets;
in
helpers.mkRaw wrapped;
snippet = helpers.ifNonNull' cfg.snippet {
expand = let
inherit (cfg.snippet) expand;
in
snippet = with snippet; {
expand =
if isString expand
then
helpers.mkRaw ''
@ -524,16 +534,12 @@ in {
else expand;
};
completion = helpers.ifNonNull' cfg.completion {
keyword_length = cfg.completion.keywordLength;
keyword_pattern = let
inherit (cfg.completion) keywordPattern;
in
completion = with completion; {
keyword_length = keywordLength;
keyword_pattern =
helpers.ifNonNull' keywordPattern
(helpers.mkRaw "[[${keywordPattern}]]");
autocomplete = let
inherit (cfg.completion) autocomplete;
in
autocomplete =
if isList autocomplete
then
map
@ -542,37 +548,35 @@ in {
autocomplete
# either null or false
else autocomplete;
inherit (cfg.completion) completeopt;
inherit completeopt;
};
confirmation = helpers.ifNonNull' cfg.confirmation {
confirmation = with confirmation; {
get_commit_characters =
if (isString cfg.confirmation.getCommitCharacters)
then helpers.mkRaw cfg.confirmation.getCommitCharacters
else cfg.confirmation.getCommitCharacters;
if (isString getCommitCharacters)
then helpers.mkRaw getCommitCharacters
else getCommitCharacters;
};
formatting = helpers.ifNonNull' cfg.formatting {
expandable_indicator = cfg.formatting.expandableIndicator;
inherit (cfg.formatting) fields;
formatting = with formatting; {
expandable_indicator = expandableIndicator;
inherit fields;
format =
helpers.ifNonNull' cfg.formatting.format
(helpers.mkRaw cfg.formatting.format);
helpers.ifNonNull' format
(helpers.mkRaw format);
};
matching = helpers.ifNonNull' cfg.matching {
disallow_fuzzy_matching = cfg.matching.disallowFuzzyMatching;
disallow_fullfuzzy_matching = cfg.matching.disallowFullfuzzyMatching;
disallow_partial_fuzzy_matching = cfg.matching.disallowPartialFuzzyMatching;
disallow_partial_matching = cfg.matching.disallowPartialMatching;
disallow_prefix_unmatching = cfg.matching.disallowPrefixUnmatching;
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 = helpers.ifNonNull' cfg.sorting {
priority_weight = cfg.sorting.priorityWeight;
comparators = let
inherit (cfg.sorting) comparators;
in
sorting = with sorting; {
priority_weight = priorityWeight;
comparators =
helpers.ifNonNull' comparators
(
map
@ -586,41 +590,42 @@ in {
sources = helpers.ifNonNull' cfg.sources (
map
(source: {
inherit (source) name option;
keyword_length = source.keywordLength;
(source:
with source; {
inherit
name
option
;
keyword_length = keywordLength;
keywordPattern =
helpers.ifNonNull' source.keywordPattern
(helpers.mkRaw "[[${source.keywordPattern}]]");
trigger_characters = source.triggerCharacters;
inherit (source) priority;
group_index = source.groupIndex;
entry_filter = source.entryFilter;
helpers.ifNonNull' keywordPattern (helpers.mkRaw "[[${keywordPattern}]]");
trigger_characters = triggerCharacters;
inherit priority;
group_index = groupIndex;
entry_filter = entryFilter;
})
cfg.sources
);
inherit (cfg) view;
window = helpers.ifNonNull' cfg.window {
completion = with cfg.window.completion;
helpers.ifNonNull' cfg.window.completion {
view = with view; {
inherit entries;
docs = with docs; {
auto_open = autoOpen;
};
};
window = with window; {
completion = with completion; {
inherit
border
winhighlight
zindex
scrolloff
scrollbar
;
col_offset = colOffset;
side_padding = sidePadding;
inherit scrollbar;
};
documentation = with cfg.window.documentation;
helpers.ifNonNull' cfg.window.documentation {
documentation = with documentation; {
inherit
border
winhighlight
@ -636,39 +641,51 @@ in {
else helpers.ifNonNull' maxHeight (helpers.mkRaw maxHeight);
};
};
inherit (cfg) experimental;
};
inherit experimental;
}
// cfg.extraOptions;
in
mkIf cfg.enable {
extraPlugins = [cfg.package];
extraConfigLua = helpers.wrapDo ''
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.
plugins = let
flattened_sources =
if (cfg.sources == null)
sourcesList =
if cfg.sources == null
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;
else cfg.sources;
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 of the corresponding plugin to enable
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
mkMerge [
(mkIf cfg.autoEnableSources attrs_enabled)
(mkIf (elem "nvim_lsp" found_sources)
(mkIf cfg.autoEnableSources attrsEnabled)
(mkIf (elem "nvim_lsp" foundSources)
{
lsp.capabilities = ''
capabilities = require('cmp_nvim_lsp').default_capabilities()

View file

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