Skip to content

Commit

Permalink
fix: parse lua table opts from string
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Jan Noort committed Jun 13, 2024
1 parent faf5491 commit 1779da8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ std = {
"type",
"os",
"pcall",
"load",
},
}
39 changes: 21 additions & 18 deletions lua/ado.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ 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)
return load_arg:find(arg_lead) ~= nil
end)
:totable()
end

---@param sub_impl string[]
---@param subcommand_args string[]
---@param subcommand string
Expand All @@ -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<string, SubCommand>
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,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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, _)
Expand Down
20 changes: 16 additions & 4 deletions lua/ado/pickers/thread.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---@diagnostic disable: undefined-doc-name

---@class ThreadEntry
---@field value Thread
---@field ordinal number
Expand Down Expand Up @@ -28,7 +30,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)
Expand Down Expand Up @@ -56,7 +58,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
Expand All @@ -67,6 +69,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]) ---@diagnostic disable-line: undefined-field
end
---@param pull_request_threads Thread[]
---@param opts ChooseThreadOpts
---@return Thread[] pull_request_threads
Expand All @@ -76,9 +88,9 @@ 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()
return thread_iter:totable() ---@diagnostic disable-line: undefined-field
end

---Choose thread
Expand Down
2 changes: 1 addition & 1 deletion lua/ado/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lua/ado/thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 1779da8

Please sign in to comment.