Skip to content

Commit 94b0316

Browse files
committed
autocomplete: move shared code into common parent
1 parent b780503 commit 94b0316

File tree

5 files changed

+131
-97
lines changed

5 files changed

+131
-97
lines changed

modules/plugins/completion/blink-cmp/config.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ in {
4242
};
4343

4444
autocomplete = {
45+
enableSharedCmpSources = true;
4546
blink-cmp.setupOpts = {
4647
sources = {
4748
default = ["lsp" "path" "snippets" "buffer"] ++ (attrNames cmpCfg.sources);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
lib,
3+
config,
4+
...
5+
}: let
6+
inherit (lib.modules) mkIf;
7+
inherit (lib.nvim.attrsets) mapListToAttrs;
8+
inherit (builtins) typeOf tryEval;
9+
10+
cfg = config.vim.autocomplete;
11+
getPluginName = plugin:
12+
if typeOf plugin == "string"
13+
then plugin
14+
else if (plugin ? pname && (tryEval plugin.pname).success)
15+
then plugin.pname
16+
else plugin.name;
17+
in {
18+
config.vim = mkIf cfg.enableSharedCmpSources {
19+
startPlugins = ["rtp-nvim"];
20+
lazy.plugins =
21+
mapListToAttrs (package: {
22+
name = getPluginName package;
23+
value = {
24+
inherit package;
25+
lazy = true;
26+
after = ''
27+
local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}')
28+
require("rtp_nvim").source_after_plugin_dir(path)
29+
'';
30+
};
31+
})
32+
cfg.nvim-cmp.sourcePlugins;
33+
};
34+
}

modules/plugins/completion/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
imports = [
3+
./module.nix
4+
./config.nix
5+
36
./nvim-cmp
47
./blink-cmp
58
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{lib, ...}: let
2+
inherit (lib.options) mkEnableOption;
3+
in {
4+
options.vim.autocomplete = {
5+
enableSharedCmpSources = mkEnableOption "sources shared by blink.cmp and nvim-cmp";
6+
};
7+
}

modules/plugins/completion/nvim-cmp/config.nix

Lines changed: 86 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -24,114 +24,103 @@
2424
in {
2525
config = mkIf cfg.enable {
2626
vim = {
27-
startPlugins = ["rtp-nvim"];
28-
lazy.plugins = mkMerge [
29-
(mapListToAttrs (package: {
30-
name = getPluginName package;
31-
value = {
32-
inherit package;
33-
lazy = true;
34-
after = ''
35-
local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}')
36-
require("rtp_nvim").source_after_plugin_dir(path)
37-
'';
38-
};
39-
})
40-
cfg.sourcePlugins)
41-
{
42-
nvim-cmp = {
43-
package = "nvim-cmp";
44-
after = ''
45-
${optionalString luasnipEnable "local luasnip = require('luasnip')"}
46-
local cmp = require("cmp")
47-
48-
local kinds = require("cmp.types").lsp.CompletionItemKind
49-
local deprio = function(kind)
50-
return function(e1, e2)
51-
if e1:get_kind() == kind then
52-
return false
53-
end
54-
if e2:get_kind() == kind then
55-
return true
56-
end
57-
return nil
27+
lazy.plugins = {
28+
nvim-cmp = {
29+
package = "nvim-cmp";
30+
after = ''
31+
${optionalString luasnipEnable "local luasnip = require('luasnip')"}
32+
local cmp = require("cmp")
33+
34+
local kinds = require("cmp.types").lsp.CompletionItemKind
35+
local deprio = function(kind)
36+
return function(e1, e2)
37+
if e1:get_kind() == kind then
38+
return false
39+
end
40+
if e2:get_kind() == kind then
41+
return true
5842
end
43+
return nil
5944
end
45+
end
6046
61-
cmp.setup(${toLuaObject cfg.setupOpts})
47+
cmp.setup(${toLuaObject cfg.setupOpts})
6248
63-
${optionalString config.vim.lazy.enable
64-
(concatStringsSep "\n" (map
65-
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
66-
cfg.sourcePlugins))}
67-
'';
49+
${optionalString config.vim.lazy.enable
50+
(concatStringsSep "\n" (map
51+
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
52+
cfg.sourcePlugins))}
53+
'';
6854

69-
event = ["InsertEnter" "CmdlineEnter"];
70-
};
71-
}
72-
];
73-
74-
autocomplete.nvim-cmp = {
75-
sources = {
76-
nvim-cmp = null;
77-
buffer = "[Buffer]";
78-
path = "[Path]";
55+
event = ["InsertEnter" "CmdlineEnter"];
7956
};
57+
};
8058

81-
sourcePlugins = ["cmp-buffer" "cmp-path"];
82-
83-
setupOpts = {
84-
sources = map (s: {name = s;}) (attrNames cfg.sources);
59+
autocomplete = {
60+
enableSharedCmpSources = true;
8561

86-
window = mkIf borders.enable {
87-
completion.border = borders.style;
88-
documentation.border = borders.style;
62+
nvim-cmp = {
63+
sources = {
64+
nvim-cmp = null;
65+
buffer = "[Buffer]";
66+
path = "[Path]";
8967
};
9068

91-
formatting.format = cfg.format;
92-
93-
# `cmp` and `luasnip` are defined above, in the `nvim-cmp` section
94-
mapping = {
95-
${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
96-
${mappings.close} = mkLuaInline "cmp.mapping.abort()";
97-
${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
98-
${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
99-
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
100-
101-
${mappings.next} = mkLuaInline ''
102-
cmp.mapping(function(fallback)
103-
local has_words_before = function()
104-
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
105-
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
106-
end
69+
sourcePlugins = ["cmp-buffer" "cmp-path"];
10770

108-
if cmp.visible() then
109-
cmp.select_next_item()
110-
${optionalString luasnipEnable ''
111-
elseif luasnip.locally_jumpable(1) then
112-
luasnip.jump(1)
113-
''}
114-
elseif has_words_before() then
115-
cmp.complete()
116-
else
117-
fallback()
118-
end
119-
end)
120-
'';
121-
122-
${mappings.previous} = mkLuaInline ''
123-
cmp.mapping(function(fallback)
124-
if cmp.visible() then
125-
cmp.select_prev_item()
126-
${optionalString luasnipEnable ''
127-
elseif luasnip.locally_jumpable(-1) then
128-
luasnip.jump(-1)
129-
''}
130-
else
131-
fallback()
132-
end
133-
end)
134-
'';
71+
setupOpts = {
72+
sources = map (s: {name = s;}) (attrNames cfg.sources);
73+
74+
window = mkIf borders.enable {
75+
completion.border = borders.style;
76+
documentation.border = borders.style;
77+
};
78+
79+
formatting.format = cfg.format;
80+
81+
# `cmp` and `luasnip` are defined above, in the `nvim-cmp` section
82+
mapping = {
83+
${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
84+
${mappings.close} = mkLuaInline "cmp.mapping.abort()";
85+
${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
86+
${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
87+
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
88+
89+
${mappings.next} = mkLuaInline ''
90+
cmp.mapping(function(fallback)
91+
local has_words_before = function()
92+
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
93+
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
94+
end
95+
96+
if cmp.visible() then
97+
cmp.select_next_item()
98+
${optionalString luasnipEnable ''
99+
elseif luasnip.locally_jumpable(1) then
100+
luasnip.jump(1)
101+
''}
102+
elseif has_words_before() then
103+
cmp.complete()
104+
else
105+
fallback()
106+
end
107+
end)
108+
'';
109+
110+
${mappings.previous} = mkLuaInline ''
111+
cmp.mapping(function(fallback)
112+
if cmp.visible() then
113+
cmp.select_prev_item()
114+
${optionalString luasnipEnable ''
115+
elseif luasnip.locally_jumpable(-1) then
116+
luasnip.jump(-1)
117+
''}
118+
else
119+
fallback()
120+
end
121+
end)
122+
'';
123+
};
135124
};
136125
};
137126
};

0 commit comments

Comments
 (0)