bug-fix: do not cancel auto-indentation if cursor is not at block end

This commit is contained in:
Le Tan 2017-09-24 12:04:35 +08:00
parent d955dedcb7
commit 04fa3654a4
4 changed files with 26 additions and 27 deletions

View File

@ -670,3 +670,22 @@ int VEditUtils::findNextEmptyBlock(const QTextCursor &p_cursor,
return p_repeat > 0 ? -1 : res; return p_repeat > 0 ? -1 : res;
} }
bool VEditUtils::needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &p_cursor)
{
// Cancel the auto indent/list if the pos is the same and cursor is at
// the end of a block.
QTextBlock block = p_cursor.block();
if (p_cursor.position() == p_autoIndentPos
&& !p_cursor.hasSelection()
&& p_cursor.atBlockEnd()) {
if (isListBlock(block)) {
return true;
} else if (isSpaceToBlockStart(block,
p_cursor.positionInBlock())) {
return true;
}
}
return false;
}

View File

@ -130,6 +130,10 @@ public:
bool p_forward, bool p_forward,
int p_repeat); int p_repeat);
// Check if we need to cancel auto indent.
// @p_autoIndentPos: the position of the cursor after auto indent.
static bool needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &p_cursor);
private: private:
VEditUtils() {} VEditUtils() {}
}; };

View File

@ -518,7 +518,7 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos)
bool resetPositionInBlock = true; bool resetPositionInBlock = true;
Key keyInfo(key, modifiers); Key keyInfo(key, modifiers);
bool unindent = false; bool unindent = false;
int autoIndentPos = -1; int autoIndentPos = p_autoIndentPos ? *p_autoIndentPos : -1;
// Handle Insert mode key press. // Handle Insert mode key press.
if (VimMode::Insert == m_mode) { if (VimMode::Insert == m_mode) {
@ -527,20 +527,8 @@ bool VVim::handleKeyPressEvent(int key, int modifiers, int *p_autoIndentPos)
// See if we need to cancel auto indent. // See if we need to cancel auto indent.
bool cancelAutoIndent = false; bool cancelAutoIndent = false;
if (p_autoIndentPos && *p_autoIndentPos > -1) { if (p_autoIndentPos && *p_autoIndentPos > -1) {
// Cancel the auto indent/list if the pos is the same and cursor is at
// the end of a block.
QTextCursor cursor = m_editor->textCursor(); QTextCursor cursor = m_editor->textCursor();
QTextBlock block = cursor.block(); cancelAutoIndent = VEditUtils::needToCancelAutoIndent(*p_autoIndentPos, cursor);
if (cursor.position() == *p_autoIndentPos && !cursor.hasSelection()) {
if (VEditUtils::isListBlock(block)) {
if (cursor.atBlockEnd()) {
cancelAutoIndent = true;
}
} else if (VEditUtils::isSpaceToBlockStart(block,
cursor.positionInBlock())) {
cancelAutoIndent = true;
}
}
if (cancelAutoIndent) { if (cancelAutoIndent) {
autoIndentPos = -1; autoIndentPos = -1;

View File

@ -553,20 +553,8 @@ bool VMdEditOperations::handleKeyReturn(QKeyEvent *p_event)
if (m_autoIndentPos > -1) { if (m_autoIndentPos > -1) {
// Cancel the auto indent/list if the pos is the same and cursor is at // Cancel the auto indent/list if the pos is the same and cursor is at
// the end of a block. // the end of a block.
bool cancelAutoIndent = false;
QTextCursor cursor = m_editor->textCursor(); QTextCursor cursor = m_editor->textCursor();
QTextBlock block = cursor.block(); if (VEditUtils::needToCancelAutoIndent(m_autoIndentPos, cursor)) {
if (cursor.position() == m_autoIndentPos && !cursor.hasSelection()) {
if (VEditUtils::isListBlock(block)) {
if (cursor.atBlockEnd()) {
cancelAutoIndent = true;
}
} else if (VEditUtils::isSpaceToBlockStart(block,
cursor.positionInBlock())) {
cancelAutoIndent = true;
}
}
if (cancelAutoIndent) {
m_autoIndentPos = -1; m_autoIndentPos = -1;
VEditUtils::deleteIndentAndListMark(cursor); VEditUtils::deleteIndentAndListMark(cursor);
m_editor->setTextCursor(cursor); m_editor->setTextCursor(cursor);