move SearchTools to SearchHost, fix exporter by including SearchHost

This commit is contained in:
Fish013
2020-02-15 23:30:30 +01:00
parent 15f105594b
commit b349a9b961
5 changed files with 57 additions and 58 deletions

View File

@@ -89,7 +89,7 @@ function DropDownClass:GetDropCount()
end
function DropDownClass:DrawSearchHighlights(label, searchInfo, x, y, width, height)
if searchInfo.matches then
if searchInfo and searchInfo.matches then
local startX = 0
local endX = 0
local last = 0

View File

@@ -11,6 +11,60 @@ local SearchHostClass = newClass("SearchHost", function(self, listAccessor, valu
self.searchInfos = {}
end)
local function splitWords(s)
local words = {}
for word in s:gmatch("%S+") do
table.insert(words, word)
end
return words
end
local function letterToCaselessPattern(c)
return string.format("[%s%s]", string.lower(c), string.upper(c))
end
local function wordsToCaselessPatterns(words)
local patterns = {}
for idx = 1, #words do
-- escape all non alphanumeric chars
patterns[idx] = string.gsub(words[idx], "%W", "%%%1")
-- make pattern case insensitive
patterns[idx] = string.gsub(patterns[idx], "%a", letterToCaselessPattern)
end
return patterns
end
local function matchWords(searchWords, entry, valueAccessor)
local value = valueAccessor and valueAccessor(entry) or entry
local searchInfo = { ranges = {}, matches = true }
local lastMatchEnd = 0
for _, word in ipairs(searchWords) do
local from, to = string.find(value, word, lastMatchEnd + 1)
if (from) then
local range = { from = from, to = to }
table.insert(searchInfo.ranges, range)
lastMatchEnd = to
else
-- at least one search word did not match at least once (respecting order)
searchInfo.matches = false
end
end
return searchInfo
end
local function matchTerm(searchTerm, list, valueAccessor)
if not searchTerm or searchTerm == "" or not list then
return {}
end
local searchInfos = {}
local searchPatterns = wordsToCaselessPatterns(splitWords(searchTerm))
for idx, entry in ipairs(list) do
searchInfos[idx] = matchWords(searchPatterns, entry, valueAccessor)
end
return searchInfos
end
function SearchHostClass:IsSearchActive()
return self.searchTerm and self.searchTerm ~= ""
end
@@ -56,7 +110,7 @@ end
function SearchHostClass:UpdateSearch()
if self.searchListAccessor then
self.searchInfos = search.match(self.searchTerm, self.searchListAccessor(), self.valueAccessor)
self.searchInfos = matchTerm(self.searchTerm, self.searchListAccessor(), self.valueAccessor)
self:UpdateMatchCount()
end
end

View File

@@ -24,6 +24,7 @@ local classList = {
"UndoHandler",
"Tooltip",
"TooltipHost",
"SearchHost",
-- Basic controls
"Control",
"LabelControl",

View File

@@ -21,7 +21,6 @@ LoadModule("Modules/ModTools")
LoadModule("Modules/ItemTools")
LoadModule("Modules/CalcTools")
LoadModule("Modules/PantheonTools")
LoadModule("Modules/SearchTools")
--[[if launch.devMode then
for skillName, skill in pairs(data["3_0"].enchantments.Helmet) do

View File

@@ -1,55 +0,0 @@
search = { }
local function splitWords(s)
local words = {}
for word in s:gmatch("%S+") do
table.insert(words, word)
end
return words
end
local function letterToCaselessPattern(c)
return string.format("[%s%s]", string.lower(c), string.upper(c))
end
local function wordsToCaselessPatterns(words)
local patterns = {}
for idx = 1, #words do
-- escape all non alphanumeric chars
patterns[idx] = string.gsub(words[idx], "%W", "%%%1")
-- make pattern case insensitive
patterns[idx] = string.gsub(patterns[idx], "%a", letterToCaselessPattern)
end
return patterns
end
local function match(searchWords, entry, valueAccessor)
local value = valueAccessor and valueAccessor(entry) or entry
local searchInfo = { ranges = {}, matches = true }
local lastMatchEnd = 0
for _, word in ipairs(searchWords) do
local from, to = string.find(value, word, lastMatchEnd + 1)
if (from) then
local range = { from = from, to = to }
table.insert(searchInfo.ranges, range)
lastMatchEnd = to
else
-- at least one search word did not match at least once (respecting order)
searchInfo.matches = false
end
end
return searchInfo
end
function search.match(searchTerm, list, valueAccessor)
if not searchTerm or searchTerm == "" or not list then
return {}
end
local searchInfos = {}
local searchPatterns = wordsToCaselessPatterns(splitWords(searchTerm))
for idx, entry in ipairs(list) do
searchInfos[idx] = match(searchPatterns, entry, valueAccessor)
end
return searchInfos
end