Skip to content

Commit

Permalink
feat: add per-request hooks
Browse files Browse the repository at this point in the history
Closes #47.
  • Loading branch information
williamboman committed Apr 11, 2023
1 parent eb450d6 commit 5af51b4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
49 changes: 49 additions & 0 deletions lua/glance/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, fun()>
---@field preview table<string, fun()>

---@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,
Expand Down
15 changes: 12 additions & 3 deletions lua/glance/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local glance = {}
Glance.__index = Glance
local initialized = false

---@param opts? GlanceOpts
function Glance.setup(opts)
if initialized then
return
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -232,6 +238,7 @@ local function open(opts)
end)
end

---@class GlanceActions
Glance.actions = {
close = function()
glance:close()
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}

Expand Down
6 changes: 6 additions & 0 deletions lua/glance/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 5af51b4

Please sign in to comment.