nvim: add psoxizsh.startup

This commit rewrites the psoxizsh init to conform to lazy.nvim plugin
schema.

This is a port of the old early/pre/post/late stages we used to use to
functions we can call during our toplevel module's eventual setup()
call.

We should probably add optional callbacks to each so that users can
retain the older method of initialization...
This commit is contained in:
Paul Stemmet 2024-08-08 12:15:30 +00:00
parent bf29a51829
commit 1637296d69
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
2 changed files with 143 additions and 207 deletions

View File

@ -1,213 +1,18 @@
local o, g, fn, cmd = vim.opt, vim.g, vim.fn, vim.cmd
local au, util = require 'psoxizsh.autocmd', require 'psoxizsh.util'
local diagnostic = require 'psoxizsh.diagnostic'
local keys = require 'psoxizsh.key.map'
local function psoxizsh_early_config()
-- Local early configuration
util.try_mreload('early')
---@class psoxizsh
local M = {}
-- Set global color scheme var
if not g.my_color_scheme then
g.my_color_scheme = 'onedark'
end
---Initialize Psoxizsh NeoVim
---@param _ table|nil
function M.setup(_)
local startup = require 'psoxizsh.startup'
local au = require 'psoxizsh.autocmd'
-- Color settings
o.background = 'dark'
o.termguicolors = true
startup.pre()
startup.early()
-- Hide buffers don't close them
o.hidden = true
-- Sane pane opening
o.splitbelow = true
o.splitright = true
-- File indent opts
o.encoding = "utf-8"
o.shiftwidth = 2
o.tabstop = 8
o.softtabstop = 2
o.expandtab = true
o.list = true
o.listchars:append {
trail = '\u{02FD}',
extends = '\u{22B3}',
precedes = '\u{22B2}',
nbsp = '\u{02EC}',
conceal = '\u{2219}',
tab = '\u{2559}\u{254C}\u{2556}',
}
o.ignorecase = true
o.infercase = true
cmd 'filetype plugin indent on'
-- Set completion messages off
o.shortmess:append { c = true }
-- Preview window + menu for autocompletions
o.completeopt:append {
'preview',
'menuone',
'longest',
}
-- Lower update time (Default 4000)
o.updatetime = 300
-- Numbered lines
o.number = true
o.relativenumber = true
o.signcolumn = 'yes:1'
-- Disable '--INSERT--' and friends
o.showmode = false
-- Use existing buffers
o.switchbuf = { "useopen", "usetab" }
-- Set global statusline (0.7+ only)
if fn.has('nvim-0.7') == 1 then o.laststatus = 3 end
-- Setup vim.diagnostic APIs
diagnostic.setup {}
au.PsoxFileAutos {
{ 'FileType', 'yaml', function() cmd 'setlocal indentkeys-=<:> ts=8 sts=2 sw=2 expandtab' end },
{ 'FileType', 'go', function() cmd 'setlocal ts=8 sts=4 sw=4 noexpandtab' end },
{ 'FileType', 'quickfix,netrw', 'setlocal nobuflisted' },
{ 'FileType', 'netrw', function() keys.Global.N.CloseNetrw:register { buffer = true } end },
}
-- Local pre plugin configuration
util.try_mreload('pre')
LazyVim.on_very_lazy(startup.post)
LazyVim.on_very_lazy(startup.late)
end
-- Load plugins
local function psoxizsh_post_config(plugs)
-- Local post plugin configuration
util.try_mreload('post')
cmd('colorscheme ' .. vim.g.my_color_scheme)
-- Setup file backups
cmd ':silent !mkdir -p ~/.vimbackup'
o.backupdir = fn.expand('~') .. '/.vimbackup'
o.directory = fn.expand('~') .. '/.vimbackup'
o.hlsearch = true
-- ripgrep settings
g.rg_highlight = 'true'
g.rg_derive_root = 'true'
-- Other
g.rainbow_active = 1
cmd([[
function! CloseNetrw() abort
for bufn in range(1, bufnr('$'))
if bufexists(bufn) && getbufvar(bufn, '&filetype') ==# 'netrw'
silent! execute 'bwipeout ' . bufn
if getline(2) =~# '^" Netrw '
silent! bwipeout
endif
return
endif
endfor
endfunction
]])
keys.Buffer.N.CloseNetrw { action = fn.CloseNetrw }
-- Open any known user configuration paths for editing
local OpenConfig = function()
local rtp = vim.fn.join(o.runtimepath:get(), ',')
local files = {
early = vim.fn.globpath(rtp, '/lua/early.lua', 0, 1)[1] or nil,
pre = vim.fn.globpath(rtp, '/lua/pre.lua', 0, 1)[1] or nil,
post = vim.fn.globpath(rtp, '/lua/post.lua', 0, 1)[1] or nil,
late = vim.fn.globpath(rtp, '/lua/late.lua', 0, 1)[1] or nil,
my = os.getenv("MYVIMRC") or nil,
}
for _, file in pairs(files) do
if vim.fn.filereadable(file) == 1 then
cmd('edit ' .. file)
end
end
end
keys.Global.N.Leader.OpenConfig { action = OpenConfig }
local ToggleGutter = function()
if o.number:get() then
o.number = false
o.relativenumber = false
o.signcolumn = 'no'
else
o.number = true
o.relativenumber = true
o.signcolumn = 'yes:1'
end
end
keys.Global.N.Leader.ToggleGutter { action = ToggleGutter }
-- Reload configuration
local function ReloadConfig()
plugs:reload()
vim.notify('Reloaded Config',
vim.log.levels.INFO,
{ title = 'Psoxizsh.Plug' }
)
end
keys.Global.N.Leader.ReloadConfig { action = ReloadConfig }
local color_highlights_override = function()
local hl = require 'psoxizsh.util.highlight'
-- For nvim-cmp, for whatever reason these are overwritten if set in the
-- config function
hl.CmpItemAbbrDeprecated { strikethrough = true }
hl.CmpItemAbbrMatch { bold = true }
hl.CmpItemAbbrMatchFuzzy { bold = true }
hl.CmpItemMenu { italic = true }
-- Disable stupid spelling highlights (the 90s were a weird time)
hl.SpellCap:clear()
hl.SpellLocal:clear()
hl.SpellRare:clear()
end
color_highlights_override()
au.PsoxColorSchemeOverrides {
{ 'ColorScheme', '*', color_highlights_override }
}
end
local function psoxizsh_late_config(_)
-- Local late configuration
util.try_mreload('late')
-- Register our key maps
keys.Global:register()
-- Rest of config below is specifically not user override-able
o.exrc = true
o.secure = true
o.modeline = true
o.modelines = 7
end
local hooks = {
early = { 'PsoxConfigEarly', psoxizsh_early_config },
post = { 'PsoxConfigPost', psoxizsh_post_config },
late = { 'PsoxConfigLate', psoxizsh_late_config },
}
return require('psoxizsh.plugins') { hooks = hooks }
return M

View File

@ -0,0 +1,131 @@
---@class psoxizsh.startup
local M = {}
---Called first, before any user callbacks are triggered
function M.pre() end
---Called before any plugins are initialized, but after any early user callbacks
function M.early()
local o, g, fn, cmd = vim.opt, vim.g, vim.fn, vim.cmd
local au = require 'psoxizsh.autocmd'
-- local diagnostic = require 'psoxizsh.diagnostic'
g.autoformat = false
-- Color settings
o.background = 'dark'
o.termguicolors = true
-- Hide buffers don't close them
o.hidden = true
-- Sane pane opening
o.splitbelow = true
o.splitright = true
-- File indent opts
o.encoding = "utf-8"
o.shiftwidth = 2
o.tabstop = 8
o.softtabstop = 2
o.expandtab = true
o.list = true
o.listchars:append {
trail = '\u{02FD}',
extends = '\u{22B3}',
precedes = '\u{22B2}',
nbsp = '\u{02EC}',
conceal = '\u{2219}',
tab = '\u{2559}\u{254C}\u{2556}',
}
o.ignorecase = true
o.infercase = true
cmd 'filetype plugin indent on'
-- Lower update time (Default 4000)
o.updatetime = 300
-- Numbered lines
o.number = true
o.relativenumber = true
o.signcolumn = 'yes:1'
-- Disable '--INSERT--' and friends
o.showmode = false
-- Use existing buffers
o.switchbuf = { "useopen", "usetab" }
o.timeoutlen = 1000
-- Set global statusline (0.7+ only)
if fn.has('nvim-0.7') == 1 then o.laststatus = 3 end
-- Setup vim.diagnostic APIs
-- diagnostic.setup {}
au.PsoxFileAutos {
{ 'FileType', 'yaml', function() cmd 'setlocal indentkeys-=<:> ts=8 sts=2 sw=2 expandtab' end },
{ 'FileType', 'go', function() cmd 'setlocal ts=4 sts=4 sw=4 noexpandtab' end },
{ 'FileType', 'quickfix,netrw', 'setlocal nobuflisted' },
}
end
---Called after plugins have finished initializing, but before any late user callbacks
function M.post()
local o, g, fn, cmd = vim.opt, vim.g, vim.fn, vim.cmd
local au = require 'psoxizsh.autocmd'
-- Setup file backups
cmd ':silent !mkdir -p ~/.vimbackup'
o.backupdir = fn.expand('~') .. '/.vimbackup'
o.directory = fn.expand('~') .. '/.vimbackup'
o.hlsearch = true
-- ripgrep settings
g.rg_highlight = 'true'
g.rg_derive_root = 'true'
-- Other
g.rainbow_active = 1
local color_highlights_override = function()
local hl = require 'psoxizsh.util.highlight'
-- For nvim-cmp, for whatever reason these are overwritten if set in the
-- config function
hl.CmpItemAbbrDeprecated { strikethrough = true }
hl.CmpItemAbbrMatch { bold = true }
hl.CmpItemAbbrMatchFuzzy { bold = true }
hl.CmpItemMenu { italic = true }
-- Disable stupid spelling highlights (the 90s were a weird time)
hl.SpellCap:clear()
hl.SpellLocal:clear()
hl.SpellRare:clear()
end
color_highlights_override()
au.PsoxColorSchemeOverrides {
{ 'ColorScheme', '*', color_highlights_override }
}
end
---Called after plugins have initialized and any user callbacks have triggered
function M.late()
-- Rest of config below is specifically not user override-able
local o = vim.opt
o.exrc = true
o.secure = true
o.modeline = true
o.modelines = 7
end
return M