From b2b8e5f539c697d387b41ec9fe3609ae093248ad Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Wed, 3 Aug 2022 14:53:35 -0400 Subject: [PATCH] actually fixed reloading --- lua/config/plugins/lsp.lua | 2 +- lua/config/plugins/telescope.lua | 2 +- lua/config/plugins/tree.lua | 2 +- lua/config/reload.lua | 33 +++++++++++++------------ lua/config/settings.lua | 7 +++--- lua/config/theme.lua | 1 - lua/config/utils.lua | 41 +++++++++++++++++++++++++------- 7 files changed, 56 insertions(+), 32 deletions(-) diff --git a/lua/config/plugins/lsp.lua b/lua/config/plugins/lsp.lua index 72bfdd9..f88fb04 100644 --- a/lua/config/plugins/lsp.lua +++ b/lua/config/plugins/lsp.lua @@ -1,5 +1,5 @@ local config = function() - local u = require 'config.utils' + local u = require 'config.utils'.mapping u.nmap('E', vim.diagnostic.open_float) u.nmap('[d', vim.diagnostic.goto_prev) diff --git a/lua/config/plugins/telescope.lua b/lua/config/plugins/telescope.lua index cc07c47..f1a6700 100644 --- a/lua/config/plugins/telescope.lua +++ b/lua/config/plugins/telescope.lua @@ -6,7 +6,7 @@ return function(use) config = function() require 'telescope'.setup {} - local u = require 'config.utils' + local u = require 'config.utils'.mapping u.nmap('ff', u.cmd('Telescope find_files')) u.nmap('fg', u.cmd('Telescope live_grep')) diff --git a/lua/config/plugins/tree.lua b/lua/config/plugins/tree.lua index 2ccbc92..9c90b0b 100644 --- a/lua/config/plugins/tree.lua +++ b/lua/config/plugins/tree.lua @@ -20,7 +20,7 @@ return function(use) }, } - local u = require 'config.utils' + local u = require 'config.utils'.mapping u.nmap('e', u.cmd('Neotree toggle')) end } diff --git a/lua/config/reload.lua b/lua/config/reload.lua index da9f138..f3331dd 100644 --- a/lua/config/reload.lua +++ b/lua/config/reload.lua @@ -1,11 +1,13 @@ -local group = vim.api.nvim_create_augroup('reload', {}) +local u = require 'config.utils'.load -local post_write = function(path, fn) - vim.api.nvim_create_autocmd('BufWritePost', { - group = group, - pattern = '**/lua/config/' .. path, - callback = fn, - }) +local g +vim.api.nvim_create_augroup('reload', {}) + +-- reload these files when written to + +--- @param arg {file: string} +local reload_file = function(arg) + u.reload(u.path_for(arg.file)) end for _, path in ipairs({ @@ -14,7 +16,7 @@ for _, path in ipairs({ 'utils.lua', 'neovide.lua', 'theme.lua', -}) do post_write(path, function(arg) dofile(arg.file) end) end +}) do u.post_write(g, path, reload_file) end -- allows you to install / clean / update plugins -- after writing to the file @@ -23,17 +25,14 @@ for _, path in ipairs({ --- @param arg {file: string} local update_plugins = function(arg) -- unload the file if it's not plugins.lua - local i = arg.file:find("config/plugins/") - if i ~= nil then - local mod = arg.file:sub(i):gsub("/", "."):sub(0, -5) - package.loaded[mod] = nil + local mod = u.path_for(arg.file) + if mod ~= 'config.plugins' then + u.unload(mod) end -- reload plugins.lua and compile - package.loaded['config.plugins'] = nil - require 'config.plugins' + u.reload('config.plugins') require 'packer'.compile() end -post_write('plugins.lua', update_plugins) -post_write('plugins/*', update_plugins) - +u.post_write(g, 'plugins.lua', update_plugins) +u.post_write(g, 'plugins/*', update_plugins) diff --git a/lua/config/settings.lua b/lua/config/settings.lua index dc0ce4a..5fd186d 100644 --- a/lua/config/settings.lua +++ b/lua/config/settings.lua @@ -40,11 +40,12 @@ vim.diagnostic.config({ }) -- terminal -u.map('t', '', '') -u.nmap('tm', 'belowright 7sptermset nonuA') +local m = u.mapping +m.map('t', '', '') +m.nmap('tm', 'belowright 7sptermset nonuA') -- off search highlight when escape is pressed -u.nmap('', 'noh') +m.nmap('', 'noh') -- colemak local l = u.layout diff --git a/lua/config/theme.lua b/lua/config/theme.lua index 649bf70..9106183 100644 --- a/lua/config/theme.lua +++ b/lua/config/theme.lua @@ -1,5 +1,4 @@ -- highlight extra white space with red vim.api.nvim_set_hl(0, 'ExtraWhitespace', { ctermbg = 'red', bg = '#ff8888' }) vim.cmd [[match ExtraWhitespace /\s\+$/]] -print("hello???") diff --git a/lua/config/utils.lua b/lua/config/utils.lua index edeefa7..c2d3a7e 100644 --- a/lua/config/utils.lua +++ b/lua/config/utils.lua @@ -1,20 +1,45 @@ local opts = { noremap = true, silent = true } -local utils = {} -utils.map = function(mode, from, to) vim.keymap.set(mode, from, to, opts) end -utils.nmap = function(from, to) vim.keymap.set('n', from, to, opts) end -utils.cmd = function(content) return '' .. content .. '' end +-- mapping +local mapping = {} +mapping.map = function(mode, from, to) vim.keymap.set(mode, from, to, opts) end +mapping.nmap = function(from, to) vim.keymap.set('n', from, to, opts) end +mapping.cmd = function(content) return '' .. content .. '' end +-- layout (mapping) local layout = {} layout.map = function(from, to) vim.keymap.set('n', from, to, opts) vim.keymap.set('x', from, to, opts) end layout.swap = function(a, b) - utils.layout.map(a, b) - utils.layout.map(b, a) + layout.map(a, b) + layout.map(b, a) end -utils.layout = layout +mapping.layout = layout -return utils +-- loading stuff +local load = {} +load.post_write = function(group, path, fn) + vim.api.nvim_create_autocmd('BufWritePost', { + group = group, + pattern = '**/lua/config/' .. path, + callback = fn, + }) +end +load.unload = function(mod) package.loaded[mod] = nil end +load.reload = function(mod) load.unload(mod) require(mod) end +--- @param path string +load.path_for = function(path) + local i = path:find("lua/config/") + if i == nil then return nil end + local mod = path:sub(i + 4):gsub("/", "."):sub(0, -5) + return mod +end + +return { + mapping = mapping, + layout = layout, + load = load, +}