style: run stylua with rocks.nvim config

This commit is contained in:
Marc Jakobi
2024-06-05 16:34:59 +02:00
parent a3313d1e7f
commit 9acc3d1053
12 changed files with 498 additions and 493 deletions
+98 -98
View File
@@ -1,32 +1,32 @@
local loader = require('lz.n.loader')
local loader = require("lz.n.loader")
---@class LzKeysHandler: LzHandler
---@type LzKeysHandler
local M = {
pending = {},
type = 'keys',
---@param value string|LzKeysSpec
---@param mode? string
---@return LzKeys
parse = function(value, mode)
value = type(value) == 'string' and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
ret.lhs = ret[1] or ''
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or 'n'
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == 'string' and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. ' (' .. table.concat(ft, ', ') .. ')'
end
if ret.mode ~= 'n' then
ret.id = ret.id .. ' (' .. ret.mode .. ')'
end
return ret
end,
pending = {},
type = "keys",
---@param value string|LzKeysSpec
---@param mode? string
---@return LzKeys
parse = function(value, mode)
value = type(value) == "string" and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
ret.lhs = ret[1] or ""
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or "n"
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == "string" and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. " (" .. table.concat(ft, ", ") .. ")"
end
if ret.mode ~= "n" then
ret.id = ret.id .. " (" .. ret.mode .. ")"
end
return ret
end,
}
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
@@ -34,102 +34,102 @@ local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
---@param keys LzKeys
---@return LzKeysBase
local function get_opts(keys)
---@type LzKeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= 'number' and not skip[k] then
opts[k] = v
---@type LzKeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= "number" and not skip[k] then
opts[k] = v
end
end
end
return opts
return opts
end
-- Create a mapping if it is managed by lz.n
---@param keys LzKeys
---@param buf integer?
local function set(keys, buf)
if keys.rhs then
local opts = get_opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
if keys.rhs then
local opts = get_opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
end
-- Delete a mapping and create the real global
-- mapping when needed
---@param keys LzKeys
local function del(keys)
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
set(keys)
end
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
set(keys)
end
end
---@param keys LzKeys
local function add_keys(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == 'a' then
lhs = lhs .. '<C-]>'
end
local feed = vim.api.nvim_replace_termcodes('<Ignore>' .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, 'i', false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd('FileType', {
pattern = keys.ft,
callback = function(event)
if M.pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
end
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == "a" then
lhs = lhs .. "<C-]>"
end
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, "i", false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
callback = function(event)
if M.pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
end
end
---@param plugin LzPlugin
function M.add(plugin)
-- TODO add plugin to M.pending
for _, key in pairs(plugin.keys or {}) do
add_keys(key)
end
-- TODO add plugin to M.pending
for _, key in pairs(plugin.keys or {}) do
add_keys(key)
end
end
return M