Skip to content

Commit dcaf95f

Browse files
committed
plugins/cmp: remove autoEnableSources option
1 parent d1766ee commit dcaf95f

File tree

11 files changed

+272
-274
lines changed

11 files changed

+272
-274
lines changed

lib/modules.nix

Lines changed: 3 additions & 8 deletions
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

Lines changed: 0 additions & 147 deletions
This file was deleted.

plugins/cmp/default.nix

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

plugins/cmp/deprecations.nix

Lines changed: 2 additions & 0 deletions
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)