diff options
author | Raphael <glephunter@gmail.com> | 2022-07-10 00:40:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-09 18:40:32 +0200 |
commit | 6b1a8f23d7da60aa2fe53cd66760176d2f589690 (patch) | |
tree | 711b222a37080bed093f2f1776e96d5cf215c838 | |
parent | 7dbe6b1a460202a700e256e44245dff3713ff435 (diff) | |
download | rneovim-6b1a8f23d7da60aa2fe53cd66760176d2f589690.tar.gz rneovim-6b1a8f23d7da60aa2fe53cd66760176d2f589690.tar.bz2 rneovim-6b1a8f23d7da60aa2fe53cd66760176d2f589690.zip |
refactor(lua): replace vim.cmd use with API calls (#19283)
Signed-off-by: Raphael <glephunter@gmail.com>
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 72 | ||||
-rw-r--r-- | runtime/lua/vim/highlight.lua | 2 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 82 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/highlighter.lua | 2 |
4 files changed, 71 insertions, 87 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 07a3c87da0..40991673f3 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -338,6 +338,32 @@ local function make_augroup_key(namespace, bufnr) return string.format('DiagnosticInsertLeave:%s:%s', bufnr, ns.name) end +---@private +local function execute_scheduled_display(namespace, bufnr) + local args = bufs_waiting_to_update[bufnr][namespace] + if not args then + return + end + + -- Clear the args so we don't display unnecessarily. + bufs_waiting_to_update[bufnr][namespace] = nil + + M.show(namespace, bufnr, nil, args) +end + +--- @deprecated +--- Callback scheduled when leaving Insert mode. +--- +--- called from the Vimscript autocommand. +--- +--- See @ref schedule_display() +--- +---@private +function M._execute_scheduled_display(namespace, bufnr) + vim.deprecate('vim.diagnostic._execute_scheduled_display', nil, '0.9') + execute_scheduled_display(namespace, bufnr) +end + --- Table of autocmd events to fire the update for displaying new diagnostic information local insert_leave_auto_cmds = { 'InsertLeave', 'CursorHoldI' } @@ -346,18 +372,15 @@ local function schedule_display(namespace, bufnr, args) bufs_waiting_to_update[bufnr][namespace] = args local key = make_augroup_key(namespace, bufnr) + local group = vim.api.nvim_create_augroup(key, { clear = true }) if not registered_autocmds[key] then - vim.cmd(string.format( - [[augroup %s - au! - autocmd %s <buffer=%s> lua vim.diagnostic._execute_scheduled_display(%s, %s) - augroup END]], - key, - table.concat(insert_leave_auto_cmds, ','), - bufnr, - namespace, - bufnr - )) + vim.api.nvim_create_autocmd(insert_leave_auto_cmds, { + group = group, + buffer = bufnr, + callback = function() + execute_scheduled_display(namespace, bufnr) + end, + }) registered_autocmds[key] = true end end @@ -367,12 +390,7 @@ local function clear_scheduled_display(namespace, bufnr) local key = make_augroup_key(namespace, bufnr) if registered_autocmds[key] then - vim.cmd(string.format( - [[augroup %s - au! - augroup END]], - key - )) + vim.api.nvim_del_augroup_by_name(key) registered_autocmds[key] = nil end end @@ -1048,26 +1066,6 @@ function M._get_virt_text_chunks(line_diags, opts) end end ---- Callback scheduled when leaving Insert mode. ---- ---- This function must be exported publicly so that it is available to be ---- called from the Vimscript autocommand. ---- ---- See @ref schedule_display() ---- ----@private -function M._execute_scheduled_display(namespace, bufnr) - local args = bufs_waiting_to_update[bufnr][namespace] - if not args then - return - end - - -- Clear the args so we don't display unnecessarily. - bufs_waiting_to_update[bufnr][namespace] = nil - - M.show(namespace, bufnr, nil, args) -end - --- Hide currently displayed diagnostics. --- --- This only clears the decorations displayed in the buffer. Diagnostics can diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index e72d45f11c..ddd504a0e0 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -11,6 +11,7 @@ M.priorities = { ---@private function M.create(higroup, hi_info, default) + vim.deprecate('vim.highlight.create', 'vim.api.nvim_set_hl', '0.9') local options = {} -- TODO: Add validation for k, v in pairs(hi_info) do @@ -28,6 +29,7 @@ end ---@private function M.link(higroup, link_to, force) + vim.deprecate('vim.highlight.link', 'vim.api.nvim_set_hl', '0.9') vim.cmd(string.format([[highlight%s link %s %s]], force and '!' or ' default', higroup, link_to)) end diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index b10f0e82f2..c061cbd216 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -1355,49 +1355,11 @@ function M.stylize_markdown(bufnr, contents, opts) end ---@private ---- Creates autocommands to close a preview window when events happen. ---- ----@param events table list of events ----@param winnr number window id of preview window ----@param bufnrs table list of buffers where the preview window will remain visible ----@see |autocmd-events| -local function close_preview_autocmd(events, winnr, bufnrs) - local augroup = 'preview_window_' .. winnr - - -- close the preview window when entered a buffer that is not - -- the floating window buffer or the buffer that spawned it - vim.cmd(string.format( - [[ - augroup %s - autocmd! - autocmd BufEnter * lua vim.lsp.util._close_preview_window(%d, {%s}) - augroup end - ]], - augroup, - winnr, - table.concat(bufnrs, ',') - )) - - if #events > 0 then - vim.cmd(string.format( - [[ - augroup %s - autocmd %s <buffer> lua vim.lsp.util._close_preview_window(%d) - augroup end - ]], - augroup, - table.concat(events, ','), - winnr - )) - end -end - ----@private --- Closes the preview window --- ---@param winnr number window id of preview window ---@param bufnrs table|nil optional list of ignored buffers -function M._close_preview_window(winnr, bufnrs) +local function close_preview_window(winnr, bufnrs) vim.schedule(function() -- exit if we are in one of ignored buffers if bufnrs and vim.tbl_contains(bufnrs, api.nvim_get_current_buf()) then @@ -1405,20 +1367,42 @@ function M._close_preview_window(winnr, bufnrs) end local augroup = 'preview_window_' .. winnr - vim.cmd(string.format( - [[ - augroup %s - autocmd! - augroup end - augroup! %s - ]], - augroup, - augroup - )) + api.nvim_del_augroup_by_name(augroup) pcall(vim.api.nvim_win_close, winnr, true) end) end +---@private +--- Creates autocommands to close a preview window when events happen. +--- +---@param events table list of events +---@param winnr number window id of preview window +---@param bufnrs table list of buffers where the preview window will remain visible +---@see |autocmd-events| +local function close_preview_autocmd(events, winnr, bufnrs) + local augroup = api.nvim_create_augroup('preview_window_' .. winnr, { + clear = true, + }) + + -- close the preview window when entered a buffer that is not + -- the floating window buffer or the buffer that spawned it + api.nvim_create_autocmd('BufEnter', { + group = augroup, + callback = function() + close_preview_window(winnr, bufnrs) + end, + }) + + if #events > 0 then + api.nvim_create_autocmd(events, { + buffer = bufnrs[2], + callback = function() + close_preview_window(winnr) + end, + }) + end +end + ---@internal --- Computes size of float needed to show contents (with optional wrapping) --- diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index 92352db70e..e27a5fa9c3 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -16,7 +16,7 @@ local _default_highlights = {} local _link_default_highlight_once = function(from, to) if not _default_highlights[from] then _default_highlights[from] = true - vim.cmd(string.format('highlight default link %s %s', from, to)) + a.nvim_set_hl(0, from, { link = to, default = true }) end return from |