diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index 0615f7318a18b40e72b46b6d712b9ce8a434145d..7b477c48176b41a25bc0df8c43bb2ba543e4aef5 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -40,8 +40,6 @@ // Qt Creator. The idea is to keep this file here in a "clean" state that // allows easy reuse with any QTextEdit or QPlainTextEdit derived class. -//#include <indenter.h> - #include <QtCore/QDebug> #include <QtCore/QFile> #include <QtCore/QObject> @@ -1718,11 +1716,15 @@ int FakeVimHandler::Private::indentDist() const const TextEditor::TextBlockIterator end(m_tc.block().next()); return indenter.indentForBottomLine(current, begin, end, QChar(' ')); #endif - return 0; + int amount = 0; + emit q->indentRegion(&amount, m_tc.block(), m_tc.block(), QChar(' ')); + return amount; } void FakeVimHandler::Private::indentRegion(QTextBlock begin, QTextBlock end, QChar typedChar) { + int amount = 0; + emit q->indentRegion(&amount, begin, end, typedChar); #if 0 // FIXME: Make independent of TextEditor if (!m_texteditor) diff --git a/src/plugins/fakevim/fakevimhandler.h b/src/plugins/fakevim/fakevimhandler.h index c308c3cb94e05c5d313655b902a27a7fd7936439..f5b4101996cbb01efbb73612b84ea44c4a24d883 100644 --- a/src/plugins/fakevim/fakevimhandler.h +++ b/src/plugins/fakevim/fakevimhandler.h @@ -36,6 +36,7 @@ #include <QtCore/QObject> #include <QtGui/QTextEdit> +#include <QtGui/QTextBlock> QT_BEGIN_NAMESPACE class QString; @@ -80,6 +81,7 @@ signals: void writeFileRequested(bool *handled, const QString &fileName, const QString &contents); void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor); + void indentRegion(int *amount, QTextBlock begin, QTextBlock end, QChar typedChar); private: bool eventFilter(QObject *ob, QEvent *ev); diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index c8b2898d88e392c8fb556001f78e98050c8b2c1d..fe60eaa2de6d2d5361ffe441eed3c02bc2ddd387 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -56,9 +56,12 @@ #include <texteditor/interactionsettings.h> #include <texteditor/tabsettings.h> #include <texteditor/texteditorsettings.h> +#include <texteditor/textblockiterator.h> #include <utils/qtcassert.h> +#include <indenter.h> + #include <QtCore/QDebug> #include <QtCore/QtPlugin> #include <QtCore/QObject> @@ -123,6 +126,7 @@ private slots: void changeSelection(const QList<QTextEdit::ExtraSelection> &selections); void writeFile(bool *handled, const QString &fileName, const QString &contents); void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor); + void indentRegion(int *amount, QTextBlock begin, QTextBlock end, QChar typedChar); private: FakeVimPlugin *q; @@ -206,6 +210,8 @@ void FakeVimPluginPrivate::installHandler(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*,QTextBlock,QTextBlock,QChar)), + this, SLOT(indentRegion(int*,QTextBlock,QTextBlock,QChar))); handler->setupWidget(); handler->setExtraData(editor); @@ -289,6 +295,45 @@ void FakeVimPluginPrivate::moveToMatchingParenthesis(bool *moved, bool *forward, } } +void FakeVimPluginPrivate::indentRegion(int *amount, QTextBlock begin, QTextBlock end, + QChar typedChar) +{ + FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender()); + if (!handler) + return; + + BaseTextEditor *bt = qobject_cast<BaseTextEditor *>(handler->widget()); + if (!bt) + return; + + typedef SharedTools::Indenter<TextEditor::TextBlockIterator> Indenter; + Indenter &indenter = Indenter::instance(); + indenter.setIndentSize(bt->tabSettings().m_indentSize); + indenter.setTabSize(bt->tabSettings().m_tabSize); + + const QTextDocument *doc = begin.document(); + const TextEditor::TextBlockIterator docStart(doc->begin()); + QTextBlock cur = begin; + do { + if (typedChar == 0 && cur.text().simplified().isEmpty()) { + *amount = 0; + if (cur != end) { + QTextCursor cursor(cur); + while (!cursor.atBlockEnd()) + cursor.deleteChar(); + } + } else { + const TextEditor::TextBlockIterator current(cur); + const TextEditor::TextBlockIterator next(cur.next()); + *amount = indenter.indentForBottomLine(current, docStart, next, typedChar); + if (cur != end) + bt->tabSettings().indentLine(cur, *amount); + } + if (cur != end) + cur = cur.next(); + } while (cur != end); +} + void FakeVimPluginPrivate::removeHandler() { if (FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender())) {