2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file autocompletion.cpp Generic auto-completion engine. */
12 #include "autocompletion.h"
14 #include "console_internal.h"
16 #include "network/network_base.h"
18 #include "safeguards.h"
20 bool AutoCompletion::AutoComplete()
22 // We are pressing TAB for the first time after reset.
23 if (this->suggestions
.empty()) {
24 this->InitSuggestions(this->textbuf
->buf
);
25 if (this->suggestions
.empty()) {
28 this->ApplySuggestion(prefix
, suggestions
[0]);
32 // We are pressing TAB again on the same text.
33 if (this->current_suggestion_index
+ 1 < this->suggestions
.size()) {
34 this->ApplySuggestion(prefix
, this->suggestions
[++this->current_suggestion_index
]);
36 // We are out of options, restore original text.
37 this->textbuf
->Assign(initial_buf
);
43 void AutoCompletion::Reset()
47 this->initial_buf
.clear();
48 this->suggestions
.clear();
49 this->current_suggestion_index
= 0;
52 void AutoCompletion::InitSuggestions(std::string_view text
)
54 this->initial_buf
= text
;
55 size_t space_pos
= this->initial_buf
.find_last_of(' ');
56 this->query
= this->initial_buf
;
57 if (space_pos
== std::string::npos
) {
60 this->prefix
= this->query
.substr(0, space_pos
+ 1);
61 this->query
.remove_prefix(space_pos
+ 1);
64 this->suggestions
= this->GetSuggestions(prefix
, query
);
65 this->current_suggestion_index
= 0;