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 --- init.lua | 54 ++++++++++++++++++++++++++---------------------------- util.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/init.lua b/init.lua index ed421ab..321518e 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,4 @@ --- Copyright (c) 2021-2024 Florian Fischer. All rights reserved. +-- Copyright (c) 2021-2026 Florian Fischer. All rights reserved. -- -- This file is part of vis-lspc. -- @@ -2092,41 +2092,39 @@ vis.events.subscribe(vis.events.QUIT, function() end end) -vis:option_register('lspc-highlight-diagnostics', 'string', function(value) - lspc.highlight_diagnostics = value - return true -end, 'How should lspc highlight available diagnostics') +util.register_lspc_option('highlight-diagnostics', 'string', + 'How should lspc highlight available diagnostics') -vis:option_register('lspc-menu-cmd', 'string', function(value) - lspc.menu_cmd = value - return true -end, 'External tool vis-lspc uses to present choices in a menu') +util.register_lspc_option('menu-cmd', 'string', + 'External tool vis-lspc uses to present choices in a menu') -vis:option_register('lspc-confirm-cmd', 'string', function(value) - lspc.confirm_cmd = value - return true -end, 'External tool vis-lspc uses to ask the user for confirmation') +util.register_lspc_option('confirm-cmd', 'string', + 'External tool vis-lspc uses to ask the user for confirmation') + +util.register_lspc_option('message-level', 'number', + 'Message level to show in UI (for server messages)') -vis:option_register('lspc-message-level', 'number', function(value) - lspc.message_level = value +local function diagnostic_styles_set(name, _, value) + lspc.diagnostic_styles[name:sub(23)] = value return true -end, 'Message level to show in UI (for server messages)') +end + +local function diagnostic_styles_get(name) + return lspc.diagnostic_styles[name:sub(23)] +end -vis:option_register('lspc-diagnostic-style-error', 'string', function(value) - lspc.diagnostic_styles.error = value -end, 'Style for diagnostic errors') +util.register_lspc_option('diagnostic-style-error', 'string', 'Style for diagnostic errors', + diagnostic_styles_set, diagnostic_styles_get) -vis:option_register('lspc-diagnostic-style-warning', 'string', function(value) - lspc.diagnostic_styles.warning = value -end, 'Style for diagnostic warnings') +util.register_lspc_option('diagnostic-style-warning', 'string', 'Style for diagnostic warnings', + diagnostic_styles_set, diagnostic_styles_get) -vis:option_register('lspc-diagnostic-style-information', 'string', function(value) - lspc.diagnostic_styles.information = value -end, 'Style for diagnostic information') +util.register_lspc_option('diagnostic-style-information', 'string', + 'Style for diagnostic information', diagnostic_styles_set, + diagnostic_styles_get) -vis:option_register('lspc-diagnostic-style-hint', 'string', function(value) - lspc.diagnostic_styles.hint = value -end, 'Style for diagnostic hints') +util.register_lspc_option('diagnostic-style-hint', 'string', 'Style for diagnostic hints', + diagnostic_styles_set, diagnostic_styles_get) dofile(source_path .. 'bindings.lua') return lspc 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