From 49c26dd03681de5e194fc8ebcabe0c7c5d8960ad Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 13 May 2026 20:33:28 +0200 Subject: support vis' new option get-callback --- util.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'util.lua') diff --git a/util.lua b/util.lua index 265c272..ddc7452 100644 --- a/util.lua +++ b/util.lua @@ -1,9 +1,9 @@ ---- Module containing simple utility functions for vis-lspc/ +--- Module containing simple utility functions for vis-lspc -- @module util -- @author Florian Fischer -- @author git-bruh -- @license GPL-3 --- @copyright 2024-2025 Florian Fischer +-- @copyright 2024-2026 Florian Fischer -- @copyright 2024 git-bruh local util = {} @@ -48,6 +48,48 @@ function util.vis_pipe(input, cmd, fullscreen) return vis:pipe(vis.win.file, {start = 0, finish = 0}, cmd) end +--- Convert a vis option name to the variable name in the lspc table. +-- @param vis_option_name The name of the option known to vis +-- @return The option name with the 'lspc-' suffix removed and '-' replaced by '_' +function util.vis_option_name_to_lspc(vis_option_name) + return vis_option_name:sub(6):gsub('-', '_') +end + +--- Register an lspc option with vis. +-- @param name Name of the option +-- @param type Type of the option +-- @param help String describing the option +-- @param[opt] set_func Function called when the option is set +-- @param[opt] get_func Function called to retrieve the option +function util.register_lspc_option(option, type, help, set_func, get_func) + if not set_func then + -- TODO: support toggable options + set_func = function(value, _, name) + lspc[util.vis_option_name_to_lspc(name)] = value + end + end + if not get_func then + get_func = function(name) + return lspc[util.vis_option_name_to_lspc(name)] + end + end + util.option_register('lspc-' .. option, type, set_func, get_func, help) +end + +--- Safe wrapper for vis:option_register. +-- If vis does not support the get-callback during option registration +-- it is dropped. +-- @param name Name of the option +-- @param type Type of the option +-- @param set_func Function called when the option is set +-- @param get_func Function called to retrieve the option +-- @param help String describing the option +function util.option_register(name, type, set_func, get_func, help) + if not pcall(vis.option_register, vis, name, type, set_func, get_func, help) then + vis:option_register(name, type, set_func, help) + end +end + --- Split a path into its components -- @param path the path to split into components -- @return a table containing the path components -- cgit v1.2.3