From 7e77fcb9317554f0df9e53e6e98da1abdbeebc82 Mon Sep 17 00:00:00 2001 From: Willem Jan Noort Date: Thu, 13 Jun 2024 09:59:32 +0200 Subject: [PATCH] fix: parse lua table opts from string --- lua/ado.lua | 39 ++++++++++++++++++++------------------ lua/ado/pickers/thread.lua | 16 +++++++++++++--- lua/ado/state.lua | 2 +- lua/ado/thread.lua | 8 ++++---- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/lua/ado.lua b/lua/ado.lua index ee5cf37..5eb26a8 100644 --- a/lua/ado.lua +++ b/lua/ado.lua @@ -8,10 +8,6 @@ function M.get_loaded_state() return state_manager.state end ----@class SubCommand ----@field impl fun(args:string[], opts: table) ----@field complete_args? string[] - local function completer(load_args, arg_lead) return vim.iter(load_args) :filter(function(load_arg) @@ -19,6 +15,7 @@ local function completer(load_args, arg_lead) end) :totable() end + ---@param sub_impl string[] ---@param subcommand_args string[] ---@param subcommand string @@ -33,23 +30,29 @@ local function prompt_target(sub_impl, subcommand_args, subcommand) end) return end - sub_impl[subcommand_args[1]]() + local load_opts = function() return load("return " .. (subcommand_args[2] or ""), 't')() end + local ok, opts = pcall(load_opts) + sub_impl[subcommand_args[1]](ok and opts or {}) end +---@class SubCommand +---@field impl fun(args:string[]) +---@field complete_args? string[] + ---@type table local subcommand_tbl = { load = { complete_args = { "context", "threads" }, - impl = function(args, opts) + impl = function(args) local sub_impl = { - context = function() + context = function(opts) if not state_manager then local context = require("ado.state").AdoContext:new() state_manager = require("ado.state").StateManager:new(context) end state_manager:choose_and_activate(opts) end, - threads = function() + threads = function(opts) M.get_loaded_state():load_pull_request_threads(opts) end, } @@ -58,15 +61,15 @@ local subcommand_tbl = { }, submit = { complete_args = { "comment", "vote", "thread_status" }, - impl = function(args, opts) + impl = function(args) local sub_impl = { - comment = function() + comment = function(opts) require("ado.thread").submit_comment(M.get_loaded_state(), opts) end, - vote = function() + vote = function(opts) require("ado.review").submit_vote(M.get_loaded_state(), opts) end, - thread_status = function() + thread_status = function(opts) require("ado.thread").update_thread_status(M.get_loaded_state(), opts) end, } @@ -75,18 +78,18 @@ local subcommand_tbl = { }, open = { complete_args = { "quickfix", "thread_picker", "new_thread", "existing_thread" }, - impl = function(args, opts) + impl = function(args) local sub_impl = { - quickfix = function() + quickfix = function(opts) require("ado.quickfix").render_quickfix(M.get_loaded_state().pull_request_threads, opts) end, - thread_picker = function() + thread_picker = function(opts) require("ado.pickers.thread").choose_thread(M.get_loaded_state(), opts) end, - new_thread = function() + new_thread = function(opts) require("ado.thread").new_thread_window(M.get_loaded_state(), opts) end, - existing_thread = function() + existing_thread = function(opts) require("ado.thread").open_thread_window(M.get_loaded_state(), opts) end, } @@ -114,7 +117,7 @@ function M.ado_pure(opts) end) return end - subcommand.impl(args, opts) + subcommand.impl(args) end function M.auto_completer(arg_lead, cmdline, _) diff --git a/lua/ado/pickers/thread.lua b/lua/ado/pickers/thread.lua index 7ec6050..3c06300 100644 --- a/lua/ado/pickers/thread.lua +++ b/lua/ado/pickers/thread.lua @@ -28,7 +28,7 @@ end ---@param thread_context ThreadContext local function open_scroll(file_path, thread_context) if vim.fn.filereadable(file_path) == 0 then - vim.notify("No such file: " .. file_path, 2) + vim.notify("No such file: " .. file_path, 3) return end vim.cmd(":edit " .. file_path) @@ -56,7 +56,7 @@ end local thread_filters = { ---@param thread Thread - hide_system_threads = function(thread) + hide_system = function(thread) return thread.comments[1].commentType == "text" end, ---@param thread Thread @@ -67,6 +67,16 @@ local thread_filters = { ---@class ChooseThreadOpts ---@field thread_filters string[] +---@param thread_iter Iter +---@param thread_filter string +---@return Iter thread_iter +local function apply_filter(thread_iter, thread_filter) + if not thread_filters[thread_filter] then + vim.notify("Invalid thread filter provided: " .. thread_filter, 3) + return thread_iter + end + return thread_iter:filter(thread_filters[thread_filter]) +end ---@param pull_request_threads Thread[] ---@param opts ChooseThreadOpts ---@return Thread[] pull_request_threads @@ -76,7 +86,7 @@ local function filter_pull_request_threads(pull_request_threads, opts) end local thread_iter = vim.iter(pull_request_threads) for _, thread_filter in ipairs(opts.thread_filters) do - thread_iter = thread_iter:filter(thread_filters[thread_filter]) + thread_iter = apply_filter(thread_iter, thread_filter) end return thread_iter:totable() end diff --git a/lua/ado/state.lua b/lua/ado/state.lua index 5577d47..f7e181d 100644 --- a/lua/ado/state.lua +++ b/lua/ado/state.lua @@ -39,7 +39,7 @@ function AdoState:new(repository, pull_request) self.__index = self self = setmetatable(o, self) self:load_pull_request_iterations() - self:load_pull_request_threads() + self:load_pull_request_threads({}) return self end diff --git a/lua/ado/thread.lua b/lua/ado/thread.lua index defdbc7..d7ac6ef 100644 --- a/lua/ado/thread.lua +++ b/lua/ado/thread.lua @@ -172,7 +172,7 @@ function M.open_thread_window(state, opts) end end if not thread_to_open then - vim.notify("Did not find thread to open;", 2) + vim.notify("Did not find thread to open;", 3) return end local bufnr, mark_id = require("ado.render").render_reply_thread(namespace, thread_to_open) @@ -220,13 +220,13 @@ function M.update_thread_status(state, _) local bufnr = vim.api.nvim_get_current_buf() local comment_reply = _get_comment_reply(state, bufnr) if not comment_reply then - vim.notify("No comment reply found;", 2) + vim.notify("No comment reply found;", 3) return end vim.ui.select(thread_status, { prompt = "Select new status;" }, function(choice) if not choice then - vim.notify("No new status chosen;", 2) + vim.notify("No new status chosen;", 3) return end @@ -269,7 +269,7 @@ function M.submit_comment(state, _) end return end - vim.notify("No comment found to create or reply to;", 2) + vim.notify("No comment found to create or reply to;", 3) end return M