OpenTTD Source  20241125-master-g5b02f51e17
autocompletion.h
Go to the documentation of this file.
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  */
7 
10 #ifndef AUTOCOMPLETION_H
11 #define AUTOCOMPLETION_H
12 
13 #include "textbuf_type.h"
14 
16 protected:
17  Textbuf *textbuf;
18 
19 private:
20  std::string initial_buf;
21 
22  std::string_view prefix;
23  std::string_view query;
24 
25  std::vector<std::string> suggestions;
26  size_t current_suggestion_index;
27 
28 public:
29  AutoCompletion(Textbuf *textbuf) : textbuf(textbuf)
30  {
31  this->Reset();
32  }
33  virtual ~AutoCompletion() = default;
34 
35  // Returns true the textbuf was updated.
36  bool AutoComplete();
37  void Reset();
38 
39 private:
40  void InitSuggestions(std::string_view text);
41 
42  virtual std::vector<std::string> GetSuggestions(std::string_view prefix, std::string_view query) = 0;
43  virtual void ApplySuggestion(std::string_view prefix, std::string_view suggestion) = 0;
44 };
45 
46 #endif /* AUTOCOMPLETION_H */
std::string_view query
Last token of the text. This is used to based the suggestions on.
std::string_view prefix
Prefix of the text before the last space.
std::string initial_buf
Value of text buffer when we started current suggestion session.
Helper/buffer for input fields.
Definition: textbuf_type.h:30
Stuff related to text buffers.