From 4cf9c2845aa78ab7bed74125670a7280d7d02e7c Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 10 Mar 2022 14:58:53 +0800 Subject: [PATCH] - fix: calculation for code snippets's tab stop positions is not correct --- RedPandaIDE/editor.cpp | 16 +++++----------- RedPandaIDE/utils.cpp | 11 +++++++++++ RedPandaIDE/utils.h | 1 + 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 445980be..c26212a5 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -2612,8 +2612,8 @@ void Editor::insertCodeSnippet(const QString &code) for (int i=0;i0) -// lastPos = -spaceCount; + if (i>0) + lastPos = countLeadingWhitespaceChars(s); while (true) { int insertPos = s.indexOf(USER_CODE_IN_INSERT_POS); if (insertPos < 0) // no %INSERT% macro in this line now @@ -2629,8 +2629,8 @@ void Editor::insertCodeSnippet(const QString &code) mUserCodeInTabStops.append(p); } lastPos = 0; -// if (i>0) -// lastPos = -spaceCount; + if (i>0) + lastPos = countLeadingWhitespaceChars(s); while (true) { int insertPos = s.indexOf(USER_CODE_IN_REPL_POS_BEGIN); if (insertPos < 0) // no %INSERT% macro in this line now @@ -3611,13 +3611,7 @@ void Editor::popUserCodeInTabStops() tabStopBegin = mTabStopEnd + p->x; tabStopEnd = mTabStopEnd + p->endX; } else { - QString line = lines()->getString(caretY()-1+p->y); - int n=0; - while (n32) - break; - n++; - } + int n=countLeadingWhitespaceChars(lines()->getString(caretY()-1+p->y)); // qDebug()<x; tabStopBegin = n+p->x+1; tabStopEnd = n+p->endX+1; diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 19291498..881e6e3e 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -1147,3 +1147,14 @@ float pointToPixel(float point, float dpi) { return point * dpi / 72; } + +int countLeadingWhitespaceChars(const QString &line) +{ + int n=0; + while (n32) + break; + n++; + } + return n; +} diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index 3de98a93..c0798754 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -180,6 +180,7 @@ QByteArray readFileToByteArray(const QString& fileName); void readFileToLines(const QString& fileName, QTextCodec* codec, LineProcessFunc lineFunc); void stringsToFile(const QStringList& list, const QString& fileName); void stringToFile(const QString& str, const QString& fileName); +int countLeadingWhitespaceChars(const QString& line); void decodeKey(int combinedKey, int& key, Qt::KeyboardModifiers& modifiers); void inflateRect(QRect& rect, int delta);