@@ -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 }
0 commit comments