diff options
author | kylo252 <[email protected]> | 2022-05-21 16:48:47 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2022-05-21 16:48:47 +0200 |
commit | 23df368b00bda0ed4a01fac92f7ad80998c1d34a (patch) | |
tree | 2362ff18ac68eab313380e814238fa15c1237934 /lua/lvim/core | |
parent | a2454310b6c0b5b530521a77b9b8eb290274e040 (diff) |
refactor: load the default options once (#2592)
BREAKING CHANGE: modifying the default options for keymaps and autocmds
is now done by overwriting them, since they won't be loaded into the
global `lvim` table anymore
* refactor: use the lua-commands api
* refactor!: use the lua-autocmds api
* fix(settings): let neovim handle spellfile
* feat: add log:set_log_level()
* chore: update examples
* chore: add deprecation notice for custom_groups
Diffstat (limited to 'lua/lvim/core')
-rw-r--r-- | lua/lvim/core/alpha.lua | 28 | ||||
-rw-r--r-- | lua/lvim/core/alpha/dashboard.lua | 13 | ||||
-rw-r--r-- | lua/lvim/core/autocmds.lua | 167 | ||||
-rw-r--r-- | lua/lvim/core/bufferline.lua | 6 | ||||
-rw-r--r-- | lua/lvim/core/commands.lua | 78 | ||||
-rw-r--r-- | lua/lvim/core/log.lua | 17 |
6 files changed, 196 insertions, 113 deletions
diff --git a/lua/lvim/core/alpha.lua b/lua/lvim/core/alpha.lua index 7612854b..fd637818 100644 --- a/lua/lvim/core/alpha.lua +++ b/lua/lvim/core/alpha.lua @@ -47,23 +47,21 @@ local function resolve_config(theme_name) end local function configure_additional_autocmds() - local aucmds = { - { - "FileType", - "alpha", - "set showtabline=0 | autocmd BufLeave <buffer> set showtabline=" .. vim.opt.showtabline._value, - }, - } + local group = "_dashboard_settings" + vim.api.nvim_create_augroup(group, {}) + vim.api.nvim_create_autocmd("FileType", { + group = group, + pattern = "alpha", + command = "set showtabline=0 | autocmd BufLeave <buffer> set showtabline=" .. vim.opt.showtabline._value, + }) if not lvim.builtin.lualine.options.globalstatus then - aucmds[#aucmds + 1] = - -- https://github.com/goolord/alpha-nvim/issues/42 - { - "FileType", - "alpha", - "set laststatus=0 | autocmd BufUnload <buffer> set laststatus=" .. vim.opt.laststatus._value, - } + -- https://github.com/goolord/alpha-nvim/issues/42 + vim.api.nvim_create_autocmd("FileType", { + group = group, + pattern = "alpha", + command = "set laststatus=0 | autocmd BufUnload <buffer> set laststatus=" .. vim.opt.laststatus._value, + }) end - require("lvim.core.autocmds").define_augroups { _alpha = aucmds } end function M.setup() diff --git a/lua/lvim/core/alpha/dashboard.lua b/lua/lvim/core/alpha/dashboard.lua index 010d8c1a..d65980fb 100644 --- a/lua/lvim/core/alpha/dashboard.lua +++ b/lua/lvim/core/alpha/dashboard.lua @@ -34,16 +34,7 @@ function M.get_sections() } local text = require "lvim.interface.text" - local git_utils = require "lvim.utils.git" - - local current_branch = git_utils.get_lvim_branch() - - local lvim_version - if current_branch ~= "HEAD" or "" then - lvim_version = current_branch .. "-" .. git_utils.get_lvim_current_sha() - else - lvim_version = "v" .. git_utils.get_lvim_tag() - end + local lvim_version = require("lvim.utils.git").get_lvim_version() local footer = { type = "text", @@ -68,7 +59,7 @@ function M.get_sections() { "SPC L c", " Configuration", - "<CMD>edit " .. require("lvim.config").get_user_config_path() .. " <CR>", + "<CMD>edit " .. require("lvim.config"):get_user_config_path() .. " <CR>", }, }, } diff --git a/lua/lvim/core/autocmds.lua b/lua/lvim/core/autocmds.lua index 64b0a9b9..0ca21439 100644 --- a/lua/lvim/core/autocmds.lua +++ b/lua/lvim/core/autocmds.lua @@ -2,7 +2,7 @@ local M = {} local Log = require "lvim.core.log" --- Load the default set of autogroups and autocommands. -function M.load_augroups() +function M.load_defaults() local user_config_file = require("lvim.config"):get_user_config_path() if vim.loop.os_uname().version:match "Windows" then @@ -10,51 +10,72 @@ function M.load_augroups() user_config_file = user_config_file:gsub("\\", "/") end - return { - _general_settings = { - { "FileType", "qf,help,man", "nnoremap <silent> <buffer> q :close<CR>" }, + local definitions = { + { + "TextYankPost", { - "TextYankPost", - "*", - "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})", + group = "_general_settings", + pattern = "*", + desc = "Highlight text on yank", + callback = function() + require("vim.highlight").on_yank { higroup = "Search", timeout = 200 } + end, }, + }, + { + "BufWritePost", { - "BufWinEnter", - "dashboard", - "setlocal cursorline signcolumn=yes cursorcolumn number", + group = "_general_settings", + pattern = user_config_file, + desc = "Trigger LvimReload on saving " .. vim.fn.expand "%:~", + callback = function() + require("lvim.config"):reload() + end, }, - { "BufWritePost", user_config_file, "lua require('lvim.config'):reload()" }, - { "FileType", "qf", "set nobuflisted" }, - -- { "VimLeavePre", "*", "set title set titleold=" }, }, - _formatoptions = { + { + "FileType", { - "BufWinEnter,BufRead,BufNewFile", - "*", - "setlocal formatoptions-=c formatoptions-=r formatoptions-=o", + group = "_filetype_settings", + pattern = "qf", + command = "set nobuflisted", }, }, - _filetypechanges = {}, - _git = { - { "FileType", "gitcommit", "setlocal wrap" }, - { "FileType", "gitcommit", "setlocal spell" }, - }, - _markdown = { - { "FileType", "markdown", "setlocal wrap" }, - { "FileType", "markdown", "setlocal spell" }, + { + "FileType", + { + group = "_filetype_settings", + pattern = { "gitcommit", "markdown" }, + command = "setlocal wrap spell", + }, }, - _buffer_bindings = { - { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" }, + { + "FileType", + { + group = "_buffer_mappings", + pattern = { "qf", "help", "man", "floaterm", "lspinfo", "lsp-installer", "null-ls-info" }, + command = "nnoremap <silent> <buffer> q :close<CR>", + }, }, - _auto_resize = { - -- will cause split windows to be resized evenly if main window is resized - { "VimResized", "*", "tabdo wincmd =" }, + { + { "BufWinEnter", "BufRead", "BufNewFile" }, + { + group = "_format_options", + pattern = "*", + command = "setlocal formatoptions-=c formatoptions-=r formatoptions-=o", + }, }, - _general_lsp = { - { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" }, + { + "VimResized", + { + group = "_auto_resize", + pattern = "*", + command = "tabdo wincmd =", + }, }, - custom_groups = {}, } + + M.define_autocmds(definitions) end local get_format_on_save_opts = function() @@ -84,7 +105,7 @@ function M.enable_format_on_save() end function M.disable_format_on_save() - pcall(vim.api.nvim_del_augroup_by_name, "lsp_format_on_save") + M.clear_augroup "lsp_format_on_save" Log:debug "disabled format-on-save" end @@ -97,11 +118,11 @@ function M.configure_format_on_save() end function M.toggle_format_on_save() - local status, _ = pcall(vim.api.nvim_get_autocmds, { + local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = "lsp_format_on_save", event = "BufWritePre", }) - if not status then + if not exists then M.enable_format_on_save() else M.disable_format_on_save() @@ -109,47 +130,61 @@ function M.toggle_format_on_save() end function M.enable_transparent_mode() - vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi NormalNC ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi MsgArea ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi TelescopeBorder ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi NvimTreeNormal ctermbg=none guibg=none" - vim.cmd "au ColorScheme * hi EndOfBuffer ctermbg=none guibg=none" - vim.cmd "let &fcs='eob: '" + vim.api.nvim_create_autocmd("ColorScheme", { + pattern = "*", + callback = function() + local hl_groups = { + "Normal", + "SignColumn", + "NormalNC", + "TelescopeBorder", + "NvimTreeNormal", + "EndOfBuffer", + "MsgArea", + } + for _, name in ipairs(hl_groups) do + vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name)) + end + end, + }) + vim.opt.fillchars = "eob: " end ---- Disable autocommand groups if it exists ---- This is more reliable than trying to delete the augroup itself +--- Clean autocommand in a group if it exists +--- This is safer than trying to delete the augroup itself ---@param name string the augroup name -function M.disable_augroup(name) +function M.clear_augroup(name) -- defer the function in case the autocommand is still in-use + local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = name }) + if not exists then + Log:debug("ignoring request to clear autocmds from non-existent group " .. name) + return + end vim.schedule(function() - if vim.fn.exists("#" .. name) == 1 then - vim.cmd("augroup " .. name) - vim.cmd "autocmd!" - vim.cmd "augroup END" + local status_ok, _ = xpcall(function() + vim.api.nvim_clear_autocmds { group = name } + end, debug.traceback) + if not status_ok then + Log:warn("problems detected while clearing autocmds from " .. name) + Log:debug(debug.traceback()) end end) end --- Create autocommand groups based on the passed definitions ----@param definitions table contains trigger, pattern and text. The key will be used as a group name -function M.define_augroups(definitions, buffer) - for group_name, definition in pairs(definitions) do - vim.cmd("augroup " .. group_name) - if buffer then - vim.cmd [[autocmd! * <buffer>]] - else - vim.cmd [[autocmd!]] - end - - for _, def in pairs(definition) do - local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ") - vim.cmd(command) +--- Also creates the augroup automatically if it doesn't exist +---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd` +function M.define_autocmds(definitions) + for _, entry in ipairs(definitions) do + local event = entry[1] + local opts = entry[2] + if type(opts.group) == "string" and opts.group ~= "" then + local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group }) + if not exists then + vim.api.nvim_create_augroup(opts.group, {}) + end end - - vim.cmd "augroup END" + vim.api.nvim_create_autocmd(event, opts) end end diff --git a/lua/lvim/core/bufferline.lua b/lua/lvim/core/bufferline.lua index cb322032..2df1e514 100644 --- a/lua/lvim/core/bufferline.lua +++ b/lua/lvim/core/bufferline.lua @@ -144,9 +144,9 @@ end -- Common kill function for bdelete and bwipeout -- credits: based on bbye and nvim-bufdel ----@param kill_command String defaults to "bd" ----@param bufnr Number defaults to the current buffer ----@param force Boolean defaults to false +---@param kill_command string defaults to "bd" +---@param bufnr? number defaults to the current buffer +---@param force? boolean defaults to false function M.buf_kill(kill_command, bufnr, force) local bo = vim.bo local api = vim.api diff --git a/lua/lvim/core/commands.lua b/lua/lvim/core/commands.lua index 4ddbfcf0..80c5bb03 100644 --- a/lua/lvim/core/commands.lua +++ b/lua/lvim/core/commands.lua @@ -1,7 +1,6 @@ local M = {} -M.defaults = { - [[ +vim.cmd [[ function! QuickFixToggle() if empty(filter(getwininfo(), 'v:val.quickfix')) copen @@ -9,21 +8,70 @@ M.defaults = { cclose endif endfunction - ]], - [[ command! BufferKill lua require('lvim.core.bufferline').buf_kill('bd') ]], - -- :LvimInfo - [[ command! LvimInfo lua require('lvim.core.info').toggle_popup(vim.bo.filetype) ]], - [[ command! LvimCacheReset lua require('lvim.utils.hooks').reset_cache() ]], - [[ command! LvimUpdate lua require('lvim.bootstrap').update() ]], - [[ command! LvimSyncCorePlugins lua require('lvim.plugin-loader'):sync_core_plugins() ]], - [[ command! LvimReload lua require('lvim.config'):reload() ]], - [[ command! LvimToggleFormatOnSave lua require('lvim.core.autocmds').toggle_format_on_save() ]], - [[ command! LvimVersion lua require('lvim.core.telescope.custom-finders').view_lunarvim_changelog() ]], +]] + +M.defaults = { + { + name = "BufferKill", + fn = function() + require("lvim.core.bufferline").buf_kill "bd" + end, + }, + { + name = "LvimToggleFormatOnSave", + fn = function() + require("lvim.core.autocmds").toggle_format_on_save() + end, + }, + { + name = "LvimInfo", + fn = function() + require("lvim.core.info").toggle_popup(vim.bo.filetype) + end, + }, + { + name = "LvimCacheReset", + fn = function() + require("lvim.utils.hooks").reset_cache() + end, + }, + { + name = "LvimReload", + fn = function() + require("lvim.config"):reload() + end, + }, + { + name = "LvimUpdate", + fn = function() + require("lvim.bootstrap"):update() + end, + }, + { + name = "LvimSyncCorePlugins", + fn = function() + require("lvim.plugin-loader").sync_core_plugins() + end, + }, + { + name = "LvimChangelog", + fn = function() + require("lvim.core.telescope.custom-finders").view_lunarvim_changelog() + end, + }, + { + name = "LvimVersion", + fn = function() + print(require("lvim.utils.git").get_lvim_version()) + end, + }, } -M.load = function(commands) - for _, command in ipairs(commands) do - vim.cmd(command) +function M.load(collection) + local common_opts = { force = true } + for _, cmd in pairs(collection) do + local opts = vim.tbl_deep_extend("force", common_opts, cmd.opts or {}) + vim.api.nvim_create_user_command(cmd.name, cmd.fn, opts) end end diff --git a/lua/lvim/core/log.lua b/lua/lvim/core/log.lua index 15ccb11c..bc05d72b 100644 --- a/lua/lvim/core/log.lua +++ b/lua/lvim/core/log.lua @@ -11,15 +11,26 @@ vim.tbl_add_reverse_lookup(Log.levels) local notify_opts = {} +function Log:set_level(level) + -- package.loaded["lvim.core.log"] = nil + local log_level = Log.levels[level:upper()] + local status_ok, logger = pcall(require("structlog").get_logger, "lvim") + if status_ok then + for _, s in ipairs(logger.sinks) do + s.level = log_level + end + end + + package.loaded["packer.log"] = nil + require("packer.log").new { level = lvim.log.level } +end + function Log:init() local status_ok, structlog = pcall(require, "structlog") if not status_ok then return nil end - package.loaded["packer.log"] = nil - require("packer.log").new { level = lvim.log.level } - local log_level = Log.levels[(lvim.log.level):upper() or "WARN"] local lvim_log = { lvim = { |