Commit 2f1e09cc authored by hjk's avatar hjk

progress on homegrown undo/redo

parent b46d8faf
...@@ -104,12 +104,24 @@ static const QString ConfigOn = "on"; ...@@ -104,12 +104,24 @@ static const QString ConfigOn = "on";
struct EditOperation struct EditOperation
{ {
enum EditType { EditInsert, EditRemove } m_type; EditOperation() : m_position(-1), m_itemCount(0) {}
int m_position; int m_position;
int m_length; int m_itemCount; // used to combine several operations
QString m_data; QString m_from;
QString m_to;
}; };
QDebug &operator<<(QDebug &ts, const EditOperation &op)
{
if (op.m_itemCount > 0) {
ts << "EDIT BLOCK WITH" << op.m_itemCount << "ITEMS";
} else {
ts << "EDIT AT " << op.m_position
<< " FROM " << op.m_from << " TO " << op.m_to;
}
return ts;
}
class FakeVimHandler::Private class FakeVimHandler::Private
{ {
public: public:
...@@ -192,6 +204,7 @@ public: ...@@ -192,6 +204,7 @@ public:
// undo handling // undo handling
void recordInsert(int position, const QString &data); void recordInsert(int position, const QString &data);
void recordRemove(int position, const QString &data);
void recordRemove(int position, int length); void recordRemove(int position, int length);
void undo(); void undo();
void redo(); void redo();
...@@ -281,6 +294,8 @@ bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev) ...@@ -281,6 +294,8 @@ bool FakeVimHandler::Private::eventFilter(QObject *ob, QEvent *ev)
void FakeVimHandler::Private::handleKey(int key, const QString &text) void FakeVimHandler::Private::handleKey(int key, const QString &text)
{ {
//qDebug() << "KEY: " << key << text << "POS: " << m_tc.position();
//qDebug() << "\nUNDO: " << m_undoStack << "\nREDO: " << m_redoStack;
if (m_mode == InsertMode) if (m_mode == InsertMode)
handleInsertMode(key, text); handleInsertMode(key, text);
else if (m_mode == CommandMode) else if (m_mode == CommandMode)
...@@ -301,6 +316,7 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommand) ...@@ -301,6 +316,7 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommand)
} else if (m_submode == DeleteSubMode) { } else if (m_submode == DeleteSubMode) {
if (!dotCommand.isEmpty()) if (!dotCommand.isEmpty())
m_dotCommand = "d" + dotCommand; m_dotCommand = "d" + dotCommand;
recordRemove(qMin(m_tc.position(), m_tc.anchor()), m_tc.selectedText());
m_registers[m_register] = m_tc.selectedText(); m_registers[m_register] = m_tc.selectedText();
m_tc.removeSelectedText(); m_tc.removeSelectedText();
m_submode = NoSubMode; m_submode = NoSubMode;
...@@ -446,11 +462,9 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -446,11 +462,9 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
} else if (key == 'a') { } else if (key == 'a') {
m_mode = InsertMode; m_mode = InsertMode;
m_lastInsertion.clear(); m_lastInsertion.clear();
///m_tc.beginEditBlock();
m_tc.movePosition(Right, MoveAnchor, 1); m_tc.movePosition(Right, MoveAnchor, 1);
} else if (key == 'A') { } else if (key == 'A') {
m_mode = InsertMode; m_mode = InsertMode;
///m_tc.beginEditBlock();
m_tc.movePosition(EndOfLine, MoveAnchor); m_tc.movePosition(EndOfLine, MoveAnchor);
m_lastInsertion.clear(); m_lastInsertion.clear();
} else if (key == 'b') { } else if (key == 'b') {
...@@ -461,10 +475,8 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -461,10 +475,8 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
finishMovement(); finishMovement();
} else if (key == 'c') { } else if (key == 'c') {
m_submode = ChangeSubMode; m_submode = ChangeSubMode;
///m_tc.beginEditBlock();
} else if (key == 'C') { } else if (key == 'C') {
m_submode = ChangeSubMode; m_submode = ChangeSubMode;
//m_tc.beginEditBlock();
m_tc.movePosition(EndOfLine, KeepAnchor); m_tc.movePosition(EndOfLine, KeepAnchor);
finishMovement(); finishMovement();
} else if (key == 'd' && m_visualLineMode) { } else if (key == 'd' && m_visualLineMode) {
...@@ -514,7 +526,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -514,7 +526,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
finishMovement(); finishMovement();
} else if (key == 'i') { } else if (key == 'i') {
m_mode = InsertMode; m_mode = InsertMode;
///m_tc.beginEditBlock();
m_lastInsertion.clear(); m_lastInsertion.clear();
} else if (key == 'I') { } else if (key == 'I') {
m_mode = InsertMode; m_mode = InsertMode;
...@@ -523,13 +534,12 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -523,13 +534,12 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
else else
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
m_tc.clearSelection(); m_tc.clearSelection();
///m_tc.beginEditBlock();
m_lastInsertion.clear(); m_lastInsertion.clear();
} else if (key == 'j' || key == Key_Down) { } else if (key == 'j' || key == Key_Down) {
m_tc.movePosition(Down, KeepAnchor, count()); m_tc.movePosition(Down, KeepAnchor, count());
finishMovement(); finishMovement();
} else if (key == 'J') { } else if (key == 'J') {
///m_tc.beginEditBlock(); EditOperation op;
if (m_submode == NoSubMode) { if (m_submode == NoSubMode) {
for (int i = qMax(count(), 2) - 1; --i >= 0; ) { for (int i = qMax(count(), 2) - 1; --i >= 0; ) {
m_tc.movePosition(EndOfLine); m_tc.movePosition(EndOfLine);
...@@ -540,7 +550,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -540,7 +550,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
if (!m_gflag) if (!m_gflag)
m_tc.movePosition(Left, MoveAnchor, 1); m_tc.movePosition(Left, MoveAnchor, 1);
} }
///m_tc.endEditBlock();
} else if (key == 'k' || key == Key_Up) { } else if (key == 'k' || key == Key_Up) {
m_tc.movePosition(Up, KeepAnchor, count()); m_tc.movePosition(Up, KeepAnchor, count());
finishMovement(); finishMovement();
...@@ -565,13 +574,11 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -565,13 +574,11 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
} else if (key == 'o') { } else if (key == 'o') {
m_mode = InsertMode; m_mode = InsertMode;
m_lastInsertion.clear(); m_lastInsertion.clear();
//m_tc.beginEditBlock(); // FIXME: unusable due to drawing errors
m_tc.movePosition(EndOfLine, MoveAnchor); m_tc.movePosition(EndOfLine, MoveAnchor);
m_tc.insertText("\n"); m_tc.insertText("\n");
} else if (key == 'O') { } else if (key == 'O') {
m_mode = InsertMode; m_mode = InsertMode;
m_lastInsertion.clear(); m_lastInsertion.clear();
//m_tc.beginEditBlock(); // FIXME: unusable due to drawing errors
m_tc.movePosition(StartOfLine, MoveAnchor); m_tc.movePosition(StartOfLine, MoveAnchor);
m_tc.movePosition(Left, MoveAnchor, 1); m_tc.movePosition(Left, MoveAnchor, 1);
m_tc.insertText("\n"); m_tc.insertText("\n");
...@@ -592,7 +599,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text) ...@@ -592,7 +599,6 @@ void FakeVimHandler::Private::handleCommandMode(int key, const QString &text)
redo(); redo();
} else if (key == 's') { } else if (key == 's') {
m_submode = ChangeSubMode; m_submode = ChangeSubMode;
//m_tc.beginEditBlock();
m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist())); m_tc.movePosition(Right, KeepAnchor, qMin(count(), rightDist()));
} else if (key == 't' || key == 'T') { } else if (key == 't' || key == 'T') {
m_subsubmode = FtSubSubMode; m_subsubmode = FtSubSubMode;
...@@ -661,7 +667,6 @@ void FakeVimHandler::Private::handleInsertMode(int key, const QString &text) ...@@ -661,7 +667,6 @@ void FakeVimHandler::Private::handleInsertMode(int key, const QString &text)
recordInsert(m_tc.position() - m_lastInsertion.size(), data); recordInsert(m_tc.position() - m_lastInsertion.size(), data);
m_tc.movePosition(Left, MoveAnchor, qMin(1, leftDist())); m_tc.movePosition(Left, MoveAnchor, qMin(1, leftDist()));
m_mode = CommandMode; m_mode = CommandMode;
///m_tc.endEditBlock();
} else if (key == Key_Left) { } else if (key == Key_Left) {
m_tc.movePosition(Left, MoveAnchor, 1); m_tc.movePosition(Left, MoveAnchor, 1);
m_lastInsertion.clear(); m_lastInsertion.clear();
...@@ -1136,13 +1141,20 @@ void FakeVimHandler::Private::undo() ...@@ -1136,13 +1141,20 @@ void FakeVimHandler::Private::undo()
if (m_undoStack.isEmpty()) if (m_undoStack.isEmpty())
return; return;
EditOperation op = m_undoStack.pop(); EditOperation op = m_undoStack.pop();
QTextCursor tc = m_tc; //qDebug() << "UNDO " << op;
tc.setPosition(op.m_position, MoveAnchor); if (op.m_itemCount > 0) {
if (op.m_type == EditOperation::EditInsert) { for (int i = op.m_itemCount; --i >= 0; )
tc.setPosition(op.m_position + op.m_length, KeepAnchor); undo();
tc.deleteChar();
} else { } else {
tc.insertText(op.m_data); //QTextCursor tc = m_tc;
m_tc.setPosition(op.m_position, MoveAnchor);
if (!op.m_to.isEmpty()) {
m_tc.setPosition(op.m_position + op.m_to.size(), KeepAnchor);
m_tc.deleteChar();
}
if (!op.m_from.isEmpty())
m_tc.insertText(op.m_from);
m_tc.setPosition(op.m_position, MoveAnchor);
} }
m_redoStack.push(op); m_redoStack.push(op);
#endif #endif
...@@ -1156,13 +1168,20 @@ void FakeVimHandler::Private::redo() ...@@ -1156,13 +1168,20 @@ void FakeVimHandler::Private::redo()
if (m_redoStack.isEmpty()) if (m_redoStack.isEmpty())
return; return;
EditOperation op = m_redoStack.pop(); EditOperation op = m_redoStack.pop();
QTextCursor tc = m_tc; //qDebug() << "REDO " << op;
tc.setPosition(op.m_position, MoveAnchor); if (op.m_itemCount > 0) {
if (op.m_type == EditOperation::EditInsert) { for (int i = op.m_itemCount; --i >= 0; )
tc.insertText(op.m_data); undo();
} else { } else {
tc.setPosition(op.m_position + op.m_length, KeepAnchor); int pos = op.m_position; // - op.m_to.size() + op.m_from.size();
tc.deleteChar(); m_tc.setPosition(pos, MoveAnchor);
if (!op.m_from.isEmpty()) {
m_tc.setPosition(pos + op.m_from.size(), KeepAnchor);
m_tc.deleteChar();
}
if (!op.m_to.isEmpty())
m_tc.insertText(op.m_to);
m_tc.setPosition(pos, MoveAnchor);
} }
m_undoStack.push(op); m_undoStack.push(op);
#endif #endif
...@@ -1170,29 +1189,30 @@ void FakeVimHandler::Private::redo() ...@@ -1170,29 +1189,30 @@ void FakeVimHandler::Private::redo()
void FakeVimHandler::Private::recordInsert(int position, const QString &data) void FakeVimHandler::Private::recordInsert(int position, const QString &data)
{ {
qDebug() << "INSERT AT " << position << " DATA: " << data;
EditOperation op; EditOperation op;
op.m_type = EditOperation::EditInsert;
op.m_position = position; op.m_position = position;
op.m_length = data.size(); op.m_to = data;
op.m_data = data;
m_undoStack.push(op); m_undoStack.push(op);
m_redoStack.clear(); m_redoStack.clear();
qDebug() << "INSERT " << op;
} }
void FakeVimHandler::Private::recordRemove(int position, int length) void FakeVimHandler::Private::recordRemove(int position, int length)
{ {
qDebug() << "REMOVE AT " << position << " LEN: " << length;
QTextCursor tc = m_tc; QTextCursor tc = m_tc;
tc.setPosition(position, MoveAnchor); tc.setPosition(position, MoveAnchor);
tc.setPosition(position + length, KeepAnchor); tc.setPosition(position + length, KeepAnchor);
recordRemove(position, tc.selection().toPlainText());
}
void FakeVimHandler::Private::recordRemove(int position, const QString &data)
{
EditOperation op; EditOperation op;
op.m_type = EditOperation::EditRemove;
op.m_position = position; op.m_position = position;
op.m_length = length; op.m_from = data;
op.m_data = tc.selection().toPlainText();
m_undoStack.push(op); m_undoStack.push(op);
m_redoStack.clear(); m_redoStack.clear();
qDebug() << "REMOVE " << op;
} }
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
......
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