diff --git a/doc/openingh.txt b/doc/openingh.txt index 21e781f..09fbebb 100644 --- a/doc/openingh.txt +++ b/doc/openingh.txt @@ -33,8 +33,9 @@ CONTENTS *openingh* :OpenInGHRepo - Opens the project's git repository page in GitHub. - :OpenInGHFile - - Opens the current file page in GitHub. + :OpenInGHFile [git-rev] + - Opens the current file page in GitHub. Providing an optional [git-rev] will override the + current branch/rev. ============================================================================== 4. USAGE *openingh-usage* @@ -47,6 +48,10 @@ CONTENTS *openingh* -- for current file page vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { expr = true, noremap = true }) + To copy the URL into a register set + `vim.g.openingh_copy_to_register = true` + `OpenInGHFile a` should place the URL into register `a` + ============================================================================== 5. LICENSE *openingh-license* diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index c8b0253..6ba2b70 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 = 1, COMMIT = 2, } +M.priority = { BRANCH = 1, COMMIT = 2 } local function get_current_branch_or_commit_with_priority(priority) if priority == M.priority.BRANCH then @@ -37,6 +37,8 @@ end function M.get_file_url( priority, --[[optional]] + branch, + --[[optional]] range_start, --[[optional]] range_end @@ -56,8 +58,12 @@ function M.get_file_url( return end + local rev = get_current_branch_or_commit_with_priority(priority) + if branch ~= nil then + rev = branch + end - local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path + local file_page_url = M.repo_url .. "/blob/" .. rev .. file_path if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 783d64b..d5978ac 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -17,10 +17,11 @@ end vim.api.nvim_create_user_command("OpenInGHFile", function(opts) local url + local branch = opts.fargs[1] if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(judge_priority(opts.bang)) - else -- Current line or block was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), branch) + else -- Current line or block was selected + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) end if opts.reg == "" then @@ -30,18 +31,20 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, range = true, bang = true, + nargs = "*", }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) local url + local branch = opts.fargs[1] if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1) - else -- Current line or block was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1) + else -- Current line or block was selected + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) end if opts.reg == "" then @@ -51,9 +54,10 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, range = true, bang = true, + nargs = "*", }) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) @@ -66,6 +70,6 @@ vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, bang = true, })