diff --git a/src/document/katedocument.h b/src/document/katedocument.h --- a/src/document/katedocument.h +++ b/src/document/katedocument.h @@ -1385,11 +1385,11 @@ /** * current autobrace range */ - QSharedPointer m_currentAutobraceRange; + QStack> m_currentAutobraceRange; /** * current autobrace closing charater (e.g. ']') */ - QChar m_currentAutobraceClosingChar; + QStack m_currentAutobraceClosingChar; private Q_SLOTS: void checkCursorForAutobrace(KTextEditor::View* view, const KTextEditor::Cursor& newPos); diff --git a/src/document/katedocument.cpp b/src/document/katedocument.cpp --- a/src/document/katedocument.cpp +++ b/src/document/katedocument.cpp @@ -2938,11 +2938,25 @@ /** * closing bracket for the autobracket we inserted earlier? */ - if ( m_currentAutobraceClosingChar == chars[0] && m_currentAutobraceRange ) { - // do nothing - m_currentAutobraceRange.reset(nullptr); - view->cursorRight(); - return true; + if ( m_currentAutobraceClosingChar.size() > 0 && m_currentAutobraceClosingChar.top() == chars[0] + && m_currentAutobraceRange.size() > 0) { + + // pop this closing char off the top of the stack + QChar top = m_currentAutobraceClosingChar.pop(); + m_currentAutobraceRange.pop(); + + // If the close bracket was eaten (m_currentAutobraceClosingChar == + // char at cursor position), then move the cursor right to skip over + // the auto-generated closing bracket without inserting another one. + // However, if the user inserted a new closing bracket, don't mess + // with the cursor position, and let it be typed as normal + if(plainKateTextLine(m_activeView->cursorPosition().line()).data() + ->at(m_activeView->cursorPosition().column()) == top) { + view->cursorRight(); + return true; + } else { + return false; + } } } @@ -3044,15 +3058,15 @@ insertText(view->cursorPosition(), QString(closingBracket)); const auto insertedAt(view->cursorPosition()); view->setCursorPosition(cursorPos); - m_currentAutobraceRange.reset(newMovingRange({cursorPos - Cursor{0, 1}, insertedAt}, - KTextEditor::MovingRange::DoNotExpand)); + m_currentAutobraceRange.push(QSharedPointer( + newMovingRange({cursorPos - Cursor{0, 1}, insertedAt}, KTextEditor::MovingRange::DoNotExpand))); connect(view, &View::cursorPositionChanged, this, &DocumentPrivate::checkCursorForAutobrace, Qt::UniqueConnection); // add bracket to chars inserted! needed for correct signals + indent chars.append(closingBracket); } - m_currentAutobraceClosingChar = closingBracket; + m_currentAutobraceClosingChar.push(closingBracket); } // end edit session here, to have updated HL in userTypedChar! @@ -3075,8 +3089,8 @@ } void KTextEditor::DocumentPrivate::checkCursorForAutobrace(KTextEditor::View*, const KTextEditor::Cursor& newPos) { - if ( m_currentAutobraceRange && ! m_currentAutobraceRange->toRange().contains(newPos) ) { - m_currentAutobraceRange.clear(); + while ( m_currentAutobraceRange.size() > 0 && ! m_currentAutobraceRange.top()->toRange().contains(newPos) ) { + m_currentAutobraceRange.pop(); } } @@ -3233,12 +3247,14 @@ } } } - if ( m_currentAutobraceRange ) { - const auto r = m_currentAutobraceRange->toRange(); + while ( m_currentAutobraceRange.size() > 0 ) { + const auto r = m_currentAutobraceRange.top()->toRange(); if ( r.columnWidth() == 1 && view->cursorPosition() == r.start() ) { // start parenthesis removed and range length is 1, remove end as well del(view, view->cursorPosition()); - m_currentAutobraceRange.clear(); + m_currentAutobraceRange.pop(); + } else { + break; } } }