From ff4e20ae18474155c7f1f0207d1b11419e490aac Mon Sep 17 00:00:00 2001 From: royqh1979 Date: Wed, 1 Sep 2021 17:34:39 +0800 Subject: [PATCH] - fix: indent error when overwrite '}' --- RedPandaIDE/editor.cpp | 5 +++-- RedPandaIDE/qsynedit/SynEdit.cpp | 27 ++++++++++++++++----------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index eac1232d..3d051752 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -1361,9 +1361,10 @@ bool Editor::handleBraceSkip() return false; BufferCoord pos = getMatchingBracket(); if (pos.Line != 0) { - setBlockBegin(caretXY()); - setBlockEnd(BufferCoord{caretX() + 1, caretY()}); + bool oldInsertMode = insertMode(); + setInsertMode(false); //set mode to overwrite CommandProcessor(SynEditorCommand::ecChar,'}'); + setInsertMode(oldInsertMode); // setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over return true; } diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 69941a35..eb90c077 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -2292,6 +2292,9 @@ void SynEdit::doAddChar(QChar AChar) if (!AChar.isPrint()) return; //DoOnPaintTransient(ttBefore); + //mCaretX will change after setSelLength; + int oldCaretX=mCaretX; + int oldCaretY=mCaretY; if ((mInserting == false) && (!selAvail())) { setSelLength(1); } @@ -2299,35 +2302,37 @@ void SynEdit::doAddChar(QChar AChar) mUndoList->BeginBlock(); if (mOptions.testFlag(eoAddIndent)) { // Remove TabWidth of indent of the current line when typing a } - if (AChar == '}' && (mCaretY<=mLines->count())) { - QString temp = mLines->getString(mCaretY-1).mid(0,mCaretX-1); + if (AChar == '}' && (oldCaretY<=mLines->count())) { + QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); // and the first nonblank char is this new } if (temp.trimmed().isEmpty()) { - BufferCoord MatchBracketPos = GetPreviousLeftBracket(mCaretX, mCaretY); + BufferCoord MatchBracketPos = GetPreviousLeftBracket(oldCaretX, oldCaretY); if (MatchBracketPos.Line > 0) { int i = 0; QString matchline = mLines->getString(MatchBracketPos.Line-1); - QString line = lineText(); + QString line = mLines->getString(oldCaretY-1); while (iputString(mCaretY-1,temp); - internalSetCaretXY(BufferCoord{i,mCaretY}); + QString temp = matchline.mid(0,i) + line.mid(oldCaretX-1); + mLines->putString(oldCaretY-1,temp); + internalSetCaretXY(BufferCoord{i+1,oldCaretY}); mUndoList->AddChange( SynChangeReason::crDelete, - BufferCoord{1, mCaretY}, - BufferCoord{line.length()+1, mCaretY}, + BufferCoord{1, oldCaretY}, + BufferCoord{line.length()+1, oldCaretY}, line, SynSelectionMode::smNormal ); mUndoList->AddChange( SynChangeReason::crInsert, - BufferCoord{1, mCaretY}, - BufferCoord{temp.length()+1, mCaretY}, + BufferCoord{1, oldCaretY}, + BufferCoord{temp.length()+1, oldCaretY}, "", SynSelectionMode::smNormal ); + mLines->getString(mCaretY-1); + return; } } }