feat(cmp): move cmp autobracket support to util class
This commit is contained in:
@ -83,21 +83,10 @@ return {
|
||||
source.group_index = source.group_index or 1
|
||||
end
|
||||
local cmp = require("cmp")
|
||||
local Kind = cmp.lsp.CompletionItemKind
|
||||
cmp.setup(opts)
|
||||
cmp.event:on("confirm_done", function(event)
|
||||
if not vim.tbl_contains(opts.auto_brackets or {}, vim.bo.filetype) then
|
||||
return
|
||||
end
|
||||
local entry = event.entry
|
||||
local item = entry:get_completion_item()
|
||||
if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) and item.insertTextFormat ~= 2 then
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local prev_char = vim.api.nvim_buf_get_text(0, cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2] + 1, {})[1]
|
||||
if prev_char ~= "(" and prev_char ~= ")" then
|
||||
local keys = vim.api.nvim_replace_termcodes("()<left>", false, false, true)
|
||||
vim.api.nvim_feedkeys(keys, "i", true)
|
||||
end
|
||||
if vim.tbl_contains(opts.auto_brackets or {}, vim.bo.filetype) then
|
||||
LazyVim.cmp.auto_brackets(event.entry)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
|
19
lua/lazyvim/util/cmp.lua
Normal file
19
lua/lazyvim/util/cmp.lua
Normal file
@ -0,0 +1,19 @@
|
||||
---@class lazyvim.util.cmp
|
||||
local M = {}
|
||||
|
||||
---@param entry cmp.Entry
|
||||
function M.auto_brackets(entry)
|
||||
local cmp = require("cmp")
|
||||
local Kind = cmp.lsp.CompletionItemKind
|
||||
local item = entry:get_completion_item()
|
||||
if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) then
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local prev_char = vim.api.nvim_buf_get_text(0, cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2] + 1, {})[1]
|
||||
if prev_char ~= "(" and prev_char ~= ")" then
|
||||
local keys = vim.api.nvim_replace_termcodes("()<left>", false, false, true)
|
||||
vim.api.nvim_feedkeys(keys, "i", true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
@ -17,6 +17,7 @@ local LazyUtil = require("lazy.core.util")
|
||||
---@field json lazyvim.util.json
|
||||
---@field lualine lazyvim.util.lualine
|
||||
---@field mini lazyvim.util.mini
|
||||
---@field cmp lazyvim.util.cmp
|
||||
local M = {}
|
||||
|
||||
---@type table<string, string|string[]>
|
||||
|
Reference in New Issue
Block a user