mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-07-11 18:04:32 +02:00
refactor: use actions
This commit is contained in:
parent
3f404079dc
commit
f40a2a1faa
7 changed files with 100 additions and 64 deletions
|
@ -180,3 +180,13 @@ map("n", "<leader><tab><tab>", "<cmd>tabnew<cr>", { desc = "New Tab" })
|
||||||
map("n", "<leader><tab>]", "<cmd>tabnext<cr>", { desc = "Next Tab" })
|
map("n", "<leader><tab>]", "<cmd>tabnext<cr>", { desc = "Next Tab" })
|
||||||
map("n", "<leader><tab>d", "<cmd>tabclose<cr>", { desc = "Close Tab" })
|
map("n", "<leader><tab>d", "<cmd>tabclose<cr>", { desc = "Close Tab" })
|
||||||
map("n", "<leader><tab>[", "<cmd>tabprevious<cr>", { desc = "Previous Tab" })
|
map("n", "<leader><tab>[", "<cmd>tabprevious<cr>", { desc = "Previous Tab" })
|
||||||
|
|
||||||
|
-- native snippets
|
||||||
|
if vim.fn.has("nvim-0.11") == 0 then
|
||||||
|
map("s", "<Tab>", function()
|
||||||
|
return vim.snippet.active({ direction = 1 }) and "<cmd>lua vim.snippet.jump(1)<cr>" or "<Tab>"
|
||||||
|
end, { expr = true, desc = "Jump Next" })
|
||||||
|
map({ "i", "s" }, "<S-Tab>", function()
|
||||||
|
return vim.snippet.active({ direction = -1 }) and "<cmd>lua vim.snippet.jump(-1)<cr>" or "<S-Tab>"
|
||||||
|
end, { expr = true, desc = "Jump Previous" })
|
||||||
|
end
|
||||||
|
|
|
@ -43,6 +43,9 @@ return {
|
||||||
cmp.abort()
|
cmp.abort()
|
||||||
fallback()
|
fallback()
|
||||||
end,
|
end,
|
||||||
|
["<tab>"] = function(fallback)
|
||||||
|
return LazyVim.cmp.map({ "snippet_forward", "ai_accept" }, fallback)()
|
||||||
|
end,
|
||||||
}),
|
}),
|
||||||
sources = cmp.config.sources({
|
sources = cmp.config.sources({
|
||||||
{ name = "nvim_lsp" },
|
{ name = "nvim_lsp" },
|
||||||
|
@ -106,29 +109,6 @@ return {
|
||||||
table.insert(opts.sources, { name = "snippets" })
|
table.insert(opts.sources, { name = "snippets" })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
-- stylua: ignore
|
|
||||||
keys = {
|
|
||||||
{
|
|
||||||
"<tab>",
|
|
||||||
function()
|
|
||||||
return vim.snippet.active({ direction = 1 }) and "<cmd>lua vim.snippet.jump(1)<cr>"
|
|
||||||
or LazyVim.cmp.ai_accept()
|
|
||||||
or "<Tab>"
|
|
||||||
end,
|
|
||||||
expr = true, silent = true, mode = "i",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
init = function()
|
|
||||||
-- Neovim enabled snippet navigation mappings by default in v0.11
|
|
||||||
if vim.fn.has("nvim-0.11") == 0 then
|
|
||||||
vim.keymap.set({ "s" }, "<Tab>", function()
|
|
||||||
return vim.snippet.active({ direction = 1 }) and "<cmd>lua vim.snippet.jump(1)<cr>" or "<Tab>"
|
|
||||||
end, { expr = true, silent = true })
|
|
||||||
vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
|
|
||||||
return vim.snippet.active({ direction = -1 }) and "<cmd>lua vim.snippet.jump(-1)<cr>" or "<S-Tab>"
|
|
||||||
end, { expr = true, silent = true })
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
-- auto pairs
|
-- auto pairs
|
||||||
|
|
|
@ -9,26 +9,26 @@ return {
|
||||||
enable_cmp_source = vim.g.ai_cmp,
|
enable_cmp_source = vim.g.ai_cmp,
|
||||||
virtual_text = {
|
virtual_text = {
|
||||||
enabled = not vim.g.ai_cmp,
|
enabled = not vim.g.ai_cmp,
|
||||||
accept_fallback = "<tab>",
|
|
||||||
key_bindings = {
|
key_bindings = {
|
||||||
accept = "<tab>",
|
accept = false, -- handled by nvim-cmp / blink.cmp
|
||||||
next = "<M-]>",
|
next = "<M-]>",
|
||||||
prev = "<M-[>",
|
prev = "<M-[>",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config = function(_, opts)
|
},
|
||||||
LazyVim.cmp.ai_accept = function()
|
|
||||||
|
-- add ai_accept action
|
||||||
|
{
|
||||||
|
"Exafunction/codeium.nvim",
|
||||||
|
opts = function()
|
||||||
|
LazyVim.cmp.actions.ai_accept = function()
|
||||||
if require("codeium.virtual_text").get_current_completion_item() then
|
if require("codeium.virtual_text").get_current_completion_item() then
|
||||||
LazyVim.create_undo()
|
LazyVim.create_undo()
|
||||||
vim.api.nvim_input(require("codeium.virtual_text").accept())
|
vim.api.nvim_input(require("codeium.virtual_text").accept())
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if opts.virtual_text.key_bindings.accept == "<tab>" then
|
|
||||||
opts.virtual_text.key_bindings.accept = false
|
|
||||||
end
|
|
||||||
require("codeium").setup(opts)
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ return {
|
||||||
},
|
},
|
||||||
dependencies = {
|
dependencies = {
|
||||||
"codeium.nvim",
|
"codeium.nvim",
|
||||||
vim.g.ai_cmp and { "saghen/blink.compat" } or {},
|
vim.g.ai_cmp and "saghen/blink.compat" or nil,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ return {
|
||||||
enabled = not vim.g.ai_cmp,
|
enabled = not vim.g.ai_cmp,
|
||||||
auto_trigger = true,
|
auto_trigger = true,
|
||||||
keymap = {
|
keymap = {
|
||||||
accept = "<tab>",
|
accept = false, -- handled by nvim-cmp / blink.cmp
|
||||||
next = "<M-]>",
|
next = "<M-]>",
|
||||||
prev = "<M-[>",
|
prev = "<M-[>",
|
||||||
},
|
},
|
||||||
|
@ -22,20 +22,19 @@ return {
|
||||||
help = true,
|
help = true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config = function(_, opts)
|
},
|
||||||
LazyVim.cmp.ai_accept = function()
|
|
||||||
|
-- add ai_accept action
|
||||||
|
{
|
||||||
|
"zbirenbaum/copilot.lua",
|
||||||
|
opts = function()
|
||||||
|
LazyVim.cmp.actions.ai_accept = function()
|
||||||
if require("copilot.suggestion").is_visible() then
|
if require("copilot.suggestion").is_visible() then
|
||||||
LazyVim.create_undo()
|
LazyVim.create_undo()
|
||||||
require("copilot.suggestion").accept()
|
require("copilot.suggestion").accept()
|
||||||
return ""
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- tab is handled by nvim-cmp / blink.cmp
|
|
||||||
local key = opts.suggestion.keymap.accept
|
|
||||||
if key == "<tab>" then
|
|
||||||
opts.suggestion.keymap.accept = false
|
|
||||||
end
|
|
||||||
require("copilot").setup(opts)
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
|
if lazyvim_docs then
|
||||||
|
-- set to `true` to follow the main branch
|
||||||
|
-- you need to have a working rust toolchain to build the plugin
|
||||||
|
-- in this case.
|
||||||
|
vim.g.lazyvim_blink_main = false
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
"hrsh7th/nvim-cmp",
|
"hrsh7th/nvim-cmp",
|
||||||
|
@ -5,7 +12,8 @@ return {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"saghen/blink.cmp",
|
"saghen/blink.cmp",
|
||||||
version = "*",
|
version = not vim.g.lazyvim_blink_main and "*",
|
||||||
|
build = vim.g.lazyvim_blink_main and "cargo build --release",
|
||||||
opts_extend = {
|
opts_extend = {
|
||||||
"sources.completion.enabled_providers",
|
"sources.completion.enabled_providers",
|
||||||
"sources.compat",
|
"sources.compat",
|
||||||
|
@ -59,11 +67,8 @@ return {
|
||||||
|
|
||||||
keymap = {
|
keymap = {
|
||||||
preset = "enter",
|
preset = "enter",
|
||||||
["<tab>"] = {
|
["<Tab>"] = {
|
||||||
"snippet_forward",
|
LazyVim.cmp.map({ "snippet_forward", "ai_accept" }),
|
||||||
function()
|
|
||||||
return LazyVim.cmp.ai_accept()
|
|
||||||
end,
|
|
||||||
"fallback",
|
"fallback",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
return {
|
return {
|
||||||
|
-- disable builtin snippet support
|
||||||
|
{ "garymjr/nvim-snippets", enabled = false },
|
||||||
|
|
||||||
|
-- add luasnip
|
||||||
{
|
{
|
||||||
"L3MON4D3/LuaSnip",
|
"L3MON4D3/LuaSnip",
|
||||||
lazy = true,
|
lazy = true,
|
||||||
|
@ -19,6 +23,19 @@ return {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
-- add snippet_forward action
|
||||||
|
{
|
||||||
|
"L3MON4D3/LuaSnip",
|
||||||
|
opts = function()
|
||||||
|
LazyVim.cmp.actions.snippet_forward = function()
|
||||||
|
if require("luasnip").jumpable(1) then
|
||||||
|
require("luasnip").jump(1)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
-- nvim-cmp integration
|
-- nvim-cmp integration
|
||||||
{
|
{
|
||||||
"nvim-cmp",
|
"nvim-cmp",
|
||||||
|
@ -34,23 +51,21 @@ return {
|
||||||
end,
|
end,
|
||||||
-- stylua: ignore
|
-- stylua: ignore
|
||||||
keys = {
|
keys = {
|
||||||
{
|
|
||||||
"<tab>",
|
|
||||||
function()
|
|
||||||
return require("luasnip").jumpable(1) and "<Plug>luasnip-jump-next"
|
|
||||||
or LazyVim.cmp.ai_accept()
|
|
||||||
or "<tab>"
|
|
||||||
end,
|
|
||||||
expr = true, silent = true, mode = "i",
|
|
||||||
},
|
|
||||||
{ "<tab>", function() require("luasnip").jump(1) end, mode = "s" },
|
{ "<tab>", function() require("luasnip").jump(1) end, mode = "s" },
|
||||||
{ "<s-tab>", function() require("luasnip").jump(-1) end, mode = { "i", "s" } },
|
{ "<s-tab>", function() require("luasnip").jump(-1) end, mode = { "i", "s" } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"garymjr/nvim-snippets",
|
|
||||||
enabled = false,
|
|
||||||
},
|
|
||||||
|
|
||||||
-- TODO: blink.cmp integration
|
-- blink.cmp integration
|
||||||
|
{
|
||||||
|
"saghen/blink.cmp",
|
||||||
|
optional = true,
|
||||||
|
opts = {
|
||||||
|
accept = {
|
||||||
|
expand_snippet = function(...)
|
||||||
|
return require("luasnip").lsp_expand(...)
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,35 @@
|
||||||
---@class lazyvim.util.cmp
|
---@class lazyvim.util.cmp
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@return string?
|
---@alias lazyvim.util.cmp.Action fun():boolean?
|
||||||
function M.ai_accept() end
|
---@type table<string, lazyvim.util.cmp.Action>
|
||||||
|
M.actions = {
|
||||||
|
-- Native Snippets
|
||||||
|
snippet_forward = function()
|
||||||
|
if vim.snippet.active({ direction = 1 }) then
|
||||||
|
vim.schedule(function()
|
||||||
|
vim.snippet.jump(1)
|
||||||
|
end)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param actions string[]
|
||||||
|
---@param fallback? string|fun()
|
||||||
|
function M.map(actions, fallback)
|
||||||
|
return function()
|
||||||
|
for _, name in ipairs(actions) do
|
||||||
|
if M.actions[name] then
|
||||||
|
local ret = M.actions[name]()
|
||||||
|
if ret then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return type(fallback) == "function" and fallback() or fallback
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@alias Placeholder {n:number, text:string}
|
---@alias Placeholder {n:number, text:string}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue