~tomleb/repo-url.nvim

Generate permalinks of the files being edited
Remove unused var
Try to support macOS
Update README

clone

read-only
https://git.sr.ht/~tomleb/repo-url.nvim
read/write
git@git.sr.ht:~tomleb/repo-url.nvim

You can also use your local clone with git send-email.

#Repo URL

RepoURL is a lua plugin for Neovim that generates URLs to the code repository of the file being edited.

image

#✨ Features

  • 🔗 Easily share permalinks with friends and colleagues
  • 🏭 Supports many software forges out of the box
  • ⚙️ Customize and extend support for additional software forges to fit your workflow perfectly.
  • 🐹 Special support for Go's go.mod and go.sum files as well as files in your GOMODCACHE

#📦 Installation

Install the plugin with your preferred package manager:

#lazy.nvim

{
  url = "https://git.sr.ht/~tomleb/repo-url.nvim",
  config = function()
    local ru = require 'repo-url'
    require('repo-url').setup {
      -- your configuration comes here
      -- or leave it empty to use the default settings
      -- refer to the configuration section below
    },
  end,
}

#⚙️ Configuration

RepoURL does not provide keymaps. Instead, you are required to assign them yourselves. Here's an example configuration.

{
  url = "https://git.sr.ht/~tomleb/repo-url.nvim",
  config = function()
    local ru = require 'repo-url'
    require('repo-url').setup {}

    vim.keymap.set({ 'n', 'v' }, '<leader>rt', ru.copy_blob_url, { desc = 'Copy blob URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rT', ru.open_blob_url, { desc = 'Open blob URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rb', ru.copy_blame_url, { desc = 'Copy blame URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rB', ru.open_blame_url, { desc = 'Open blame URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rh', ru.copy_history_url, { desc = 'Copy history URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rH', ru.open_history_url, { desc = 'Open history URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rr', ru.copy_raw_url, { desc = 'Copy raw URL' })
    vim.keymap.set({ 'n', 'v' }, '<leader>rR', ru.open_raw_url, { desc = 'Open raw URL' })

    -- For Go's go.mod file, generate permalinks to the dependency under the
    -- cursor and copy to the clipboard or open in the default browser.
    vim.api.nvim_create_autocmd('FileType', {
      pattern = 'gomod',
      group = filetype,
      callback = function(args)
        vim.keymap.set('n', '<leader>rg', function()
          ru.copy_gomod_tree_url()
        end, { desc = 'Copy gomod URL' })
        vim.keymap.set('n', '<leader>rG', function()
          ru.open_gomod_tree_url()
        end, { desc = 'Open gomod URL' })
      end,
    })

    -- For Go's go.sum file, generate permalinks to the dependency under the
    -- cursor and copy to the clipboard or open in the default browser.
    vim.api.nvim_create_autocmd('FileType', {
      pattern = 'gosum',
      group = filetype,
      callback = function(args)
        vim.keymap.set('n', '<leader>rg', function()
          ru.copy_gosum_tree_url()
        end, { desc = 'Copy gosum URL' })
        vim.keymap.set('n', '<leader>rG', function()
          ru.open_gosum_tree_url()
        end, { desc = 'Open gosum URL' })
      end,
    })
  end,
}

RepoURL comes with the following defaults:

{
  -- Ordered list of remotes to look for to generate the permalinks for git
  -- repositories if the current branch doesn't track a specific remote
  preferred_remotes = { 'origin' },
  -- Mapping of software forge hostname to URL builders. More details in the URL
  -- builder's section.
  urls_by_hostname = {
    ['github.com'] = M.urls.github,
    ['git.sr.ht'] = M.urls.sourcehut,
    ['gitlab.com'] = M.urls.gitlab,
    ['gitlab.alpinelinux.org'] = M.urls.gitlab,
    ['codeberg.org'] = M.urls.codeberg,
    ['go.googlesource.com'] = M.urls.gitiles,
    ['git.zx2c4.com'] = M.urls.cgit,
  },
}

#Functions

Here are the available functions. Note that for all functions, special handling is in place to support files that are in $GOMODCACHE. This is useful when using LSP's Go To Definition where you might end up in a dependency stored in GOMODCACHE. RepoURL will still be able to generate a permalink.

require('repo-url').get_blob_url()

Generates a permalink to the blob view of the current line under the cursor or selected lines.

require('repo-url').copy_blob_url()

Generates a permalink using get_blob_url() and set it to the + register.

require('repo-url').open_blob_url()

Generates a permalink using get_blob_url() and opens it in your browser.

require('repo-url').get_blame_url()

Generates a permalink to the blame view of the current line under the cursor or selected lines.

require('repo-url').copy_blame_url()

Generates a permalink using get_blame_url() and set it to the + register.

require('repo-url').open_blame_url()

Generates a permalink using get_blame_url() and opens it in your browser.

require('repo-url').get_history_url()

Generates a permalink to the history view of the current line under the cursor or selected lines.

require('repo-url').copy_history_url()

Generates a permalink using get_history_url() and set it to the + register.

require('repo-url').open_history_url()

Generates a permalink using get_history_url() and opens it in your browser.

require('repo-url').get_raw_url()

Generates a permalink to the raw view of the current line under the cursor or selected lines.

require('repo-url').copy_raw_url()

Generates a permalink using get_raw_url() and set it to the + register.

require('repo-url').open_raw_url()

Generates a permalink using get_raw_url() and opens it in your browser.

require('repo-url').get_tree_url()

Generates a permalink to the tree view of the repository that the current file belongs to.

require('repo-url').copy_tree_url()

Generates a permalink using get_tree_url() and set it to the + register.

require('repo-url').open_tree_url()

Generates a permalink using get_tree_url() and opens it in your browser.

require('repo-url').get_gomod_tree_url()

Generates a permalink to the tree view of the repository of the Go dependency under the cursor in a go.mod. Parses the version to point to the specified commit.

For example, if the cursor is under the following line:

require k8s.io/client-go v0.30.0

then get_gomod_tree_url will generate the following link: https://github.com/kubernetes/client-go/tree/v0.30.0.

require('repo-url').copy_gomod_tree_url()

Generates a permalink using get_gomod_tree_url() and set it to the + register.

require('repo-url').open_gomod_tree_url()

Generates a permalink using get_gomod_tree_url() and opens it in your browser.

require('repo-url').get_gosum_tree_url()

Generates a permalink to the tree view of the repository of the Go dependency under the cursor in a go.sum. Parses the version to point to the specified commit.

For example, if the cursor is under the following line:

k8s.io/client-go v0.30.0/go.mod h1:g7li5O5256qe6TYdAMyX/otJqMhIiGgTapdLchhmOaY=

then get_gosum_tree_url will generate the following link: https://github.com/kubernetes/client-go/tree/v0.30.0.

require('repo-url').copy_gosum_tree_url()

Generates a permalink using get_gosum_tree_url() and set it to the + register.

require('repo-url').open_gosum_tree_url()

Generates a permalink using get_gosum_tree_url() and opens it in your browser.

#URL builder

You can teach RepoURL how to generate URLs for forges that it does not support out of the box.

Simply provide an object with the following properties:

{
  -- Link goes to default software forge browsing page. Required.
  blob_url = function(opts)
    return "https://..."
  end,
  -- Link goes to the blame page, showing which commit and author last modified
  -- each line. Optional.
  blame_url = function(opts)
    return "https://..."
  end,
  -- Link goes to the history page, showing all previous commits affecting the
  -- current file. Optional.
  history_url = function(opts)
    return "https://..."
  end,
  -- Link goes to the raw file. Optional.
  raw_url = function(opts)
    return "https://..."
  end,
  -- Link goes to the tree that the current file belongs to. Optional.
  tree_url = function(opts)
    return "https://..."
  end,

These functions will be given the following object (opts):

{
  -- The base URL (eg: "https://github.com/my-org/my-project")
  base_url = "",
  -- The commit hash OR the tag
  ref = "",
  -- The filepath relative to the root of the repository. Not given for
  -- `history_url` and `tree_url`.
  filepath = "",
  -- For normal mode, the line under the cursor. For range mode, the beginning
  -- of the range.
  line1 = 0,
  -- For normal mode, the line under the cursor. For range mode, the end of the
  -- range.
  line2 = 0,
}

#Contributing

Configure git with the following commands to send patches to my mailing list.

git config sendemail.to "~tomleb/public-inbox@lists.sr.ht"
git config format.subjectprefix "PATCH repo-url.nvim"
Do not follow this link