Skip to content

Instantly share code, notes, and snippets.

@rafpaf
Last active May 18, 2024 20:58
Show Gist options
  • Save rafpaf/3b7bdeedde4d6a7d6e2a3a43331eba4c to your computer and use it in GitHub Desktop.
Save rafpaf/3b7bdeedde4d6a7d6e2a3a43331eba4c to your computer and use it in GitHub Desktop.
-- Increment and decrement Mantine sizes in Neovim
-- But it's better to use the "switch" plugin
sizes = {"xs", "sm", "md", "lg", "xl"}
size_inc_map = {}
for i=1, #sizes do
local next_i = i % #sizes + 1
size_inc_map[sizes[i]] = sizes[next_i]
end
size_dec_map = {}
for i=1, #sizes do
local prev_i = (i - 2) % #sizes + 1
size_dec_map[sizes[i]] = sizes[prev_i]
end
function _G.adjust_size(direction)
local map = {}
if direction == "inc" then
map = size_inc_map
else
map = size_dec_map
end
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
col = col + 1 -- Adjust for 0-indexing
local current_line = vim.api.nvim_get_current_line()
local pattern = "%f[%a](xs)%f[%A]"
local s, e = string.find(current_line, pattern, col)
for _, size in ipairs(sizes) do
local size_pattern = "%f[%a](" .. size .. ")%f[%A]"
s, e = string.find(current_line, size_pattern, col)
if s and e then
local new_size = map[size] or size
local before = string.sub(current_line, 1, s-1)
local after = string.sub(current_line, e+1)
vim.api.nvim_set_current_line(before .. new_size .. after)
vim.api.nvim_win_set_cursor(0, {line, s-1})
return
end
end
end
vim.keymap.set("n", "++", ":lua adjust_size('inc')<CR>")
vim.keymap.set("n", "+-", ":lua adjust_size('dec')<CR>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment