Skip to content

Commit

Permalink
Skip console autocomplete when navigating history
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 6, 2023
1 parent e02dc1f commit b265bbd
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Source/panels/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ TextInputState ConsoleInputState {
.maxLength = sizeof(ConsoleInputBuffer) - 1,
}
};
bool InputTextChanged = false;

enum class InputTextState {
UpToDate,
Edited,
RestoredFromHistory
};

InputTextState CurrentInputTextState = InputTextState::UpToDate;
std::string WrappedInputText { Prompt };
std::vector<LuaAutocompleteSuggestion> AutocompleteSuggestions;
int AutocompleteSuggestionsMaxWidth = -1;
Expand Down Expand Up @@ -315,7 +322,7 @@ const ConsoleLine &GetConsoleLineFromEnd(int index)

void SetHistoryIndex(int index)
{
InputTextChanged = true;
CurrentInputTextState = InputTextState::RestoredFromHistory;
HistoryIndex = std::ssize(ConsoleLines) - (index + 1);
if (HistoryIndex == -1) {
ConsoleInputState.assign(DraftInput);
Expand Down Expand Up @@ -455,7 +462,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
return false;
}
if (HandleTextInputEvent(event, ConsoleInputState)) {
InputTextChanged = true;
CurrentInputTextState = InputTextState::Edited;
return true;
}
const auto modState = SDL_GetModState();
Expand Down Expand Up @@ -491,7 +498,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
case SDLK_TAB:
if (AutocompleteSuggestionFocusIndex != -1) {
AcceptSuggestion();
InputTextChanged = true;
CurrentInputTextState = InputTextState::Edited;
}
return true;
case SDLK_RETURN:
Expand All @@ -505,7 +512,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
SendInput();
}
}
InputTextChanged = true;
CurrentInputTextState = InputTextState::Edited;
return true;
case SDLK_PAGEUP:
++PendingScrollPages;
Expand Down Expand Up @@ -556,12 +563,16 @@ void DrawConsole(const Surface &out)
OuterRect.size = { out.w(), out.h() - GetMainPanel().size.height - 2 };

const std::string_view originalInputText = ConsoleInputState.value();
if (InputTextChanged) {
if (CurrentInputTextState != InputTextState::UpToDate) {
WrappedInputText = WordWrapString(StrCat(Prompt, originalInputText), OuterRect.size.width - 2 * TextPaddingX, TextFontSize, TextSpacing);
GetLuaAutocompleteSuggestions(originalInputText.substr(0, ConsoleInputCursor.position), GetLuaReplEnvironment(), /*maxSuggestions=*/MaxSuggestions, AutocompleteSuggestions);
if (CurrentInputTextState == InputTextState::RestoredFromHistory) {
AutocompleteSuggestions.clear();
} else {
GetLuaAutocompleteSuggestions(originalInputText.substr(0, ConsoleInputCursor.position), GetLuaReplEnvironment(), /*maxSuggestions=*/MaxSuggestions, AutocompleteSuggestions);
}
AutocompleteSuggestionsMaxWidth = -1;
AutocompleteSuggestionFocusIndex = AutocompleteSuggestions.empty() ? -1 : 0;
InputTextChanged = false;
CurrentInputTextState = InputTextState::UpToDate;
}

const int numLines = static_cast<int>(c_count(WrappedInputText, '\n')) + 1;
Expand Down

0 comments on commit b265bbd

Please sign in to comment.