From ec78acb42a933245363861e99900e43c340d6b6f Mon Sep 17 00:00:00 2001 From: Artyom Shalkhakov Date: Fri, 21 Feb 2025 01:26:24 -0700 Subject: [PATCH] Fix backspace for good. --- neo/libs/ImGuiColorTextEdit/TextEditor.cpp | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/neo/libs/ImGuiColorTextEdit/TextEditor.cpp b/neo/libs/ImGuiColorTextEdit/TextEditor.cpp index ade81a0a..99fbab71 100644 --- a/neo/libs/ImGuiColorTextEdit/TextEditor.cpp +++ b/neo/libs/ImGuiColorTextEdit/TextEditor.cpp @@ -2070,6 +2070,30 @@ int TextEditor::ReplaceAll(const char* find, const char* replace, bool matchCase return replacements; } +static int GetGlyphWidth(const TextEditor::Line& line, int cindex, int maxColumn, int tabSize) +{ + if (line[cindex].mChar == '\t') + { + int c = 0; + int i = 0; + int charWidth = 0; + for (; i < line.size() && i < cindex + 1 && c < maxColumn;) + { + if (line[i].mChar == '\t') + charWidth = ((c / tabSize) * tabSize + tabSize) - c; + else + charWidth = 1; + c += charWidth; + i += UTF8CharLength(line[i].mChar); + } + return charWidth; + } + else + { + return 1; + } +} + void TextEditor::Delete() { assert(!mReadOnly); @@ -2186,24 +2210,9 @@ void TextEditor::Backspace() // --cindex; u.mRemovedStart = u.mRemovedEnd = GetActualCursorCoordinates(); - if (line[cindex].mChar == '\t') - { - if (pos.mColumn == GetLineMaxColumn(pos.mLine)) - { - u.mRemovedStart.mColumn -= mTabSize - 1; - mState.mCursorPosition.mColumn -= mTabSize - 1; - } - else - { - u.mRemovedStart.mColumn -= mTabSize; - mState.mCursorPosition.mColumn -= mTabSize; - } - } - else - { - --u.mRemovedStart.mColumn; - --mState.mCursorPosition.mColumn; - } + int width = GetGlyphWidth(line, cindex, mState.mCursorPosition.mColumn, mTabSize); + u.mRemovedStart.mColumn -= width; + mState.mCursorPosition.mColumn -= width; while (cindex < line.size() && cend-- > cindex) {