Skip to content

Commit dbcf73e

Browse files
committed
plugins/cmp: remove autoEnableSources option
1 parent 9bbb6b4 commit dbcf73e

File tree

12 files changed

+275
-275
lines changed

12 files changed

+275
-275
lines changed

flake/dev/list-plugins/list-plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"plugins/by-name/rustaceanvim/renamed-options.nix",
3434
"plugins/by-name/telescope/extensions/_mk-extension.nix",
3535
"plugins/by-name/telescope/extensions/default.nix",
36-
"plugins/cmp/auto-enable.nix",
36+
"plugins/cmp/deprecated-auto-enable.nix",
3737
"plugins/cmp/options/",
3838
"plugins/cmp/sources/cmp-fish.nix",
3939
"plugins/cmp/sources/default.nix",

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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
77+
lines = lib.flatten (
78+
lib.singleton "manually enable `${pluginOpt.enable}`"
79+
++ lib.optional (sourceDefs ? default) (showSrcDef [ "settings" ] [ "default" ] sourceDefs.default)
80+
++ lib.mapAttrsToList (name: showSrcDef [ "cmdline" name ] [ "cmdline" name ]) (
81+
sourceDefs.cmdline or { }
82+
)
83+
++ lib.mapAttrsToList (name: showSrcDef [ "filetype" name ] [ "filetypes" name ]) (
84+
sourceDefs.filetype or { }
85+
)
86+
);
87+
88+
lineCount = builtins.length lines;
89+
in
90+
lib.mkIf
91+
(
92+
cfg.enable
93+
&& !pluginCfg.enable
94+
&& pluginOpt.enable.highestPrio == 1500
95+
&& pluginCfg.cmp.enable
96+
&& sourceDefs != { }
97+
)
98+
(
99+
lib.nixvim.mkWarnings (lib.showOption pluginLoc) ''
100+
The ${builtins.toJSON pluginCfg.cmp.name} nvim-cmp source has been defined via `${lib.showOption cmpLoc}`, howevew `${pluginOpt.enable}` is not enabled.
101+
You should${
102+
if lineCount == 1 then
103+
" " + builtins.head lines
104+
else
105+
":"
106+
+ lib.concatImapStrings (
107+
i: line: "\n${builtins.toString i}. ${lib.nixvim.utils.upperFirstChar line}"
108+
) lines
109+
}
110+
You can suppress this warning by explicitly setting `${pluginOpt.enable} = false`.
111+
'' # TODO: link to PR/docs/guide/faq
112+
);
113+
in
114+
{
115+
imports = [
116+
(lib.mkRemovedOptionModule [
117+
"cmpSourcePlugins"
118+
] "Use `lib.nixvim.modules.mkCmpPluginModule` instead.")
119+
(lib.mkRemovedOptionModule [ "plugins" "cmp" "autoEnableSources" ] ''
120+
Instead of defining `${
121+
lib.showOption (opt.settings.loc ++ [ "sources" ])
122+
}` and using `autoEnableSources` to enable the relevant plugins,
123+
you should now enable the plugins and they will automatically add themselves to `${
124+
lib.showOption (opt.settings.loc ++ [ "sources" ])
125+
}`.
126+
'') # TODO: add a link to PR/docs/faq/guide
127+
];
128+
129+
warnings = lib.pipe options.plugins [
130+
(lib.flip builtins.removeAttrs [
131+
# lspkind has its own `cmp` options, but isn't a nvim-cmp source
132+
"lspkind"
133+
])
134+
# Actual options are probably aliases, not plugins
135+
(lib.filterAttrs (_: opt: !lib.isOption opt))
136+
(lib.filterAttrs (_: opt: opt ? cmp))
137+
builtins.attrNames
138+
(builtins.map mkWarningDef)
139+
lib.mkMerge
140+
];
141+
}

0 commit comments

Comments
 (0)