Skip to content

Commit a52a3b8

Browse files
committed
plugins/cmp: remove autoEnableSources option
1 parent afbf679 commit a52a3b8

File tree

10 files changed

+248
-266
lines changed

10 files changed

+248
-266
lines changed

lib/modules.nix

+3-8
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ in
266266
cmp = {
267267
enable = lib.mkOption {
268268
type = lib.types.bool;
269-
# FIXME: this should default to true, but it is incompatible with autoEnableCmpSources
270-
default = false;
271-
example = true;
269+
default = true;
270+
example = false;
272271
description = ''
273272
Whether to integrate this plugin with nvim-cmp.
274273
'';
@@ -316,11 +315,7 @@ in
316315
};
317316
};
318317
config = lib.mkIf (pluginCfg.enable && cfg.enable) {
319-
# FIXME: even though we have the mkIf, we also need optionalAttrs
320-
# to avoid inf-recursion caused by `autoEnableSources`
321-
plugins.cmp = lib.optionalAttrs cfg.enable {
322-
# TODO: consider setting:
323-
# autoEnableSources = lib.mkDefault false;
318+
plugins.cmp = {
324319
settings = lib.mkIf (cfg.default != false) (toSources cfg.default);
325320
cmdline = lib.mkIf (cfg.cmdline != { }) (builtins.mapAttrs (_: toSources) cfg.cmdline);
326321
filetype = lib.mkIf (cfg.filetypes != { }) (builtins.mapAttrs (_: toSources) cfg.filetypes);

plugins/cmp/auto-enable.nix

-147
This file was deleted.

plugins/cmp/default.nix

+24-17
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,55 @@ lib.nixvim.plugins.mkNeovimPlugin {
1717
description = ''
1818
### Completion Source Installation
1919
20-
If `plugins.cmp.autoEnableSources` is `true` Nixivm will automatically enable the corresponding source
21-
plugins. This is the default behavior, but will work only when this option is set to a list.
20+
By default, plugins that offer a cmp source will enable the source when they are enabled.
21+
This can be configured or disabled using the `plugins.*.cmp` options.
2222
23-
If you use a raw lua string or set `plugins.cmp.autoEnableSources` to `false`, you will need to explicitly enable the relevant source plugins in
24-
your nixvim configuration.
23+
<!-- TODO:
24+
How should the new system work with raw-lua `sources` options?
25+
Maybe we should write our definitions to an internal option that can be easily read in the raw-lua and/or copied to `settings.sources`?
26+
-->
2527
2628
#### With auto-enabled sources
2729
```nix
28-
plugins.cmp = {
29-
autoEnableSources = true;
30-
settings.sources = [
31-
{ name = "nvim_lsp"; }
32-
{ name = "path"; }
33-
{ name = "buffer"; }
34-
];
30+
plugins = {
31+
cmp.enable = true;
32+
cmp-nvim-lsp.enable = true;
33+
cmp-path.enable = true;
34+
cmp-buffer.enable = true;
3535
};
3636
```
3737
3838
#### Without auto-enabled sources
3939
```nix
4040
plugins = {
4141
cmp = {
42-
autoEnableSources = false;
42+
enable = true;
4343
settings.sources = [
4444
{ name = "nvim_lsp"; }
4545
{ name = "path"; }
4646
{ name = "buffer"; }
4747
];
4848
};
49-
cmp-nvim-lsp.enable = true;
50-
cmp-path.enable = true;
51-
cmp-buffer.enable = true;
49+
cmp-nvim-lsp = {
50+
enable = true;
51+
cmp.enable = false;
52+
};
53+
cmp-path = {
54+
enable = true;
55+
cmp.enable = false;
56+
};
57+
cmp-buffer = {
58+
enable = true;
59+
cmp.enable = false;
60+
};
5261
};
53-
5462
```
5563
'';
5664

5765
imports = [
5866
# Introduced on 2024 February 21
5967
# TODO: remove ~June 2024
6068
./deprecations.nix
61-
./auto-enable.nix
6269
./sources
6370
];
6471
deprecateExtraOptions = true;
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
lib,
3+
config,
4+
options,
5+
...
6+
}:
7+
let
8+
cfg = config.plugins.cmp;
9+
opt = options.plugins.cmp;
10+
11+
cleanSrc = lib.flip builtins.removeAttrs [
12+
"name"
13+
];
14+
15+
getSrcDefs =
16+
sourceName: list:
17+
lib.pipe list [
18+
(builtins.filter (src: src.name == sourceName))
19+
(builtins.map (lib.filterAttrs (_: v: v != null)))
20+
lib.mergeAttrsList
21+
];
22+
23+
getNestedSrcDefs =
24+
sourceName: set:
25+
lib.pipe set [
26+
(builtins.mapAttrs (_: settings: getSrcDefs sourceName settings.sources))
27+
(lib.filterAttrs (_: v: v != { }))
28+
(builtins.mapAttrs (_: cleanSrc))
29+
];
30+
31+
# Check if the source is listed in `plugins.cmp.settings.sources` without the plugin being enabled
32+
# This is most likely to happen when a user was relying on the now removed `autoEnableSources` option
33+
# Produce a warning with detailed migration instructions.
34+
mkWarningDef =
35+
name:
36+
let
37+
pluginOpt = options.plugins.${name};
38+
pluginCfg = config.plugins.${name};
39+
pluginLoc = lib.dropEnd 1 pluginOpt.enable.loc;
40+
cmpLoc = lib.dropEnd 1 options.plugins.cmp.enable.loc;
41+
42+
# Collect defined sources for this plugin
43+
defaultDef = getSrcDefs pluginCfg.cmp.name cfg.settings.sources;
44+
sourceDefs =
45+
lib.optionalAttrs (defaultDef != { }) {
46+
default = cleanSrc defaultDef;
47+
}
48+
// lib.filterAttrs (_: v: v != { }) {
49+
cmdline = getNestedSrcDefs pluginCfg.cmp.name cfg.cmdline;
50+
filetypes = getNestedSrcDefs pluginCfg.cmp.name cfg.filetype;
51+
};
52+
53+
indent = " ";
54+
55+
showSrcDef =
56+
loc: new: def:
57+
let
58+
defLoc = lib.showOption (cmpLoc ++ loc ++ [ "sources" ]);
59+
join = if lib.hasInfix "\n" suggestion then "\n" + indent else " ";
60+
suggestion = "${lib.showOption (pluginLoc ++ [ "cmp" ] ++ new)} = ${
61+
if def == { } then "true" else lib.generators.toPretty { inherit indent; } def
62+
};";
63+
in
64+
''
65+
remove ${builtins.toJSON pluginCfg.cmp.name} from: ${defLoc}
66+
Instead define:${join}${suggestion}'';
67+
68+
lines =
69+
lib.singleton "manually enable `${pluginOpt.enable}`"
70+
++ lib.optional (sourceDefs ? default) (showSrcDef [ "settings" ] [ "default" ] sourceDefs.default)
71+
++ lib.mapAttrsToList (name: showSrcDef [ "cmdline" name ] [ "cmdline" name ]) (
72+
sourceDefs.cmdline or { }
73+
)
74+
++ lib.mapAttrsToList (name: showSrcDef [ "filetype" name ] [ "filetypes" name ]) (
75+
sourceDefs.filetype or { }
76+
);
77+
78+
lineCount = builtins.length lines;
79+
in
80+
lib.mkIf
81+
(
82+
cfg.enable
83+
&& !pluginCfg.enable
84+
&& pluginOpt.enable.highestPrio == 1500
85+
&& pluginCfg.cmp.enable
86+
&& sourceDefs != { }
87+
)
88+
(
89+
lib.nixvim.mkWarnings (lib.showOption pluginLoc) ''
90+
The ${builtins.toJSON pluginCfg.cmp.name} nvim-cmp source has been defined via `${lib.showOption cmpLoc}`, howevew `${pluginOpt.enable}` is not enabled.
91+
You should${
92+
if lineCount == 1 then
93+
" " + builtins.head lines
94+
else
95+
":"
96+
+ lib.concatImapStrings (
97+
i: line: "\n${builtins.toString i}. ${lib.nixvim.utils.upperFirstChar line}"
98+
) lines
99+
}
100+
(You can suppress this warning by explicitly setting `${pluginOpt.enable} = true`)
101+
'' # TODO: link to PR/docs/guide/faq
102+
);
103+
in
104+
{
105+
imports = [
106+
(lib.mkRemovedOptionModule [
107+
"cmpSourcePlugins"
108+
] "Use `lib.nixvim.modules.mkCmpPluginModule` instead.")
109+
(lib.mkRemovedOptionModule [ "plugins" "cmp" "autoEnableSources" ] ''
110+
Instead of defining `${
111+
lib.showOption (opt.settings.loc ++ [ "sources" ])
112+
}` and using `autoEnableSources` to enable the relevant plugins,
113+
you should now enable the plugins and they will automatically add themselves to `${
114+
lib.showOption (opt.settings.loc ++ [ "sources" ])
115+
}`.
116+
'') # TODO: add a link to PR/docs/faq/guide
117+
];
118+
119+
warnings = lib.pipe options.plugins [
120+
(lib.flip builtins.removeAttrs [
121+
# lspkind has its own `cmp` options, but isn't a nvim-cmp source
122+
"lspkind"
123+
])
124+
# Actual options are probably aliases, not plugins
125+
(lib.filterAttrs (_: opt: !lib.isOption opt))
126+
(lib.filterAttrs (_: opt: opt ? cmp))
127+
builtins.attrNames
128+
(builtins.map mkWarningDef)
129+
lib.mkMerge
130+
];
131+
}

plugins/cmp/deprecations.nix

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ let
168168
in
169169
{
170170
imports = renameWarnings ++ [
171+
# auto-enable was removed 2025-02-07
172+
./deprecated-auto-enable.nix
171173
(mkRenamedOptionModule (oldPluginBasePath ++ [ "enable" ]) (newPluginBasePath ++ [ "enable" ]))
172174
(mkRenamedOptionModule (oldPluginBasePath ++ [ "autoEnableSources" ]) (
173175
newPluginBasePath ++ [ "autoEnableSources" ]

0 commit comments

Comments
 (0)