Skip to content

Commit

Permalink
perf(routers): always add '?plain=1' for github (#156)
Browse files Browse the repository at this point in the history

perf(routers): always add '?display=source' for codeberg (#156)
  • Loading branch information
linrongbin16 authored Nov 27, 2023
1 parent 9315320 commit 0a8925f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ require('gitlinker').setup({
.. "{_A.USER}/"
.. "{_A.REPO}/blob/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?plain=1' or ''}" -- '?plain=1'
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/gitlinker.nvim/blob/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
Expand All @@ -207,15 +206,14 @@ require('gitlinker').setup({
.. "{_A.USER}/"
.. "{_A.REPO}/src/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?display=source' or ''}" -- '?display=source'
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l6
-- dev repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.USER) > 0 and (_A.USER .. '/') or ''}" -- 'p=samba.git' or 'p=bbaumbach/samba.git'
.. "{string.len(_A.USER) > 0 and (_A.USER .. '/') or ''}" -- 'p=samba.git;' or 'p=bbaumbach/samba.git;'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE};"
.. "hb={_A.REV}"
Expand All @@ -227,8 +225,7 @@ require('gitlinker').setup({
.. "{_A.USER}/"
.. "{_A.REPO}/blame/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?plain=1' or ''}"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/gitlinker.nvim/blame/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
Expand All @@ -252,7 +249,7 @@ require('gitlinker').setup({
.. "{_A.USER}/"
.. "{_A.REPO}/blame/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
},
Expand Down
13 changes: 5 additions & 8 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ local Defaults = {
.. "{_A.USER}/"
.. "{_A.REPO}/blob/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?plain=1' or ''}" -- '?plain=1'
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/gitlinker.nvim/blob/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
Expand All @@ -58,8 +57,7 @@ local Defaults = {
.. "{_A.USER}/"
.. "{_A.REPO}/src/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?display=source' or ''}" -- '?display=source'
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example:
Expand All @@ -78,8 +76,7 @@ local Defaults = {
.. "{_A.USER}/"
.. "{_A.REPO}/blame/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{(string.len(_A.FILE) >= 3 and _A.FILE:sub(#_A.FILE-2) == '.md') and '?plain=1' or ''}"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/gitlinker.nvim/blame/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
Expand All @@ -90,7 +87,7 @@ local Defaults = {
.. "{_A.FILE}"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://bitbucket.org/linrongbin16/gitlinker.nvim/annotate/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
-- example: https://bitbucket.org/linrongbin16/gitlinker.nvim/annotate/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#lines-3:4
["^bitbucket%.org"] = "https://bitbucket.org/"
.. "{_A.USER}/"
.. "{_A.REPO}/annotate/"
Expand All @@ -103,7 +100,7 @@ local Defaults = {
.. "{_A.USER}/"
.. "{_A.REPO}/blame/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
},
Expand Down
37 changes: 33 additions & 4 deletions lua/gitlinker/routers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local logger = require("gitlinker.logger")
local Builder = {}

--- @alias gitlinker.RangeStringify fun(r:gitlinker.Range?):string?

--- @param r gitlinker.Range?
--- @return string?
local function LC_range(r)
Expand All @@ -25,6 +26,34 @@ local function LC_range(r)
return tmp
end

--- @param r gitlinker.Range?
--- @return string?
local function github_LC_range(r)
if not range.is_range(r) then
return nil
end
assert(r ~= nil)
local tmp = string.format([[?plain=1#L%d]], r.lstart)
if type(r.lend) == "number" and r.lend > r.lstart then
tmp = tmp .. string.format([[-L%d]], r.lend)
end
return tmp
end

--- @param r gitlinker.Range?
--- @return string?
local function codeberg_LC_range(r)
if not range.is_range(r) then
return nil
end
assert(r ~= nil)
local tmp = string.format([[?display=source#L%d]], r.lstart)
if type(r.lend) == "number" and r.lend > r.lstart then
tmp = tmp .. string.format([[-L%d]], r.lend)
end
return tmp
end

--- @param r gitlinker.Range?
--- @return string?
local function lines_range(r)
Expand Down Expand Up @@ -114,7 +143,7 @@ end
--- @param lk gitlinker.Linker
--- @return string
local function github_browse(lk)
local builder = Builder:new(lk, LC_range)
local builder = Builder:new(lk, github_LC_range)
return builder:build("blob")
end

Expand All @@ -135,7 +164,7 @@ end
--- @param lk gitlinker.Linker
--- @return string
local function codeberg_browse(lk)
local builder = Builder:new(lk, LC_range)
local builder = Builder:new(lk, codeberg_LC_range)
return builder:build("src/commit")
end

Expand All @@ -146,7 +175,7 @@ end
--- @param lk gitlinker.Linker
--- @return string
local function github_blame(lk)
local builder = Builder:new(lk, LC_range)
local builder = Builder:new(lk, github_LC_range)
return builder:build("blame")
end

Expand All @@ -167,7 +196,7 @@ end
--- @param lk gitlinker.Linker
--- @return string
local function codeberg_blame(lk)
local builder = Builder:new(lk, LC_range)
local builder = Builder:new(lk, codeberg_LC_range)
return builder:build("blame/commit")
end

Expand Down
24 changes: 12 additions & 12 deletions test/gitlinker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("gitlinker", function()
.. "{_A.USER}/"
.. "{_A.REPO}/blob/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{_A.FILE}?plain=1"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
},
Expand All @@ -28,7 +28,7 @@ describe("gitlinker", function()
.. "{_A.USER}/"
.. "{_A.REPO}/blame/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "{_A.FILE}?plain=1"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
},
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L13-L47"
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L13-L47"
)
assert_eq(actual, routers.github_browse(lk))
end)
Expand All @@ -163,7 +163,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1"
"https://github.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L1"
)
assert_eq(actual, routers.github_browse(lk))
end)
Expand All @@ -184,7 +184,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://git.xyz.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L13-L47"
"https://git.xyz.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L13-L47"
)
assert_eq(actual, routers.github_browse(lk))
end)
Expand All @@ -205,7 +205,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://git.xyz.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1"
"https://git.xyz.com/linrongbin16/gitlinker.nvim/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L1"
)
assert_eq(actual, routers.github_browse(lk))
end)
Expand Down Expand Up @@ -305,7 +305,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://codeberg.org/linrongbin16/gitlinker.nvim/src/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L17"
"https://codeberg.org/linrongbin16/gitlinker.nvim/src/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?display=source#L17"
)
assert_eq(actual, routers.codeberg_browse(lk))
end)
Expand All @@ -325,7 +325,7 @@ describe("gitlinker", function()
local actual = gitlinker._browse(lk)
assert_eq(
actual,
"https://codeberg.org/linrongbin16/gitlinker.nvim/src/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L27-L53"
"https://codeberg.org/linrongbin16/gitlinker.nvim/src/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?display=source#L27-L53"
)
assert_eq(actual, routers.codeberg_browse(lk))
end)
Expand All @@ -347,7 +347,7 @@ describe("gitlinker", function()
local actual = gitlinker._blame(lk)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1"
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L1"
)
assert_eq(actual, routers.github_blame(lk))
end)
Expand All @@ -367,7 +367,7 @@ describe("gitlinker", function()
local actual = gitlinker._blame(lk)
assert_eq(
actual,
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L1-L2"
"https://github.com/linrongbin16/gitlinker.nvim/blame/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L1-L2"
)
assert_eq(actual, routers.github_blame(lk))
end)
Expand Down Expand Up @@ -467,7 +467,7 @@ describe("gitlinker", function()
local actual = gitlinker._blame(lk)
assert_eq(
actual,
"https://codeberg.org/linrongbin16/gitlinker.nvim/blame/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L13"
"https://codeberg.org/linrongbin16/gitlinker.nvim/blame/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?display=source#L13"
)
assert_eq(actual, routers.codeberg_blame(lk))
end)
Expand All @@ -488,7 +488,7 @@ describe("gitlinker", function()
local actual = gitlinker._blame(lk)
assert_eq(
actual,
"https://codeberg.org/linrongbin16/gitlinker.nvim/blame/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua#L13-L21"
"https://codeberg.org/linrongbin16/gitlinker.nvim/blame/commit/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?display=source#L13-L21"
)
assert_eq(actual, routers.codeberg_blame(lk))
end)
Expand Down

0 comments on commit 0a8925f

Please sign in to comment.