diff --git a/nvim/.luacheckrc b/nvim/.luacheckrc index 86ee88d3..cd7373eb 100644 --- a/nvim/.luacheckrc +++ b/nvim/.luacheckrc @@ -1,29 +1,4 @@ globals = { 'vim', 'dbg', - Path = { - fields = { - join = function(...) end, - }, - }, - Clipboard = { - fields = { - copy = function(str) end, - }, - }, - LSP = { - fields = { - on_attach = function(client, bufnr) end, - }, - }, - TblUtils = { - fields = { - join_lists = function(...) end, - }, - }, - Url = { - fields = { - open = function(url) end, - }, - }, } diff --git a/nvim/init.lua b/nvim/init.lua index 12ffaa99..c8ab303a 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -1,7 +1,30 @@ -require('my.globals') require('my.settings') require('my.plugins') +---Debug Lua stuff and print a nice debug message via `vim.inspect`. +---@param ... any +_G.dbg = function(...) + local info = debug.getinfo(2, 'S') + local source = info.source:sub(2) + source = vim.loop.fs_realpath(source) or source + source = vim.fn.fnamemodify(source, ':~:.') .. ':' .. info.linedefined + local what = { ... } + if vim.tbl_islist(what) and vim.tbl_count(what) <= 1 then + what = what[1] + end + local msg = vim.inspect(vim.deepcopy(what)) + vim.notify(msg, vim.log.levels.INFO, { + title = 'Debug: ' .. source, + on_open = function(win) + vim.wo[win].conceallevel = 3 + vim.wo[win].concealcursor = '' + vim.wo[win].spell = false + local buf = vim.api.nvim_win_get_buf(win) + vim.treesitter.start(buf, 'lua') + end, + }) +end + vim.api.nvim_create_autocmd('UiEnter', { callback = function() local bufs = vim.api.nvim_list_bufs() diff --git a/nvim/lua/my/configure/heirline/init.lua b/nvim/lua/my/configure/heirline/init.lua index 62c2e5ce..b7312033 100644 --- a/nvim/lua/my/configure/heirline/init.lua +++ b/nvim/lua/my/configure/heirline/init.lua @@ -2,7 +2,7 @@ return { { 'SmiteshP/nvim-navic', init = function() - LSP.on_attach(function(client, bufnr) + require('my.utils.lsp').on_attach(function(client, bufnr) if client.server_capabilities.documentSymbolProvider then require('nvim-navic').attach(client, bufnr) end diff --git a/nvim/lua/my/configure/heirline/shared.lua b/nvim/lua/my/configure/heirline/shared.lua index cdc4ca0f..88aca43e 100644 --- a/nvim/lua/my/configure/heirline/shared.lua +++ b/nvim/lua/my/configure/heirline/shared.lua @@ -29,18 +29,6 @@ function M.FileIcon(bg_color) } end -M.FilePath = { - init = function(self) - self.bufname = vim.api.nvim_buf_get_name(0) - end, - condition = function(self) - return self.bufname ~= '' and not vim.startswith(self.bufname, 'component://') - end, - provider = function(self) - return Path.relative(self.bufname) - end, -} - local icons = require('my.lsp.icons') local diagnostics_order = { vim.diagnostic.severity.HINT, diff --git a/nvim/lua/my/configure/heirline/statusline.lua b/nvim/lua/my/configure/heirline/statusline.lua index 4261e314..3cf6f053 100644 --- a/nvim/lua/my/configure/heirline/statusline.lua +++ b/nvim/lua/my/configure/heirline/statusline.lua @@ -1,6 +1,7 @@ local conditions = require('heirline.conditions') local myconditions = require('my.configure.heirline.conditions') local sep = require('my.configure.heirline.separators') +local path = require('my.utils.path') local M = {} @@ -127,7 +128,7 @@ M.FilePath = { if mdpreview_session then buf = mdpreview_session.source_buf end - return Path.relative(vim.api.nvim_buf_get_name(buf)) + return path.relative(vim.api.nvim_buf_get_name(buf)) end, }, } @@ -208,7 +209,7 @@ M.OnePassword = { M.LspFormatToggle = { provider = function() local buf = vim.b.mdpreview_session and vim.b.mdpreview_session.source_buf or 0 - if require('my.lsp.utils').is_formatting_supported(buf) then + if require('my.utils.lsp').is_formatting_supported(buf) then return '  ' else return '  ' @@ -217,7 +218,7 @@ M.LspFormatToggle = { hl = { bg = 'bg_statusline' }, on_click = { callback = function() - require('my.lsp.utils').toggle_formatting_enabled() + require('my.utils.lsp').toggle_formatting_enabled() end, name = 'heirline_LSP', }, @@ -228,7 +229,7 @@ M.LspFormatToggle = { { provider = function() local buf = vim.b.mdpreview_session and vim.b.mdpreview_session.source_buf or 0 - local name = require('my.lsp.utils').get_formatter_name(buf) + local name = require('my.utils.lsp').get_formatter_name(buf) if name then return string.format(' (%s) ', name) end diff --git a/nvim/lua/my/configure/legendary.lua b/nvim/lua/my/configure/legendary.lua index af92d741..cdff5c47 100644 --- a/nvim/lua/my/configure/legendary.lua +++ b/nvim/lua/my/configure/legendary.lua @@ -66,7 +66,7 @@ return { lazy = false, priority = 1000000, init = function() - LSP.on_attach(function(client, bufnr) + require('my.utils.lsp').on_attach(function(client, bufnr) if vim.tbl_contains(lsp_bound_buffer_ids, bufnr) then return end diff --git a/nvim/lua/my/configure/lspconfig.lua b/nvim/lua/my/configure/lspconfig.lua index d7df1b89..da9b8283 100644 --- a/nvim/lua/my/configure/lspconfig.lua +++ b/nvim/lua/my/configure/lspconfig.lua @@ -106,7 +106,7 @@ return { }, event = 'BufReadPre', init = function() - LSP.on_attach(require('my.lsp.utils').on_attach) + require('my.utils.lsp').on_attach(require('my.utils.lsp').on_attach_default) end, config = function() local efm_setup_done = false diff --git a/nvim/lua/my/configure/telescope.lua b/nvim/lua/my/configure/telescope.lua index db1649df..ede9ccb8 100644 --- a/nvim/lua/my/configure/telescope.lua +++ b/nvim/lua/my/configure/telescope.lua @@ -1,3 +1,5 @@ +local path = require('my.utils.path') + return { 'nvim-telescope/telescope.nvim', cmd = 'Telescope', @@ -77,7 +79,7 @@ return { end local ripgrep_ignore_file_path = ( - Path.join(vim.env.XDG_CONFIG_HOME or Path.join(vim.env.HOME, '.config'), 'ripgrep_ignore') + path.join(vim.env.XDG_CONFIG_HOME or path.join(vim.env.HOME, '.config'), 'ripgrep_ignore') ) local telescope = require('telescope') diff --git a/nvim/lua/my/globals.lua b/nvim/lua/my/globals.lua deleted file mode 100644 index d1dc5221..00000000 --- a/nvim/lua/my/globals.lua +++ /dev/null @@ -1,98 +0,0 @@ --- Global helper functions - ----Debug Lua stuff and print a nice debug message via `vim.inspect`. ----@param ... any -_G.dbg = function(...) - local info = debug.getinfo(2, 'S') - local source = info.source:sub(2) - source = vim.loop.fs_realpath(source) or source - source = vim.fn.fnamemodify(source, ':~:.') .. ':' .. info.linedefined - local what = { ... } - if vim.tbl_islist(what) and vim.tbl_count(what) <= 1 then - what = what[1] - end - local msg = vim.inspect(vim.deepcopy(what)) - vim.notify(msg, vim.log.levels.INFO, { - title = 'Debug: ' .. source, - on_open = function(win) - vim.wo[win].conceallevel = 3 - vim.wo[win].concealcursor = '' - vim.wo[win].spell = false - local buf = vim.api.nvim_win_get_buf(win) - vim.treesitter.start(buf, 'lua') - end, - }) -end - ---- A collection of filesystem path related utils -_G.Path = { - ---Join two or more paths together - ---@param ... string - ---@return string - join = function(...) - return vim.fn.simplify(table.concat({ ... }, '/')) - end, - - ---Get path relative to current working directory, - ---replacing `$HOME` with `~` if applicable. - ---@param path string - ---@return string - relative = function(path) - return vim.fn.fnamemodify(path, ':~:.') or path - end, -} - ----Utils for copying to clipboard -_G.Clipboard = { - ---Copy string to system clipboard - ---@param str string - copy = function(str) - if vim.loop.os_uname().sysname == 'Darwin' then - vim.fn.jobstart(string.format('echo -n %q | pbcopy', str), { detach = true }) - else - vim.fn.jobstart(string.format('echo -n %q | xclip -sel clip', str), { detach = true }) - end - end, -} - -_G.TblUtils = { - ---Join two or more lists together - ---@param ... table - ---@return table - join_lists = function(...) - local lists = { ... } - local result = {} - for _, list in ipairs(lists) do - vim.list_extend(result, list) - end - return result - end, -} - -_G.LSP = { - ---Set up a callback to run on LSP attach - ---@param callback fun(client:table,bufnr:number) - on_attach = function(callback) - vim.api.nvim_create_autocmd('LspAttach', { - callback = function(args) - local bufnr = args.buf - local client = vim.lsp.get_client_by_id(args.data.client_id) - if client then - callback(client, bufnr) - end - end, - }) - end, -} - -_G.Url = { - ---Open URL in default browser. Supports GitHub shorthands like `owner/repo` - ---@param url string - open = function(url) - -- plugin paths as interpreted by plugin manager, e.g. mrjones2014/op.nvim - if not string.match(url, '[a-z]*://[^ >,;]*') and string.match(url, '[%w%p\\-]*/[%w%p\\-]*') then - url = string.format('https://github.com/%s', url) - end - vim.fn.jobstart({ vim.fn.has('macunix') ~= 0 and 'open' or 'xdg-open', url }, { detach = true }) - end, -} diff --git a/nvim/lua/my/legendary/autocmds.lua b/nvim/lua/my/legendary/autocmds.lua index caeb683a..42906290 100644 --- a/nvim/lua/my/legendary/autocmds.lua +++ b/nvim/lua/my/legendary/autocmds.lua @@ -78,7 +78,7 @@ function M.lsp_autocmds(bufnr, server_name) then table.insert(augroup, { 'BufWritePost', - require('my.lsp.utils').format_document, + require('my.utils.lsp').format_document, opts = { buffer = bufnr }, }) end diff --git a/nvim/lua/my/legendary/commands.lua b/nvim/lua/my/legendary/commands.lua index 48d49471..765bd0c2 100644 --- a/nvim/lua/my/legendary/commands.lua +++ b/nvim/lua/my/legendary/commands.lua @@ -1,3 +1,5 @@ +local clipboard = require('my.utils.clipboard') + local M = {} function M.default_commands() @@ -22,7 +24,7 @@ function M.default_commands() return end - Clipboard.copy(icon) + clipboard.copy(icon) vim.notify('Copied icon to clipboard.', vim.log.levels.INFO) end) end, @@ -73,7 +75,7 @@ function M.default_commands() return end - Url.open(selected) + require('my.utils.url').open(selected) end) end, description = 'Search installed plugins and open the repo in browser', @@ -103,17 +105,17 @@ function M.lsp_commands(bufnr, server_name) } if not (vim.api.nvim_buf_get_commands(0, {}) or {}).Format then - commands = TblUtils.join_lists(commands, { + vim.list_extend(commands, { { ':Format', - require('my.lsp.utils').format_document, + require('my.utils.lsp').format_document, description = 'Format the current document with LSP', opts = { buffer = bufnr }, }, { ':DisableFormatting', function() - require('my.lsp.utils').toggle_formatting_enabled(false) + require('my.utils.lsp').toggle_formatting_enabled(false) end, description = 'Disable LSP formatting', opts = { buffer = bufnr }, @@ -121,7 +123,7 @@ function M.lsp_commands(bufnr, server_name) { ':EnableFormatting', function() - require('my.lsp.utils').toggle_formatting_enabled(true) + require('my.utils.lsp').toggle_formatting_enabled(true) end, description = 'Enable LSP formatting', opts = { buffer = bufnr }, @@ -131,7 +133,7 @@ function M.lsp_commands(bufnr, server_name) if not (vim.api.nvim_buf_get_commands(0, {}) or {}).Test then -- Neotest - commands = TblUtils.join_lists(commands, { + vim.list_extend(commands, { { ':Test', h.lazy_required_fn('neotest', 'run.run'), diff --git a/nvim/lua/my/legendary/functions.lua b/nvim/lua/my/legendary/functions.lua index ae9d23f3..62e0e7ce 100644 --- a/nvim/lua/my/legendary/functions.lua +++ b/nvim/lua/my/legendary/functions.lua @@ -1,10 +1,13 @@ +local clipboard = require('my.utils.clipboard') +local path = require('my.utils.path') + local M = {} function M.default_functions() return { { function() - Clipboard.copy(vim.fn.simplify(Path.relative(vim.fn.expand('%') --[[@as string]]))) + clipboard.copy(vim.fn.simplify(path.relative(vim.fn.expand('%') --[[@as string]]))) end, description = 'Copy current relative filepath to clipboard', }, diff --git a/nvim/lua/my/legendary/keymap.lua b/nvim/lua/my/legendary/keymap.lua index 918baa12..fa10b1ff 100644 --- a/nvim/lua/my/legendary/keymap.lua +++ b/nvim/lua/my/legendary/keymap.lua @@ -37,7 +37,7 @@ function M.default_keymaps() 'gx', function() local url = vim.fn.expand('') - Url.open(url) + require('my.utils.url').open(url) end, description = 'Open URL under cursor', }, diff --git a/nvim/lua/my/startup.lua b/nvim/lua/my/startup.lua index c1eed63d..4ab83f82 100644 --- a/nvim/lua/my/startup.lua +++ b/nvim/lua/my/startup.lua @@ -1,3 +1,5 @@ +local path = require('my.utils.path') + local M = {} local function longest_line(strs) @@ -23,7 +25,7 @@ local function center(strs) end local header = center({ - Path.relative(vim.loop.cwd()), + path.relative(vim.loop.cwd()), '', '', '', diff --git a/nvim/lua/my/utils/clipboard.lua b/nvim/lua/my/utils/clipboard.lua new file mode 100644 index 00000000..7c8e1e39 --- /dev/null +++ b/nvim/lua/my/utils/clipboard.lua @@ -0,0 +1,13 @@ +local M = {} + +---Copy string to system clipboard +---@param str string +function M.copy(str) + if vim.loop.os_uname().sysname == 'Darwin' then + vim.fn.jobstart(string.format('echo -n %q | pbcopy', str), { detach = true }) + else + vim.fn.jobstart(string.format('echo -n %q | xclip -sel clip', str), { detach = true }) + end +end + +return M diff --git a/nvim/lua/my/lsp/utils/init.lua b/nvim/lua/my/utils/lsp.lua similarity index 91% rename from nvim/lua/my/lsp/utils/init.lua rename to nvim/lua/my/utils/lsp.lua index d6c574f7..41cf7e9c 100644 --- a/nvim/lua/my/lsp/utils/init.lua +++ b/nvim/lua/my/utils/lsp.lua @@ -6,7 +6,21 @@ local init_done = false local formatting_enabled = true -function M.on_attach(client, bufnr) +---Set up a callback to run on LSP attach +---@param callback fun(client:table,bufnr:number) +function M.on_attach(callback) + vim.api.nvim_create_autocmd('LspAttach', { + callback = function(args) + local bufnr = args.buf + local client = vim.lsp.get_client_by_id(args.data.client_id) + if client then + callback(client, bufnr) + end + end, + }) +end + +function M.on_attach_default(client, bufnr) if not init_done then init_done = true M.setup_async_formatting() diff --git a/nvim/lua/my/utils/path.lua b/nvim/lua/my/utils/path.lua new file mode 100644 index 00000000..15215034 --- /dev/null +++ b/nvim/lua/my/utils/path.lua @@ -0,0 +1,18 @@ +local M = {} + +---Join two or more paths together +---@param ... string +---@return string +function M.join(...) + return vim.fn.simplify(table.concat({ ... }, '/')) +end + +---Get path relative to current working directory, +---replacing `$HOME` with `~` if applicable. +---@param path string +---@return string +function M.relative(path) + return vim.fn.fnamemodify(path, ':~:.') or path +end + +return M diff --git a/nvim/lua/my/utils/url.lua b/nvim/lua/my/utils/url.lua new file mode 100644 index 00000000..7b46ca4b --- /dev/null +++ b/nvim/lua/my/utils/url.lua @@ -0,0 +1,13 @@ +local M = {} + +---Open URL in default browser. Supports GitHub shorthands like `owner/repo` +---@param url string +function M.open(url) + -- plugin paths as interpreted by plugin manager, e.g. mrjones2014/op.nvim + if not string.match(url, '[a-z]*://[^ >,;]*') and string.match(url, '[%w%p\\-]*/[%w%p\\-]*') then + url = string.format('https://github.com/%s', url) + end + vim.fn.jobstart({ vim.fn.has('macunix') ~= 0 and 'open' or 'xdg-open', url }, { detach = true }) +end + +return M