From 238e43e5b370ef17267c344954e28003cd26614a Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Mon, 3 Jan 2022 11:06:45 +0100 Subject: refactor: more consistent autocmds (#2133) --- lua/lvim/core/autocmds.lua | 83 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 19 deletions(-) (limited to 'lua/lvim/core') diff --git a/lua/lvim/core/autocmds.lua b/lua/lvim/core/autocmds.lua index adb2c509..712fd323 100644 --- a/lua/lvim/core/autocmds.lua +++ b/lua/lvim/core/autocmds.lua @@ -86,16 +86,12 @@ function M.enable_format_on_save(opts) end function M.disable_format_on_save() - M.remove_augroup "format_on_save" + M.disable_augroup "format_on_save" Log:debug "disabled format-on-save" end function M.configure_format_on_save() if lvim.format_on_save then - if vim.fn.exists "#format_on_save#BufWritePre" == 1 then - M.remove_augroup "format_on_save" - Log:debug "reloading format-on-save configuration" - end local opts = get_format_on_save_opts() M.enable_format_on_save(opts) else @@ -112,24 +108,73 @@ function M.toggle_format_on_save() end end -function M.remove_augroup(name) - if vim.fn.exists("#" .. name) == 1 then - vim.cmd("au! " .. name) - end +function M.enable_lsp_document_highlight(client_id) + M.define_augroups({ + lsp_document_highlight = { + { + "CursorHold", + "", + string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id), + }, + { + "CursorMoved", + "", + "lua vim.lsp.buf.clear_references()", + }, + }, + }, true) +end + +function M.disable_lsp_document_highlight() + M.disable_augroup "lsp_document_highlight" +end + +function M.enable_code_lens_refresh() + M.define_augroups({ + lsp_code_lens_refresh = { + { + "InsertLeave ", + "", + "lua vim.lsp.codelens.refresh()", + }, + { + "InsertLeave ", + "", + "lua vim.lsp.codelens.display()", + }, + }, + }, true) end -function M.define_augroups(definitions) -- {{{1 - -- Create autocommand groups based on the passed definitions - -- - -- The key will be the name of the group, and each definition - -- within the group should have: - -- 1. Trigger - -- 2. Pattern - -- 3. Text - -- just like how they would normally be defined from Vim itself +function M.disable_code_lens_refresh() + M.disable_augroup "lsp_code_lens_refresh" +end + +--- Disable autocommand groups if it exists +--- This is more reliable than trying to delete the augroup itself +---@param name string the augroup name +function M.disable_augroup(name) + -- defer the function in case the autocommand is still in-use + vim.schedule(function() + if vim.fn.exists("#" .. name) == 1 then + vim.cmd("augroup " .. name) + vim.cmd "autocmd!" + vim.cmd "augroup END" + 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 +---@param buffer boolean indicate if the augroup should be local to the buffer +function M.define_augroups(definitions, buffer) for group_name, definition in pairs(definitions) do vim.cmd("augroup " .. group_name) - vim.cmd "autocmd!" + if buffer then + vim.cmd [[autocmd! * ]] + else + vim.cmd [[autocmd!]] + end for _, def in pairs(definition) do local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ") -- cgit v1.2.3