Console: Prefer command names with underscores in auto-completion

This commit is contained in:
Jonathan G Rennison
2024-03-16 16:52:57 +00:00
parent 3fcfe5b53e
commit 1a279a844c

View File

@@ -466,55 +466,61 @@ static void IConsoleTabCompletion()
return; return;
} }
} }
extern std::string RemoveUnderscores(std::string name);
std::string prefix = RemoveUnderscores(std::string(input, cmdptr - input));
size_t prefix_length = prefix.size();
if (prefix_length == 0) return; struct match_state {
std::string prefix;
char buffer[4096]; std::string candidate_str;
char *b = buffer;
uint matches = 0;
std::string common_prefix; std::string common_prefix;
auto check_candidate = [&](const std::string &cmd_name_str) { uint matches = 0;
const char *cmd_name = cmd_name_str.c_str(); };
if (matches == 0) { match_state match_input;
common_prefix = cmd_name_str; match_state match_input_no_underscores;
match_input.prefix = std::string(input, cmdptr - input);
if (match_input.prefix.empty()) return;
extern std::string RemoveUnderscores(std::string name);
match_input_no_underscores.prefix = RemoveUnderscores(match_input.prefix);
if (match_input_no_underscores.prefix.empty()) return;
auto check_candidate = [&](const char *cmd_name, match_state &state) {
if (strncmp(cmd_name, state.prefix.c_str(), state.prefix.size()) != 0) return;
if (state.matches == 0) {
state.common_prefix = cmd_name;
} else { } else {
const char *cp = common_prefix.c_str(); const char *cp = state.common_prefix.c_str();
const char *cmdp = cmd_name; const char *cmdp = cmd_name;
while (true) { while (true) {
const char *end = cmdp; const char *end = cmdp;
char32_t a = Utf8Consume(cp); char32_t a = Utf8Consume(cp);
char32_t b = Utf8Consume(cmdp); char32_t b = Utf8Consume(cmdp);
if (a == 0 || b == 0 || a != b) { if (a == 0 || b == 0 || a != b) {
common_prefix.resize(end - cmd_name); state.common_prefix.resize(end - cmd_name);
break; break;
} }
} }
} }
matches++; state.matches++;
b += seprintf(b, lastof(buffer), "%s ", cmd_name); if (!state.candidate_str.empty()) state.candidate_str += ' ';
state.candidate_str += cmd_name;
}; };
for (auto &it : IConsole::Commands()) { for (auto &it : IConsole::Commands()) {
const char *cmd_name = it.first.c_str();
const IConsoleCmd *cmd = &it.second; const IConsoleCmd *cmd = &it.second;
if (strncmp(cmd_name, prefix.c_str(), prefix_length) == 0) {
if ((_settings_client.gui.console_show_unlisted || !cmd->unlisted) && (cmd->hook == nullptr || cmd->hook(false) != CHR_HIDE)) { if ((_settings_client.gui.console_show_unlisted || !cmd->unlisted) && (cmd->hook == nullptr || cmd->hook(false) != CHR_HIDE)) {
check_candidate(it.first); check_candidate(it.first.c_str(), match_input_no_underscores);
} check_candidate(cmd->name.c_str(), match_input);
} }
} }
for (auto &it : IConsole::Aliases()) { for (auto &it : IConsole::Aliases()) {
const char *cmd_name = it.first.c_str(); check_candidate(it.first.c_str(), match_input_no_underscores);
if (strncmp(cmd_name, prefix.c_str(), prefix_length) == 0) { check_candidate(it.second.name.c_str(), match_input);
check_candidate(it.first);
} }
} match_state &best = match_input_no_underscores.matches > match_input.matches ? match_input_no_underscores : match_input;
if (matches > 0) { if (best.matches > 0) {
_iconsole_cmdline.Assign(common_prefix.c_str()); _iconsole_cmdline.Assign(best.common_prefix.c_str());
if (matches > 1) { if (best.matches > 1) {
IConsolePrint(CC_WHITE, buffer); IConsolePrint(CC_WHITE, best.candidate_str.c_str());
} }
} }
} }