From dbbed0b7bbd299b6f3c953e3d73345724af54c98 Mon Sep 17 00:00:00 2001 From: Chiizujin Date: Tue, 7 May 2024 22:51:26 +1000 Subject: [PATCH 1/3] Improve help search matching --- evennia/commands/default/help.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/evennia/commands/default/help.py b/evennia/commands/default/help.py index 2a8ef389cc..7b3110ba4b 100644 --- a/evennia/commands/default/help.py +++ b/evennia/commands/default/help.py @@ -487,6 +487,7 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): {"field_name": "tags", "boost": 1}, # tags are not used by default ] match, suggestions = None, None + base_query = query.lstrip("@") for match_query in (query, f"{query}*"): # We first do an exact word-match followed by a start-by query. The # return of this will either be a HelpCategory, a Command or a @@ -494,9 +495,28 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): matches, suggestions = help_search_with_index( match_query, entries, suggestion_maxnum=self.suggestion_maxnum, fields=search_fields ) + # Move an exact match (including aliases) to the front of the list, treating 'command' + # and '@command' as the same thing + for m in matches[:]: + aliases = [m.key] + if not isinstance(m, HelpCategory): + # Aliases for help created with 'sethelp' is an AliasHandler + aliases += m.aliases if isinstance(m.aliases, list) else m.aliases.all() + if base_query in [alias.lstrip("@") for alias in aliases]: + matches.remove(m) + matches.insert(0, m) + break if matches: match = matches[0] break + if match: + # Move an exact suggestion match to the front of the list + for s in suggestions[:]: + if base_query == s.lstrip("@"): + suggestions.remove(s) + suggestions.insert(0, s) + break + return match, suggestions def parse(self): From 91a04cb9203621eebebae2b5336eb36ccf5290d3 Mon Sep 17 00:00:00 2001 From: Chiizujin Date: Tue, 13 Aug 2024 13:15:07 +1000 Subject: [PATCH 2/3] Add support for CMD_IGNORE_PREFIXES in improved help search --- evennia/commands/default/help.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/evennia/commands/default/help.py b/evennia/commands/default/help.py index 7b3110ba4b..cb679bc359 100644 --- a/evennia/commands/default/help.py +++ b/evennia/commands/default/help.py @@ -477,6 +477,11 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): tuple: A tuple (match, suggestions). """ + def strip_prefix(query): + if query[0] in settings.CMD_IGNORE_PREFIXES: + return query[1:] + return query + if not search_fields: # lunr search fields/boosts search_fields = [ @@ -487,7 +492,7 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): {"field_name": "tags", "boost": 1}, # tags are not used by default ] match, suggestions = None, None - base_query = query.lstrip("@") + base_query = strip_prefix(query) for match_query in (query, f"{query}*"): # We first do an exact word-match followed by a start-by query. The # return of this will either be a HelpCategory, a Command or a @@ -495,14 +500,14 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): matches, suggestions = help_search_with_index( match_query, entries, suggestion_maxnum=self.suggestion_maxnum, fields=search_fields ) - # Move an exact match (including aliases) to the front of the list, treating 'command' - # and '@command' as the same thing + # Move an exact match (including aliases) to the front of the list, treating a prefixed + # and non-prefixed command as the same thing for m in matches[:]: aliases = [m.key] if not isinstance(m, HelpCategory): # Aliases for help created with 'sethelp' is an AliasHandler aliases += m.aliases if isinstance(m.aliases, list) else m.aliases.all() - if base_query in [alias.lstrip("@") for alias in aliases]: + if base_query in [strip_prefix(alias) for alias in aliases]: matches.remove(m) matches.insert(0, m) break @@ -512,7 +517,7 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): if match: # Move an exact suggestion match to the front of the list for s in suggestions[:]: - if base_query == s.lstrip("@"): + if base_query == strip_prefix(s): suggestions.remove(s) suggestions.insert(0, s) break From 7edcdb0f3dd5e1cca04ad52584681f267da25383 Mon Sep 17 00:00:00 2001 From: Chiizujin Date: Fri, 16 Aug 2024 22:02:15 +1000 Subject: [PATCH 3/3] Handle empty query in improved help search --- evennia/commands/default/help.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evennia/commands/default/help.py b/evennia/commands/default/help.py index cb679bc359..4e9b06bdde 100644 --- a/evennia/commands/default/help.py +++ b/evennia/commands/default/help.py @@ -478,7 +478,7 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): """ def strip_prefix(query): - if query[0] in settings.CMD_IGNORE_PREFIXES: + if query and query[0] in settings.CMD_IGNORE_PREFIXES: return query[1:] return query