Initial work on massive refactor
This commit is contained in:
parent
b4f1a4f441
commit
c01b7077ff
28 changed files with 717 additions and 530 deletions
.config/nvim/lua
|
@ -36,76 +36,115 @@ function M.format(opts)
|
|||
return vim.lsp.buf.format(opts)
|
||||
end
|
||||
|
||||
-- Modified version of a function stolen from LunarVim
|
||||
function M.buf_kill(kill_command, bufnr, force)
|
||||
kill_command = kill_command or "bd"
|
||||
-- Ripped from AstroNvim
|
||||
function M.buf_close(bufnr, force)
|
||||
if not bufnr or bufnr == 0 then bufnr = vim.api.nvim_get_current_buf() end
|
||||
local buftype = vim.api.nvim_get_option_value("buftype", { buf = bufnr })
|
||||
vim.cmd(("silent! %s %d"):format((force or buftype == "terminal") and "bdelete!" or "confirm bdelete", bufnr))
|
||||
end
|
||||
|
||||
local bo = vim.bo
|
||||
local api = vim.api
|
||||
local fmt = string.format
|
||||
local fnamemodify = vim.fn.fnamemodify
|
||||
-- Ripped from AstroNvim
|
||||
function M.is_available(plugin)
|
||||
local lazy_config_avail, lazy_config = pcall(require, "lazy.core.config")
|
||||
return lazy_config_avail and lazy_config.spec.plugins[plugin] ~= nil
|
||||
end
|
||||
|
||||
if bufnr == 0 or bufnr == nil then
|
||||
bufnr = api.nvim_get_current_buf()
|
||||
-- Ripped from AstroNvim
|
||||
-- TODO: Check this on 0.10.0 release
|
||||
function M.empty_map_table()
|
||||
local maps = {}
|
||||
for _, mode in ipairs { "", "n", "v", "x", "s", "o", "!", "i", "l", "c", "t" } do
|
||||
maps[mode] = {}
|
||||
end
|
||||
|
||||
local bufname = api.nvim_buf_get_name(bufnr)
|
||||
|
||||
if not force then
|
||||
local warning
|
||||
if bo[bufnr].modified then
|
||||
warning = fmt([[No write since last change for (%s)]], fnamemodify(bufname, ":t"))
|
||||
elseif api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then
|
||||
warning = fmt([[Terminal %s will be killed]], bufname)
|
||||
end
|
||||
if warning then
|
||||
vim.ui.input({
|
||||
prompt = string.format([[%s. Close it anyway? [y]es or [n]o (default: no): ]], warning),
|
||||
}, function(choice)
|
||||
if choice:match "ye?s?" then force = true end
|
||||
end)
|
||||
if not force then return end
|
||||
if vim.fn.has "nvim-0.10.0" == 1 then
|
||||
for _, abbr_mode in ipairs { "ia", "ca", "!a" } do
|
||||
maps[abbr_mode] = {}
|
||||
end
|
||||
end
|
||||
return maps
|
||||
end
|
||||
|
||||
-- Get list of windows IDs with the buffer to close
|
||||
local windows = vim.tbl_filter(function(win)
|
||||
return api.nvim_win_get_buf(win) == bufnr
|
||||
end, api.nvim_list_wins())
|
||||
|
||||
if #windows == 0 then return end
|
||||
|
||||
if force then
|
||||
kill_command = kill_command .. "!"
|
||||
-- Ripped from AstroNvim and modified
|
||||
function M.toggle_term_cmd(opts)
|
||||
if not vim.g.user_terminals then
|
||||
vim.g.user_terminals = {}
|
||||
end
|
||||
local terms = vim.g.user_terminals
|
||||
-- if a command string is provided, create a basic table for Terminal:new() options
|
||||
if type(opts) == "string" then opts = { cmd = opts, hidden = true } end
|
||||
local num = vim.v.count > 0 and vim.v.count or 1
|
||||
-- if terminal doesn't exist yet, create it
|
||||
if not terms[opts.cmd] then terms[opts.cmd] = {} end
|
||||
if not terms[opts.cmd][num] then
|
||||
if not opts.count then opts.count = vim.tbl_count(terms) * 100 + num end
|
||||
if not opts.on_exit then opts.on_exit = function() terms[opts.cmd][num] = nil end end
|
||||
terms[opts.cmd][num] = require("toggleterm.terminal").Terminal:new(opts)
|
||||
end
|
||||
-- toggle the terminal
|
||||
terms[opts.cmd][num]:toggle()
|
||||
end
|
||||
|
||||
-- Get list of active buffers
|
||||
local buffers = vim.tbl_filter(function(buf)
|
||||
return api.nvim_buf_is_valid(buf) and bo[buf].buflisted
|
||||
end, api.nvim_list_bufs())
|
||||
|
||||
-- If there is only one buffer (which has to be the current one), vim will
|
||||
-- create a new buffer on :bd.
|
||||
-- For more than one buffer, pick the previous buffer (wrapping around if necessary)
|
||||
if #buffers > 1 then
|
||||
for i, v in ipairs(buffers) do
|
||||
if v == bufnr then
|
||||
local prev_buf_idx = i == 1 and (#buffers - 1) or (i - 1)
|
||||
local prev_buffer = buffers[prev_buf_idx]
|
||||
for _, win in ipairs(windows) do
|
||||
api.nvim_win_set_buf(win, prev_buffer)
|
||||
end
|
||||
end
|
||||
-- Ripped from AstroNvim and modified
|
||||
-- TODO: test this
|
||||
function M.file_worktree(file, worktrees)
|
||||
worktrees = worktrees or vim.g.git_worktrees
|
||||
if not worktrees then return end
|
||||
file = file or vim.fn.expand("%")
|
||||
for _, worktree in ipairs(worktrees) do
|
||||
local r = vim.fn.system({
|
||||
"git",
|
||||
"--work-tree",
|
||||
worktree.toplevel,
|
||||
"--git-dir",
|
||||
worktree.gitdir,
|
||||
"ls-files",
|
||||
"--error-unmatch",
|
||||
file
|
||||
}):wait()
|
||||
if r.code == 0 then
|
||||
return worktree
|
||||
end
|
||||
else
|
||||
vim.cmd('q!')
|
||||
end
|
||||
|
||||
-- Check if buffer still exists, to ensure the target buffer wasn't killed
|
||||
-- due to options like bufhidden=wipe.
|
||||
if api.nvim_buf_is_valid(bufnr) and bo[bufnr].buflisted then
|
||||
vim.cmd(string.format("%s %d", kill_command, bufnr))
|
||||
end
|
||||
end
|
||||
|
||||
-- Ripped from AstroNvim
|
||||
function M.which_key_register()
|
||||
if M.which_key_queue then
|
||||
local wk_avail, wk = pcall(require, "which-key")
|
||||
if wk_avail then
|
||||
for mode, registration in pairs(M.which_key_queue) do
|
||||
wk.register(registration, { mode = mode })
|
||||
end
|
||||
M.which_key_queue = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Ripped from AstroNvim
|
||||
function M.set_mappings(map_table, base)
|
||||
base = base or {}
|
||||
for mode, maps in pairs(map_table) do
|
||||
for keymap, options in pairs(maps) do
|
||||
if options then
|
||||
local cmd = options
|
||||
local keymap_opts = base
|
||||
if type(options) == "table" then
|
||||
cmd = options[1]
|
||||
keymap_opts = vim.tbl_deep_extend("force", keymap_opts, options)
|
||||
keymap_opts[1] = nil
|
||||
end
|
||||
if not cmd or keymap_opts.name then -- which-key mapping
|
||||
if not keymap_opts.name then keymap_opts.name = keymap_opts.desc end
|
||||
if not M.which_key_queue then M.which_key_queue = {} end
|
||||
if not M.which_key_queue[mode] then M.which_key_queue[mode] = {} end
|
||||
M.which_key_queue[mode][keymap] = keymap_opts
|
||||
else -- not which-key mapping
|
||||
vim.keymap.set(mode, keymap, cmd, keymap_opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if package.loaded["which-key"] then M.which_key_register() end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue