add override branch option

This commit is contained in:
Sathvik Birudavolu 2023-12-29 18:11:14 -06:00
parent 5c9e851d7c
commit 02e6036f39
4 changed files with 33 additions and 15 deletions

View file

@ -33,8 +33,9 @@ CONTENTS *openingh*
:OpenInGHRepo :OpenInGHRepo
- Opens the project's git repository page in GitHub. - Opens the project's git repository page in GitHub.
:OpenInGHFile :OpenInGHFile [git-rev]
- Opens the current file page in GitHub. - Opens the current file page in GitHub. Providing an optional [git-rev] will override the
current branch/rev.
============================================================================== ==============================================================================
4. USAGE *openingh-usage* 4. USAGE *openingh-usage*
@ -47,6 +48,10 @@ CONTENTS *openingh*
-- for current file page -- for current file page
vim.api.nvim_set_keymap("n", "<Leader>gf", ":OpenInGHFile <CR>", { expr = true, noremap = true }) vim.api.nvim_set_keymap("n", "<Leader>gf", ":OpenInGHFile <CR>", { 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* 5. LICENSE *openingh-license*

View file

@ -22,7 +22,7 @@ function M.setup()
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
end end
M.priority = { BRANCH = 1, COMMIT = 2, } M.priority = { BRANCH = 1, COMMIT = 2 }
local function get_current_branch_or_commit_with_priority(priority) local function get_current_branch_or_commit_with_priority(priority)
if priority == M.priority.BRANCH then if priority == M.priority.BRANCH then
@ -37,6 +37,8 @@ end
function M.get_file_url( function M.get_file_url(
priority, priority,
--[[optional]] --[[optional]]
branch,
--[[optional]]
range_start, range_start,
--[[optional]] --[[optional]]
range_end range_end
@ -56,8 +58,12 @@ function M.get_file_url(
return return
end 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 if range_start and not range_end then
file_page_url = file_page_url .. "#L" .. range_start file_page_url = file_page_url .. "#L" .. range_start

View file

@ -17,10 +17,11 @@ end
vim.api.nvim_create_user_command("OpenInGHFile", function(opts) vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
local url local url
local branch = opts.fargs[1]
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
url = openingh.get_file_url(judge_priority(opts.bang)) url = openingh.get_file_url(judge_priority(opts.bang), branch)
else -- Current line or block was selected 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, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -30,18 +31,20 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
print("URL put into register " .. opts.reg) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = true, register = vim.g.openingh_copy_to_register,
range = true, range = true,
bang = true, bang = true,
nargs = "*",
}) })
vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
local url local url
local branch = opts.fargs[1]
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
url = openingh.get_file_url(judge_priority(opts.bang), opts.line1) url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1)
else -- Current line or block was selected 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, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -51,9 +54,10 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
print("URL put into register " .. opts.reg) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = true, register = vim.g.openingh_copy_to_register,
range = true, range = true,
bang = true, bang = true,
nargs = "*",
}) })
vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) 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) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = true, register = vim.g.openingh_copy_to_register,
bang = true, bang = true,
}) })

View file

@ -40,11 +40,14 @@ describe("encode_uri_component", function()
input = input .. string.char(i) input = input .. string.char(i)
end end
local output = utils.encode_uri_component(input) local output = utils.encode_uri_component(input)
assert.is.Equal("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F", output) assert.is.Equal(
"%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
output
)
end) end)
it("returns encoded string for a non-ascii string input (UTF-8)", function() it("returns encoded string for a non-ascii string input (UTF-8)", function()
local input = "ほげ" -- UTF-8 bytewise representation is "e3 81 bb e3 81 92" local input = "ほげ" -- UTF-8 bytewise representation is "e3 81 bb e3 81 92"
local output = utils.encode_uri_component(input) local output = utils.encode_uri_component(input)
assert.is.Equal("%E3%81%BB%E3%81%92", output) assert.is.Equal("%E3%81%BB%E3%81%92", output)
end) end)