Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a href="https://app.codecov.io/github/linrongbin16/gitlinker.nvim"><img alt="codecov" src="https://img.shields.io/codecov/c/github/linrongbin16/gitlinker.nvim?logo=codecov&logoColor=F01F7A&label=Codecov" /></a>
</p>

> Maintained fork of [ruifm's gitlinker](https://github.com/ruifm/gitlinker.nvim), refactored with bug fixes, git alias host, `/blame` url support and other improvements.
> Maintained fork of [ruifm's gitlinker](https://github.com/ruifm/gitlinker.nvim), refactored with bug fixes, ssh host alias, `/blame` url support and other improvements.

A lua plugin for [Neovim](https://github.com/neovim/neovim) to generate sharable file permalinks (with line ranges) for git host websites. Inspired by [tpope/vim-fugitive](https://github.com/tpope/vim-fugitive)'s `:GBrowse`.

Expand Down Expand Up @@ -53,7 +53,7 @@ PRs are welcomed for other git host websites!
- Customize/disable default key mappings.
2. New Features:
- Windows support.
- Respect ssh config alias host.
- Respect ssh host alias.
- Add `?plain=1` for markdown files.
- Support `/blame` (by default is `/blob`).
3. Improvements:
Expand All @@ -67,7 +67,7 @@ Requirement:

- neovim &ge; v0.7.
- [git](https://git-scm.com/).
- [ssh](https://www.openssh.com/) (optional for resolve git alias host).
- [ssh](https://www.openssh.com/) (optional for resolve ssh host alias).

### [packer.nvim](https://github.com/wbthomason/packer.nvim)

Expand Down
22 changes: 4 additions & 18 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ local deprecation = require("gitlinker.deprecation")
--- @type gitlinker.Options
local Defaults = {
-- print permanent url in command line
--
--- @type boolean
message = true,

-- highlight the linked region
--
--- @type integer
highlight_duration = 500,

-- key mappings
--
--- @type table<string, {action:gitlinker.Action,desc:string?}>
mapping = {
["<leader>gl"] = {
action = require("gitlinker.actions").clipboard,
Expand All @@ -31,8 +25,6 @@ local Defaults = {
},

-- router bindings
--
--- @type table<"browse"|"blame", table<string, gitlinker.Router>>
router_binding = {
browse = {
["^github%.com"] = require("gitlinker.routers").github_browse,
Expand All @@ -47,18 +39,12 @@ local Defaults = {
},

-- enable debug
--
--- @type boolean
debug = false,

-- write logs to console(command line)
--
--- @type boolean
console_log = true,

-- write logs to file
--
--- @type boolean
file_log = false,
}

Expand Down Expand Up @@ -148,9 +134,9 @@ local function setup(opts)

-- Configure highlight group
if Configs.highlight_duration > 0 then
local hl_name = "NvimGitLinkerHighlightTextObject"
if not highlight.hl_group_exists(hl_name) then
vim.api.nvim_set_hl(0, hl_name, { link = "Search" })
local hl_group = "NvimGitLinkerHighlightTextObject"
if not highlight.hl_group_exists(hl_group) then
vim.api.nvim_set_hl(0, hl_group, { link = "Search" })
end
end

Expand Down Expand Up @@ -179,7 +165,7 @@ local function link(opts)
return nil
end

local ok, url = pcall(router, lk)
local ok, url = pcall(router, lk, true)
logger.ensure(
ok and type(url) == "string" and string.len(url) > 0,
"fatal: failed to generate permanent url from remote url (%s): %s",
Expand Down
50 changes: 29 additions & 21 deletions lua/gitlinker/routers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,20 @@ local BROWSE_BINDING = {}

--- @alias gitlinker.Router fun(lk:gitlinker.Linker):string?
--- @param lk gitlinker.Linker
--- @param _placeholder boolean
--- @return string?
local function browse(lk)
local function browse(lk, _placeholder)
-- logger.debug(
-- "|routers.browse| BROWSE_BINDING:%s",
-- vim.inspect(BROWSE_BINDING)
-- )
assert(
type(_placeholder) == "boolean" and _placeholder,
string.format(
"%s must be true, make sure you didn't set this function in 'router_binding'",
vim.inspect(_placeholder)
)
)
for pattern, route in pairs(BROWSE_BINDING) do
if string.match(lk.host, pattern) then
logger.debug(
Expand All @@ -131,10 +139,12 @@ local function browse(lk)
return route(lk)
end
end
logger.ensure(
assert(
false,
"%s not support, please bind it in 'router_binding'!",
vim.inspect(lk.host)
string.format(
"%s not support, please bind it in 'router_binding'!",
vim.inspect(lk.host)
)
)
return nil
end
Expand Down Expand Up @@ -163,18 +173,28 @@ end
local BLAME_BINDING = {}

--- @param lk gitlinker.Linker
--- @param _placeholder boolean
--- @return string?
local function blame(lk)
logger.debug("|routers.blame| BLAME_BINDING:%s", vim.inspect(BLAME_BINDING))
local function blame(lk, _placeholder)
-- logger.debug("|routers.blame| BLAME_BINDING:%s", vim.inspect(BLAME_BINDING))
assert(
type(_placeholder) == "boolean" and _placeholder,
string.format(
"%s must be true, make sure you didn't set this function in 'router_binding'",
vim.inspect(_placeholder)
)
)
for pattern, route in pairs(BLAME_BINDING) do
if string.match(lk.host, pattern) then
return route(lk)
end
end
logger.ensure(
assert(
false,
"%s not support, please bind it in 'router_binding'!",
vim.inspect(lk.host)
string.format(
"%s not support, please bind it in 'router_binding'!",
vim.inspect(lk.host)
)
)
return nil
end
Expand All @@ -186,23 +206,11 @@ local function setup(router_binding)
vim.deepcopy(BROWSE_BINDING),
router_binding.browse or {}
)
for _, route in pairs(BROWSE_BINDING) do
logger.ensure(
route ~= browse,
"must not use 'browse' itself in 'router_binding.browse'! please use other implementations e.g. github_browse, bitbucket_browse, etc."
)
end
BLAME_BINDING = vim.tbl_extend(
"force",
vim.deepcopy(BLAME_BINDING),
router_binding.blame or {}
)
for _, route in pairs(BLAME_BINDING) do
logger.ensure(
route ~= blame,
"must not use 'blame' itself in 'router_binding.blame'! please use other implementations e.g. github_blame, bitbucket_blame, etc."
)
end
end

local M = {
Expand Down
12 changes: 6 additions & 6 deletions test/routers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("routers", function()
rev = "399b1d05473c711fc5592a6ffc724e231c403486",
file = "lua/gitlinker/logger.lua",
file_changed = false,
} --[[@as gitlinker.Linker]])
} --[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua"
Expand All @@ -46,7 +46,7 @@ describe("routers", function()
lstart = 1,
lend = 1,
file_changed = false,
}--[[@as gitlinker.Linker]])
}--[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1"
Expand All @@ -64,7 +64,7 @@ describe("routers", function()
lstart = 1,
lend = 1,
file_changed = false,
}--[[@as gitlinker.Linker]])
}--[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1"
Expand All @@ -82,7 +82,7 @@ describe("routers", function()
lstart = 2,
lend = 5,
file_changed = false,
}--[[@as gitlinker.Linker]])
}--[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L2-L5"
Expand Down Expand Up @@ -134,7 +134,7 @@ describe("routers", function()
rev = "399b1d05473c711fc5592a6ffc724e231c403486",
file = "lua/gitlinker/logger.lua",
file_changed = false,
} --[[@as gitlinker.Linker]])
} --[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua"
Expand All @@ -152,7 +152,7 @@ describe("routers", function()
lstart = 1,
lend = 2,
file_changed = false,
}--[[@as gitlinker.Linker]])
}--[[@as gitlinker.Linker]], true)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1-L2"
Expand Down