From 5af51b45df76b7bdde3846716d3a5a7d4a60520c Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 11 Apr 2023 16:59:37 +0200 Subject: [PATCH] feat: add per-request hooks Closes #47. --- lua/glance/config.lua | 49 +++++++++++++++++++++++++++++++++++++++++++ lua/glance/init.lua | 15 ++++++++++--- lua/glance/lsp.lua | 6 ++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lua/glance/config.lua b/lua/glance/config.lua index 44efa4f..8c9d951 100644 --- a/lua/glance/config.lua +++ b/lua/glance/config.lua @@ -5,6 +5,55 @@ config.options = {} config.namespace = vim.api.nvim_create_namespace('Glance') config.hl_ns = 'Glance' +---@class GlancePreviewWinOpts +---@field enable boolean +---@field top_char string +---@field bottom_char string + +---@class GlanceListOpts +---@field position ('"left"' | '"right"') +---@field width number + +---@class GlanceThemeOpts +---@field enable boolean +---@field mode ('"brighten"' | '"darken"' | '"auto"') + +---@class GlanceMappingsOpts +---@field list table +---@field preview table + +---@class GlanceHooksOpts +---@field before_open fun(results: table[], open: fun(locations: table[]), jump: fun(location: table), method: GlanceMethod) +---@field before_close fun() +---@field after_close fun() + +---@class GlanceFoldsOpts +---@field fold_closed string +---@field fold_open string +---@field folded boolean + +---@class GlanceIndentLinesOpts +---@field enable boolean +---@field icon string + +---@class GlanceWinbarOpts +---@field enable boolean + +---@class GlanceOpts +---@field height integer +---@field zindex integer +---@field detached (fun(winid: integer): boolean) | boolean) +---@field preview_win_opts GlancePreviewWinOpts +---@field list GlanceListOpts +---@field theme GlanceThemeOpts +---@field mappings GlanceMappingsOpts +---@field hooks GlanceHooksOpts +---@field folds GlanceFoldsOpts +---@field indent_lines GlanceIndentLinesOpts +---@field winbar GlanceWinbarOpts + +---@param user_config GlanceOpts | nil +---@param actions GlanceActions function config.setup(user_config, actions) local defaults = { height = 18, diff --git a/lua/glance/init.lua b/lua/glance/init.lua index f734a94..595f062 100644 --- a/lua/glance/init.lua +++ b/lua/glance/init.lua @@ -7,6 +7,7 @@ local glance = {} Glance.__index = Glance local initialized = false +---@param opts? GlanceOpts function Glance.setup(opts) if initialized then return @@ -180,6 +181,11 @@ local function create( }) end +---@class GlanceOpenOpts +---@field method GlanceMethod +---@field hooks GlanceHooksOpts + +---@param opts GlanceOpenOpts local function open(opts) local lsp = require('glance.lsp') local parent_bufnr = vim.api.nvim_get_current_buf() @@ -221,7 +227,7 @@ local function open(opts) vim.lsp.util.jump_to_location(result, client.offset_encoding) end - local hooks = config.options.hooks + local hooks = opts.hooks or config.options.hooks if hooks and type(hooks.before_open) == 'function' then hooks.before_open(results, _open, _jump, opts.method) @@ -232,6 +238,7 @@ local function open(opts) end) end +---@class GlanceActions Glance.actions = { close = function() glance:close() @@ -301,7 +308,9 @@ Glance.actions = { jump_split = function() glance:jump({ cmd = 'split' }) end, - open = function(method) + ---@param method GlanceMethod + ---@param opts? { hooks: GlanceHooks } + open = function(method, opts) vim.validate({ method = utils.valid_enum( method, @@ -312,7 +321,7 @@ Glance.actions = { -- Manually call the setup in case user hasn't initialized the plugin -- It will only run once Glance.setup() - open({ method = method }) + open({ method = method, hooks = opts and opts.hooks }) end, } diff --git a/lua/glance/lsp.lua b/lua/glance/lsp.lua index 3091b62..dfaac07 100644 --- a/lua/glance/lsp.lua +++ b/lua/glance/lsp.lua @@ -44,6 +44,12 @@ local function create_handler(method) end end +---@alias GlanceMethod +--- | '"type_definitions"' +--- | '"implementations"' +--- | '"definitions"' +--- | '"references"' + M.methods = { type_definitions = { label = 'type definitions',