From f3f9e4a4b5657858fa5ed4e5525336ee39f1a6e4 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sat, 25 May 2019 10:07:00 -0400 Subject: [PATCH] matcher: Allow rules to be addressed using an identifier. --- docs/common/rule.ldoc | 12 ++++++++++++ lib/gears/matcher.lua | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/common/rule.ldoc b/docs/common/rule.ldoc index 1d2c0fc8..776e50bd 100644 --- a/docs/common/rule.ldoc +++ b/docs/common/rule.ldoc @@ -44,3 +44,15 @@ -- @param table -- @see rule -- @see except + +--- An identifier for this rule. +-- +-- It can be anything. It will be compared with the `==` operator. Strings are +-- highly recommended. +-- +-- Setting an `id` is useful to be able to remove the rule by using its id +-- instead of a table reference. Modules can also listen to `rule::appended` and +-- modify or disable a rule. +-- +-- @rulecomponent id +-- @param table|string|number|function diff --git a/lib/gears/matcher.lua b/lib/gears/matcher.lua index 21a55025..8b693855 100644 --- a/lib/gears/matcher.lua +++ b/lib/gears/matcher.lua @@ -362,14 +362,14 @@ end --- Remove a new rule to the default set. -- @tparam string source The source name. --- @tparam table rule A valid rule. +-- @tparam string|table rule An existing rule or its `id`. -- @treturn boolean If the rule was removed. -- @method remove_rule function matcher:remove_rule(source, rule) if not self._matching_rules[source] then return end for k, v in ipairs(self._matching_rules[source]) do - if v == rule then + if v == rule or v.id == rule then table.remove(self._matching_rules[source], k) self:emit_signal("rule::removed", rule, source, self._matching_rules[source]) return true