Commit f2940adb authored by Martin Aumueller's avatar Martin Aumueller
Browse files

fakevim: implement indenting a block and autoindent

parent c446f9ef
......@@ -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)
......
......@@ -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);
......
......@@ -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())) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment