mirror of
https://github.com/LunarVim/LunarVim.git
synced 2024-09-20 18:45:08 +02:00
refactor: more consistent autocmds (#2133)
This commit is contained in:
parent
b3cfd165fb
commit
238e43e5b3
5 changed files with 93 additions and 47 deletions
|
@ -86,16 +86,12 @@ function M.enable_format_on_save(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.disable_format_on_save()
|
function M.disable_format_on_save()
|
||||||
M.remove_augroup "format_on_save"
|
M.disable_augroup "format_on_save"
|
||||||
Log:debug "disabled format-on-save"
|
Log:debug "disabled format-on-save"
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.configure_format_on_save()
|
function M.configure_format_on_save()
|
||||||
if lvim.format_on_save then
|
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()
|
local opts = get_format_on_save_opts()
|
||||||
M.enable_format_on_save(opts)
|
M.enable_format_on_save(opts)
|
||||||
else
|
else
|
||||||
|
@ -112,24 +108,73 @@ function M.toggle_format_on_save()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.remove_augroup(name)
|
function M.enable_lsp_document_highlight(client_id)
|
||||||
if vim.fn.exists("#" .. name) == 1 then
|
M.define_augroups({
|
||||||
vim.cmd("au! " .. name)
|
lsp_document_highlight = {
|
||||||
end
|
{
|
||||||
|
"CursorHold",
|
||||||
|
"<buffer>",
|
||||||
|
string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"CursorMoved",
|
||||||
|
"<buffer>",
|
||||||
|
"lua vim.lsp.buf.clear_references()",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.define_augroups(definitions) -- {{{1
|
function M.disable_lsp_document_highlight()
|
||||||
-- Create autocommand groups based on the passed definitions
|
M.disable_augroup "lsp_document_highlight"
|
||||||
--
|
end
|
||||||
-- The key will be the name of the group, and each definition
|
|
||||||
-- within the group should have:
|
function M.enable_code_lens_refresh()
|
||||||
-- 1. Trigger
|
M.define_augroups({
|
||||||
-- 2. Pattern
|
lsp_code_lens_refresh = {
|
||||||
-- 3. Text
|
{
|
||||||
-- just like how they would normally be defined from Vim itself
|
"InsertLeave ",
|
||||||
|
"<buffer>",
|
||||||
|
"lua vim.lsp.codelens.refresh()",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"InsertLeave ",
|
||||||
|
"<buffer>",
|
||||||
|
"lua vim.lsp.codelens.display()",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
for group_name, definition in pairs(definitions) do
|
||||||
vim.cmd("augroup " .. group_name)
|
vim.cmd("augroup " .. group_name)
|
||||||
vim.cmd "autocmd!"
|
if buffer then
|
||||||
|
vim.cmd [[autocmd! * <buffer>]]
|
||||||
|
else
|
||||||
|
vim.cmd [[autocmd!]]
|
||||||
|
end
|
||||||
|
|
||||||
for _, def in pairs(definition) do
|
for _, def in pairs(definition) do
|
||||||
local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
|
local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
|
||||||
|
|
|
@ -1,24 +1,13 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
local Log = require "lvim.core.log"
|
local Log = require "lvim.core.log"
|
||||||
local utils = require "lvim.utils"
|
local utils = require "lvim.utils"
|
||||||
|
local autocmds = require "lvim.core.autocmds"
|
||||||
|
|
||||||
local function lsp_highlight_document(client)
|
local function lsp_highlight_document(client)
|
||||||
if lvim.lsp.document_highlight == false then
|
if lvim.lsp.document_highlight == false then
|
||||||
return -- we don't need further
|
return -- we don't need further
|
||||||
end
|
end
|
||||||
-- Set autocommands conditional on server_capabilities
|
autocmds.enable_lsp_document_highlight(client.id)
|
||||||
if client.resolved_capabilities.document_highlight then
|
|
||||||
vim.api.nvim_exec(
|
|
||||||
[[
|
|
||||||
augroup lsp_document_highlight
|
|
||||||
autocmd! * <buffer>
|
|
||||||
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
|
||||||
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
|
||||||
augroup END
|
|
||||||
]],
|
|
||||||
false
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function lsp_code_lens_refresh(client)
|
local function lsp_code_lens_refresh(client)
|
||||||
|
@ -27,16 +16,7 @@ local function lsp_code_lens_refresh(client)
|
||||||
end
|
end
|
||||||
|
|
||||||
if client.resolved_capabilities.code_lens then
|
if client.resolved_capabilities.code_lens then
|
||||||
vim.api.nvim_exec(
|
autocmds.enable_code_lens_refresh()
|
||||||
[[
|
|
||||||
augroup lsp_code_lens_refresh
|
|
||||||
autocmd! * <buffer>
|
|
||||||
autocmd InsertLeave <buffer> lua vim.lsp.codelens.refresh()
|
|
||||||
autocmd InsertLeave <buffer> lua vim.lsp.codelens.display()
|
|
||||||
augroup END
|
|
||||||
]],
|
|
||||||
false
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -101,6 +81,15 @@ local function select_default_formater(client)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.common_on_exit(_, _)
|
||||||
|
if lvim.lsp.document_highlight then
|
||||||
|
autocmds.disable_lsp_document_highlight()
|
||||||
|
end
|
||||||
|
if lvim.lsp.code_lens_refresh then
|
||||||
|
autocmds.disable_code_lens_refresh()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M.common_on_init(client, bufnr)
|
function M.common_on_init(client, bufnr)
|
||||||
if lvim.lsp.on_init_callback then
|
if lvim.lsp.on_init_callback then
|
||||||
lvim.lsp.on_init_callback(client, bufnr)
|
lvim.lsp.on_init_callback(client, bufnr)
|
||||||
|
@ -132,6 +121,7 @@ function M.get_common_opts()
|
||||||
return {
|
return {
|
||||||
on_attach = M.common_on_attach,
|
on_attach = M.common_on_attach,
|
||||||
on_init = M.common_on_init,
|
on_init = M.common_on_init,
|
||||||
|
on_exit = M.common_on_exit,
|
||||||
capabilities = M.common_capabilities(),
|
capabilities = M.common_capabilities(),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -171,7 +161,7 @@ function M.setup()
|
||||||
|
|
||||||
require("lvim.lsp.null-ls").setup()
|
require("lvim.lsp.null-ls").setup()
|
||||||
|
|
||||||
require("lvim.core.autocmds").configure_format_on_save()
|
autocmds.configure_format_on_save()
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -24,6 +24,7 @@ local function resolve_config(name, user_config)
|
||||||
local config = {
|
local config = {
|
||||||
on_attach = require("lvim.lsp").common_on_attach,
|
on_attach = require("lvim.lsp").common_on_attach,
|
||||||
on_init = require("lvim.lsp").common_on_init,
|
on_init = require("lvim.lsp").common_on_init,
|
||||||
|
on_exit = require("lvim.lsp").common_on_exit,
|
||||||
capabilities = require("lvim.lsp").common_capabilities(),
|
capabilities = require("lvim.lsp").common_capabilities(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,4 +76,14 @@ function M.get_all_supported_filetypes()
|
||||||
return vim.tbl_keys(lsp_installer_filetypes or {})
|
return vim.tbl_keys(lsp_installer_filetypes or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.conditional_document_highlight(id)
|
||||||
|
local client_ok, method_supported = pcall(function()
|
||||||
|
return vim.lsp.get_client_by_id(id).resolved_capabilities.document_highlight
|
||||||
|
end)
|
||||||
|
if not client_ok or not method_supported then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.lsp.buf.document_highlight()
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -21,14 +21,14 @@ function M.run_on_packer_complete()
|
||||||
require("lvim.plugin-loader").recompile()
|
require("lvim.plugin-loader").recompile()
|
||||||
-- forcefully activate nvim-web-devicons
|
-- forcefully activate nvim-web-devicons
|
||||||
require("nvim-web-devicons").set_up_highlights()
|
require("nvim-web-devicons").set_up_highlights()
|
||||||
|
if package.loaded["lspconfig"] then
|
||||||
|
vim.cmd [[ LspStart ]]
|
||||||
|
end
|
||||||
Log:info "Reloaded configuration"
|
Log:info "Reloaded configuration"
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.run_post_reload()
|
function M.run_post_reload()
|
||||||
Log:debug "Starting post-reload hook"
|
Log:debug "Starting post-reload hook"
|
||||||
if package.loaded["lspconfig"] then
|
|
||||||
vim.cmd [[ LspRestart ]]
|
|
||||||
end
|
|
||||||
|
|
||||||
M.reset_cache()
|
M.reset_cache()
|
||||||
require("lvim.plugin-loader").ensure_installed()
|
require("lvim.plugin-loader").ensure_installed()
|
||||||
|
@ -68,7 +68,7 @@ function M.run_post_update()
|
||||||
-- TODO: add a changelog
|
-- TODO: add a changelog
|
||||||
vim.notify("Update complete", vim.log.levels.INFO)
|
vim.notify("Update complete", vim.log.levels.INFO)
|
||||||
if package.loaded["lspconfig"] then
|
if package.loaded["lspconfig"] then
|
||||||
vim.cmd [[ LspRestart ]]
|
vim.cmd [[ LspStart ]]
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue