From 2388e69546885e115f8f1f0710f9ebba10dd2d98 Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Sat, 23 Sep 2023 10:07:01 +0900 Subject: [PATCH 1/3] Control priority by bang --- lua/openingh/init.lua | 2 +- plugin/openingh.lua | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 88e34f6..c8b0253 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,7 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end -M.priority = { BRANCH = 'branch-priority', COMMIT = 'commit-priority', } +M.priority = { BRANCH = 1, COMMIT = 2, } local function get_current_branch_or_commit_with_priority(priority) if priority == M.priority.BRANCH then diff --git a/plugin/openingh.lua b/plugin/openingh.lua index ffba143..783d64b 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,20 +5,22 @@ vim.g.openingh = true local openingh = require("openingh") -local complete_list = { openingh.priority.BRANCH, openingh.priority.COMMIT, } -local function complete_func(arg_lead, _, _) - return vim.tbl_filter(function(item) - return vim.startswith(item, arg_lead) - end, complete_list) +local function judge_priority(bang) + -- When the command executed with bang `!`, prioritizes commit rather than branch. + if bang then + return openingh.priority.COMMIT + else + return openingh.priority.BRANCH + end end vim.api.nvim_create_user_command("OpenInGHFile", function(opts) local url if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(opts.args) + url = openingh.get_file_url(judge_priority(opts.bang)) else -- Current line or block was selected - url = openingh.get_file_url(opts.args, opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) end if opts.reg == "" then @@ -30,17 +32,16 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts) end, { register = true, range = true, - nargs = '?', - complete = complete_func, + bang = true, }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) local url if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(opts.args, opts.line1) + url = openingh.get_file_url(judge_priority(opts.bang), opts.line1) else -- Current line or block was selected - url = openingh.get_file_url(opts.args, opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) end if opts.reg == "" then @@ -52,12 +53,11 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) end, { register = true, range = true, - nargs = '?', - complete = complete_func, + bang = true, }) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) - local url = openingh.get_repo_url(opts.args) + local url = openingh.get_repo_url(judge_priority(opts.bang)) if opts.reg == "" then openingh.open_url(url) @@ -67,6 +67,5 @@ vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) end end, { register = true, - nargs = '?', - complete = complete_func, + bang = true, }) From 7d4f57a5cae11c41d21c75cebabceec47482d919 Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Sat, 23 Sep 2023 11:10:49 +0900 Subject: [PATCH 2/3] Added description about priority and how to control it --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 4b7b43a..f6b4738 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,16 @@ All of the commands above optionally take a register, e.g. `:OpenInGHFileLines+` In this case, the URL will not be opened in the browser, but put into the register given. This is especially useful if you're running neovim on a remote machine, but want to open the URL locally. +## Priority + +At first, this plugin try to identify the page url based on the current branch you are working in, +and if it fails then switches its base to commit. + +This behavior can be inverted via bang `!`, e.g. `:OpenInGHFile!`. + +Since commit based url tends to have long durablity compared to branch based one, it can be useful in some situation. +It won't be affected by subsequent commits, branch deletion after merge, and so on. + ## Usage You can call the commands directly or define mappings them: From 31ad282d692d29957a65ec42adc1c272a54d2f22 Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Mon, 25 Sep 2023 06:14:43 +0900 Subject: [PATCH 3/3] feat!: introduce bang to control url identification method BREAKING CHANGE: command arg style method control is no longer supported.