From e63b1a669fb75f7a684dcbaeb4461021666cf8bd Mon Sep 17 00:00:00 2001 From: Ben Elan Date: Sun, 5 Nov 2023 16:06:06 -0800 Subject: [PATCH 1/4] feat(picker): add config options for changing mappings --- lua/octo/config.lua | 6 ++++++ lua/octo/pickers/telescope/provider.lua | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lua/octo/config.lua b/lua/octo/config.lua index 07eee423..bcf4439c 100644 --- a/lua/octo/config.lua +++ b/lua/octo/config.lua @@ -4,6 +4,12 @@ M.defaults = { picker = "telescope", picker_config = { use_emojis = false, + mappings = { + open_in_browser = { lhs = "", desc = "open issue in browser" }, + copy_url = { lhs = "", desc = "copy url to system clipboard" }, + checkout_pr = { lhs = "", desc = "checkout pull request" }, + merge_pr = { lhs = "", desc = "merge pull request" }, + }, }, default_remote = { "upstream", "origin" }, ssh_aliases = {}, diff --git a/lua/octo/pickers/telescope/provider.lua b/lua/octo/pickers/telescope/provider.lua index e1db58ca..4f3830ca 100644 --- a/lua/octo/pickers/telescope/provider.lua +++ b/lua/octo/pickers/telescope/provider.lua @@ -169,8 +169,8 @@ function M.issues(opts) action_set.select:replace(function(prompt_bufnr, type) open(type)(prompt_bufnr) end) - map("i", "", open_in_browser()) - map("i", "", copy_url()) + map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser()) + map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url()) return true end, }) @@ -315,10 +315,10 @@ function M.pull_requests(opts) action_set.select:replace(function(prompt_bufnr, type) open(type)(prompt_bufnr) end) - map("i", "", checkout_pull_request()) - map("i", "", open_in_browser()) - map("i", "", copy_url()) - map("i", "", merge_pull_request()) + map("i", cfg.picker_config.mappings.checkout_pr.lhs, checkout_pull_request()) + map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser()) + map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url()) + map("i", cfg.picker_config.mappings.merge_pr.lhs, merge_pull_request()) return true end, }) @@ -479,6 +479,7 @@ end --- function M.search(opts) opts = opts or {} + local cfg = octo_config.get_config() local requester = function() return function(prompt) if not opts.prompt and utils.is_blank(prompt) then @@ -531,8 +532,8 @@ function M.search(opts) action_set.select:replace(function(prompt_bufnr, type) open(type)(prompt_bufnr) end) - map("i", "", open_in_browser()) - map("i", "", copy_url()) + map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser()) + map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url()) return true end, }) @@ -923,6 +924,7 @@ end -- function M.repos(opts) opts = opts or {} + local cfg = octo_config.get_config() if not opts.login then if vim.g.octo_viewer then opts.login = vim.g.octo_viewer @@ -967,8 +969,8 @@ function M.repos(opts) action_set.select:replace(function(prompt_bufnr, type) open(type)(prompt_bufnr) end) - map("i", "", open_in_browser()) - map("i", "", copy_url()) + map("i", cfg.picker_config.mappings.open_in_browser.lhs, open_in_browser()) + map("i", cfg.picker_config.mappings.copy_url.lhs, copy_url()) return true end, }) From 4703356ae36d33da8058bb2977ed158a08436f9b Mon Sep 17 00:00:00 2001 From: Ben Elan Date: Sun, 5 Nov 2023 17:26:49 -0800 Subject: [PATCH 2/4] docs(readme): add new options to example config --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index f99204cd..cec7dbc2 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,14 @@ require"octo".setup({ enable_builtin = false, -- shows a list of builtin actions when no action is provided default_remote = {"upstream", "origin"}; -- order to try remotes ssh_aliases = {}, -- SSH aliases. e.g. `ssh_aliases = {["github.aaakk.us.kg-work"] = "github.com"}` + picker_config = { + mappings = { + open_in_browser = { lhs = "", desc = "open issue in browser" }, + copy_url = { lhs = "", desc = "copy url to system clipboard" }, + checkout_pr = { lhs = "", desc = "checkout pull request" }, + merge_pr = { lhs = "", desc = "merge pull request" }, + }, + }, reaction_viewer_hint_icon = ""; -- marker for user reactions user_icon = " "; -- user icon timeline_marker = ""; -- timeline marker From 6a9845efb2915d6938ebf9e606f180735b2d7981 Mon Sep 17 00:00:00 2001 From: Ben Elan Date: Wed, 8 Nov 2023 22:49:15 -0800 Subject: [PATCH 3/4] refactor: use picker mappings for fzf too --- .../pickers/fzf-lua/pickers/fzf_actions.lua | 6 ++++-- lua/octo/pickers/fzf-lua/pickers/prs.lua | 2 +- lua/octo/pickers/fzf-lua/pickers/utils.lua | 6 ++++++ lua/tests/plenary/utils_spec.lua | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua b/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua index 8ae28311..96a52bb9 100644 --- a/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua +++ b/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua @@ -1,4 +1,5 @@ local picker_utils = require "octo.pickers.fzf-lua.pickers.utils" +local octo_config = require("octo.config") local M = {} M.common_buffer_actions = function(formatted_items) @@ -19,11 +20,12 @@ M.common_buffer_actions = function(formatted_items) end M.common_open_actions = function(formatted_items) + local cfg = octo_config.get_config() return vim.tbl_extend("force", M.common_buffer_actions(formatted_items), { - ["ctrl-b"] = function(selected) + [picker_utils.convert_mapping(cfg.picker_config.mappings.open_in_browser.lhs)] = function(selected) picker_utils.open_in_browser(formatted_items[selected[1]]) end, - ["ctrl-y"] = function(selected) + [picker_utils.convert_mapping(cfg.picker_config.mappings.copy_url.lhs)] = function(selected) picker_utils.copy_url(formatted_items[selected[1]]) end, }) diff --git a/lua/octo/pickers/fzf-lua/pickers/prs.lua b/lua/octo/pickers/fzf-lua/pickers/prs.lua index 64a976c4..9ffb0c20 100644 --- a/lua/octo/pickers/fzf-lua/pickers/prs.lua +++ b/lua/octo/pickers/fzf-lua/pickers/prs.lua @@ -79,7 +79,7 @@ return function(opts) ["--info"] = "default", }, actions = vim.tbl_extend("force", fzf_actions.common_open_actions(formatted_pulls), { - ["ctrl-o"] = function(selected) + [picker_utils.convert_mapping(cfg.picker_config.mappings.checkout_pr.lhs)] = function(selected) local entry = formatted_pulls[selected[1]] checkout_pull_request(entry) end, diff --git a/lua/octo/pickers/fzf-lua/pickers/utils.lua b/lua/octo/pickers/fzf-lua/pickers/utils.lua index ce9c2b0d..b245acfb 100644 --- a/lua/octo/pickers/fzf-lua/pickers/utils.lua +++ b/lua/octo/pickers/fzf-lua/pickers/utils.lua @@ -135,4 +135,10 @@ function M.pad_string(s, length) return string.format("%s%" .. (length - #string_s) .. "s", string_s, " ") end +function M.convert_mapping(vim_mapping) + local fzf_mapping = string.gsub(vim_mapping, "<[cC]%-(.*)>", "ctrl-%1") + fzf_mapping = string.gsub(fzf_mapping, "<[amAM]%-(.*)>", "alt-%1") + return string.lower(fzf_mapping) +end + return M diff --git a/lua/tests/plenary/utils_spec.lua b/lua/tests/plenary/utils_spec.lua index f9961df6..67dd3cd7 100644 --- a/lua/tests/plenary/utils_spec.lua +++ b/lua/tests/plenary/utils_spec.lua @@ -27,3 +27,21 @@ describe("Utils module:", function() end) end) end) + +describe("Utils module (fzf picker):", function() + it("convert_mapping changes vim mappings to fzf mappings", function() + local fzf_picker_utils = require "octo.pickers.fzf-lua.pickers.utils" + local mappings = { + [""] = "ctrl-j", + [""] = "ctrl-k", + [""] = "alt-j", + [""] = "alt-k", + [""] = "alt-tab", + [""] = "alt-up", + } + + for vim_mapping, fzf_mapping in pairs(mappings) do + eq(fzf_picker_utils.convert_mapping(vim_mapping), fzf_mapping) + end + end) +end) From 600457fa2cad8b8fdda01b208528e19275d3fd18 Mon Sep 17 00:00:00 2001 From: Ben Elan Date: Thu, 9 Nov 2023 13:53:31 -0800 Subject: [PATCH 4/4] chore: resolve PR checks --- .../pickers/fzf-lua/pickers/fzf_actions.lua | 7 +++-- lua/octo/pickers/fzf-lua/pickers/prs.lua | 2 +- lua/octo/pickers/fzf-lua/pickers/utils.lua | 6 ---- lua/octo/utils.lua | 6 ++++ lua/tests/plenary/utils_spec.lua | 30 +++++++++---------- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua b/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua index 96a52bb9..d05ef845 100644 --- a/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua +++ b/lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua @@ -1,5 +1,6 @@ local picker_utils = require "octo.pickers.fzf-lua.pickers.utils" -local octo_config = require("octo.config") +local octo_config = require "octo.config" +local utils = require "octo.utils" local M = {} M.common_buffer_actions = function(formatted_items) @@ -22,10 +23,10 @@ end M.common_open_actions = function(formatted_items) local cfg = octo_config.get_config() return vim.tbl_extend("force", M.common_buffer_actions(formatted_items), { - [picker_utils.convert_mapping(cfg.picker_config.mappings.open_in_browser.lhs)] = function(selected) + [utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_in_browser.lhs)] = function(selected) picker_utils.open_in_browser(formatted_items[selected[1]]) end, - [picker_utils.convert_mapping(cfg.picker_config.mappings.copy_url.lhs)] = function(selected) + [utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.copy_url.lhs)] = function(selected) picker_utils.copy_url(formatted_items[selected[1]]) end, }) diff --git a/lua/octo/pickers/fzf-lua/pickers/prs.lua b/lua/octo/pickers/fzf-lua/pickers/prs.lua index 9ffb0c20..ad925340 100644 --- a/lua/octo/pickers/fzf-lua/pickers/prs.lua +++ b/lua/octo/pickers/fzf-lua/pickers/prs.lua @@ -79,7 +79,7 @@ return function(opts) ["--info"] = "default", }, actions = vim.tbl_extend("force", fzf_actions.common_open_actions(formatted_pulls), { - [picker_utils.convert_mapping(cfg.picker_config.mappings.checkout_pr.lhs)] = function(selected) + [utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.checkout_pr.lhs)] = function(selected) local entry = formatted_pulls[selected[1]] checkout_pull_request(entry) end, diff --git a/lua/octo/pickers/fzf-lua/pickers/utils.lua b/lua/octo/pickers/fzf-lua/pickers/utils.lua index b245acfb..ce9c2b0d 100644 --- a/lua/octo/pickers/fzf-lua/pickers/utils.lua +++ b/lua/octo/pickers/fzf-lua/pickers/utils.lua @@ -135,10 +135,4 @@ function M.pad_string(s, length) return string.format("%s%" .. (length - #string_s) .. "s", string_s, " ") end -function M.convert_mapping(vim_mapping) - local fzf_mapping = string.gsub(vim_mapping, "<[cC]%-(.*)>", "ctrl-%1") - fzf_mapping = string.gsub(fzf_mapping, "<[amAM]%-(.*)>", "alt-%1") - return string.lower(fzf_mapping) -end - return M diff --git a/lua/octo/utils.lua b/lua/octo/utils.lua index 471198b0..843b4a27 100644 --- a/lua/octo/utils.lua +++ b/lua/octo/utils.lua @@ -1309,4 +1309,10 @@ function M.get_lines_from_context(calling_context) return line_number_start, line_number_end end +function M.convert_vim_mapping_to_fzf(vim_mapping) + local fzf_mapping = string.gsub(vim_mapping, "<[cC]%-(.*)>", "ctrl-%1") + fzf_mapping = string.gsub(fzf_mapping, "<[amAM]%-(.*)>", "alt-%1") + return string.lower(fzf_mapping) +end + return M diff --git a/lua/tests/plenary/utils_spec.lua b/lua/tests/plenary/utils_spec.lua index 67dd3cd7..145121ed 100644 --- a/lua/tests/plenary/utils_spec.lua +++ b/lua/tests/plenary/utils_spec.lua @@ -25,23 +25,21 @@ describe("Utils module:", function() eq(this.parse_remote_url(remote_urls[5], aliases).host, "github.com") eq(this.parse_remote_url(remote_urls[5], aliases).repo, "pwntester/octo.nvim") end) - end) -end) -describe("Utils module (fzf picker):", function() - it("convert_mapping changes vim mappings to fzf mappings", function() - local fzf_picker_utils = require "octo.pickers.fzf-lua.pickers.utils" - local mappings = { - [""] = "ctrl-j", - [""] = "ctrl-k", - [""] = "alt-j", - [""] = "alt-k", - [""] = "alt-tab", - [""] = "alt-up", - } + it("convert_vim_mapping_to_fzf changes vim mappings to fzf mappings", function() + local utils = require "octo.utils" + local mappings = { + [""] = "ctrl-j", + [""] = "ctrl-k", + [""] = "alt-j", + [""] = "alt-k", + [""] = "alt-tab", + [""] = "alt-up", + } - for vim_mapping, fzf_mapping in pairs(mappings) do - eq(fzf_picker_utils.convert_mapping(vim_mapping), fzf_mapping) - end + for vim_mapping, fzf_mapping in pairs(mappings) do + eq(utils.convert_vim_mapping_to_fzf(vim_mapping), fzf_mapping) + end + end) end) end)