~tomleb/repo-url.nvim

4b506631facc490e70b1823c846db6c517064464 — Tom Lebreux 7 months ago 61a52ba
Add tree_url support
1 files changed, 75 insertions(+), 21 deletions(-)

M lua/repo-url/init.lua
M lua/repo-url/init.lua => lua/repo-url/init.lua +75 -21
@@ 12,6 12,7 @@ local M = {}
--- @field blame_url (fun(RepoUrl.InfoOpts): string)?
--- @field history_url (fun(RepoUrl.InfoOpts): string)?
--- @field raw_url (fun(RepoUrl.InfoOpts): string)?
--- @field tree_url (fun(RepoUrl.InfoOpts): string)?

local fallback_gocache_dir = nil



@@ 31,11 32,7 @@ M.urls = {
  github = {
    blob_url = function(opts)
      local position = pos_to_path(opts, '#L%d', '#L%d-L%d')
      local path = 'blob'
      if not opts.filepath or opts.filepath == '' then
        path = 'tree'
      end
      return string.format('%s/%s/%s/%s%s', opts.base_url, path, opts.ref, opts.filepath, position)
      return string.format('%s/blob/%s/%s%s', opts.base_url, opts.ref, opts.filepath, position)
    end,
    blame_url = function(opts)
      local position = pos_to_path(opts, '#L%d', '#L%d-L%d')


@@ 48,6 45,9 @@ M.urls = {
      local base_url = string.gsub(opts.base_url, 'github.com', 'raw.githubusercontent.com', 1)
      return string.format('%s/%s/%s', base_url, opts.ref, opts.filepath)
    end,
    tree_url = function(opts)
      return string.format('%s/tree/%s', opts.base_url, opts.ref)
    end,
  },
  sourcehut = {
    blob_url = function(opts)


@@ 64,6 64,9 @@ M.urls = {
    raw_url = function(opts)
      return string.format('%s/blob/%s/%s', opts.base_url, opts.ref, opts.filepath)
    end,
    tree_url = function(opts)
      return string.format('%s/tree/%s', opts.base_url, opts.ref)
    end,
  },
  gitlab = {
    blob_url = function(opts)


@@ 80,6 83,9 @@ M.urls = {
    raw_url = function(opts)
      return string.format('%s/-/raw/%s/%s', opts.base_url, opts.ref, opts.filepath)
    end,
    tree_url = function(opts)
      return string.format('%s/-/tree/%s/', opts.base_url, opts.ref)
    end,
  },
  codeberg = {
    blob_url = function(opts)


@@ 96,6 102,11 @@ M.urls = {
    raw_url = function(opts)
      return string.format('%s/raw/commit/%s/%s', opts.base_url, opts.ref, opts.filepath)
    end,
    tree_url = function(opts)
      -- XXX: For tags it should be ../src/tag and for branch names it should be ../src/branch
      -- Similar for gitiles/cgit, opts should contain the full ref if possible
      return string.format('%s/src/commit/%s', opts.base_url, opts.ref)
    end,
  },
  -- gitiles doesn't support raw_url
  gitiles = {


@@ 419,6 430,9 @@ M.get_blame_url = function()
  if urls.blame_url == nil then
    error("blame_url not defined for '%s'", res.hostname)
  end
  if not urls.blame_url then
    error(string.format("blame_url() not defined for hostname '%s'", res.hostname))
  end
  return urls.blame_url(res.info)
end



@@ 447,6 461,9 @@ M.get_history_url = function()
  if urls.history_url == nil then
    error("history_url not defined for '%s'", res.hostname)
  end
  if not urls.history_url then
    error(string.format("history_url() not defined for hostname '%s'", res.hostname))
  end
  return urls.history_url(res.info)
end



@@ 475,6 492,9 @@ M.get_raw_url = function()
  if urls.raw_url == nil then
    error("raw_url not defined for '%s'", res.hostname)
  end
  if not urls.raw_url then
    error(string.format("raw_url() not defined for hostname '%s'", res.hostname))
  end
  return urls.raw_url(res.info)
end



@@ 494,6 514,34 @@ M.open_raw_url = function()
  open_url(url)
end

M.get_tree_url = function()
  local res = get_repo_info()
  local urls = pick_urls(res.hostname)
  if urls == nil then
    error(string.format("hostname '%s' not matching any URLs", res.hostname))
  end
  if not urls.tree_url then
    error(string.format("tree_url() not defined for hostname '%s'", res.hostname))
  end
  return urls.tree_url(res.info)
end

M.copy_tree_url = function()
  local ok, url = pcall(M.get_tree_url)
  if not ok then
    notify_error('copy_tree_url failed: %s', url)
  end
  clipboard_url(url)
end

M.open_tree_url = function()
  local ok, url = pcall(M.get_tree_url)
  if not ok then
    notify_error('open_tree_url failed: %s', url)
  end
  open_url(url)
end

local get_gomod_repo_and_version = function(mline)
  local line = mline or vim.fn.getline '.'
  local match, repo, version


@@ 545,7 593,7 @@ local get_gomod_repo_and_version = function(mline)
  return nil, nil
end

M.get_gomod_blob_url = function()
M.get_gomod_tree_url = function()
  local repo, version = get_gomod_repo_and_version()
  if not repo then
    error 'unsupported go.mod directive'


@@ 566,22 614,25 @@ M.get_gomod_blob_url = function()
    if urls == nil then
      error(string.format("hostname '%s' not matching any URLs", hostname))
    end
    return urls.blob_url(stuff.info)
    if not urls.tree_url then
      error(string.format("tree_url() not defined for hostname '%s'", hostname))
    end
    return urls.tree_url(stuff.info)
  end
end

M.copy_gomod_blob_url = function()
  local ok, url = pcall(M.get_gomod_blob_url)
M.copy_gomod_tree_url = function()
  local ok, url = pcall(M.get_gomod_tree_url)
  if not ok then
    notify_error('copy_gomod_blob_url failed: %s', url)
    notify_error('copy_gomod_tree_url failed: %s', url)
  end
  clipboard_url(url)
end

M.open_gomod_blob_url = function()
  local ok, url = pcall(M.get_gomod_blob_url)
M.open_gomod_tree_url = function()
  local ok, url = pcall(M.get_gomod_tree_url)
  if not ok then
    notify_error('open_gomod_blob_url failed: %s', url)
    notify_error('open_gomod_tree_url failed: %s', url)
  end
  open_url(url)
end


@@ 600,7 651,7 @@ local get_gosum_repo_and_version = function()
  return nil, nil
end

M.get_gosum_blob_url = function()
M.get_gosum_tree_url = function()
  local repo, version = get_gosum_repo_and_version()
  if not repo then
    error 'unsupported go.sum directive'


@@ 621,22 672,25 @@ M.get_gosum_blob_url = function()
    if urls == nil then
      error(string.format("hostname '%s' not matching any URLs", hostname))
    end
    return urls.blob_url(stuff.info)
    if not urls.tree_url then
      error(string.format("tree_url() not defined for hostname '%s'", hostname))
    end
    return urls.tree_url(stuff.info)
  end
end

M.copy_gosum_blob_url = function()
  local ok, url = pcall(M.get_gosum_blob_url)
M.copy_gosum_tree_url = function()
  local ok, url = pcall(M.get_gosum_tree_url)
  if not ok then
    notify_error('copy_gosum_blob_url failed: %s', url)
    notify_error('copy_gosum_tree_url failed: %s', url)
  end
  clipboard_url(url)
end

M.open_gosum_blob_url = function()
  local ok, url = pcall(M.get_gosum_blob_url)
M.open_gosum_tree_url = function()
  local ok, url = pcall(M.get_gosum_tree_url)
  if not ok then
    notify_error('open_gosum_blob_url failed: %s', url)
    notify_error('open_gosum_tree_url failed: %s', url)
  end
  open_url(url)
end