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 = {
|
||||
definition_identifier = "gd",
|
||||
@@ -9,81 +9,83 @@ M.references_available = false
|
||||
M.reference_count = 0
|
||||
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 cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||
return bufnr, { line = cursor[1] - 1, character = cursor[2] }, bufname
|
||||
end
|
||||
|
||||
local params = {
|
||||
textDocument = { uri = bufname },
|
||||
position = { line = cursor[1] - 1, character = cursor[2] },
|
||||
local function lsp_request(method, params, callback)
|
||||
local bufnr, position, uri = get_current_context()
|
||||
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 },
|
||||
}
|
||||
|
||||
vim.lsp.buf_request(bufnr, "textDocument/references", params, function(err, result, _, _)
|
||||
}, function(err, result, _, _)
|
||||
if err then
|
||||
error(tostring(err))
|
||||
end
|
||||
|
||||
if not result then
|
||||
if not result or vim.tbl_count(result) == 0 then
|
||||
M.references_available = false
|
||||
M.reference_count = 0
|
||||
return false
|
||||
end
|
||||
|
||||
if vim.tbl_count(result) > 0 then
|
||||
M.references_available = true
|
||||
M.reference_count = vim.tbl_count(result) - 1
|
||||
return true
|
||||
end
|
||||
|
||||
M.references_available = false
|
||||
M.reference_count = 0
|
||||
return false
|
||||
M.references_available = true
|
||||
M.reference_count = vim.tbl_count(result) - 1
|
||||
return true
|
||||
end)
|
||||
end
|
||||
|
||||
local function definition()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
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, _, _)
|
||||
lsp_request("textDocument/definition", {}, function(err, result, _, _)
|
||||
if err then
|
||||
error(tostring(err))
|
||||
end
|
||||
|
||||
if not result then
|
||||
if not result or vim.tbl_count(result) == 0 then
|
||||
M.definition_available = false
|
||||
return false
|
||||
end
|
||||
|
||||
if vim.tbl_count(result) > 0 then
|
||||
M.definition_available = true
|
||||
return true
|
||||
end
|
||||
|
||||
M.definition_available = false
|
||||
return false
|
||||
M.definition_available = true
|
||||
return true
|
||||
end)
|
||||
end
|
||||
|
||||
local debounced_references = debounce(references, 100)
|
||||
local debounced_definition = debounce(definition, 100)
|
||||
|
||||
M.statusline = function()
|
||||
references()
|
||||
definition()
|
||||
debounced_references()
|
||||
debounced_definition()
|
||||
|
||||
local definition_status = ""
|
||||
|
||||
if M.definition_available then
|
||||
definition_status = require("statusline-action-hints").config.definition_identifier
|
||||
definition_status = M.config.definition_identifier
|
||||
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
|
||||
|
||||
M.setup = function(options)
|
||||
@@ -93,7 +95,7 @@ M.setup = function(options)
|
||||
|
||||
-- merge user supplied options with defaults..
|
||||
for k, v in pairs(options) do
|
||||
require("statusline-action-hints").config[k] = v
|
||||
M.config[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user