New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: Basic autocompletion on tab for console commands #12163
Conversation
I like the idea. Without having looked at the actual code or tried it, a few formal things: Also, indentation is with tabs, not spaces. |
Handling of Also |
2cd9ddd
to
39eeddc
Compare
Thanks for the feedback. Yes, when in game, I can even see Fast-Forward button flash pressed when I press tab. I will take a look at network_gui auto-completion implementation and refactoring into a separate class for reusing. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Most hotkeys are disabled when the cursor is captured in a text box, because they are being used to type in the box — try pressing X for transparency when editing the name of a station, for example. I see no reason why should be any different. But it is, both in the console and in text boxes. Changing this might be a separate PR. |
f7faeca
to
ce15834
Compare
I have re-implemented the algorithm, used to auto-complete network chat in a generic way. It leads to more memory allocations, but given that we auto-complete on a pretty small set of options (network client names and towns for network chat and commands and aliases for console), I assumed that code readability and ease of maintenance is more important in this case. |
Yeah, we're not talking large amounts of memory here, I can't imagine that's an issue. Especially with the frequency that it'd be used I will say that I'm not sure it requires 6 new files though - we don't have any sort of requirement for one class per file, so feel free to shove them wherever is most appropriate (which might be 1 new cpp/h files, might be none at all, idk) |
443a2a8
to
d5c9e08
Compare
b8ab42b
to
dfec5dd
Compare
@glx22 Thanks for the review. I have moved the code as suggested. |
f42aa93
to
4345b8a
Compare
20feb3c
to
acc4623
Compare
acc4623
to
54cafd3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Largely codestyle nitpicks
src/autocompletion.h
Outdated
|
||
class AutoCompletion { | ||
protected: | ||
Textbuf* textbuf; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we no longer generally bother with the alignment stuff, especially not multiple "sections"
Also, *
goes on the variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not everywhere :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked through the code one more time and fix the only place I noticed.
src/console_gui.cpp
Outdated
while (prefix.ends_with(" ")) { | ||
prefix.remove_suffix(1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a "trim" string function already? Seems like it would be more efficient than the explicit loop like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at string_func.h and there are two trim functions:
One for std::string and one for a null-terminated utf-8 strings.
Both of them doesn't combine well with std::string_view (as they can be not null terminated).
I can add one more for std::string_view.
Something like std::string_view StrTrimView(std::string_view s).
It also should be possible to implement StrTrimInPlace as (s = StrTrimView(s)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented my suggestion and added unit tests.
Please let me know if you want me to extract it into a separate Pr.
b210ec2
to
4a79e1f
Compare
d71179e
to
f6a50c3
Compare
f6a50c3
to
7f132e2
Compare
7f132e2
to
bf51120
Compare
bf51120
to
c2d1458
Compare
IConsoleWindow::scroll = 0; | ||
IConsoleResetHistoryPos(); | ||
this->SetDirty(); | ||
} | ||
} | ||
|
||
Textbuf *GetFocusedTextbuf() const override | ||
const Textbuf *GetFocusedTextbuf() const override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why on Earth did it compile without it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return type of the functions are covariant if the satisfy the following criteria:
both are pointers to classes or references to classes98)
the class in the return type of B::f is the same class as the class in the return type of D::f or, is an unambiguous direct or indirect base class of the class in the return type of D::f and is accessible in D
both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL.
But it is confusing, so I would like to fix it.
c2d1458
to
f957720
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM
Motivation / Problem
When typing console commands, it is very common to have some kind of autocompletion when pressing tab.
It is also a great way to discover console commands.
Description
When in console, the user pressed tab and have at least one character entered, simple autocompletion will happen:
This is the same logic that is used in network_chat_gui. It is extracted into autocomplete.h/.cpp and is used in both places.
Limitations
This is very basic algorithm.
It can be improved by using more context-dependent hints.
For example, when trying to autocomplete
start_ai A
we can look at the list of installed AIs and autocomplete from it. For examplestart_ai AdmiralAI
.However, this will require more complex code modifications, so I want to test my waters with more simple PR, before submitting something bigger.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.