2023-11-23 21:27:17 +01:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.set_title()
|
|
|
|
local title = " %t"
|
|
|
|
local f = io.popen([[zsh -c '
|
|
|
|
source $XDG_CONFIG_HOME/zsh/configs/autogenerated/hashes
|
|
|
|
print -Pn "$USER@$HOST [%3~] "
|
|
|
|
']])
|
|
|
|
if f ~= nil then
|
|
|
|
title = f:read("*a") or ""
|
|
|
|
title = title .. " %t"
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
vim.opt.titlestring = title
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.format_filter(client)
|
|
|
|
local filetype = vim.bo.filetype
|
|
|
|
local n = require "null-ls"
|
|
|
|
local s = require "null-ls.sources"
|
|
|
|
local method = n.methods.FORMATTING
|
|
|
|
local available_formatters = s.get_available(filetype, method)
|
|
|
|
|
|
|
|
if #available_formatters > 0 then
|
|
|
|
return client.name == "null-ls"
|
|
|
|
elseif client.supports_method "textDocument/formatting" then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.format(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.filter = opts.filter or M.format_filter
|
|
|
|
return vim.lsp.buf.format(opts)
|
|
|
|
end
|
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- 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
|
2023-11-23 21:27:17 +01:00
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- 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
|
2023-11-23 21:27:17 +01:00
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- 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] = {}
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
2024-01-24 00:48:37 +01:00
|
|
|
if vim.fn.has "nvim-0.10.0" == 1 then
|
|
|
|
for _, abbr_mode in ipairs { "ia", "ca", "!a" } do
|
|
|
|
maps[abbr_mode] = {}
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
|
|
|
end
|
2024-01-24 00:48:37 +01:00
|
|
|
return maps
|
|
|
|
end
|
2023-11-23 21:27:17 +01:00
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- Ripped from AstroNvim and modified
|
|
|
|
function M.toggle_term_cmd(opts)
|
|
|
|
if not vim.g.user_terminals then
|
|
|
|
vim.g.user_terminals = {}
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
2024-01-24 00:48:37 +01:00
|
|
|
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
|
2023-11-23 21:27:17 +01:00
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- 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
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
|
|
|
end
|
2024-01-24 00:48:37 +01:00
|
|
|
end
|
2023-11-23 21:27:17 +01:00
|
|
|
|
2024-01-24 00:48:37 +01:00
|
|
|
-- 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
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
2024-01-24 00:48:37 +01:00
|
|
|
end
|
|
|
|
if package.loaded["which-key"] then M.which_key_register() end
|
2023-11-23 21:27:17 +01:00
|
|
|
end
|
|
|
|
|
2024-01-24 10:53:48 +01:00
|
|
|
function M.lsp_on_attach(client, bufnr)
|
|
|
|
local lsp_mappings = M.empty_map_table()
|
|
|
|
|
|
|
|
-- TODO: CONDITIONALLY GATE THIS
|
|
|
|
lsp_mappings.n["<leader>lf"] = { function() vim.lsp.buf.format() end, desc = "Format" }
|
|
|
|
|
|
|
|
lsp_mappings.n["<leader>ld"] = { function() vim.diagnostic.open_float() end, desc = "Hover diagnostics" }
|
|
|
|
lsp_mappings.n["[d"] = { function() vim.diagnostic.goto_prev() end, desc = "Previous diagnostic" }
|
|
|
|
lsp_mappings.n["]d"] = { function() vim.diagnostic.goto_next() end, desc = "Next diagnostic" }
|
|
|
|
lsp_mappings.n["gl"] = { function() vim.diagnostic.open_float() end, desc = "Hover diagnostics" }
|
|
|
|
|
|
|
|
if M.is_available("telescope.nvim") then
|
|
|
|
lsp_mappings.n["<leader>lD"] = {
|
|
|
|
function() require("telescope.builtin").diagnostics() end,
|
|
|
|
desc = "Search diagnostics",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if M.is_available "mason-lspconfig.nvim" then
|
|
|
|
lsp_mappings.n["<leader>li"] = { "<cmd>LspInfo<cr>", desc = "LSP information" }
|
|
|
|
end
|
|
|
|
|
|
|
|
if M.is_available "null-ls.nvim" then
|
|
|
|
lsp_mappings.n["<leader>lI"] = { "<cmd>NullLsInfo<cr>", desc = "Null-ls information" }
|
|
|
|
end
|
|
|
|
|
|
|
|
if client.supports_method "textDocument/codeAction" then
|
|
|
|
lsp_mappings.n["<leader>la"] = {
|
|
|
|
function() vim.lsp.buf.code_action() end,
|
|
|
|
desc = "LSP code action",
|
|
|
|
}
|
|
|
|
lsp_mappings.v["<leader>la"] = lsp_mappings.n["<leader>la"]
|
|
|
|
end
|
|
|
|
|
|
|
|
M.set_mappings(lsp_mappings, { buffer = bufnr })
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-23 21:27:17 +01:00
|
|
|
return M
|