From 7ed4b61c988dc1f400bebe634e3b212936f5c96d Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Tue, 3 Mar 2026 15:03:48 -0500 Subject: [PATCH] refactor(commands): derive completion from dispatch table (#15) --- lua/preview/commands.lua | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lua/preview/commands.lua b/lua/preview/commands.lua index f82c726..2c52c5a 100644 --- a/lua/preview/commands.lua +++ b/lua/preview/commands.lua @@ -1,22 +1,22 @@ local M = {} -local subcommands = { 'compile', 'stop', 'clean', 'toggle', 'open', 'status' } - ----@param args string -local function dispatch(args) - local subcmd = args ~= '' and args or 'compile' - - if subcmd == 'compile' then +local handlers = { + compile = function() require('preview').compile() - elseif subcmd == 'stop' then + end, + stop = function() require('preview').stop() - elseif subcmd == 'clean' then + end, + clean = function() require('preview').clean() - elseif subcmd == 'toggle' then + end, + toggle = function() require('preview').toggle() - elseif subcmd == 'open' then + end, + open = function() require('preview').open() - elseif subcmd == 'status' then + end, + status = function() local s = require('preview').status() local parts = {} if s.compiling then @@ -28,6 +28,15 @@ local function dispatch(args) table.insert(parts, 'watching') end vim.notify('[preview.nvim]: ' .. table.concat(parts, ', '), vim.log.levels.INFO) + end, +} + +---@param args string +local function dispatch(args) + local subcmd = args ~= '' and args or 'compile' + local handler = handlers[subcmd] + if handler then + handler() else vim.notify('[preview.nvim]: unknown subcommand: ' .. subcmd, vim.log.levels.ERROR) end @@ -38,7 +47,7 @@ end local function complete(lead) return vim.tbl_filter(function(s) return s:find(lead, 1, true) == 1 - end, subcommands) + end, vim.tbl_keys(handlers)) end function M.setup()