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..dfb8ad9 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -1,9 +1,11 @@ +local remote_format = "%s/blob/%s/%s" +local bb_remote_format = "%s/src/%s/%s" local utils = require("openingh.utils") local M = {} function M.setup() -- get the current working directory and set the url - local current_buffer = vim.fn.expand("%:p:h") + local current_buffer = vim.fn.expand("%:p:h"):gsub("%[", "\\["):gsub("%]", "\\]") local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url") if repo_url:len() == 0 then @@ -12,17 +14,17 @@ function M.setup() return end - local gh = utils.parse_gh_remote(repo_url) - if gh == nil then - print("Error parsing GitHub remote URL") + local remote = utils.parse_remote(repo_url) + if remote == nil then + print("Error parsing remote URL") vim.g.openingh = false return end - 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", remote.host, remote.user_or_org, remote.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 +39,8 @@ end function M.get_file_url( priority, --[[optional]] + branch, + --[[optional]] range_start, --[[optional]] range_end @@ -56,8 +60,18 @@ 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 = "" + + if string.find(M.repo_url, "bitbucket") then + file_page_url = string.format(bb_remote_format, M.repo_url, rev, file_path) + else + file_page_url = string.format(remote_format, M.repo_url, rev, file_path) + end if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 30a58e9..5164453 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -25,20 +25,23 @@ end -- url encode -- see: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 function M.encode_uri_component(string) - return (string:gsub("[^%w_~%.%-]", function (c) return string.format("%%%02X", string.byte(c)) end)) + return (string:gsub("[^%w_~%.%-]", function(c) + return string.format("%%%02X", string.byte(c)) + end)) end -- returns a table with the host, user/org and the reponame given a github remote url -- nil is returned when the url cannot be parsed -function M.parse_gh_remote(url) +function M.parse_remote(url) -- 3 capture groups for host, org/user and repo. whitespace is trimmed -- when cloning with http://, gh redirects to https://, but remote stays http local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") } -- ssh url can be of type: -- git@some.github.com:user_or_org/reponame.git -- ssh://git@some.github.com/user_or_org/reponame.git + -- ssh://org-12345@some.github.com/org/reponame.git -- .* is used for ssh:// since lua matching doesn't support optional groups, only chars - local ssh = { string.find(url, ".*git@(.*)[:/]([^/]*)/([^%s/]*)") } + local ssh = { string.find(url, ".*@(.*)[:/]([^/]*)/([^%s/]*)") } local matches = http[1] == nil and ssh or http if matches[1] == nil then @@ -125,13 +128,17 @@ function M.get_current_relative_file_path() local absolute_file_path = vim.api.nvim_buf_get_name(0) local git_path = vim.fn.system("git rev-parse --show-toplevel") + if vim.fn.has("win32") == 1 then + absolute_file_path = string.gsub(absolute_file_path, "\\", "/") + end + local relative_file_path_components = M.split(string.sub(absolute_file_path, git_path:len() + 1), "/") local encoded_components = {} for i, path_component in pairs(relative_file_path_components) do table.insert(encoded_components, i, M.encode_uri_component(path_component)) end - return "/" .. table.concat(encoded_components, '/') + return "/" .. table.concat(encoded_components, "/") end -- get the line number in the buffer 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, })