mirror of
https://github.com/tiyn/action-hints.nvim.git
synced 2025-11-13 13:59:45 +01:00
Refactor / add debounce
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
M = {}
|
local M = {}
|
||||||
|
|
||||||
M.config = {
|
M.config = {
|
||||||
definition_identifier = "gd",
|
definition_identifier = "gd",
|
||||||
@@ -9,81 +9,83 @@ M.references_available = false
|
|||||||
M.reference_count = 0
|
M.reference_count = 0
|
||||||
M.definition_available = false
|
M.definition_available = false
|
||||||
|
|
||||||
local function references()
|
local function debounce(func, delay)
|
||||||
|
local timer_id = nil
|
||||||
|
return function(...)
|
||||||
|
if timer_id then
|
||||||
|
vim.fn.timer_stop(timer_id)
|
||||||
|
end
|
||||||
|
local args = { ... }
|
||||||
|
timer_id = vim.fn.timer_start(delay, function()
|
||||||
|
func(unpack(args))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_current_context()
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||||
|
return bufnr, { line = cursor[1] - 1, character = cursor[2] }, bufname
|
||||||
|
end
|
||||||
|
|
||||||
local params = {
|
local function lsp_request(method, params, callback)
|
||||||
textDocument = { uri = bufname },
|
local bufnr, position, uri = get_current_context()
|
||||||
position = { line = cursor[1] - 1, character = cursor[2] },
|
params.textDocument = { uri = uri }
|
||||||
|
params.position = position
|
||||||
|
vim.lsp.buf_request(bufnr, method, params, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function references()
|
||||||
|
lsp_request("textDocument/references", {
|
||||||
context = { includeDeclaration = true },
|
context = { includeDeclaration = true },
|
||||||
}
|
}, function(err, result, _, _)
|
||||||
|
|
||||||
vim.lsp.buf_request(bufnr, "textDocument/references", params, function(err, result, _, _)
|
|
||||||
if err then
|
if err then
|
||||||
error(tostring(err))
|
error(tostring(err))
|
||||||
end
|
end
|
||||||
|
|
||||||
if not result then
|
if not result or vim.tbl_count(result) == 0 then
|
||||||
M.references_available = false
|
M.references_available = false
|
||||||
M.reference_count = 0
|
M.reference_count = 0
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.tbl_count(result) > 0 then
|
M.references_available = true
|
||||||
M.references_available = true
|
M.reference_count = vim.tbl_count(result) - 1
|
||||||
M.reference_count = vim.tbl_count(result) - 1
|
return true
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
M.references_available = false
|
|
||||||
M.reference_count = 0
|
|
||||||
return false
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function definition()
|
local function definition()
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
lsp_request("textDocument/definition", {}, function(err, result, _, _)
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
|
||||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
|
||||||
|
|
||||||
local params = {
|
|
||||||
textDocument = { uri = bufname },
|
|
||||||
position = { line = cursor[1] - 1, character = cursor[2] },
|
|
||||||
}
|
|
||||||
|
|
||||||
vim.lsp.buf_request(bufnr, "textDocument/definition", params, function(err, result, _, _)
|
|
||||||
if err then
|
if err then
|
||||||
error(tostring(err))
|
error(tostring(err))
|
||||||
end
|
end
|
||||||
|
|
||||||
if not result then
|
if not result or vim.tbl_count(result) == 0 then
|
||||||
M.definition_available = false
|
M.definition_available = false
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.tbl_count(result) > 0 then
|
M.definition_available = true
|
||||||
M.definition_available = true
|
return true
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
M.definition_available = false
|
|
||||||
return false
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local debounced_references = debounce(references, 100)
|
||||||
|
local debounced_definition = debounce(definition, 100)
|
||||||
|
|
||||||
M.statusline = function()
|
M.statusline = function()
|
||||||
references()
|
debounced_references()
|
||||||
definition()
|
debounced_definition()
|
||||||
|
|
||||||
local definition_status = ""
|
local definition_status = ""
|
||||||
|
|
||||||
if M.definition_available then
|
if M.definition_available then
|
||||||
definition_status = require("statusline-action-hints").config.definition_identifier
|
definition_status = M.config.definition_identifier
|
||||||
end
|
end
|
||||||
|
|
||||||
return string.format(require("statusline-action-hints").config.template, definition_status, M.reference_count)
|
return string.format(M.config.template, definition_status, M.reference_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.setup = function(options)
|
M.setup = function(options)
|
||||||
@@ -93,7 +95,7 @@ M.setup = function(options)
|
|||||||
|
|
||||||
-- merge user supplied options with defaults..
|
-- merge user supplied options with defaults..
|
||||||
for k, v in pairs(options) do
|
for k, v in pairs(options) do
|
||||||
require("statusline-action-hints").config[k] = v
|
M.config[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user