Browse Source

Skip console autocomplete when navigating history

pull/6790/head
Gleb Mazovetskiy 2 years ago
parent
commit
b265bbdbb4
  1. 27
      Source/panels/console.cpp

27
Source/panels/console.cpp

@ -54,7 +54,14 @@ TextInputState ConsoleInputState {
.maxLength = sizeof(ConsoleInputBuffer) - 1, .maxLength = sizeof(ConsoleInputBuffer) - 1,
} }
}; };
bool InputTextChanged = false;
enum class InputTextState {
UpToDate,
Edited,
RestoredFromHistory
};
InputTextState CurrentInputTextState = InputTextState::UpToDate;
std::string WrappedInputText { Prompt }; std::string WrappedInputText { Prompt };
std::vector<LuaAutocompleteSuggestion> AutocompleteSuggestions; std::vector<LuaAutocompleteSuggestion> AutocompleteSuggestions;
int AutocompleteSuggestionsMaxWidth = -1; int AutocompleteSuggestionsMaxWidth = -1;
@ -315,7 +322,7 @@ const ConsoleLine &GetConsoleLineFromEnd(int index)
void SetHistoryIndex(int index) void SetHistoryIndex(int index)
{ {
InputTextChanged = true; CurrentInputTextState = InputTextState::RestoredFromHistory;
HistoryIndex = std::ssize(ConsoleLines) - (index + 1); HistoryIndex = std::ssize(ConsoleLines) - (index + 1);
if (HistoryIndex == -1) { if (HistoryIndex == -1) {
ConsoleInputState.assign(DraftInput); ConsoleInputState.assign(DraftInput);
@ -455,7 +462,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
return false; return false;
} }
if (HandleTextInputEvent(event, ConsoleInputState)) { if (HandleTextInputEvent(event, ConsoleInputState)) {
InputTextChanged = true; CurrentInputTextState = InputTextState::Edited;
return true; return true;
} }
const auto modState = SDL_GetModState(); const auto modState = SDL_GetModState();
@ -491,7 +498,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
case SDLK_TAB: case SDLK_TAB:
if (AutocompleteSuggestionFocusIndex != -1) { if (AutocompleteSuggestionFocusIndex != -1) {
AcceptSuggestion(); AcceptSuggestion();
InputTextChanged = true; CurrentInputTextState = InputTextState::Edited;
} }
return true; return true;
case SDLK_RETURN: case SDLK_RETURN:
@ -505,7 +512,7 @@ bool ConsoleHandleEvent(const SDL_Event &event)
SendInput(); SendInput();
} }
} }
InputTextChanged = true; CurrentInputTextState = InputTextState::Edited;
return true; return true;
case SDLK_PAGEUP: case SDLK_PAGEUP:
++PendingScrollPages; ++PendingScrollPages;
@ -556,12 +563,16 @@ void DrawConsole(const Surface &out)
OuterRect.size = { out.w(), out.h() - GetMainPanel().size.height - 2 }; OuterRect.size = { out.w(), out.h() - GetMainPanel().size.height - 2 };
const std::string_view originalInputText = ConsoleInputState.value(); 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); 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; AutocompleteSuggestionsMaxWidth = -1;
AutocompleteSuggestionFocusIndex = AutocompleteSuggestions.empty() ? -1 : 0; AutocompleteSuggestionFocusIndex = AutocompleteSuggestions.empty() ? -1 : 0;
InputTextChanged = false; CurrentInputTextState = InputTextState::UpToDate;
} }
const int numLines = static_cast<int>(c_count(WrappedInputText, '\n')) + 1; const int numLines = static_cast<int>(c_count(WrappedInputText, '\n')) + 1;

Loading…
Cancel
Save