This commit is contained in:
Ching Pei Yang 2024-12-23 12:10:26 +03:00 committed by GitHub
commit c7a66f0f8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 347 additions and 112 deletions

View file

@ -188,6 +188,22 @@
"type": "github"
}
},
"plugin-blink-cmp": {
"flake": false,
"locked": {
"lastModified": 1734880418,
"narHash": "sha256-jR9fvo+I51DKYQb+N3nFvQ50N+lvYzfFaQtrn7cxDu4=",
"owner": "saghen",
"repo": "blink.cmp",
"rev": "f93af0f486ada13e8c34f42c911788b9232b811f",
"type": "github"
},
"original": {
"owner": "saghen",
"repo": "blink.cmp",
"type": "github"
}
},
"plugin-bufdelete-nvim": {
"flake": false,
"locked": {
@ -2076,6 +2092,7 @@
"nmd": "nmd",
"plugin-alpha-nvim": "plugin-alpha-nvim",
"plugin-base16": "plugin-base16",
"plugin-blink-cmp": "plugin-blink-cmp",
"plugin-bufdelete-nvim": "plugin-bufdelete-nvim",
"plugin-catppuccin": "plugin-catppuccin",
"plugin-ccc": "plugin-ccc",

View file

@ -288,6 +288,11 @@
flake = false;
};
plugin-blink-cmp = {
url = "github:saghen/blink.cmp";
flake = false;
};
plugin-nvim-cmp = {
url = "github:hrsh7th/nvim-cmp";
flake = false;

View file

@ -72,6 +72,37 @@
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
};
blink-cmp = let
version = inputs.plugin-blink-cmp.shortRev or inputs.plugin-blink-cmp.shortDirtyRev or "dirty";
src = inputs.plugin-blink-cmp;
blink-fuzzy-lib = pkgs.rustPlatform.buildRustPackage {
pname = "blink-fuzzy-lib";
inherit version src;
env = {
# TODO: remove this if plugin stops using nightly rust
RUSTC_BOOTSTRAP = true;
};
cargoLock = {
lockFile = "${src}/Cargo.lock";
outputHashes = {
"frizbee-0.1.0" = "sha256-pt6sMsRyjXrbrTK7t/YvWeen/n3nU8UUaiNYTY1LczE=";
};
};
};
libExt =
if pkgs.hostPlatform.isDarwin
then "dylib"
else "so";
in
buildPlug {
pname = "blink-cmp";
inherit version src;
preInstall = ''
mkdir -p target/release
ln -s ${blink-fuzzy-lib}/lib/libblink_cmp_fuzzy.${libExt} target/release/libblink_cmp_fuzzy.${libExt}
'';
};
};
buildConfigPlugins = plugins:

View file

@ -58,8 +58,8 @@ in {
};
};
autocomplete.nvim-cmp = {
sources = {copilot = "[Copilot]";};
autocomplete = {
nvim-cmp.sources = {copilot = "[Copilot]";};
sourcePlugins = ["copilot-cmp"];
};

View file

@ -0,0 +1,64 @@
{lib, ...}: let
inherit (lib.options) mkEnableOption mkOption literalMD;
inherit (lib.types) listOf str either attrsOf submodule enum;
inherit (lib.nvim.types) mkPluginSetupOption luaInline;
inherit (lib.nvim.binds) mkMappingOption;
keymapType = submodule {
freeformType = attrsOf (listOf (either str luaInline));
options = {
preset = mkOption {
type = enum ["default" "none" "super-tab" "enter"];
default = "none";
description = "keymap presets";
};
};
};
in {
options.vim.autocomplete.blink-cmp = {
enable = mkEnableOption "blink.cmp";
setupOpts = mkPluginSetupOption "blink.cmp" {
sources = {
default = mkOption {
type = listOf str;
description = "Default list of sources to enable for completion.";
default = ["lsp" "path" "snippets" "buffer"];
};
};
keymap = mkOption {
type = keymapType;
default = {};
description = "blink.cmp keymap";
example = literalMD ''
```nix
vim.autocomplete.blink-cmp.setupOpts.keymap = {
preset = "none";
"<Up>" = ["select_prev" "fallback"];
"<C-n>" = [
(lib.generators.mkLuaInline ''''
function(cmp)
if some_condition then return end -- runs the next command
return true -- doesn't run the next command
end,
'''')
"select_next"
];
};
```
'';
};
};
mappings = {
complete = mkMappingOption "Complete [blink.cmp]" "<C-Space>";
confirm = mkMappingOption "Confirm [blink.cmp]" "<CR>";
next = mkMappingOption "Next item [blink.cmp]" "<Tab>";
previous = mkMappingOption "Previous item [blink.cmp]" "<S-Tab>";
close = mkMappingOption "Close [blink.cmp]" "<C-e>";
scrollDocsUp = mkMappingOption "Scroll docs up [blink.cmp]" "<C-d>";
scrollDocsDown = mkMappingOption "Scroll docs down [blink.cmp]" "<C-f>";
};
};
}

View file

@ -0,0 +1,74 @@
{
lib,
config,
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.generators) mkLuaInline;
cfg = config.vim.autocomplete.blink-cmp;
inherit (cfg) mappings;
in {
vim = mkIf cfg.enable {
lazy.plugins.blink-cmp = {
package = "blink-cmp";
setupModule = "blink.cmp";
inherit (cfg) setupOpts;
# TODO: lazy disabled until lspconfig is lazy loaded
#
# event = ["InsertEnter" "CmdlineEnter"];
};
autocomplete = {
enableSharedCmpSources = true;
blink-cmp.setupOpts = {
snippets = mkIf config.vim.snippets.luasnip.enable {
expand = mkLuaInline ''
function(snippet)
return require("luasnip").lsp_expand(snippet)
end
'';
active = mkLuaInline ''
function(filter)
if filter and filter.direction then
return require('luasnip').jumpable(filter.direction)
end
return require('luasnip').in_snippet()
end
'';
jump = mkLuaInline "function(direction) require('luasnip').jump(direction) end";
};
keymap = {
${mappings.complete} = ["show" "fallback"];
${mappings.close} = ["hide" "fallback"];
${mappings.scrollDocsUp} = ["scroll_documentation_up" "fallback"];
${mappings.scrollDocsDown} = ["scroll_documentation_down" "fallback"];
${mappings.confirm} = ["accept" "fallback"];
${mappings.next} = [
"select_next"
"snippet_forward"
(mkLuaInline ''
function(cmp)
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
has_words_before = col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
if has_words_before then
return cmp.show()
end
end
'')
"fallback"
];
${mappings.previous} = [
"select_prev"
"snippet_backward"
"fallback"
];
};
};
};
};
}

View file

@ -0,0 +1,3 @@
{
imports = [./blink-cmp.nix ./config.nix];
}

View file

@ -0,0 +1,34 @@
{
lib,
config,
...
}: let
inherit (builtins) typeOf tryEval;
inherit (lib.modules) mkIf;
inherit (lib.nvim.attrsets) mapListToAttrs;
cfg = config.vim.autocomplete;
getPluginName = plugin:
if typeOf plugin == "string"
then plugin
else if (plugin ? pname && (tryEval plugin.pname).success)
then plugin.pname
else plugin.name;
in {
vim = mkIf cfg.enableSharedCmpSources {
startPlugins = ["rtp-nvim"];
lazy.plugins =
mapListToAttrs (package: {
name = getPluginName package;
value = {
inherit package;
lazy = true;
after = ''
local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}')
require("rtp_nvim").source_after_plugin_dir(path)
'';
};
})
cfg.sourcePlugins;
};
}

View file

@ -1,5 +1,9 @@
{
imports = [
./config.nix
./options.nix
./nvim-cmp
./blink-cmp
];
}

View file

@ -3,16 +3,16 @@
config,
...
}: let
inherit (lib.modules) mkIf mkMerge;
inherit (lib.modules) mkIf;
inherit (lib.strings) optionalString;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (builtins) attrNames typeOf tryEval concatStringsSep;
borders = config.vim.ui.borders.plugins.nvim-cmp;
cfg = config.vim.autocomplete.nvim-cmp;
autocompleteCfg = config.vim.autocomplete;
luasnipEnable = config.vim.snippets.luasnip.enable;
getPluginName = plugin:
if typeOf plugin == "string"
@ -24,114 +24,102 @@
in {
config = mkIf cfg.enable {
vim = {
startPlugins = ["rtp-nvim"];
lazy.plugins = mkMerge [
(mapListToAttrs (package: {
name = getPluginName package;
value = {
inherit package;
lazy = true;
after = ''
local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}')
require("rtp_nvim").source_after_plugin_dir(path)
'';
};
})
cfg.sourcePlugins)
{
nvim-cmp = {
package = "nvim-cmp";
after = ''
${optionalString luasnipEnable "local luasnip = require('luasnip')"}
local cmp = require("cmp")
lazy.plugins = {
nvim-cmp = {
package = "nvim-cmp";
after = ''
${optionalString luasnipEnable "local luasnip = require('luasnip')"}
local cmp = require("cmp")
local kinds = require("cmp.types").lsp.CompletionItemKind
local deprio = function(kind)
return function(e1, e2)
if e1:get_kind() == kind then
return false
end
if e2:get_kind() == kind then
return true
end
return nil
local kinds = require("cmp.types").lsp.CompletionItemKind
local deprio = function(kind)
return function(e1, e2)
if e1:get_kind() == kind then
return false
end
if e2:get_kind() == kind then
return true
end
return nil
end
end
cmp.setup(${toLuaObject cfg.setupOpts})
cmp.setup(${toLuaObject cfg.setupOpts})
${optionalString config.vim.lazy.enable
(concatStringsSep "\n" (map
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
cfg.sourcePlugins))}
'';
${optionalString config.vim.lazy.enable
(concatStringsSep "\n" (map
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
autocompleteCfg.sourcePlugins))}
'';
event = ["InsertEnter" "CmdlineEnter"];
};
}
];
autocomplete.nvim-cmp = {
sources = {
nvim-cmp = null;
buffer = "[Buffer]";
path = "[Path]";
event = ["InsertEnter" "CmdlineEnter"];
};
};
autocomplete = {
enableSharedCmpSources = true;
sourcePlugins = ["cmp-buffer" "cmp-path"];
setupOpts = {
sources = map (s: {name = s;}) (attrNames cfg.sources);
window = mkIf borders.enable {
completion.border = borders.style;
documentation.border = borders.style;
nvim-cmp = {
sources = {
nvim-cmp = null;
buffer = "[Buffer]";
path = "[Path]";
};
formatting.format = cfg.format;
setupOpts = {
sources = map (s: {name = s;}) (attrNames cfg.sources);
# `cmp` and `luasnip` are defined above, in the `nvim-cmp` section
mapping = {
${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
${mappings.close} = mkLuaInline "cmp.mapping.abort()";
${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
window = mkIf borders.enable {
completion.border = borders.style;
documentation.border = borders.style;
};
${mappings.next} = mkLuaInline ''
cmp.mapping(function(fallback)
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
formatting.format = cfg.format;
if cmp.visible() then
cmp.select_next_item()
${optionalString luasnipEnable ''
elseif luasnip.locally_jumpable(1) then
luasnip.jump(1)
''}
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end)
'';
# `cmp` and `luasnip` are defined above, in the `nvim-cmp` section
mapping = {
${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
${mappings.close} = mkLuaInline "cmp.mapping.abort()";
${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
${mappings.previous} = mkLuaInline ''
cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
${optionalString luasnipEnable ''
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
''}
else
fallback()
end
end)
'';
${mappings.next} = mkLuaInline ''
cmp.mapping(function(fallback)
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
if cmp.visible() then
cmp.select_next_item()
${optionalString luasnipEnable ''
elseif luasnip.locally_jumpable(1) then
luasnip.jump(1)
''}
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end)
'';
${mappings.previous} = mkLuaInline ''
cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
${optionalString luasnipEnable ''
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
''}
else
fallback()
end
end)
'';
};
};
};
};

View file

@ -4,10 +4,10 @@
...
}: let
inherit (lib.options) mkEnableOption mkOption literalExpression literalMD;
inherit (lib.types) str attrsOf nullOr either listOf;
inherit (lib.types) str attrsOf nullOr either;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.nvim.types) mkPluginSetupOption luaInline mergelessListOf pluginType;
inherit (lib.nvim.types) mkPluginSetupOption luaInline mergelessListOf;
inherit (lib.nvim.lua) toLuaObject;
inherit (builtins) isString;
@ -107,11 +107,5 @@ in {
}
'';
};
sourcePlugins = mkOption {
type = listOf pluginType;
default = [];
description = "List of source plugins used by nvim-cmp.";
};
};
}

View file

@ -0,0 +1,15 @@
{lib, ...}: let
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.types) listOf;
inherit (lib.nvim.types) pluginType;
in {
options.vim.autocomplete = {
enableSharedCmpSources = mkEnableOption "cmp sources shared by nvim-cmp and blink.cmp";
sourcePlugins = mkOption {
type = listOf pluginType;
default = [];
description = "List of cmp source plugins.";
};
};
}

View file

@ -11,6 +11,7 @@
cfg = config.vim.lsp;
usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable;
usingBlinkCmp = config.vim.autocomplete.blink-cmp.enable;
self = import ./module.nix {inherit config lib pkgs;};
mappingDefinitions = self.options.vim.lsp.mappings;
@ -22,8 +23,8 @@
in {
config = mkIf cfg.enable {
vim = {
autocomplete.nvim-cmp = {
sources = {nvim_lsp = "[LSP]";};
autocomplete = mkIf usingNvimCmp {
nvim-cmp.sources = {nvim_lsp = "[LSP]";};
sourcePlugins = ["cmp-nvim-lsp"];
};
@ -170,6 +171,10 @@ in {
},
}
''}
${optionalString usingBlinkCmp ''
capabilities = require('blink.cmp').get_lsp_capabilities(capabilities)
''}
'';
};
};

View file

@ -8,11 +8,12 @@
inherit (lib.nvim.lua) toLuaObject;
cfg = config.vim.lsp.lspkind;
inherit (config.vim) autocomplete;
in {
config = mkIf cfg.enable {
assertions = [
{
assertion = config.vim.autocomplete.nvim-cmp.enable;
assertion = autocomplete.nvim-cmp.enable || autocomplete.blink-cmp.enable;
message = ''
While lspkind supports Neovim's native lsp upstream, using that over
nvim-cmp isn't recommended, nor supported by nvf.

View file

@ -48,7 +48,7 @@ in {
# If using nvim-cmp, otherwise set to false
type = bool;
description = "If using nvim-cmp, otherwise set to false";
default = config.vim.autocomplete.nvim-cmp.enable;
default = config.vim.autocomplete.enableSharedCmpSources;
};
};
};

View file

@ -17,8 +17,8 @@ in {
};
};
startPlugins = cfg.providers;
autocomplete.nvim-cmp = {
sources = {luasnip = "[LuaSnip]";};
autocomplete = {
nvim-cmp.sources = {luasnip = "[LuaSnip]";};
sourcePlugins = ["cmp-luasnip"];
};
};

View file

@ -20,8 +20,8 @@ in {
vim = {
startPlugins = ["nvim-treesitter"];
autocomplete.nvim-cmp = {
sources = {treesitter = "[Treesitter]";};
autocomplete = {
nvim-cmp.sources = {treesitter = "[Treesitter]";};
sourcePlugins = ["cmp-treesitter"];
};