From 23a9971ad2aa336043ed24ab1c6996221ec6d626 Mon Sep 17 00:00:00 2001 From: Martin Aumueller <aumuell@reserv.at> Date: Wed, 28 Jan 2009 22:03:51 +0100 Subject: [PATCH] fakevim: implement paren matching w/o making FakeVimHandler depend on Qt Creator's TextEditor --- src/plugins/fakevim/fakevimhandler.cpp | 10 +++++++ src/plugins/fakevim/fakevimhandler.h | 1 + src/plugins/fakevim/fakevimplugin.cpp | 39 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index 57f212ea7e7..6c179cf902b 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -1846,6 +1846,16 @@ void FakeVimHandler::Private::moveToNextWord(bool simple) void FakeVimHandler::Private::moveToMatchingParanthesis() { + bool moved = false; + bool forward = false; + + emit q->moveToMatchingParenthesis(&moved, &forward, &m_tc); + + if (moved && forward) { + if (m_submode == NoSubMode || m_submode == ZSubMode || m_submode == RegisterSubMode) + m_tc.movePosition(Left, KeepAnchor, 1); + } + #if 0 // FIXME: remove TextEditor dependency bool undoFakeEOL = false; diff --git a/src/plugins/fakevim/fakevimhandler.h b/src/plugins/fakevim/fakevimhandler.h index a083f346dd4..c308c3cb94e 100644 --- a/src/plugins/fakevim/fakevimhandler.h +++ b/src/plugins/fakevim/fakevimhandler.h @@ -79,6 +79,7 @@ signals: void selectionChanged(const QList<QTextEdit::ExtraSelection> &selection); void writeFileRequested(bool *handled, const QString &fileName, const QString &contents); + void moveToMatchingParenthesis(bool *moved, bool *forward, QTextCursor *cursor); private: bool eventFilter(QObject *ob, QEvent *ev); diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index 50d45ff8769..c8b2898d88e 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -122,6 +122,7 @@ private slots: void showExtraInformation(const QString &msg); 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); private: FakeVimPlugin *q; @@ -203,6 +204,8 @@ void FakeVimPluginPrivate::installHandler(Core::IEditor *editor) this, SLOT(writeFile(bool*,QString,QString))); connect(handler, SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)), this, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>))); + connect(handler, SIGNAL(moveToMatchingParenthesis(bool*,bool*,QTextCursor*)), + this, SLOT(moveToMatchingParenthesis(bool*,bool*,QTextCursor*))); handler->setupWidget(); handler->setExtraData(editor); @@ -250,6 +253,42 @@ void FakeVimPluginPrivate::writeFile(bool *handled, } } +void FakeVimPluginPrivate::moveToMatchingParenthesis(bool *moved, bool *forward, + QTextCursor *cursor) +{ + *moved = false; + + bool undoFakeEOL = false; + if (cursor->atBlockEnd() && cursor->block().length() > 1) { + cursor->movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1); + undoFakeEOL = true; + } + TextEditor::TextBlockUserData::MatchType match + = TextEditor::TextBlockUserData::matchCursorForward(cursor); + if (match == TextEditor::TextBlockUserData::Match) { + *moved = true; + *forward = true; + } else { + if (undoFakeEOL) + cursor->movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1); + if (match == TextEditor::TextBlockUserData::NoMatch) { + // backward matching is according to the character before the cursor + bool undoMove = false; + if (!cursor->atBlockEnd()) { + cursor->movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1); + undoMove = true; + } + match = TextEditor::TextBlockUserData::matchCursorBackward(cursor); + if (match == TextEditor::TextBlockUserData::Match) { + *moved = true; + *forward = false; + } else if (undoMove) { + cursor->movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1); + } + } + } +} + void FakeVimPluginPrivate::removeHandler() { if (FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender())) { -- GitLab