8000 mini.nvim/lua/mini/pairs.lua at main · saep/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
639 lines (559 loc) · 25.6 KB

pairs.lua

File metadata and controls

639 lines (559 loc) · 25.6 KB
buf_pairs.bs[pair] = register.bs == true and true or nil
buf_pairs.cr[pair] = register.cr == true and true or nil
end
H.unregister_pair = function(pair, mode, buffer)
local mode_pairs = H.registered_pairs[mode]
if not (mode_pairs and mode_pairs[buffer]) then return end
local buf_pairs = mode_pairs[buffer]
buf_pairs.bs[pair], buf_pairs.cr[pair] = nil, nil
end
H.is_pair_registered = function(pair, mode, key)
local mode_pairs = H.registered_pairs[mode]
if not mode_pairs then return false end
if mode_pairs['all'][key][pair] then return true end
local buf_pairs = mode_pairs[vim.api.nvim_get_current_buf()]
if not buf_pairs then return false end
return buf_pairs[key][pair] == true
end
H.ensure_cr_bs = function(mode)
local has_any_cr_pair, has_any_bs_pair = false, false
for _, pair_tbl in pairs(H.registered_pairs[mode]) do
has_any_cr_pair = has_any_cr_pair or not vim.tbl_isempty(pair_tbl.cr)
has_any_bs_pair = has_any_bs_pair or not vim.tbl_isempty(pair_tbl.bs)
end
-- NOTE: this doesn't distinguish between global and buffer mappings. Both
-- <BS> and <CR> should work as normal even if no pairs are registered
-- NOTE: do not autocreate mappings if there is already one present. This
-- allows creating more complicated `<CR>`/`<BS>` mappings and not worry
-- about the initialization/setup order.
if has_any_bs_pair and vim.fn.maparg('<BS>', mode) == '' then
-- Use not `silent` in Command mode to make it redraw
local opts = { silent = mode ~= 'c', expr = true, replace_keycodes = false, desc = 'MiniPairs <BS>' }
H.map(mode, '<BS>', 'v:lua.MiniPairs.bs()', opts)
end
if mode == 'i' and has_any_cr_pair and vim.fn.maparg('<CR>', mode) == '' then
local opts = { expr = true, replace_keycodes = false, desc = 'MiniPairs <CR>' }
H.map(mode, '<CR>', 'v:lua.MiniPairs.cr()', opts)
end
end
-- Work with pair_info --------------------------------------------------------
H.validate_pair_info = function(x, prefix)
prefix = prefix or 'pair_info'
H.check_type(prefix, x, 'table')
x = vim.tbl_deep_extend('force', H.default_pair_info, x)
H.check_type(prefix .. '.action', x.action, 'string')
H.check_type(prefix .. '.pair', x.pair, 'string')
H.check_type(prefix .. '.neigh_pattern', x.neigh_pattern, 'string')
H.check_type(prefix .. '.register', x.register, 'table')
H.check_type(prefix .. '.register.bs', x.register.bs, 'boolean')
H.check_type(prefix .. '.register.cr', x.register.cr, 'boolean')
return x
end
H.pair_info_to_map_rhs = function(x)
return string.format('v:lua.MiniPairs.%s(%s, %s)', x.action, vim.inspect(x.pair), vim.inspect(x.neigh_pattern))
end
H.infer_mapping_description = function(x)
local action_name = x.action:sub(1, 1):upper() .. x.action:sub(2)
return string.format('%s action for %s pair', action_name, vim.inspect(x.pair))
end
-- Utilities ------------------------------------------------------------------
H.error = function(msg) error('(mini.pairs) ' .. msg, 0) end
H.check_type = function(name, val, ref, allow_nil)
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
end
H.get_neigh = function(neigh_type)
local is_command_mode = vim.fn.mode() == 'c'
-- Get line and add '\r' and '\n' to always return 2 characters
local line = is_command_mode and vim.fn.getcmdline() or vim.api.nvim_get_current_line()
line = '\r' .. line .. '\n'
-- Get start character index accounting for added '\r' at the start
local start = is_command_mode and vim.fn.charidx(line, vim.fn.getcmdpos()) or vim.fn.charcol('.')
start = start - 1
return vim.fn.strcharpart(line, start + (neigh_type == 'right' and 1 or 0), neigh_type == 'whole' and 2 or 1)
end
H.neigh_match = function(pattern) return H.get_neigh('whole'):find(pattern or '') ~= nil end
H.get_open_char = function(x) return vim.fn.strcharpart(x, 0, 1) end
H.get_close_char = function(x) return vim.fn.strcharpart(x, 1, 1) end
H.get_arrow_key = function(key)
return vim.fn.mode() == 'i' and (key == 'right' and H.keys.right_undo or H.keys.left_undo)
or (key == 'right' and H.keys.right or H.keys.left)
end
H.map = function(mode, lhs, rhs, opts)
if lhs == '' then return end
opts = vim.tbl_deep_extend('force', { silent = true }, opts or {})
vim.keymap.set(mode, lhs, rhs, opts)
end
H.with_temp_option = function(name, value)
-- Cache option only once to not override it later with temporary set value
-- Like in case of `nvim_feedkeys('(\r(\r')`
if H.options_cache[name] == nil then H.options_cache[name] = vim.o[name] end
vim.o[name] = value
H.restore_option_later(name)
end
H.restore_option_later = vim.schedule_wrap(function(name)
if H.options_cache[name] == nil then return end
vim.o[name] = H.options_cache[name]
H.options_cache[name] = nil
end)
return MiniPairs
0