diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index c2e9884..4da7982 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,20 @@ 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', } + +local function get_current_branch_or_commit_with_priority(priority) + if priority == M.priority.BRANCH then + return utils.get_current_branch_or_commit() + elseif priority == M.priority.COMMIT then + return utils.get_current_commit_or_branch() + else + return utils.get_current_branch_or_commit() + end +end + function M.open_file( + priority, --[[optional]] range_start, --[[optional]] @@ -43,7 +56,8 @@ function M.open_file( return end - local file_page_url = M.repo_url .. "/blob/" .. utils.get_current_branch_or_commit() .. file_path + + local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start @@ -58,7 +72,7 @@ function M.open_file( end end -function M.open_repo() +function M.open_repo(priority) -- make sure to update the current directory M.setup() if M.is_no_git_origin then @@ -66,7 +80,8 @@ function M.open_repo() return end - local url = M.repo_url .. "/tree/" .. utils.get_current_branch_or_commit() + + local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority) if not utils.open_url(url) then utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR) end diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index ef39177..6e965dc 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -96,6 +96,23 @@ function M.get_current_branch_or_commit() return M.encode_uri_component(M.get_default_branch()) end +-- Returns the current commit or branch if they are available on remote +-- otherwise this will return the default branch of the repo +-- (This function prioritizes commit than branch) +function M.get_current_commit_or_branch() + local commit_hash = get_current_commit_hash() + if M.is_commit_upstreamed(commit_hash) then + return commit_hash + end + + local current_branch = get_current_branch() + if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then + return M.encode_uri_component(current_branch) + end + + return M.encode_uri_component(M.get_default_branch()) +end + -- get the active buf relative file path form the .git function M.get_current_relative_file_path() -- we only want the active buffer name diff --git a/plugin/openingh.lua b/plugin/openingh.lua index a40e828..531db6d 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,26 +5,40 @@ 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) +end + vim.api.nvim_create_user_command("OpenInGHFile", function(opts) if opts.range == 0 then -- Nothing was selected - openingh.open_file() + openingh.open_file(opts.args) else -- Current line or block was selected - openingh.open_file(opts.line1, opts.line2) + openingh.open_file(opts.args, opts.line1, opts.line2) end end, { range = true, + nargs = '?', + complete = complete_func, }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) if opts.range == 0 then -- Nothing was selected - openingh.open_file(opts.line1) + openingh.open_file(opts.args, opts.line1) else -- Current line or block was selected - openingh.open_file(opts.line1, opts.line2) + openingh.open_file(opts.args, opts.line1, opts.line2) end end, { range = true, + nargs = '?', + complete = complete_func, }) -vim.api.nvim_create_user_command("OpenInGHRepo", function() - openingh.open_repo() -end, {}) +vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) + openingh.open_repo(opts.args) +end, { + nargs = '?', + complete = complete_func, +})