diff options
author | kylo252 <[email protected]> | 2022-05-03 01:10:51 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2022-05-03 03:40:51 +0430 |
commit | cfa702e6fe2f875a2c674182fe2f86e8f613aa1e (patch) | |
tree | a129a8a43c649a883b14e691dbf086abd52195c4 /lua/lvim/core | |
parent | 10449b4e09086791abf382a53d2c0d61c85e89ad (diff) |
refactor: use api-autocmds for lsp functions (#2549)
* refactor: use api-autocmds for lsp functions
* chore: use the existing client.supports_method api
* fix: a callback in an autocmd doesn't pass nil
* revert: keep changes minimal to which-key
Co-authored-by: Abouzar Parvan <[email protected]>
Diffstat (limited to 'lua/lvim/core')
-rw-r--r-- | lua/lvim/core/autocmds.lua | 72 | ||||
-rw-r--r-- | lua/lvim/core/which-key.lua | 13 |
2 files changed, 24 insertions, 61 deletions
diff --git a/lua/lvim/core/autocmds.lua b/lua/lvim/core/autocmds.lua index e10a42db..17c9bc22 100644 --- a/lua/lvim/core/autocmds.lua +++ b/lua/lvim/core/autocmds.lua @@ -70,79 +70,44 @@ local get_format_on_save_opts = function() } end -function M.enable_format_on_save(opts) - local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout) - M.define_augroups { - format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } }, - } +function M.enable_format_on_save() + local opts = get_format_on_save_opts() + vim.api.nvim_create_augroup("lsp_format_on_save", {}) + vim.api.nvim_create_autocmd("BufWritePre", { + group = "lsp_format_on_save", + pattern = opts.pattern, + callback = function() + vim.lsp.buf.format { timeout_ms = opts.timeout, filter = opts.filter } + end, + }) Log:debug "enabled format-on-save" end function M.disable_format_on_save() - M.disable_augroup "format_on_save" + pcall(vim.api.nvim_del_augroup_by_name, "lsp_format_on_save") Log:debug "disabled format-on-save" end function M.configure_format_on_save() if lvim.format_on_save then - local opts = get_format_on_save_opts() - M.enable_format_on_save(opts) + M.enable_format_on_save() else M.disable_format_on_save() end end function M.toggle_format_on_save() - if vim.fn.exists "#format_on_save#BufWritePre" == 0 then - local opts = get_format_on_save_opts() - M.enable_format_on_save(opts) + local status, _ = pcall(vim.api.nvim_get_autocmds, { + group = "lsp_format_on_save", + event = "BufWritePre", + }) + if not status then + M.enable_format_on_save() else M.disable_format_on_save() end end -function M.enable_lsp_document_highlight(client_id) - M.define_augroups({ - lsp_document_highlight = { - { - "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 - -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 ", - "<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 - 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" @@ -170,7 +135,6 @@ 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) diff --git a/lua/lvim/core/which-key.lua b/lua/lvim/core/which-key.lua index 21620b01..3c3cc66b 100644 --- a/lua/lvim/core/which-key.lua +++ b/lua/lvim/core/which-key.lua @@ -153,32 +153,31 @@ M.config = function() "Git Diff", }, }, - l = { name = "LSP", a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" }, d = { "<cmd>Telescope diagnostics bufnr=0 theme=get_ivy<cr>", "Buffer Diagnostics" }, w = { "<cmd>Telescope diagnostics<cr>", "Diagnostics" }, - f = { "<cmd>lua vim.lsp.buf.formatting()<cr>", "Format" }, + f = { require("lvim.lsp.utils").format, "Format" }, i = { "<cmd>LspInfo<cr>", "Info" }, I = { "<cmd>LspInstallInfo<cr>", "Installer Info" }, j = { - "<cmd>lua vim.diagnostic.goto_next()<cr>", + vim.diagnostic.goto_next, "Next Diagnostic", }, k = { - "<cmd>lua vim.diagnostic.goto_prev()<cr>", + vim.diagnostic.goto_prev, "Prev Diagnostic", }, - l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" }, + l = { vim.lsp.codelens.run, "CodeLens Action" }, p = { name = "Peek", d = { "<cmd>lua require('lvim.lsp.peek').Peek('definition')<cr>", "Definition" }, t = { "<cmd>lua require('lvim.lsp.peek').Peek('typeDefinition')<cr>", "Type Definition" }, i = { "<cmd>lua require('lvim.lsp.peek').Peek('implementation')<cr>", "Implementation" }, }, - q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Quickfix" }, - r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" }, + q = { vim.diagnostic.setloclist, "Quickfix" }, + r = { vim.lsp.buf.rename, "Rename" }, s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" }, S = { "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>", |