refactor(lua): replace vim.cmd use with API calls (#19283)

Signed-off-by: Raphael <glephunter@gmail.com>
This commit is contained in:
Raphael
2022-07-10 00:40:32 +08:00
committed by GitHub
parent 7dbe6b1a46
commit 6b1a8f23d7
4 changed files with 71 additions and 87 deletions

View File

@@ -338,6 +338,32 @@ local function make_augroup_key(namespace, bufnr)
return string.format('DiagnosticInsertLeave:%s:%s', bufnr, ns.name) return string.format('DiagnosticInsertLeave:%s:%s', bufnr, ns.name)
end 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 --- Table of autocmd events to fire the update for displaying new diagnostic information
local insert_leave_auto_cmds = { 'InsertLeave', 'CursorHoldI' } 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 bufs_waiting_to_update[bufnr][namespace] = args
local key = make_augroup_key(namespace, bufnr) local key = make_augroup_key(namespace, bufnr)
local group = vim.api.nvim_create_augroup(key, { clear = true })
if not registered_autocmds[key] then if not registered_autocmds[key] then
vim.cmd(string.format( vim.api.nvim_create_autocmd(insert_leave_auto_cmds, {
[[augroup %s group = group,
au! buffer = bufnr,
autocmd %s <buffer=%s> lua vim.diagnostic._execute_scheduled_display(%s, %s) callback = function()
augroup END]], execute_scheduled_display(namespace, bufnr)
key, end,
table.concat(insert_leave_auto_cmds, ','), })
bufnr,
namespace,
bufnr
))
registered_autocmds[key] = true registered_autocmds[key] = true
end end
end end
@@ -367,12 +390,7 @@ local function clear_scheduled_display(namespace, bufnr)
local key = make_augroup_key(namespace, bufnr) local key = make_augroup_key(namespace, bufnr)
if registered_autocmds[key] then if registered_autocmds[key] then
vim.cmd(string.format( vim.api.nvim_del_augroup_by_name(key)
[[augroup %s
au!
augroup END]],
key
))
registered_autocmds[key] = nil registered_autocmds[key] = nil
end end
end end
@@ -1048,26 +1066,6 @@ function M._get_virt_text_chunks(line_diags, opts)
end end
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. --- Hide currently displayed diagnostics.
--- ---
--- This only clears the decorations displayed in the buffer. Diagnostics can --- This only clears the decorations displayed in the buffer. Diagnostics can

View File

@@ -11,6 +11,7 @@ M.priorities = {
---@private ---@private
function M.create(higroup, hi_info, default) function M.create(higroup, hi_info, default)
vim.deprecate('vim.highlight.create', 'vim.api.nvim_set_hl', '0.9')
local options = {} local options = {}
-- TODO: Add validation -- TODO: Add validation
for k, v in pairs(hi_info) do for k, v in pairs(hi_info) do
@@ -28,6 +29,7 @@ end
---@private ---@private
function M.link(higroup, link_to, force) 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)) vim.cmd(string.format([[highlight%s link %s %s]], force and '!' or ' default', higroup, link_to))
end end

View File

@@ -1354,50 +1354,12 @@ function M.stylize_markdown(bufnr, contents, opts)
return stripped return stripped
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 = '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 ---@private
--- Closes the preview window --- Closes the preview window
--- ---
---@param winnr number window id of preview window ---@param winnr number window id of preview window
---@param bufnrs table|nil optional list of ignored buffers ---@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() vim.schedule(function()
-- exit if we are in one of ignored buffers -- exit if we are in one of ignored buffers
if bufnrs and vim.tbl_contains(bufnrs, api.nvim_get_current_buf()) then 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 end
local augroup = 'preview_window_' .. winnr local augroup = 'preview_window_' .. winnr
vim.cmd(string.format( api.nvim_del_augroup_by_name(augroup)
[[
augroup %s
autocmd!
augroup end
augroup! %s
]],
augroup,
augroup
))
pcall(vim.api.nvim_win_close, winnr, true) pcall(vim.api.nvim_win_close, winnr, true)
end) end)
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 ---@internal
--- Computes size of float needed to show contents (with optional wrapping) --- Computes size of float needed to show contents (with optional wrapping)
--- ---

View File

@@ -16,7 +16,7 @@ local _default_highlights = {}
local _link_default_highlight_once = function(from, to) local _link_default_highlight_once = function(from, to)
if not _default_highlights[from] then if not _default_highlights[from] then
_default_highlights[from] = true _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 end
return from return from