Skip to content

Commit 7d310b5

Browse files
committed
Lua console: Trigger autocomplete less often
Filter out more cases where autocomplete just gets in the way.
1 parent 18211f5 commit 7d310b5

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Source/lua/autocomplete.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,39 @@ void SuggestionsFromUserdata(UserdataQuery query, std::string_view prefix,
218218
}
219219
}
220220

221+
bool IsAlnum(char c)
222+
{
223+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
224+
}
225+
226+
bool IsIdentifierChar(char c)
227+
{
228+
return IsAlnum(c) || c == '_';
229+
}
230+
231+
bool IsIdentifierOrExprChar(char c)
232+
{
233+
return IsIdentifierChar(c) || c == '-' || c == '+' || c == '*' || c == '/' || c == '=';
234+
}
235+
221236
} // namespace
222237

223-
void GetLuaAutocompleteSuggestions(std::string_view text, const sol::environment &lua,
238+
void GetLuaAutocompleteSuggestions(std::string_view text, size_t cursorPos, const sol::environment &lua,
224239
size_t maxSuggestions, std::vector<LuaAutocompleteSuggestion> &out)
225240
{
226241
out.clear();
227-
if (text.empty()) return;
228-
std::string_view token = GetLastToken(text);
229-
const char prevChar = token.data() == text.data() ? '\0' : *(token.data() - 1);
242+
const std::string_view textPrefix = text.substr(0, cursorPos);
243+
if (textPrefix.empty()) return;
244+
const std::string_view textSuffix = text.substr(cursorPos);
245+
if (!textSuffix.empty()) {
246+
const char c = textSuffix[0];
247+
if (IsIdentifierOrExprChar(c) || (c == ' ' && textSuffix.size() > 1)) return;
248+
}
249+
if (textPrefix.size() >= 2 && textPrefix.back() == ' ' && IsIdentifierChar(textPrefix[textPrefix.size() - 2])) {
250+
return;
251+
}
252+
std::string_view token = GetLastToken(textPrefix);
253+
const char prevChar = token.data() == textPrefix.data() ? '\0' : *(token.data() - 1);
230254
if (prevChar == '(' || prevChar == ',') return;
231255
const size_t dotPos = token.find_last_of(".:");
232256
const std::string_view prefix = token.substr(dotPos + 1);
@@ -259,7 +283,7 @@ void GetLuaAutocompleteSuggestions(std::string_view text, const sol::environment
259283
addSuggestions(obj->as<sol::table>());
260284
} else if (obj->get_type() == sol::type::userdata) {
261285
const sol::userdata &data = obj->as<sol::userdata>();
262-
SuggestionsFromUserdata(UserdataQuery { &data, completionChar == ':' },
286+
SuggestionsFromUserdata(UserdataQuery { .obj = &data, .colonAccess = completionChar == ':' },
263287
prefix, maxSuggestions, suggestions);
264288
}
265289
}

Source/lua/autocomplete.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct LuaAutocompleteSuggestion {
2626
};
2727

2828
void GetLuaAutocompleteSuggestions(
29-
std::string_view text, const sol::environment &lua,
29+
std::string_view text, size_t cursorPos, const sol::environment &lua,
3030
size_t maxSuggestions, std::vector<LuaAutocompleteSuggestion> &out);
3131

3232
} // namespace devilution

Source/panels/console.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void DrawConsole(const Surface &out)
560560
if (CurrentInputTextState == InputTextState::RestoredFromHistory) {
561561
AutocompleteSuggestions.clear();
562562
} else {
563-
GetLuaAutocompleteSuggestions(originalInputText.substr(0, ConsoleInputCursor.position), GetLuaReplEnvironment(), /*maxSuggestions=*/MaxSuggestions, AutocompleteSuggestions);
563+
GetLuaAutocompleteSuggestions(originalInputText, ConsoleInputCursor.position, GetLuaReplEnvironment(), /*maxSuggestions=*/MaxSuggestions, AutocompleteSuggestions);
564564
}
565565
AutocompleteSuggestionsMaxWidth = -1;
566566
AutocompleteSuggestionFocusIndex = AutocompleteSuggestions.empty() ? -1 : 0;

0 commit comments

Comments
 (0)