local helper = require("kyoh86.plug.ddu.helper") ---@type LazySpec local spec = { "kyoh86/ddu-source-github", dependencies = { "ddu.vim" }, config = function() local custom_comment = function(tp, args) if #args.items ~= 1 then vim.notify("invalid action: it can edit only one file at once", vim.log.levels.WARN, {}) return 1 end local url = args.items[1].action.html_url local words = vim.iter(vim.split(url, "/", { plain = true })):rev():totable() local number, _, name, owner, _ = unpack(words) require("kyoh86.conf.github.comment").create({ type = tp, repo = owner .. "/" .. name, number = number }) return 0 end local custom_view = function(args, split) for _, item in pairs(args.items) do local url = item.action.html_url local words = vim.iter(vim.split(url, "/", { plain = true })):rev():totable() local number, _, name, owner, _ = unpack(words) vim.fn["denops#github#issue#view"](owner, name, number, { split = split }) end return 0 end vim.fn["ddu#custom#action"]("kind", "github_issue", "custom:issue-comment", function(args) return custom_comment("issue", args) end) vim.fn["ddu#custom#action"]("kind", "github_issue", "custom:view", function(args) return custom_view(args, "none") end) vim.fn["ddu#custom#action"]("kind", "github_issue", "custom:view:above", function(args) return custom_view(args, "above") end) vim.fn["ddu#custom#action"]("kind", "github_issue", "custom:view:left", function(args) return custom_view(args, "left") end) vim.fn["ddu#custom#action"]("kind", "github_pull", "custom:pr-comment", function(args) return custom_comment("pr", args) end) vim.fn["ddu#custom#patch_global"]({ kindOptions = { github_pull = { defaultAction = "browse", }, github_issue = { defaultAction = "custom:view", }, github_repo = { defaultAction = "browse", }, }, }) local linkFormat = "${this.html_url.replace(/^https:\\/\\/[^\\/]+\\/([^\\/]+)\\/([^\\/]+)\\/(?:issues|pull)\\/(\\d+)/, '$1/$2#$3')}" local fullFormat = "${this.title} ${this.html_url.replace(/^https:\\/\\/[^\\/]+\\/([^\\/]+)\\/([^\\/]+)\\/(?:issues|pull)\\/(\\d+)/, '$1/$2#$3')}" local spacer = vim.fn["kyoh86#ddu#source#github#spacer"]() local map = { ["b"] = { action = "itemAction", params = { name = "browse" } }, ["e"] = { action = "itemAction", params = { name = "edit" } }, ["v"] = { action = "itemAction", params = { name = "custom:view:left" } }, ["x"] = { action = "itemAction", params = { name = "custom:view:above" } }, ["h"] = { action = "itemAction", params = { name = "custom:view:above" } }, ["p"] = { action = "itemAction", params = { name = "append", params = { format = linkFormat, spacer = spacer } } }, ["P"] = { action = "itemAction", params = { name = "insert", params = { format = linkFormat, spacer = spacer } } }, ["f"] = { action = "itemAction", params = { name = "append", params = { format = fullFormat, spacer = spacer } } }, ["F"] = { action = "itemAction", params = { name = "insert", params = { format = fullFormat, spacer = spacer } } }, } local nextState = { open = "closed", closed = "all", all = "open", } helper.setup("github-repo-issues", { sources = { { name = "github_repo_issue", params = { source = "cwd", state = "all" }, options = { matchers = { "matcher_github_issue_like", "matcher_fzf" }, }, } }, uiParams = { ff = { inputFunc = "kyoh86#ddu#source#github#cancel_input", }, }, input = "is:open ", }, { start = { key = "fghi", desc = "GitHub Issues", }, localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:issue-comment" } }, ["t"] = function() local opts = vim.fn["ddu#custom#get_current"]("github-repo-issues") local state = nextState[opts.sourceParams.github_repo_issue.state] vim.fn["ddu#ui#do_action"]("updateOptions", { sourceParams = { github_repo_issue = { state = state, }, }, }) vim.fn["ddu#ui#do_action"]("redraw", { method = "refreshItems" }) end, }), }) helper.setup("github-my-issues", { sources = { { name = "github_my_issue", params = { state = "all" }, options = { columns = { "github_issue_full_name", "github_issue_title", "github_issue_state" }, matchers = { "matcher_github_issue_like", "matcher_fzf" }, }, } }, uiParams = { ff = { inputFunc = "kyoh86#ddu#source#github#cancel_input", }, }, input = "is:open ", }, { start = { key = "fghgi", desc = "GitHub My Issues", }, localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:issue-comment" } }, ["t"] = function() local opts = vim.fn["ddu#custom#get_current"]("github-my-issues") local state = nextState[opts.sourceParams.github_my_issue.state] vim.fn["ddu#ui#do_action"]("updateOptions", { sourceParams = { github_my_issue = { state = state, }, }, }) vim.fn["ddu#ui#do_action"]("redraw", { method = "refreshItems" }) end, }), }) helper.setup("github-pulls", { sources = { { name = "github_repo_pull", params = { source = "cwd", state = "all" }, options = { matchers = { "matcher_github_issue_like", "matcher_fzf" }, }, } }, uiParams = { ff = { inputFunc = "kyoh86#ddu#source#github#cancel_input", }, }, input = "is:open ", }, { start = { key = "fghp", desc = "GitHub Pull Requests", }, localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:pr-comment" } }, ["s"] = { action = "itemAction", params = { name = "checkout" } }, ["t"] = function() local opts = vim.fn["ddu#custom#get_current"]("github-pulls") local state = nextState[opts.sourceParams.github_repo_pull.state] vim.fn["ddu#ui#do_action"]("updateOptions", { sourceParams = { github_repo_pull = { state = state, }, }, }) vim.fn["ddu#ui#do_action"]("redraw", { method = "refreshItems" }) end, }), }) helper.setup("github-my-pulls", { sources = { { name = "github_my_pull", params = { hostname = "github.com", role = "created", state = "all" }, options = { matchers = { "matcher_github_issue_like", "matcher_fzf" }, }, } }, uiParams = { ff = { inputFunc = "kyoh86#ddu#source#github#cancel_input", }, }, input = "is:open ", }, { start = { key = "fghgp", desc = "GitHub My Pull Requests", }, localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:pr-comment" } }, ["t"] = function() local opts = vim.fn["ddu#custom#get_current"]("github-pulls") local state = nextState[opts.sourceParams.github_repo_pull.state] vim.fn["ddu#ui#do_action"]("updateOptions", { sourceParams = { github_repo_pull = { state = state, }, }, }) vim.fn["ddu#ui#do_action"]("redraw", { method = "refreshItems" }) end, }), }) helper.setup("github-search-issues", { sources = { { name = "github_search_issue", params = { hostname = "github.com" }, } }, input = "is:open ", }, { localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:issue-comment" } }, }), }) helper.setup("github-search-pulls", { sources = { { name = "github_search_pull", params = { hostname = "github.com" }, } }, input = "is:open ", }, { localmap = vim.tbl_extend("force", map, { ["c"] = { action = "itemAction", params = { name = "custom:issue-comment" } }, }), }) -- Start all GitHub contexts local contextList = { { title = "Issues in current repo", context = "github-repo-issues" }, { title = "Pull requests in current repo", context = "github-pulls" }, { title = "My issues", context = "github-my-issues" }, { title = "My pull requests", context = "github-my-pulls" }, } local contextDict = vim.iter(contextList):fold({}, function(acc, item) acc[item.title] = item.context return acc end) local startContext = function(text) local context = contextDict[text] if not context or context == "" then return end vim.fn["ddu#start"]({ name = context, push = true, }) end local startContextId = vim.fn["denops#callback#register"](startContext, { once = false }) helper.setup("github", { sources = { { name = "custom-list", params = { texts = vim .iter(contextList) :map(function(item) return item.title end) :totable(), callbackId = startContextId, }, } }, kindOptions = { ["custom-list"] = { defaultAction = "callback", }, }, }, { start = { key = "fgh", desc = "GitHub", }, }) helper.setup("github-search-ddu-sources", { sources = { { name = "github_search_repo", options = { matchers = {}, volatile = true, }, } }, input = "topic:ddu-source", }, {}) helper.setup("github-search-ddu-filters", { sources = { { name = "github_search_repo", options = { matchers = {}, volatile = true, }, } }, input = "topic:ddu-filter", }, {}) helper.setup("github-search-ddu-kinds", { sources = { { name = "github_search_repo", options = { matchers = {}, volatile = true, }, } }, input = "topic:ddu-kind", }, {}) vim.api.nvim_create_user_command("DduSources", function() vim.fn["ddu#start"]({ name = "github-search-ddu-sources", }) end, {}) vim.api.nvim_create_user_command("DduFilters", function() vim.fn["ddu#start"]({ name = "github-search-ddu-filters", }) end, {}) vim.api.nvim_create_user_command("DduKinds", function() vim.fn["ddu#start"]({ name = "github-search-ddu-kinds", }) end, {}) end, } return spec