Skip to content

Commit

Permalink
fix(utils): Adeed support for priority for patterns.
Browse files Browse the repository at this point in the history
This will be used whdn 2 patterns can be matched(e.g. "github%.com/.+",
"github%.com").

Ref: #177
  • Loading branch information
OXY2DEV committed Jan 10, 2025
1 parent 3fe66bb commit 9296450
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions lua/markview/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,36 @@ utils.match = function (config, name, opts)

local match = {};

local sort_keys = function (values)
local w_priority = {};
local n_priority = {};

for k, v in pairs(values or {}) do
if type(v) == "table" and type(v.priority) == "number" then
table.insert(w_priority, {
key = k,
priority = v.priority
});
elseif k ~= "default" and vim.list_contains(opts.ignore_keys or {}, k) == false then
table.insert(n_priority, k);
end
end

table.sort(w_priority, function (a, b)
return a.priority > b.priority;
end)

local keys = {};

for _, item in ipairs(w_priority) do
table.insert(keys, item.key);
end

keys = vim.list_extend(n_priority, keys);

return keys;
end

local function is_valid (value, pattern)
local ignore = opts.ignore_keys or {};

Expand All @@ -337,9 +367,16 @@ utils.match = function (config, name, opts)
end
end

for key, val in pairs(config) do
--- NOTE, We should sort the keys so that we
--- don't get different results every time
--- when multiple patterns can be matched.
---
---@type string[]
local keys = sort_keys(config or {});

for _, key in ipairs(keys) do
if is_valid(name, key) == true then
match = val;
match = config[key];
break
end
end
Expand Down

0 comments on commit 9296450

Please sign in to comment.