From 93474d7f4347e0223f07c2cfc78ff2010228c173 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 19 Oct 2021 13:22:08 +0200 Subject: [PATCH] ui: Make text editor automatically close (, [, {, " and ' --- external/ImGui/source/TextEditor.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/external/ImGui/source/TextEditor.cpp b/external/ImGui/source/TextEditor.cpp index a4fd0eeef..cf5ebfbbc 100644 --- a/external/ImGui/source/TextEditor.cpp +++ b/external/ImGui/source/TextEditor.cpp @@ -767,8 +767,26 @@ void TextEditor::HandleKeyboardInputs() for (int i = 0; i < io.InputQueueCharacters.Size; i++) { auto c = io.InputQueueCharacters[i]; - if (c != 0 && (c == '\n' || c >= 32)) - EnterCharacter(c, shift); + if (c != 0 && (c == '\n' || c >= 32)) { + constexpr std::array, 5> doubleChars = {{ { '(', ')' }, { '[', ']' }, { '{', '}' }, { '"', '"' }, {'\'', '\'' } }}; + + bool handled = false; + for (auto [doubleCharOpen, doubleCharClose] : doubleChars) { + if (c == doubleCharOpen) { + EnterCharacter(doubleCharOpen, shift); + auto cursorPos = GetCursorPosition(); + EnterCharacter(doubleCharClose, shift); + SetCursorPosition(cursorPos); + + handled = true; + + break; + } + } + + if (!handled) + EnterCharacter(c, shift); + } } io.InputQueueCharacters.resize(0); }