Skip to content

Commit

Permalink
chore(nvim): Remove globals in favor of utility modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Mar 28, 2024
1 parent a73674c commit f0a1543
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 156 deletions.
25 changes: 0 additions & 25 deletions nvim/.luacheckrc
Original file line number Diff line number Diff line change
@@ -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,
},
},
}
25 changes: 24 additions & 1 deletion nvim/init.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion nvim/lua/my/configure/heirline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions nvim/lua/my/configure/heirline/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions nvim/lua/my/configure/heirline/statusline.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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 ''
Expand All @@ -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',
},
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nvim/lua/my/configure/legendary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nvim/lua/my/configure/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion nvim/lua/my/configure/telescope.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local path = require('my.utils.path')

return {
'nvim-telescope/telescope.nvim',
cmd = 'Telescope',
Expand Down Expand Up @@ -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')
Expand Down
98 changes: 0 additions & 98 deletions nvim/lua/my/globals.lua

This file was deleted.

2 changes: 1 addition & 1 deletion nvim/lua/my/legendary/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions nvim/lua/my/legendary/commands.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local clipboard = require('my.utils.clipboard')

local M = {}

function M.default_commands()
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -103,25 +105,25 @@ 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 },
},
{
':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 },
Expand All @@ -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'),
Expand Down
5 changes: 4 additions & 1 deletion nvim/lua/my/legendary/functions.lua
Original file line number Diff line number Diff line change
@@ -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',
},
Expand Down
2 changes: 1 addition & 1 deletion nvim/lua/my/legendary/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function M.default_keymaps()
'gx',
function()
local url = vim.fn.expand('<cfile>')
Url.open(url)
require('my.utils.url').open(url)
end,
description = 'Open URL under cursor',
},
Expand Down
4 changes: 3 additions & 1 deletion nvim/lua/my/startup.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local path = require('my.utils.path')

local M = {}

local function longest_line(strs)
Expand All @@ -23,7 +25,7 @@ local function center(strs)
end

local header = center({
Path.relative(vim.loop.cwd()),
path.relative(vim.loop.cwd()),
'',
'',
'',
Expand Down
13 changes: 13 additions & 0 deletions nvim/lua/my/utils/clipboard.lua
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit f0a1543

Please sign in to comment.