forked from qt-creator/qt-creator
Make it work for both QTextEdit and QPlainTextEdit.
Scrolling is broken for plain QTextEdit.
This commit is contained in:
@@ -50,13 +50,13 @@
|
|||||||
using namespace FakeVim::Internal;
|
using namespace FakeVim::Internal;
|
||||||
|
|
||||||
#define StartOfLine QTextCursor::StartOfLine
|
#define StartOfLine QTextCursor::StartOfLine
|
||||||
#define EndOfLine QTextCursor::EndOfLine
|
#define EndOfLine QTextCursor::EndOfLine
|
||||||
#define MoveAnchor QTextCursor::MoveAnchor
|
#define MoveAnchor QTextCursor::MoveAnchor
|
||||||
#define KeepAnchor QTextCursor::KeepAnchor
|
#define KeepAnchor QTextCursor::KeepAnchor
|
||||||
#define Up QTextCursor::Up
|
#define Up QTextCursor::Up
|
||||||
#define Down QTextCursor::Down
|
#define Down QTextCursor::Down
|
||||||
#define Right QTextCursor::Right
|
#define Right QTextCursor::Right
|
||||||
#define Left QTextCursor::Left
|
#define Left QTextCursor::Left
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
@@ -65,6 +65,8 @@ using namespace FakeVim::Internal;
|
|||||||
//
|
//
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#define EDITOR(s) (m_textedit ? m_textedit->s : m_plaintextedit->s)
|
||||||
|
|
||||||
const int ParagraphSeparator = 0x00002029;
|
const int ParagraphSeparator = 0x00002029;
|
||||||
|
|
||||||
using namespace Qt;
|
using namespace Qt;
|
||||||
@@ -138,7 +140,8 @@ public:
|
|||||||
SubSubMode m_subsubmode;
|
SubSubMode m_subsubmode;
|
||||||
int m_subsubdata;
|
int m_subsubdata;
|
||||||
QString m_input;
|
QString m_input;
|
||||||
QPlainTextEdit *m_editor;
|
QTextEdit *m_textedit;
|
||||||
|
QPlainTextEdit *m_plaintextedit;
|
||||||
QTextCursor m_tc;
|
QTextCursor m_tc;
|
||||||
QHash<int, QString> m_registers;
|
QHash<int, QString> m_registers;
|
||||||
int m_register;
|
int m_register;
|
||||||
@@ -190,11 +193,13 @@ bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
|
|||||||
//qDebug() << "KEY: " << key << Qt::ShiftModifier;
|
//qDebug() << "KEY: " << key << Qt::ShiftModifier;
|
||||||
|
|
||||||
// Fake "End of line"
|
// Fake "End of line"
|
||||||
m_editor = qobject_cast<QPlainTextEdit *>(ob);
|
m_textedit = qobject_cast<QTextEdit *>(ob);
|
||||||
if (!m_editor)
|
m_plaintextedit = qobject_cast<QPlainTextEdit *>(ob);
|
||||||
|
if (!m_textedit && !m_plaintextedit)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_tc = m_editor->textCursor();
|
m_tc = EDITOR(textCursor());
|
||||||
|
|
||||||
if (m_fakeEnd) {
|
if (m_fakeEnd) {
|
||||||
//m_fakeEnd = false;
|
//m_fakeEnd = false;
|
||||||
m_tc.movePosition(Right, MoveAnchor, 1);
|
m_tc.movePosition(Right, MoveAnchor, 1);
|
||||||
@@ -224,8 +229,8 @@ bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
|
|||||||
// << " BLOCK LEN: " << m_tc.block().length()
|
// << " BLOCK LEN: " << m_tc.block().length()
|
||||||
// << " LEFT: " << leftDist() << " RIGHT: " << rightDist();
|
// << " LEFT: " << leftDist() << " RIGHT: " << rightDist();
|
||||||
|
|
||||||
m_editor->setTextCursor(m_tc);
|
EDITOR(setTextCursor(m_tc));
|
||||||
m_editor->ensureCursorVisible();
|
EDITOR(ensureCursorVisible());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +383,7 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
|
|||||||
m_tc.movePosition(Left, KeepAnchor, n);
|
m_tc.movePosition(Left, KeepAnchor, n);
|
||||||
finishMovement();
|
finishMovement();
|
||||||
} else if (key == 'H') {
|
} else if (key == 'H') {
|
||||||
m_tc = m_editor->cursorForPosition(QPoint(0, 0));
|
m_tc = EDITOR(cursorForPosition(QPoint(0, 0)));
|
||||||
m_tc.movePosition(Down, KeepAnchor, qMax(count() - 1, 0));
|
m_tc.movePosition(Down, KeepAnchor, qMax(count() - 1, 0));
|
||||||
moveToFirstNonBlankOnLine();
|
moveToFirstNonBlankOnLine();
|
||||||
finishMovement();
|
finishMovement();
|
||||||
@@ -394,14 +399,14 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
|
|||||||
m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()));
|
m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()));
|
||||||
finishMovement();
|
finishMovement();
|
||||||
} else if (key == 'L') {
|
} else if (key == 'L') {
|
||||||
int heigth = m_editor->height();
|
int heigth = EDITOR(height());
|
||||||
m_tc = m_editor->cursorForPosition(QPoint(0, heigth));
|
m_tc = EDITOR(cursorForPosition(QPoint(0, heigth)));
|
||||||
m_tc.movePosition(Up, KeepAnchor, qMax(count(), 1));
|
m_tc.movePosition(Up, KeepAnchor, qMax(count(), 1));
|
||||||
moveToFirstNonBlankOnLine();
|
moveToFirstNonBlankOnLine();
|
||||||
finishMovement();
|
finishMovement();
|
||||||
} else if (key == 'M') {
|
} else if (key == 'M') {
|
||||||
int heigth = m_editor->height();
|
int heigth = EDITOR(height());
|
||||||
m_tc = m_editor->cursorForPosition(QPoint(0, heigth / 2));
|
m_tc = EDITOR(cursorForPosition(QPoint(0, heigth / 2)));
|
||||||
moveToFirstNonBlankOnLine();
|
moveToFirstNonBlankOnLine();
|
||||||
finishMovement();
|
finishMovement();
|
||||||
} else if (key == 'n') {
|
} else if (key == 'n') {
|
||||||
@@ -422,12 +427,12 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
|
|||||||
m_tc.movePosition(Left);
|
m_tc.movePosition(Left);
|
||||||
}
|
}
|
||||||
} else if (key == control('r')) {
|
} else if (key == control('r')) {
|
||||||
m_editor->redo();
|
EDITOR(redo());
|
||||||
} else if (key == 't' || key == 'T') {
|
} else if (key == 't' || key == 'T') {
|
||||||
m_subsubmode = FtSubSubMode;
|
m_subsubmode = FtSubSubMode;
|
||||||
m_subsubdata = key;
|
m_subsubdata = key;
|
||||||
} else if (key == 'u') {
|
} else if (key == 'u') {
|
||||||
m_editor->undo();
|
EDITOR(undo());
|
||||||
} else if (key == 'w') {
|
} else if (key == 'w') {
|
||||||
moveToNextWord(false);
|
moveToNextWord(false);
|
||||||
finishMovement();
|
finishMovement();
|
||||||
@@ -524,7 +529,10 @@ void FakeVimHandler::Private::handleExMode(int key, const QString &text)
|
|||||||
m_tc.setPosition(m_tc.block().document()
|
m_tc.setPosition(m_tc.block().document()
|
||||||
->findBlockByNumber(n - 1).position());
|
->findBlockByNumber(n - 1).position());
|
||||||
} else if (m_commandBuffer == "q!" || m_commandBuffer == "q") {
|
} else if (m_commandBuffer == "q!" || m_commandBuffer == "q") {
|
||||||
q->quitRequested(m_editor);
|
if (m_textedit)
|
||||||
|
q->quitRequested(m_textedit);
|
||||||
|
else
|
||||||
|
q->quitRequested(m_plaintextedit);
|
||||||
}
|
}
|
||||||
m_commandBuffer.clear();
|
m_commandBuffer.clear();
|
||||||
m_mode = CommandMode;
|
m_mode = CommandMode;
|
||||||
@@ -562,18 +570,18 @@ void FakeVimHandler::Private::search(const QString &needle, bool forward)
|
|||||||
if (forward)
|
if (forward)
|
||||||
m_tc.movePosition(Right, MoveAnchor, 1);
|
m_tc.movePosition(Right, MoveAnchor, 1);
|
||||||
|
|
||||||
m_editor->setTextCursor(m_tc);
|
EDITOR(setTextCursor(m_tc));
|
||||||
if (m_editor->find(needle, flags)) {
|
if (EDITOR(find(needle, flags))) {
|
||||||
m_tc = m_editor->textCursor();
|
m_tc = EDITOR(textCursor());
|
||||||
// the qMax seems to be needed for QPlainTextEdit only
|
// the qMax seems to be needed for QPlainTextEdit only
|
||||||
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
|
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_tc.setPosition(forward ? 0 : lastPositionInDocument() - 1);
|
m_tc.setPosition(forward ? 0 : lastPositionInDocument() - 1);
|
||||||
m_editor->setTextCursor(m_tc);
|
EDITOR(setTextCursor(m_tc));
|
||||||
if (m_editor->find(needle, flags)) {
|
if (EDITOR(find(needle, flags))) {
|
||||||
m_tc = m_editor->textCursor();
|
m_tc = EDITOR(textCursor());
|
||||||
// the qMax seems to be needed for QPlainTextEdit only
|
// the qMax seems to be needed for QPlainTextEdit only
|
||||||
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
|
m_tc.movePosition(Left, MoveAnchor, qMax(1, needle.size() - 1));
|
||||||
if (forward)
|
if (forward)
|
||||||
@@ -695,15 +703,15 @@ void FakeVimHandler::Private::moveToNextWord(bool simple)
|
|||||||
|
|
||||||
int FakeVimHandler::Private::cursorLineOnScreen() const
|
int FakeVimHandler::Private::cursorLineOnScreen() const
|
||||||
{
|
{
|
||||||
QRect rect = m_editor->cursorRect();
|
QRect rect = EDITOR(cursorRect());
|
||||||
return rect.y() / rect.height();
|
return rect.y() / rect.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
int FakeVimHandler::Private::linesOnScreen() const
|
int FakeVimHandler::Private::linesOnScreen() const
|
||||||
{
|
{
|
||||||
QRect rect = m_editor->cursorRect();
|
QRect rect = EDITOR(cursorRect());
|
||||||
//qDebug() << m_editor->height() / rect.height();
|
//qDebug() << EDITOR(height()) / rect.height();
|
||||||
return m_editor->height() / rect.height();
|
return EDITOR(height()) / rect.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
int FakeVimHandler::Private::cursorLineInDocument() const
|
int FakeVimHandler::Private::cursorLineInDocument() const
|
||||||
@@ -715,7 +723,7 @@ int FakeVimHandler::Private::cursorLineInDocument() const
|
|||||||
void FakeVimHandler::Private::scrollToLineInDocument(int line)
|
void FakeVimHandler::Private::scrollToLineInDocument(int line)
|
||||||
{
|
{
|
||||||
// FIXME: works only for QPlainTextEdit
|
// FIXME: works only for QPlainTextEdit
|
||||||
QScrollBar *scrollBar = m_editor->verticalScrollBar();
|
QScrollBar *scrollBar = EDITOR(verticalScrollBar());
|
||||||
scrollBar->setValue(line);
|
scrollBar->setValue(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user