vim-mode: support canceling autoindent after o/O

This commit is contained in:
Le Tan 2017-06-21 22:00:11 +08:00
parent 78674efaa0
commit 2734b8407c
3 changed files with 26 additions and 9 deletions

View File

@ -340,9 +340,9 @@ static int percentageToBlockNumber(const QTextDocument *p_doc, int p_percent)
return num >= 0 ? num : 0; return num >= 0 ? num : 0;
} }
bool VVim::handleKeyPressEvent(QKeyEvent *p_event) bool VVim::handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented)
{ {
bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers()); bool ret = handleKeyPressEvent(p_event->key(), p_event->modifiers(), p_autoIndented);
if (ret) { if (ret) {
p_event->accept(); p_event->accept();
} }
@ -350,13 +350,17 @@ bool VVim::handleKeyPressEvent(QKeyEvent *p_event)
return ret; return ret;
} }
bool VVim::handleKeyPressEvent(int key, int modifiers) bool VVim::handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented)
{ {
bool ret = false; bool ret = false;
bool resetPositionInBlock = true; bool resetPositionInBlock = true;
Key keyInfo(key, modifiers); Key keyInfo(key, modifiers);
bool unindent = false; bool unindent = false;
if (p_autoIndented) {
*p_autoIndented = false;
}
// Handle Insert mode key press. // Handle Insert mode key press.
if (VimMode::Insert == m_mode) { if (VimMode::Insert == m_mode) {
if (key == Qt::Key_Escape if (key == Qt::Key_Escape
@ -739,16 +743,22 @@ bool VVim::handleKeyPressEvent(int key, int modifiers)
1); 1);
} }
bool textInserted = false;
if (vconfig.getAutoIndent()) { if (vconfig.getAutoIndent()) {
VEditUtils::indentBlockAsPreviousBlock(cursor); textInserted = VEditUtils::indentBlockAsPreviousBlock(cursor);
if (vconfig.getAutoList()) { if (vconfig.getAutoList()) {
VEditUtils::insertListMarkAsPreviousBlock(cursor); textInserted = VEditUtils::insertListMarkAsPreviousBlock(cursor)
|| textInserted;
} }
} }
cursor.endEditBlock(); cursor.endEditBlock();
m_editor->setTextCursor(cursor); m_editor->setTextCursor(cursor);
if (p_autoIndented && textInserted) {
*p_autoIndented = true;
}
setMode(VimMode::Insert); setMode(VimMode::Insert);
} }

View File

@ -148,7 +148,7 @@ public:
// Handle key press event. // Handle key press event.
// Returns true if the event is consumed and need no more handling. // Returns true if the event is consumed and need no more handling.
bool handleKeyPressEvent(QKeyEvent *p_event); bool handleKeyPressEvent(QKeyEvent *p_event, bool *p_autoIndented = NULL);
// Return current mode. // Return current mode.
VimMode getMode() const; VimMode getMode() const;
@ -449,7 +449,7 @@ private:
}; };
// Returns true if the event is consumed and need no more handling. // Returns true if the event is consumed and need no more handling.
bool handleKeyPressEvent(int key, int modifiers); bool handleKeyPressEvent(int key, int modifiers, bool *p_autoIndented = NULL);
// Reset all key state info. // Reset all key state info.
void resetState(); void resetState();

View File

@ -193,8 +193,15 @@ bool VMdEditOperations::insertImage()
bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event) bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
{ {
if (m_editConfig->m_enableVimMode && m_vim->handleKeyPressEvent(p_event)) { bool autoIndentedVim = false;
if (m_editConfig->m_enableVimMode
&& m_vim->handleKeyPressEvent(p_event, &autoIndentedVim)) {
if (autoIndentedVim) {
m_autoIndentPos = m_editor->textCursor().position();
} else {
m_autoIndentPos = -1; m_autoIndentPos = -1;
}
return true; return true;
} }