diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index 799e83f42aa63a0505af2a37abab87ebdf0e7bcb..af32068b83314e58965db1efda5e720c9ac63f38 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -630,7 +630,7 @@ public: // Helper functions for indenting/ bool isElectricCharacter(QChar c) const; void indentSelectedText(QChar lastTyped = QChar()); - int indentText(const Range &range, QChar lastTyped = QChar()); + void indentText(const Range &range, QChar lastTyped = QChar()); void shiftRegionLeft(int repeat = 1); void shiftRegionRight(int repeat = 1); @@ -3540,20 +3540,17 @@ void FakeVimHandler::Private::indentSelectedText(QChar typedChar) setDotCommand("%1==", endLine - beginLine + 1); } -int FakeVimHandler::Private::indentText(const Range &range, QChar typedChar) +void FakeVimHandler::Private::indentText(const Range &range, QChar typedChar) { int beginLine = lineForPosition(range.beginPos); int endLine = lineForPosition(range.endPos); if (beginLine > endLine) qSwap(beginLine, endLine); - int amount = 0; - // lineForPosition has returned 1-based line numbers - emit q->indentRegion(&amount, beginLine - 1, endLine - 1, typedChar); - fixMarks(firstPositionInLine(beginLine), amount); + // LineForPosition has returned 1-based line numbers. + emit q->indentRegion(beginLine - 1, endLine - 1, typedChar); if (beginLine != endLine) showBlackMessage("MARKS ARE OFF NOW"); - return amount; } bool FakeVimHandler::Private::isElectricCharacter(QChar c) const @@ -4432,7 +4429,9 @@ void FakeVimHandler::Private::insertAutomaticIndentation(bool goingDown) if (hasConfig(ConfigSmartIndent)) { Range range(m_tc.block().position(), m_tc.block().position()); - m_justAutoIndented = indentText(range, QLatin1Char('\n')); + const int oldSize = m_tc.block().text().size(); + indentText(range, QLatin1Char('\n')); + m_justAutoIndented = m_tc.block().text().size() - oldSize; } else { QTextBlock block = goingDown ? m_tc.block().previous() : m_tc.block().next(); QString text = block.text(); @@ -4702,6 +4701,10 @@ QString FakeVimHandler::tabExpand(int n) const return d->tabExpand(n); } +void FakeVimHandler::fixMarks(int positionAction, int positionChange) +{ + d->fixMarks(positionAction, positionChange); +} } // namespace Internal } // namespace FakeVim diff --git a/src/plugins/fakevim/fakevimhandler.h b/src/plugins/fakevim/fakevimhandler.h index 4e877d31c8ec3e42602207be0e03cf0cc1b0f418..f5b0db1c040d9dcf7ce4d3f8e75c26803d25e06d 100644 --- a/src/plugins/fakevim/fakevimhandler.h +++ b/src/plugins/fakevim/fakevimhandler.h @@ -104,6 +104,7 @@ public slots: int physicalIndentation(const QString &line) const; int logicalIndentation(const QString &line) const; QString tabExpand(int n) const; + void fixMarks(int positionAction, int positionChange); signals: void commandBufferChanged(const QString &msg); @@ -113,7 +114,7 @@ signals: void writeAllRequested(QString *error); void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor); void checkForElectricCharacter(bool *result, QChar c); - void indentRegion(int *amount, int beginLine, int endLine, QChar typedChar); + void indentRegion(int beginLine, int endLine, QChar typedChar); void completionRequested(); void windowCommandRequested(int key); void findRequested(bool reverse); diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index 5c66cec0786f3ffa9532f78fc58d50129cc133df..35d87ae71e8f6a591b9aea75739a0fd1a900311e 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -70,8 +70,6 @@ #include <cpptools/cpptoolsconstants.h> -#include <indenter.h> - #include <QtCore/QDebug> #include <QtCore/QFile> #include <QtCore/QtPlugin> @@ -508,7 +506,7 @@ private slots: void changeSelection(const QList<QTextEdit::ExtraSelection> &selections); void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor); void checkForElectricCharacter(bool *result, QChar c); - void indentRegion(int *amount, int beginLine, int endLine, QChar typedChar); + void indentRegion(int beginLine, int endLine, QChar typedChar); void handleExCommand(bool *handled, const ExCommand &cmd); void handleDelayedQuitAll(bool forced); @@ -837,8 +835,8 @@ void FakeVimPluginPrivate::editorOpened(Core::IEditor *editor) this, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>))); connect(handler, SIGNAL(moveToMatchingParenthesis(bool*,bool*,QTextCursor*)), this, SLOT(moveToMatchingParenthesis(bool*,bool*,QTextCursor*))); - connect(handler, SIGNAL(indentRegion(int*,int,int,QChar)), - this, SLOT(indentRegion(int*,int,int,QChar))); + connect(handler, SIGNAL(indentRegion(int,int,QChar)), + this, SLOT(indentRegion(int,int,QChar))); connect(handler, SIGNAL(checkForElectricCharacter(bool*,QChar)), this, SLOT(checkForElectricCharacter(bool*,QChar))); connect(handler, SIGNAL(completionRequested()), @@ -1047,7 +1045,7 @@ void FakeVimPluginPrivate::moveToMatchingParenthesis(bool *moved, bool *forward, } } -void FakeVimPluginPrivate::indentRegion(int *amount, int beginLine, int endLine, +void FakeVimPluginPrivate::indentRegion(int beginLine, int endLine, QChar typedChar) { FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender()); @@ -1058,33 +1056,43 @@ void FakeVimPluginPrivate::indentRegion(int *amount, int beginLine, int endLine, if (!bt) return; - TextEditor::TabSettings tabSettings; + const TextEditor::TabSettings oldTabSettings = bt->tabSettings(); + + TabSettings tabSettings; tabSettings.m_indentSize = theFakeVimSetting(ConfigShiftWidth)->value().toInt(); tabSettings.m_tabSize = theFakeVimSetting(ConfigTabStop)->value().toInt(); tabSettings.m_spacesForTabs = theFakeVimSetting(ConfigExpandTab)->value().toBool(); - typedef SharedTools::Indenter<TextEditor::TextBlockIterator> Indenter; - Indenter &indenter = Indenter::instance(); - indenter.setIndentSize(tabSettings.m_indentSize); - indenter.setTabSize(tabSettings.m_tabSize); - - const QTextDocument *doc = bt->document(); - const TextEditor::TextBlockIterator docStart(doc->begin()); - QTextBlock cur = doc->findBlockByNumber(beginLine); + bt->setTabSettings(tabSettings); + + QTextDocument *doc = bt->document(); + QTextBlock startBlock = doc->findBlockByNumber(beginLine); + + // Record line lenghts for mark adjustments + QVector<int> lineLengths(endLine - beginLine + 1); + QTextBlock block = startBlock; + for (int i = beginLine; i <= endLine; ++i) { - if (typedChar == 0 && cur.text().simplified().isEmpty()) { + lineLengths[i - beginLine] = block.text().length(); + if (typedChar == 0 && block.text().simplified().isEmpty()) { // clear empty lines - *amount = 0; - QTextCursor cursor(cur); + QTextCursor cursor(block); while (!cursor.atBlockEnd()) cursor.deleteChar(); } else { - const TextEditor::TextBlockIterator current(cur); - const TextEditor::TextBlockIterator next(cur.next()); - *amount = indenter.indentForBottomLine(current, docStart, next, typedChar); - tabSettings.indentLine(cur, *amount); + bt->indentBlock(doc, block, typedChar); } - cur = cur.next(); + block = block.next(); } + + // Adjust marks. + block = startBlock; + for (int i = beginLine; i <= endLine; ++i) { + const int amount = block.text().length() - lineLengths[i - beginLine]; + handler->fixMarks(block.position(), amount); + block = block.next(); + } + + bt->setTabSettings(oldTabSettings); } void FakeVimPluginPrivate::quitFakeVim()