Skip to content

Commit

Permalink
fix(config): fix merged user routers configs (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Nov 27, 2023
1 parent 0a8925f commit 3e33ba8
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,33 @@ end
--- @return table<string, {list_routers:table,map_routers:table}>
local function _merge_routers(opts)
local result = {}
-- default routers
-- default_router_type: browse, blame, etc

-- users list
if type(opts.router) == "table" then
-- user_router_type: browse, blame, etc
for user_router_type, user_router_bindings in pairs(opts.router) do
if result[user_router_type] == nil then
result[user_router_type] = {}
result[user_router_type].list_routers = {}
result[user_router_type].map_routers = {}
end
-- list
for i, tuple in ipairs(user_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
-- prepend to head for higher priority
table.insert(result[user_router_type].list_routers, 1, tuple)
end
end
end
end

-- default map
for default_router_type, default_router_bindings in pairs(Defaults.router) do
if result[default_router_type] == nil then
result[default_router_type] = {}
result[default_router_type].list_routers = {}
result[default_router_type].map_routers = {}
end
-- list
for i, tuple in ipairs(default_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
table.insert(result[default_router_type].list_routers, tuple)
end
end
-- map
for pattern, route in pairs(default_router_bindings) do
if result[default_router_type].map_routers == nil then
Expand All @@ -443,21 +456,20 @@ local function _merge_routers(opts)
end
end
end

-- default list
for default_router_type, default_router_bindings in pairs(Defaults.router) do
-- list
for i, tuple in ipairs(default_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
table.insert(result[default_router_type].list_routers, tuple)
end
end
end

-- user map
if type(opts.router) == "table" then
-- user_router_type: browse, blame, etc
for user_router_type, user_router_bindings in pairs(opts.router) do
if result[user_router_type] == nil then
result[user_router_type] = {}
result[user_router_type].list_routers = {}
result[user_router_type].map_routers = {}
end
-- list
for i, tuple in ipairs(user_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
-- prepend to head for higher priority
table.insert(result[user_router_type].list_routers, 1, tuple)
end
end
-- map
for pattern, route in pairs(user_router_bindings) do
if result[user_router_type].map_routers == nil then
Expand Down

0 comments on commit 3e33ba8

Please sign in to comment.