fakevim: implement indenting a block and autoindent

This commit is contained in:
Martin Aumueller
2009-02-03 10:39:57 +01:00
parent c446f9efc6
commit f2940adb4d
3 changed files with 52 additions and 3 deletions

View File

@@ -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())) {