Update: Translations from eints
[openttd-github.git] / src / autocompletion.cpp
blobe88150c75262187f87f2422a9e5117d5032b289f
1 /*
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/>.
6 */
8 /** @file autocompletion.cpp Generic auto-completion engine. */
10 #include "stdafx.h"
12 #include "autocompletion.h"
14 #include "console_internal.h"
15 #include "town.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()) {
26 return false;
28 this->ApplySuggestion(prefix, suggestions[0]);
29 return true;
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]);
35 } else {
36 // We are out of options, restore original text.
37 this->textbuf->Assign(initial_buf);
38 this->Reset();
40 return true;
43 void AutoCompletion::Reset()
45 this->prefix = "";
46 this->query = "";
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) {
58 this->prefix = "";
59 } else {
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;