Back to Home

ESO Lua File v101050

ingame/slashcommands/slashcommandautocomplete.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
ZO_SlashCommandAutoComplete = ZO_AutoComplete:Subclass()
function ZO_SlashCommandAutoComplete:Initialize(editControl, ...)
    ZO_AutoComplete.Initialize(self, editControl, ...)
    self.possibleCommandMatches = {}
    self.commandString = nil
    self.commandInfo = nil
    self.possibleOptionMatches = {}
    self:SetUseCallbacks(true)
    self:SetAnchorStyle(AUTO_COMPLETION_ANCHOR_BOTTOM)
    self:SetOwner(nil)
    local function OnAutoCompleteEntrySelected(text, selectionMethod)
        if self.commandString and self.commandString ~= "" then
            editControl:SetText(string.format("%s %s", self.commandString, text))
        else
            editControl:SetText(text)
        end
    end
    self:RegisterCallback(ZO_AutoComplete.ON_ENTRY_SELECTED, OnAutoCompleteEntrySelected)
    ZO_PreHook("ZO_ChatTextEntry_PreviousCommand", function(...)
        if not IsShiftKeyDown() and self:IsOpen() then
            local index = self:GetAutoCompleteIndex()
            if not index or index > 1 then
                self:ChangeAutoCompleteIndex(-1)
                return true
            end
        end
    end)
    ZO_PreHook("ZO_ChatTextEntry_NextCommand", function(...)
        if not IsShiftKeyDown() and self:IsOpen() then
            local index = self:GetAutoCompleteIndex()
            if not index or index < self:GetNumAutoCompleteEntries() then
                self:ChangeAutoCompleteIndex(1)
                return true --Handled
            end
        end
    end)
    local function OnEmoteSlashCommandsUpdated()
    end
    PLAYER_EMOTE_MANAGER:RegisterCallback("EmoteSlashCommandsUpdated", OnEmoteSlashCommandsUpdated)
end
function ZO_SlashCommandAutoComplete:ClearPossibleCommandMatches()
    self.possibleCommandMatches = {}
end
function ZO_SlashCommandAutoComplete:AddCommandsToPossibleResults(tableOfCommands)
    if tableOfCommands == nil then
        return
    end
    for command, info in pairs(tableOfCommands) do
        if #command > 0 then
            self.possibleCommandMatches[command:lower()] = command
            -- add in the command aliases, if any, as well
            if info.aliases then
                for index, alias in ipairs(info.aliases) do
                    self.possibleCommandMatches[alias:lower()] = alias
                end
            end
        end
    end
end
function ZO_SlashCommandAutoComplete:BuildPossibleCommandMatches()
    for command in pairs(SLASH_COMMANDS) do
        if #command > 0 then
            self.possibleCommandMatches[command:lower()] = command
        end
    end
    if BRACKET_COMMANDS then
        for command in pairs(BRACKET_COMMANDS) do
            if #command > 0 then
                self.possibleCommandMatches[command:lower()] = command
            end
        end
    end
    self:AddCommandsToPossibleResults(ZO_REGIONCOMMANDS)
    self:AddCommandsToPossibleResults(ZO_CLIENTCOMMANDS)
    local switchLookup = ZO_ChatSystem_GetChannelSwitchLookupTable()
    for channelId, switchString in ipairs(switchLookup) do
        self.possibleCommandMatches[switchString:lower()] = switchString
    end
end
function ZO_SlashCommandAutoComplete:GetCommandAutoCompletionResults(text)
    if next(self.possibleCommandMatches) == nil then
    end
    local results = GetTopMatchesByLevenshteinSubStringScore(self.possibleCommandMatches, text, 2, self.maxResults)
    if results then
        return unpack(results)
    end
    return nil
end
function ZO_SlashCommandAutoComplete:ClearPossibleOptionMatches()
    self.possibleOptionMatches = {}
end
function ZO_SlashCommandAutoComplete:BuildPossibleOptionMatches()
    if self.commandInfo and self.commandInfo.options then
        for index, option in ipairs(self.commandInfo.options) do
            self.possibleOptionMatches[option:lower()] = option
        end
    end
end
function ZO_SlashCommandAutoComplete:SetCommandInfo(commandString, commandInfo)
    self.commandString = commandString
    if commandInfo == self.commandInfo then
        return
    end
    self.commandInfo = commandInfo
end
function ZO_SlashCommandAutoComplete:ClearCommandInfo()
    self:SetCommandInfo(nil, nil)
end
function ZO_SlashCommandAutoComplete:GetOptionAutoCompletionResults(command, text)
    local commandLower = command:lower()
    local commandInfo = nil
    if ZO_REGIONCOMMANDS then
        commandInfo = ZO_REGIONCOMMANDS[commandLower]
    end
    if ZO_CLIENTCOMMANDS then
        local clientCommandInfo = ZO_CLIENTCOMMANDS[commandLower]
        commandInfo = clientCommandInfo or commandInfo
    end
    if not commandInfo then
        return nil
    end
    self:SetCommandInfo(commandLower, commandInfo)
    local results = GetTopMatchesByLevenshteinSubStringScore(self.possibleOptionMatches, text, 1, self.maxResults)
    if results then
        return unpack(results)
    end
end
function ZO_SlashCommandAutoComplete:GetAutoCompletionResults(text)
    if #text < 3 then
        return
    end
    -- make sure the text starts with one of the command prefixes
    local startChar = text:sub(1, 1)
    if startChar ~= "/" and startChar ~= "]" then
        return
    end
    -- if there's a space in the string, then it could be a command + option beign typed
    local firstSpaceStart = text:find(" ", 1, true)
    if firstSpaceStart then
        local secondWordStart = firstSpaceStart + 1
        local optionText = zo_strsub(text, secondWordStart)
        if optionText:find(" ", 1, true) then
            -- there's a second space, so no longer typing an option
            return
        end
        local command = zo_strsub(text, 1, firstSpaceStart - 1)
        return self:GetOptionAutoCompletionResults(command, optionText)
    end
    -- no space, check for auto-completing a command
end