Files
nvim/lua/helpers.lua
T

174 lines
4.6 KiB
Lua
Raw Normal View History

2025-04-12 23:49:02 +01:00
local helpers = {}
--- Creates a keymap to edit a certain config file
--- @param map string the keys that follow <Leader>e
--- @param path string the relative path from the config root
helpers.edit_cf = function(map, path)
vim.keymap.set('n', '<Leader>e' .. map, function()
vim.cmd('tabedit ' .. vim.fn.stdpath 'config' .. '/' .. path)
end, { desc = 'Edit ' .. path .. ' in a new tab' })
end
2025-07-24 14:27:38 +01:00
helpers.map = function(keys, func, opts, mode)
mode = mode or 'n'
vim.keymap.set(mode, keys, func, opts)
end
2025-04-12 23:49:02 +01:00
helpers.edit_cf('h', '/lua/helpers.lua')
2026-07-03 16:05:57 +01:00
---@param opts? { cmd?: string, win_opts?: table, buf_opts?: table }
helpers.open_term = function(opts, buf_opts)
opts = opts or {}
local cmd = opts.cmd or ''
local win_opts = opts.win_opts
local buf_options = buf_opts or opts.buf_opts or {
bufhidden = 'wipe',
modifiable = false,
}
2025-11-07 22:10:53 +00:00
local buf = vim.api.nvim_create_buf(false, true)
2026-07-08 08:58:18 +01:00
if buf_options.name then
vim.api.nvim_buf_set_name(buf, buf_options.name)
buf_options.name = nil
end
2026-07-03 16:05:57 +01:00
for k, v in pairs(buf_options) do
vim.api.nvim_set_option_value(k, v, { buf = buf })
end
2025-11-07 22:10:53 +00:00
local height = math.ceil(vim.o.lines * 0.9)
local width = math.ceil(vim.o.columns * 0.9)
2026-07-03 16:05:57 +01:00
local win = vim.api.nvim_open_win(buf, true, win_opts or {
2025-11-07 22:10:53 +00:00
style = 'minimal',
relative = 'editor',
width = width,
height = height,
row = math.ceil((vim.o.lines - height) / 2),
col = math.ceil((vim.o.columns - width) / 2),
border = 'single',
})
vim.api.nvim_set_current_win(win)
2026-07-03 16:05:57 +01:00
vim.fn.jobstart(cmd, {
2025-11-07 22:10:53 +00:00
term = true,
on_exit = function(_, _, _)
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end,
})
vim.cmd.startinsert()
end
2025-11-10 09:19:30 +00:00
helpers.has_copilot = function()
2026-06-16 16:26:28 +01:00
return vim.fn.getenv 'COPILOT_API_KEY' ~= vim.NIL
end
helpers.open_file_modal = function(path, title)
local width = math.floor(vim.o.columns * 0.85)
local height = math.floor(vim.o.lines * 0.85)
local row = math.floor((vim.o.lines - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_open_win(buf, true, {
relative = 'editor',
width = width,
height = height,
row = row,
col = col,
style = 'minimal',
border = 'rounded',
title = ' ' .. title .. ' ',
title_pos = 'center',
})
vim.api.nvim_buf_set_name(buf, path)
local lines = vim.fn.readfile(path)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].filetype = 'markdown'
vim.bo[buf].buftype = 'nofile'
vim.bo[buf].bufhidden = 'wipe'
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = false
vim.bo[buf].readonly = true
vim.wo.wrap = false
vim.wo.number = false
vim.wo.relativenumber = false
vim.wo.cursorline = true
vim.keymap.set('n', 'q', '<cmd>close<CR>', {
buffer = buf,
silent = true,
desc = 'Close cheatsheet',
})
vim.keymap.set('n', '<Esc>', '<cmd>close<CR>', {
buffer = buf,
silent = true,
desc = 'Close cheatsheet',
})
2025-11-10 09:19:30 +00:00
end
2026-07-03 13:48:02 +01:00
local function get_formatter_bin(formatter)
return vim.fn.stdpath 'data' .. '/mason/bin/' .. formatter
end
local formatter_args = {
pint = function(file)
return {
get_formatter_bin 'pint',
'--stdin-filename',
file,
}
end,
eslint_d = function(file)
return {
get_formatter_bin 'eslint_d',
'--stdin',
'--stdin-filename',
file,
'--fix-to-stdout',
}
end,
stylua = function(file)
return {
get_formatter_bin 'stylua',
'--stdin-filepath',
file,
'-',
}
end,
}
helpers.format_buffer = function(formatter, buf, cwd)
local file = vim.api.nvim_buf_get_name(buf)
local input = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n')
local result = vim.system(formatter_args[formatter](file), {
text = true,
stdin = input,
cwd = cwd,
}):wait()
if result.code ~= 0 then
vim.notify(result.stderr, vim.log.levels.ERROR)
return
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(result.stdout, '\n', { plain = true }))
end
2026-07-09 16:43:59 +01:00
helpers.create_bookmark = function(key, bookmark)
helpers.map('<Leader>b' .. key, function()
vim.cmd('edit ' .. bookmark)
end, { desc = 'Navigate to ' .. bookmark })
end
2025-04-12 23:49:02 +01:00
return helpers
2026-07-08 08:58:18 +01:00