Skip to content

Commit b2243ca

Browse files
committed
plugins/cmp/auto-enable: warn if used alongside plugins.*.cmp
1 parent 4f8b10b commit b2243ca

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

plugins/cmp/auto-enable.nix

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
lib,
33
config,
4+
options,
45
...
56
}:
67
let
@@ -25,6 +26,27 @@ let
2526
(map (lib.getAttr "name"))
2627
lib.unique
2728
];
29+
30+
newStylePlugins = lib.pipe options.plugins [
31+
# First, a manual blacklist
32+
(lib.flip builtins.removeAttrs [
33+
# lspkind has its own `cmp` options, but isn't a nvim-cmp source
34+
"lspkind"
35+
])
36+
builtins.attrValues
37+
# Filter for non-options (all plugins are plain attrsets, not options)
38+
# i.e. remove rename aliases
39+
(builtins.filter (opt: !lib.isOption opt))
40+
# Filter for plugins that have `cmp` enabled
41+
(builtins.filter (opt: opt.cmp.value.enable or false))
42+
# Collect the enable options' `loc`s
43+
(builtins.catAttrs "enable")
44+
(builtins.catAttrs "loc")
45+
# Drop the `"enable"` part of the option-loc
46+
(builtins.map (lib.lists.dropEnd 1))
47+
# Render each plugin loc as an option string
48+
(builtins.map lib.showOption)
49+
];
2850
in
2951
{
3052
options = {
@@ -55,9 +77,23 @@ in
5577
};
5678
};
5779

58-
config = lib.mkIf (cfg.enable && cfg.autoEnableSources) (
59-
lib.mkMerge [
60-
{
80+
config = lib.mkMerge (
81+
[
82+
(lib.mkIf (cfg.enable && cfg.autoEnableSources && newStylePlugins != [ ]) {
83+
# Warn when the new and old systems are used together.
84+
# `autoEnableSources` is incompatible with the new `plugins.*.cmp` options
85+
# TODO:
86+
# - Have `autoEnableSources` default to no `plugins.*.cmp` options being enabled?
87+
# - Maybe warn when `autoEnableSources` has highestPrio 1500?
88+
# - I'm not sure how best to migrate to having `plugins.*.cmp.enable` default to true...
89+
warnings = lib.nixvim.mkWarnings "plugins.cmp" ''
90+
You have enabled `autoEnableSources` that tells Nixvim to automatically enable the source plugins with respect to the list of sources provided in `settings.sources`.
91+
However, ${builtins.toString (builtins.length newStylePlugins)} plugins have cmp integration configured via `plugins.*.cmp`:${
92+
lib.concatMapStrings (opt: "\n- `${opt}`") newStylePlugins
93+
}
94+
'';
95+
})
96+
(lib.mkIf (cfg.enable && cfg.autoEnableSources) {
6197
warnings = lib.nixvim.mkWarnings "plugins.cmp" [
6298
# TODO: expand this warning to ft & cmd sources lists and `showDefs` the offending definitions
6399
{
@@ -82,21 +118,42 @@ in
82118
}
83119
];
84120

85-
# If the user has enabled the `foo` and `bar` sources, then `plugins` will look like:
86-
# {
87-
# cmp-foo.enable = true;
88-
# cmp-bar.enable = true;
89-
# }
90-
plugins = lib.mapAttrs' (source: name: {
91-
inherit name;
92-
value.enable = lib.mkIf (lib.elem source enabledSources) true;
93-
}) config.cmpSourcePlugins;
94-
}
95-
{
96121
plugins.lsp.capabilities = lib.mkIf (lib.elem "nvim_lsp" enabledSources) ''
97122
capabilities = vim.tbl_deep_extend("force", capabilities, require('cmp_nvim_lsp').default_capabilities())
98123
'';
99-
}
124+
})
125+
]
126+
# If the user has enabled the `foo` and `bar` sources, then `plugins` will look like:
127+
# {
128+
# cmp-foo.enable = true;
129+
# cmp-bar.enable = true;
130+
# }
131+
#
132+
# To avoid inf-rec, we have to define _all_ declared plugins,
133+
# outside of any conditional that depends on config.plugins.
134+
# We can use mkIf _within_ definitions though.
135+
++ lib.pipe options.plugins [
136+
(lib.flip builtins.removeAttrs [
137+
# lspkind has its own `cmp` options, but isn't a nvim-cmp source
138+
"lspkind"
139+
])
140+
# Actual options are probably aliases, not plugins
141+
(lib.filterAttrs (_: opt: !lib.isOption opt))
142+
(lib.filterAttrs (_: opt: opt ? cmp))
143+
builtins.attrNames
144+
(builtins.map (
145+
name:
146+
let
147+
inherit (config.plugins.${name}) cmp;
148+
in
149+
lib.mkIf (
150+
cfg.enable
151+
&& cfg.autoEnableSources
152+
# Avoid inf-rec by not auto-enabling sources that add themselves to sources?
153+
&& !cmp.enable or false
154+
&& builtins.elem cmp.name enabledSources
155+
) { plugins.${name}.enable = true; }
156+
))
100157
]
101158
);
102159
}

0 commit comments

Comments
 (0)