fakevim: Fix crash when adding text to last line

Workaround for crash when in edit block and adding text to the last
empty line.

Change-Id: I46b84074bd6f6fbcae698671716e151ebab98f5b
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Lukas Holecek
2012-11-01 20:43:20 +01:00
committed by hjk
parent e3e8cc4386
commit afc8cd6932

View File

@@ -981,7 +981,14 @@ public:
setCursor(tc); setCursor(tc);
} }
QTextCursor cursor() const { return EDITOR(textCursor()); } QTextCursor cursor() const { return EDITOR(textCursor()); }
void setCursor(const QTextCursor &tc) { EDITOR(setTextCursor(tc)); } void setCursor(const QTextCursor &tc) {
// Workaround for crash when in edit block and adding text to last empty line.
if (m_editBlockLevel > 0)
cursor().endEditBlock();
EDITOR(setTextCursor(tc));
if (m_editBlockLevel > 0)
cursor().joinPreviousEditBlock();
}
bool handleFfTt(QString key); bool handleFfTt(QString key);
@@ -1002,12 +1009,29 @@ public:
QTextDocument *document() const { return EDITOR(document()); } QTextDocument *document() const { return EDITOR(document()); }
QChar characterAtCursor() const QChar characterAtCursor() const
{ return document()->characterAt(position()); } { return document()->characterAt(position()); }
int m_editBlockLevel; // current level of edit blocks
void beginEditBlock() void beginEditBlock()
{ UNDO_DEBUG("BEGIN EDIT BLOCK"); cursor().beginEditBlock(); } {
UNDO_DEBUG("BEGIN EDIT BLOCK");
++m_editBlockLevel;
cursor().beginEditBlock();
}
void endEditBlock() void endEditBlock()
{ UNDO_DEBUG("END EDIT BLOCK"); cursor().endEditBlock(); } {
UNDO_DEBUG("END EDIT BLOCK");
QTC_ASSERT(m_editBlockLevel > 0,
qDebug() << "beginEditBlock() not called before endEditBlock()!"; return);
--m_editBlockLevel;
cursor().endEditBlock();
}
void joinPreviousEditBlock() void joinPreviousEditBlock()
{ UNDO_DEBUG("JOIN"); cursor().joinPreviousEditBlock(); } {
UNDO_DEBUG("JOIN");
++m_editBlockLevel;
cursor().joinPreviousEditBlock();
}
void breakEditBlock() { void breakEditBlock() {
QTextCursor tc = cursor(); QTextCursor tc = cursor();
tc.clearSelection(); tc.clearSelection();
@@ -1281,6 +1305,7 @@ void FakeVimHandler::Private::init()
m_oldExternalPosition = -1; m_oldExternalPosition = -1;
m_oldPosition = -1; m_oldPosition = -1;
m_lastChangePosition = -1; m_lastChangePosition = -1;
m_editBlockLevel = 0;
setupCharClass(); setupCharClass();
} }
@@ -3205,7 +3230,7 @@ EventResult FakeVimHandler::Private::handleInsertMode(const Input &input)
QTextCursor tc = EDITOR(textCursor()); QTextCursor tc = EDITOR(textCursor());
moveToWordBoundary(false, false); moveToWordBoundary(false, false);
QString str = selectText(Range(position(), tc.position())); QString str = selectText(Range(position(), tc.position()));
EDITOR(setTextCursor(tc)); setCursor(tc);
emit q->simpleCompletionRequested(str, input.isControl('n')); emit q->simpleCompletionRequested(str, input.isControl('n'));
} else if (!input.text().isEmpty()) { } else if (!input.text().isEmpty()) {
insertInInsertMode(input.text()); insertInInsertMode(input.text());