refactoring of completion

This commit is contained in:
Simon Hirsig
2015-10-16 12:56:50 +02:00
parent 598861f56f
commit fe17f87b2f
+88 -82
View File
@@ -2,7 +2,7 @@ Aux.completion = {}
local NUM_MATCHES = 5
local fuzzy, populate_dropdown, toggle_dropdown, suggestions, update_highlighting
local fuzzy, make_suggestions
local item_names = {}
@@ -33,18 +33,7 @@ function fuzzy(input)
end
end
function update_highlighting()
for i=1,32 do
local highlight = getglobal('DropDownList1Button' .. i .. 'Highlight')
if i == selected_index then
highlight:Show()
else
highlight:Hide()
end
end
end
function suggestions(input)
function generate_suggestions(input)
local matcher = fuzzy(input)
function fuzzy_sort(array)
sort(array, function(a, b) return strlen(a.name) < strlen(b.name) end)
@@ -70,93 +59,110 @@ function suggestions(input)
return Aux.util.map(best, function(match) return match.name end)
end
function populate_dropdown(input_box, suggestions)
for _, suggestion in ipairs(suggestions) do
UIDropDownMenu_AddButton{
text = suggestion,
value = suggestion,
notCheckable = true,
func = function()
Aux.completion.set_quietly(input_box, this.value)
end,
}
end
end
function toggle_dropdown(input_box)
ToggleDropDownMenu(1, nil, AuxCompletionDropDown, input_box, -12, 4)
end
function Aux.completion.completor()
function Aux.completion.completor(edit_box)
local self = {}
local current_suggestions = {}
local selected_index = 0
local current_input
local programmatically_set_input
local suggestions = {}
local index = 0
local input
local quietly_set_text
function self.close()
CloseDropDownMenus(1)
local fill_dropdown, update_dropdown, update_highlighting
function fill_dropdown()
for _, suggestion in ipairs(suggestions) do
UIDropDownMenu_AddButton{
text = suggestion,
value = suggestion,
notCheckable = true,
func = function()
self:set_quietly(edit_box, this.value)
end,
}
end
end
function self.set_quietly(edit_box, text)
programmatically_set_input = text
edit_box:SetText(text)
end
function self.highlighted()
return selected_index ~= 0
end
function self.suggest(input_box)
local input = input_box:GetText()
if programmatically_set_input == input then
programmatically_set_input = nil
return
end
programmatically_set_input = nil
current_input = input
selected_index = 0
update_highlighting()
if current_input == '' then
current_suggestions = {}
else
current_suggestions = suggestions(current_input)
UIDropDownMenu_Initialize(AuxCompletionDropDown, function() populate_dropdown(input_box, current_suggestions) end)
function update_dropdown()
function toggle()
ToggleDropDownMenu(1, nil, AuxCompletionDropDown, edit_box, -12, 4)
end
if DropDownList1:IsVisible() then
toggle_dropdown(input_box)
toggle()
end
toggle_dropdown(input_box)
toggle()
end
function self.next(input_box)
if getn(current_suggestions) > 0 then
selected_index = selected_index > 0 and math.mod(selected_index + 1, getn(current_suggestions) + 1) or 1
update_highlighting()
if selected_index == 0 then
Aux.completion.set_quietly(input_box, current_input)
else
Aux.completion.set_quietly(input_box, current_suggestions[selected_index])
function update_highlighting()
for i=1,32 do
local highlight = getglobal('DropDownList1Button' .. i .. 'Highlight')
if i == index then
highlight:Show()
else
highlight:Hide()
end
end
end
function self.previous(input_box)
if getn(current_suggestions) > 0 then
selected_index = selected_index > 0 and math.mod(selected_index + 1, getn(current_suggestions) + 1) or 1
update_highlighting()
if selected_index == 0 then
Aux.completion.set_quietly(input_box, current_input)
function self:close()
CloseDropDownMenus(1)
end
function self:set_quietly(text)
quietly_set_text = text
edit_box:SetText(text)
end
function self:completed()
return index ~= 0
end
function self:suggest()
local new_input = edit_box:GetText()
if new_input == quietly_set_text then
quietly_set_text = nil
return
end
quietly_set_text = nil
input = new_input
index = 0
update_highlighting()
if input == '' then
suggestions = {}
else
suggestions = generate_suggestions(input)
UIDropDownMenu_Initialize(AuxCompletionDropDown, function() fill_dropdown() end)
end
update_dropdown()
end
function self:next()
if getn(suggestions) > 0 then
index = index > 0 and math.mod(index + 1, getn(suggestions) + 1) or 1
self:update_highlighting()
if index == 0 then
self:set_quietly(input)
else
Aux.completion.set_quietly(input_box, current_suggestions[selected_index])
self:set_quietly(suggestions[index])
end
end
end
function self.previous()
if getn(suggestions) > 0 then
index = index > 0 and index - 1 or getn(suggestions)
update_highlighting()
if index == 0 then
self:set_quietly(input)
else
self:set_quietly(suggestions[index])
end
end
end
return self
end
end