diff options
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin.lua | 5 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/autocmds.lua | 12 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/diagnostic.lua | 12 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/keymaps.lua | 3 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/lsp/handlers.lua | 6 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/plugins/dap.lua | 11 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/plugins/noice.lua | 18 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/buffer.lua | 41 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua | 48 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/debug.lua | 36 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/diagnostic.lua | 157 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/documentation.lua | 118 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/fs.lua | 48 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/hover.lua | 132 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/init.lua | 36 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/job.lua | 97 | ||||
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/utils/status.lua | 30 |
17 files changed, 36 insertions, 774 deletions
diff --git a/nvim/.config/nvim/lua/tobyvin.lua b/nvim/.config/nvim/lua/tobyvin.lua index c75ed52..212fdf2 100644 --- a/nvim/.config/nvim/lua/tobyvin.lua +++ b/nvim/.config/nvim/lua/tobyvin.lua @@ -5,6 +5,9 @@ require("tobyvin.keymaps") require("tobyvin.lsp") require("tobyvin.diagnostic") require("tobyvin.lazy") -require("tobyvin.utils.dashboard") vim.cmd.colorscheme("gruvbox") + +-- if require("tobyvin.utils").normal_startup() then +-- require("tobyvin.utils.dashboard") +-- end diff --git a/nvim/.config/nvim/lua/tobyvin/autocmds.lua b/nvim/.config/nvim/lua/tobyvin/autocmds.lua index 4bc2671..2c386de 100644 --- a/nvim/.config/nvim/lua/tobyvin/autocmds.lua +++ b/nvim/.config/nvim/lua/tobyvin/autocmds.lua @@ -25,13 +25,23 @@ vim.api.nvim_create_autocmd("CmdlineLeave", { vim.api.nvim_create_autocmd("VimLeavePre", { group = vim.api.nvim_create_augroup("session", { clear = true }), callback = function() - if #vim.fn.getbufinfo({ buflisted = 1, bufloaded = 1 }) > 0 and #vim.fn.argv() == 0 then + if vim.fn.argc() == 0 and #vim.fn.getbufinfo({ buflisted = 1, bufloaded = 1 }) > 0 then pcall(require("tobyvin.utils.session").write) end end, desc = "write session on vim exit", }) +vim.api.nvim_create_autocmd("VimEnter", { + group = vim.api.nvim_create_augroup("dashboard", { clear = true }), + callback = function() + if vim.fn.argc() == 0 then + require("tobyvin.utils.dashboard") + end + end, + desc = "show dashboard on startup", +}) + vim.api.nvim_create_autocmd("FocusLost", { group = augroup, pattern = "*", diff --git a/nvim/.config/nvim/lua/tobyvin/diagnostic.lua b/nvim/.config/nvim/lua/tobyvin/diagnostic.lua index 88953bb..0b789b0 100644 --- a/nvim/.config/nvim/lua/tobyvin/diagnostic.lua +++ b/nvim/.config/nvim/lua/tobyvin/diagnostic.lua @@ -1,5 +1,3 @@ -local diagnostic = require("tobyvin.utils.diagnostic") - vim.diagnostic.config({ virtual_text = { source = "if_many", @@ -15,14 +13,12 @@ vim.diagnostic.config({ }, }) -vim.fn.sign_define("DiagnosticSignError", diagnostic.signs.error) -vim.fn.sign_define("DiagnosticSignWarn", diagnostic.signs.warn) -vim.fn.sign_define("DiagnosticSignInfo", diagnostic.signs.info) -vim.fn.sign_define("DiagnosticSignHint", diagnostic.signs.hint) +vim.fn.sign_define("DiagnosticSignError", { text = " ", texthl = "DiagnosticSignHint" }) +vim.fn.sign_define("DiagnosticSignWarn", { text = " ", texthl = "DiagnosticSignInfo" }) +vim.fn.sign_define("DiagnosticSignInfo", { text = " ", texthl = "DiagnosticSignWarn" }) +vim.fn.sign_define("DiagnosticSignHint", { text = " ", texthl = "DiagnosticSignError" }) vim.keymap.set("n", "gl", vim.diagnostic.open_float, { desc = "open diagnostic float" }) vim.keymap.set("n", "gL", vim.diagnostic.setqflist, { desc = "qf diagnostic" }) vim.keymap.set("n", "]g", vim.diagnostic.goto_next, { desc = "next diagnostic" }) vim.keymap.set("n", "[g", vim.diagnostic.goto_prev, { desc = "prev diagnostic" }) -vim.keymap.set("n", "]G", diagnostic.goto_next_workspace, { desc = "next workspace diagnostic" }) -vim.keymap.set("n", "[G", diagnostic.goto_prev_workspace, { desc = "prev workspace diagnostic" }) diff --git a/nvim/.config/nvim/lua/tobyvin/keymaps.lua b/nvim/.config/nvim/lua/tobyvin/keymaps.lua index f653c92..13c0b59 100644 --- a/nvim/.config/nvim/lua/tobyvin/keymaps.lua +++ b/nvim/.config/nvim/lua/tobyvin/keymaps.lua @@ -1,9 +1,6 @@ vim.keymap.set("n", "gn", vim.cmd.bnext, { desc = "go to next buffer in the buffer list" }) vim.keymap.set("n", "gp", vim.cmd.bprevious, { desc = "go to previous buffer in the buffer list" }) -vim.keymap.set("n", "<C-u>", "<C-u>zz", { desc = "up half page and center" }) -vim.keymap.set("n", "<C-d>", "<C-d>zz", { desc = "down half page and center" }) - vim.keymap.set("n", "<a-j>", "<CMD>m +1<CR>", { desc = "move line down" }) vim.keymap.set("n", "<a-k>", "<CMD>m -2<CR>", { desc = "move line up" }) vim.keymap.set("v", "<a-k>", "<CMD>m '<-2<CR>gv=gv", { desc = "move selection up" }) diff --git a/nvim/.config/nvim/lua/tobyvin/lsp/handlers.lua b/nvim/.config/nvim/lua/tobyvin/lsp/handlers.lua index 12c5c3b..9df2915 100644 --- a/nvim/.config/nvim/lua/tobyvin/lsp/handlers.lua +++ b/nvim/.config/nvim/lua/tobyvin/lsp/handlers.lua @@ -54,12 +54,6 @@ return { return result, err end, - ["window/showMessage"] = function(_, result, ctx) - vim.notify(string.format("%s", result.message), 5 - result.type, { - title = string.format("[LSP] %s", vim.lsp.get_client_by_id(ctx.client_id)), - }) - end, - ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "single", }), diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua b/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua index a263c70..8d64977 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua @@ -74,12 +74,11 @@ function M.config() end end - local signs = require("tobyvin.utils.debug").signs - vim.fn.sign_define("DapBreakpoint", signs.breakpoint) - vim.fn.sign_define("DapBreakpointCondition", signs.condition) - vim.fn.sign_define("DapBreakpointRejected", signs.rejected) - vim.fn.sign_define("DapStopped", signs.stopped) - vim.fn.sign_define("DapLogPoint", signs.logpoint) + vim.fn.sign_define("DapBreakpoint", { text = " ", texthl = "debugBreakpoint" }) + vim.fn.sign_define("DapBreakpointCondition", { text = "ﳁ ", texthl = "debugBreakpoint" }) + vim.fn.sign_define("DapBreakpointRejected", { text = " ", texthl = "debugBreakpoint" }) + vim.fn.sign_define("DapLogPoint", { text = " ", texthl = "debugBreakpoint" }) + vim.fn.sign_define("DapStopped", { text = " ", texthl = "debugBreakpoint" }) end return M diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/noice.lua b/nvim/.config/nvim/lua/tobyvin/plugins/noice.lua index 8a9e78e..4831175 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/noice.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/noice.lua @@ -31,7 +31,7 @@ local M = { }, format_done = { { - require("tobyvin.utils.status").signs.done.text, + " ", hl_group = "NoiceLspProgressDone", }, { "{data.progress.title} ", hl_group = "NoiceLspProgressTitle" }, @@ -94,28 +94,24 @@ local M = { function M.init() vim.api.nvim_set_hl(0, "NoiceLspProgressSpinner", { - link = require("tobyvin.utils.status").signs.spinner.texthl, + link = "DiagnosticSignInfo", }) vim.api.nvim_set_hl(0, "NoiceLspProgressDone", { - link = require("tobyvin.utils.status").signs.done.texthl, + link = "DiffAdd", }) - vim.keymap.set("n", "<leader>nn", function() - require("noice").cmd("last") - end, { desc = "last notification" }) - vim.keymap.set({ "n", "i", "s" }, "<c-d>", function() if not require("noice.lsp").scroll(4) then - return "<C-d>zz" + return "<c-d>" end - end, { desc = "up half page and center", expr = true }) + end, { desc = "up half page", expr = true }) vim.keymap.set({ "n", "i", "s" }, "<c-u>", function() if not require("noice.lsp").scroll(-4) then - return "<C-u>zz" + return "<c-u>" end - end, { desc = "down half page and center", expr = true }) + end, { desc = "down half page", expr = true }) end return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/buffer.lua b/nvim/.config/nvim/lua/tobyvin/utils/buffer.lua deleted file mode 100644 index e3b943c..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/buffer.lua +++ /dev/null @@ -1,41 +0,0 @@ -local Path = require("plenary").path -local M = {} - -M.get_visual_range = function() - local start_pos = vim.fn.getpos("v") - local end_pos = vim.fn.getcurpos() - return { start_pos[2], end_pos[2] } -end - -M.popup = function(file_path) - local buf = vim.api.nvim_create_buf(false, true) - - vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe") - - local width = vim.api.nvim_get_option("columns") - local height = vim.api.nvim_get_option("lines") - - local win_height = math.ceil(height * 0.8 - 4) - local win_width = math.ceil(width * 0.8) - - local row = math.ceil((height - win_height) / 2 - 1) - local col = math.ceil((width - win_width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = win_width, - height = win_height, - row = row, - col = col, - border = "single", - } - - local win = vim.api.nvim_open_win(buf, true, opts) - vim.api.nvim_win_set_option(win, "cursorline", true) - vim.api.nvim_buf_set_option(buf, "modifiable", true) - vim.api.nvim_command("$read" .. file_path) - vim.api.nvim_buf_set_option(0, "modifiable", false) -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua b/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua index ab07add..d5f9f66 100644 --- a/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua +++ b/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua @@ -25,46 +25,6 @@ local sections = setmetatable({}, { end, }) -local function should_skip() - -- don't start when opening a file - if vim.fn.argc() > 0 then - return true - end - - -- skip stdin - if vim.fn.line2byte(vim.fn.line("$")) ~= -1 then - return true - end - - -- Handle nvim -M - if not vim.o.modifiable then - return true - end - - for _, arg in pairs(vim.v.argv) do - -- whitelisted arguments - -- always open - if arg == "--startuptime" then - return false - end - - -- blacklisted arguments - -- always skip - if - arg == "-b" - -- commands, typically used for scripting - or arg == "-c" - or vim.startswith(arg, "+") - or arg == "-S" - then - return true - end - end - - -- base case: don't skip - return false -end - local function max_len(lines) local max = 0 for _, line in ipairs(lines) do @@ -95,9 +55,9 @@ local function render(buf, win) vim.bo[buf].modifiable = false end -if should_skip() then - return -end +-- if require("tobyvin.utils").normal_startup() then +-- return +-- end local curr_buf = vim.api.nvim_get_current_buf() local buf = vim.api.nvim_create_buf(false, true) @@ -207,3 +167,5 @@ vim.api.nvim_create_autocmd("User", { end, desc = "dashboard lazy stats", }) + +print("setup dashboard") diff --git a/nvim/.config/nvim/lua/tobyvin/utils/debug.lua b/nvim/.config/nvim/lua/tobyvin/utils/debug.lua deleted file mode 100644 index 6327ca0..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/debug.lua +++ /dev/null @@ -1,36 +0,0 @@ -local M = {} - -M.signs = { - breakpoint = { text = " ", texthl = "debugBreakpoint" }, - condition = { text = "ﳁ ", texthl = "debugBreakpoint" }, - rejected = { text = " ", texthl = "debugBreakpoint" }, - logpoint = { text = " ", texthl = "debugBreakpoint" }, - stopped = { text = " ", texthl = "debugBreakpoint", linehl = "debugPC", numhl = "debugPC" }, -} - -M.notifs = {} - -M.get_notif_data = function(id, token) - if not M.notifs[id] then - M.notifs[id] = {} - end - - if not M.notifs[id][token] then - M.notifs[id][token] = {} - end - - return M.notifs[id][token] -end - -M.format_title = function(title, client) - if type(client) == "table" then - client = client.name - end - return client .. (#title > 0 and ": " .. title or "") -end - -M.format_message = function(message, percentage) - return (percentage and percentage .. "%\t" or "") .. (message or "") -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/diagnostic.lua b/nvim/.config/nvim/lua/tobyvin/utils/diagnostic.lua deleted file mode 100644 index 9a41639..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/diagnostic.lua +++ /dev/null @@ -1,157 +0,0 @@ -local M = {} - -M.signs = { - hint = { text = " ", texthl = "DiagnosticSignHint" }, - info = { text = " ", texthl = "DiagnosticSignInfo" }, - warn = { text = " ", texthl = "DiagnosticSignWarn" }, - error = { text = " ", texthl = "DiagnosticSignError" }, -} - -setmetatable(M.signs, { - __index = function(t, k) - if type(k) == "number" then - return t[vim.diagnostic.severity[k]] - end - return t[k:lower():gsub("warning", "warn")] - end, -}) - ----@param bufnr number? ----@return table -M.count = function(bufnr) - local items = {} - for i, level in ipairs(vim.diagnostic.severity) do - items[level] = #vim.diagnostic.get(bufnr, { severity = i }) - end - - setmetatable(items, { - __index = function(t, k) - if type(k) == "number" then - return t[vim.diagnostic.severity[k]] - end - return t[k:upper():gsub("WARNING", "WARN")] - end, - }) - - return items -end - ----@param bufnr number? ----@return string -M.indicator = function(bufnr) - local diagnostic_count = M.count(bufnr) - local tbl = {} - for level, count in pairs(diagnostic_count) do - if count > 0 then - local color = "%#" .. M.signs[level].texthl .. "#" - local indicator = color .. M.signs[level].text .. count - table.insert(tbl, indicator) - end - end - return table.concat(tbl, " ") -end - -M.buf_count = function(bufnr) - return M.count(vim.F.if_nil(bufnr, vim.fn.bufnr())) -end - -M.buf_indicator = function(bufnr) - return M.indicator(vim.F.if_nil(bufnr, vim.fn.bufnr())) -end - -local function get_buffers(opts) - local buffers = {} - - local diagnostics = vim.diagnostic.get(nil, opts) - for _, diagnostic in ipairs(diagnostics) do - local bufnr = diagnostic.bufnr --[[@as number]] - if not vim.tbl_contains(buffers, bufnr) then - table.insert(buffers, bufnr) - end - end - - return buffers -end - ----@param opts table ----@param search_forward boolean Search forward ----@return number? Buffer number -local function next_buffer(opts, search_forward) - local win_id = opts.win_id or vim.api.nvim_get_current_win() - local bufnr = vim.api.nvim_win_get_buf(win_id) - local buffers = get_buffers(opts) - local sort_buffers, is_next - - if search_forward then - sort_buffers = function(a, b) - return a < b - end - is_next = function(a) - return a > bufnr - end - else - sort_buffers = function(a, b) - return a > b - end - is_next = function(a) - return a < bufnr - end - end - - table.sort(buffers, sort_buffers) - - for _, buffer in ipairs(buffers) do - if is_next(buffer) then - return buffer - end - end - - if opts.wrap then - return buffers[1] - end -end - ----@param opts table? ----@param search_forward boolean Search forward -local function goto_diagnostic(opts, search_forward) - opts = opts or {} - opts.wrap = opts.wrap == nil or opts.wrap - local win_id = opts.win_id or vim.api.nvim_get_current_win() - - local get_pos, goto_pos - if search_forward then - get_pos = vim.diagnostic.get_next_pos - goto_pos = vim.diagnostic.goto_next - else - get_pos = vim.diagnostic.get_prev_pos - goto_pos = vim.diagnostic.goto_prev - end - - local pos_opts = vim.tbl_extend("force", opts, { wrap = false }) - local pos = get_pos(pos_opts) - if not pos then - local buffer = next_buffer(opts, true) - if buffer then - vim.api.nvim_win_set_buf(win_id, buffer) - vim.api.nvim_win_set_cursor(win_id, { 1, 0 }) - end - end - - return goto_pos(opts) -end - ---- Move to the next diagnostic in the workspace. ---- ----@param opts table? -function M.goto_next_workspace(opts) - goto_diagnostic(opts, true) -end - ---- Move to the prev diagnostic in the workspace. ---- ----@param opts table? -function M.goto_prev_workspace(opts) - goto_diagnostic(opts, false) -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/documentation.lua b/nvim/.config/nvim/lua/tobyvin/utils/documentation.lua deleted file mode 100644 index 683e0b6..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/documentation.lua +++ /dev/null @@ -1,118 +0,0 @@ -local M = {} - ----@type Provider[] -vim.g.doc_providers = {} - -local default_opts = { - enabled = function() - return true - end, -} - ----@param buffer number? ----@return Provider[] -local get_providers = function(buffer) - if buffer then - return vim.F.if_nil(vim.b[buffer].doc_providers, {}) - else - return vim.g.doc_providers - end -end - ----@param buffer number? ----@param providers Provider[] -local set_providers = function(buffer, providers) - if buffer == nil then - vim.g.doc_providers = providers - else - vim.b[buffer].doc_providers = providers - end -end - ----@param a Provider ----@param b Provider ----@return boolean -local sort_providers = function(a, b) - if a.opts.priority and b.opts.priority then - return a.opts.priority > b.opts.priority - else - return not b.opts.priority - end -end - ----@param buffer number ----@return Provider[] -M.buf_providers = function(buffer) - local providers = {} - if vim.api.nvim_buf_is_valid(buffer) and type(vim.b[buffer].doc_providers) == "table" then - vim.list_extend(providers, get_providers(buffer)) - end - vim.list_extend(providers, get_providers()) - table.sort(providers, sort_providers) - return providers -end - ----@param handler ProviderHandler ----@param opts ProviderOpts ----@return ProviderId -M.register = function(handler, opts) - ---@type ProviderOpts - opts = vim.F.if_nil(opts, {}) - opts = vim.tbl_extend("keep", opts, default_opts) - - ---@type Provider - local provider = { handler = handler, opts = opts } - - local providers = get_providers(provider.opts.buffer) - local id - - if #providers > 0 and provider.opts.priority then - for i, p in ipairs(providers) do - if not p.opts.priority or p.opts.priority < provider.opts.priority or i == #providers then - table.insert(providers, i, provider) - id = i - break - end - end - else - table.insert(providers, provider) - id = #providers - end - - set_providers(provider.opts.buffer, providers) - return id -end - ----@param id ProviderId ----@param buffer number? -M.unregister = function(id, buffer) - local providers = get_providers(buffer) - - local provider = table.remove(providers, id) - - set_providers(buffer, providers) - return provider -end - ---- Returns `true` if a provider successfully handled the request, otherwise returns `false`. --- Example usage: --- ```lua --- vim.keymap.set("n", "gx", function() --- if utils.documentation.open() then --- return "<Ignore>" --- end --- return "gx" --- end, { desc = "documentation", expr = true }) --- ``` ----@param buffer number? -M.open = function(buffer) - buffer = buffer or vim.api.nvim_get_current_buf() - local providers = M.buf_providers(buffer) - for _, provider in ipairs(providers) do - if provider.opts.enabled and provider.opts.enabled() and not provider.handler() then - return true - end - end -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua b/nvim/.config/nvim/lua/tobyvin/utils/fs.lua deleted file mode 100644 index b757ab5..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua +++ /dev/null @@ -1,48 +0,0 @@ -local Path = require("plenary").path -local Reload = require("plenary").reload -local M = {} - -M.config_path = function(package) - local rel_path = Path:new(vim.fn.stdpath("config")):normalize(vim.env.HOME) - return Path:new(vim.env.HOME, ".dotfiles", package, rel_path) -end - -M.module_from_path = function(path) - path = vim.F.if_nil(path, vim.api.nvim_buf_get_name(0)) - path = Path:new(path) - local lua_dir = M.config_path("nvim"):joinpath("lua").filename - if path:exists() and not path:is_dir() and string.match(path.filename, "^" .. lua_dir) then - return path:normalize(lua_dir):gsub(".lua$", ""):gsub("/", ".") - end -end - -M.reload = function(module_name, starts_with_only) - module_name = vim.F.if_nil(module_name, "") - starts_with_only = vim.F.if_nil(starts_with_only, "tobyvin") - - module = package.loaded[module_name] - - Reload.reload_module(module_name, starts_with_only) - - if not pcall(require, module_name) then - package.loaded[module_name] = module - end - - return package.loaded[module_name] -end - -M.shorten_path = function(filename, len) - local path = Path:new(filename) - local short_len = 0 - for _, part in pairs(path:parents()) do - short_len = math.max(short_len, #part) - end - filename = path:make_relative() - while short_len > 0 and vim.fn.strlen(filename) > len - 10 do - filename = Path:new(path:make_relative()):shorten(short_len, { -1, 1 }) - short_len = short_len - 1 - end - return filename -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/hover.lua b/nvim/.config/nvim/lua/tobyvin/utils/hover.lua deleted file mode 100644 index 6cef8dd..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/hover.lua +++ /dev/null @@ -1,132 +0,0 @@ ----@diagnostic disable: missing-parameter -local M = {} - ----@alias ProviderHandler fun():boolean? The handler for hover. Return true to indicate failure and skip ----@alias ProviderId number - ----@class ProviderOpts ----@field desc string? ----@field enabled fun():boolean? ----@field buffer number? ----@field priority number? - ----@class Provider ----@field handler ProviderHandler ----@field opts ProviderOpts - ----@type Provider[] -vim.g.hover_providers = {} - -local default_opts = { - enabled = function() - return true - end, -} - ----@param buffer number? ----@return Provider[] -local get_providers = function(buffer) - if buffer then - return vim.F.if_nil(vim.b[buffer].hover_providers, {}) - else - return vim.g.hover_providers - end -end - ----@param buffer number? ----@param providers Provider[] -local set_providers = function(buffer, providers) - if buffer == nil then - vim.g.hover_providers = providers - else - vim.b[buffer].hover_providers = providers - end -end - ----@param a Provider ----@param b Provider ----@return boolean -local sort_providers = function(a, b) - if a.opts.priority and b.opts.priority then - return a.opts.priority > b.opts.priority - else - return not b.opts.priority - end -end - ----@param buffer number ----@return Provider[] -M.buf_providers = function(buffer) - local providers = {} - if vim.api.nvim_buf_is_valid(buffer) and type(vim.b[buffer].hover_providers) == "table" then - vim.list_extend(providers, get_providers(buffer)) - end - vim.list_extend(providers, get_providers()) - table.sort(providers, sort_providers) - return providers -end - ----@param handler ProviderHandler ----@param opts ProviderOpts ----@return ProviderId -M.register = function(handler, opts) - ---@type ProviderOpts - opts = vim.F.if_nil(opts, {}) - opts = vim.tbl_extend("keep", opts, default_opts) - - ---@type Provider - local provider = { handler = handler, opts = opts } - - local providers = get_providers(provider.opts.buffer) - local id - - if #providers > 0 and provider.opts.priority then - for i, p in ipairs(providers) do - if not p.opts.priority or p.opts.priority < provider.opts.priority or i == #providers then - table.insert(providers, i, provider) - id = i - break - end - end - else - table.insert(providers, provider) - id = #providers - end - - set_providers(provider.opts.buffer, providers) - return id -end - ----@param id ProviderId ----@param buffer number? -M.unregister = function(id, buffer) - local providers = get_providers(buffer) - - local provider = table.remove(providers, id) - - set_providers(buffer, providers) - return provider -end - ---- Returns `true` if a provider successfully handled the request, otherwise returns `false`. --- Example usage: --- ```lua --- vim.keymap.set("n", "K", function() --- if utils.hover.open() then --- return "<Ignore>" --- end --- return "K" --- end, { desc = "hover", expr = true } --- ``` ----@param buffer number? -M.open = function(buffer) - buffer = buffer or vim.api.nvim_get_current_buf() - local providers = M.buf_providers(buffer) - for _, provider in ipairs(providers) do - if provider.opts.enabled and provider.opts.enabled() and not provider.handler() then - return true - end - end -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/init.lua b/nvim/.config/nvim/lua/tobyvin/utils/init.lua deleted file mode 100644 index 5f02f9e..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/init.lua +++ /dev/null @@ -1,36 +0,0 @@ --- Lazy load -local M = {} - -function M.require(mod) - local ok, ret = M.try(require, mod) - return ok and ret -end - -function M.try(fn, ...) - local args = { ... } - - return xpcall(function() - return fn(unpack(args)) - end, function(err) - local lines = {} - table.insert(lines, err) - table.insert(lines, debug.traceback("", 3)) - - M.error(table.concat(lines, "\n")) - return err - end) -end - -setmetatable(M, { - __index = function(t, k) - local ok, val = pcall(require, string.format("tobyvin.utils.%s", k)) - - if ok then - rawset(t, k, val) - end - - return val - end, -}) - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/job.lua b/nvim/.config/nvim/lua/tobyvin/utils/job.lua deleted file mode 100644 index 231a9bb..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/job.lua +++ /dev/null @@ -1,97 +0,0 @@ -local M = {} - ----@param cmd? string Default command to run. ----@param args string[]? Default arguments. ----@param quiet boolean? Silence stdout of the job. -M.cmd = function(cmd, args, quiet) - quiet = quiet or false - cmd = cmd or "" - args = args or {} - vim.ui.input({ - prompt = "Run command:", - default = cmd .. " " .. table.concat(args, " "), - completion = "shellcmd", - kind = "cmd", - }, function(input) - if input ~= nil then - args = {} - for i, arg in ipairs(vim.split(input, " ", { trimempty = true })) do - if i == 1 then - cmd = arg - else - table.insert(args, vim.fn.expand(arg)) - end - end - M.with_notify(cmd, args, quiet):start() - end - end) -end - -M.with_notify = function(cmd, args, quiet) - local Job = require("plenary").job - local notification - local win, height - local output = "" - local length = 0 - local width = 0 - - local on_data = function(status, data) - if data ~= nil then - output = output .. data .. "\n" - width = math.max(width, string.len(data) + 2) - end - - notification = vim.notify(vim.trim(output), vim.log.levels.INFO, { - title = string.format("[%s] %s", cmd, status), - icon = M.status_signs[status].text, - replace = notification, - on_open = function(win_) - win, height = win_, vim.api.nvim_win_get_height(win_) - end, - timeout = 10000, - }) - - vim.api.nvim_win_set_width(win, width) - if height then - vim.api.nvim_win_set_height(win, height + length) - end - - length = length + 1 - end - - local on_start = function() - if not quiet then - on_data("started", string.format("$ %s %s", cmd, table.concat(args, " "))) - end - end - - local on_stdout = function(_, data) - if not quiet then - on_data("running", data) - end - end - - local on_stderr = function(_, data) - on_data("running", data) - end - - local on_exit = function(_, code) - if code ~= 0 then - on_data("failed") - elseif not quiet then - on_data("completed") - end - end - - return Job:new({ - command = cmd, - args = args, - enabled_recording = true, - on_start = vim.schedule_wrap(on_start), - on_stdout = vim.schedule_wrap(on_stdout), - on_stderr = vim.schedule_wrap(on_stderr), - on_exit = vim.schedule_wrap(on_exit), - }) -end - -return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils/status.lua b/nvim/.config/nvim/lua/tobyvin/utils/status.lua deleted file mode 100644 index 20d38fa..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/status.lua +++ /dev/null @@ -1,30 +0,0 @@ -local M = {} - -M.signs = { - started = { text = "ﳁ ", texthl = "diffChanged" }, - running = { text = "ﳁ ", texthl = "DiagnosticSignInfo" }, - failed = { text = " ", texthl = "DiagnosticSignError" }, - done = { text = " ", texthl = "diffAdded" }, - spinner = { text = { "⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾" }, texthl = "DiagnosticSignInfo" }, -} - -M.update_spinner = function(client_id, token) - local notif_data = M.get_notif_data(client_id, token) - - if notif_data.spinner then - local new_spinner = (notif_data.spinner + 1) % #M.status_signs.spinner.text - notif_data.spinner = new_spinner - - notif_data.notification = vim.notify(nil, nil, { - hide_from_history = true, - icon = M.status_signs.spinner.text[new_spinner], - replace = notif_data.notification, - }) - - vim.defer_fn(function() - M.update_spinner(client_id, token) - end, 100) - end -end - -return M |