From 217be6b58d16004192ed3191d9154ee87be11586 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Tue, 16 May 2023 12:36:58 -0700 Subject: [PATCH] make full text search configurable for list filters --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 8 ++++++++ library/lua/gui/widgets.lua | 19 ++++++++++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index fc706cdba..f301a1055 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -47,6 +47,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Misc Improvements - Terminal console no longer appears in front of the game window on startup +- `gui/control-panel`: new preference for whether filters in lists search for substrings in the middle of words (e.g. if set to true, then "ee" will match "steel") - `gui/design`: Improved performance for drawing shapes ## Documentation diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 85e51d11d..6215fd250 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -5106,6 +5106,14 @@ The widget implements: Same as with an ordinary list. +Filter behavior: + +By default, the filter matches substrings that start at the beginning of a word +(or after any punctuation). You can instead configure filters to match any +substring with a command like:: + + :lua require('gui.widgets').FILTER_FULL_TEXT=true + TabBar class ------------ diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 7aa79cbc3..e5d808903 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1932,6 +1932,8 @@ end -- Filtered List -- ------------------- +FILTER_FULL_TEXT = false + FilteredList = defclass(FilteredList, Widget) FilteredList.ATTRS { @@ -2102,19 +2104,22 @@ function FilteredList:setFilter(filter, pos) end for _,key in ipairs(tokens) do key = key:escape_pattern() - -- start matches at non-space or non-punctuation. this allows - -- punctuation itself to be matched if that is useful (e.g. - -- filenames or parameter names) if key ~= '' then if not self.case_sensitive then search_key = string.lower(search_key) key = string.lower(key) end - if not search_key:match('%f[^%p\x00]'..key) and - not search_key:match('%f[^%s\x00]'..key) then - ok = false - break + -- the separate checks for non-space or non-punctuation allows + -- punctuation itself to be matched if that is useful (e.g. + -- filenames or parameter names) + if not FILTER_FULL_TEXT and not search_key:match('%f[^%p\x00]'..key) + and not search_key:match('%f[^%s\x00]'..key) then + ok = false + break + elseif FILTER_FULL_TEXT and not search_key:find(key) then + ok = false + break end end end