mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-06 14:29:54 +08:00
[bugfix] When paste from register, discard selected texts first (#37)
* [bugfix] When paste from register, discard selected texts first * Additional fix for the paste command to follow vim spec * Refactor the code for paste command to make it clear
This commit is contained in:
parent
73630448a6
commit
78bb2d5c41
@ -3822,6 +3822,7 @@ void VVim::processPasteAction(QList<Token> &p_tokens, bool p_pasteBefore)
|
|||||||
|
|
||||||
Register ® = m_registers[m_regName];
|
Register ® = m_registers[m_regName];
|
||||||
QString value = reg.read();
|
QString value = reg.read();
|
||||||
|
bool isBlock = reg.isBlock();
|
||||||
if (value.isEmpty()) {
|
if (value.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -3834,36 +3835,65 @@ void VVim::processPasteAction(QList<Token> &p_tokens, bool p_pasteBefore)
|
|||||||
|
|
||||||
QTextCursor cursor = m_editor->textCursor();
|
QTextCursor cursor = m_editor->textCursor();
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
if (reg.isBlock()) {
|
|
||||||
if (p_pasteBefore) {
|
// Different from Vim:
|
||||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
// In visual mode, by default vim select the current char, so paste operation will replace
|
||||||
cursor.insertBlock();
|
// current char, but here it is strange to follow this specification
|
||||||
cursor.movePosition(QTextCursor::PreviousBlock);
|
bool hasSelection = cursor.hasSelection();
|
||||||
} else {
|
bool isVisualLine = checkMode(VimMode::VisualLine);
|
||||||
cursor.movePosition(QTextCursor::EndOfBlock);
|
if (hasSelection) {
|
||||||
cursor.insertBlock();
|
int pos = cursor.selectionStart();
|
||||||
|
deleteSelectedText(cursor, false);
|
||||||
|
if (isBlock && !isVisualLine) {
|
||||||
|
insertChangeBlockAfterDeletion(cursor, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// inserBlock() already insert a new line, so eliminate one here.
|
if (isBlock) {
|
||||||
cursor.insertText(text.left(text.size() - 1));
|
if (!isVisualLine) {
|
||||||
|
cursor.movePosition(QTextCursor::EndOfBlock);
|
||||||
|
cursor.insertBlock();
|
||||||
|
}
|
||||||
|
|
||||||
int nrBlock = text.count('\n');
|
int nrBlock = text.count('\n');
|
||||||
if (nrBlock > 0) {
|
if (nrBlock > 0) {
|
||||||
message(tr("%1 more %2").arg(nrBlock).arg(nrBlock > 1 ? tr("lines")
|
message(tr("%1 more %2").arg(nrBlock).arg(nrBlock > 1 ? tr("lines")
|
||||||
: tr("line")));
|
: tr("line")));
|
||||||
}
|
}
|
||||||
} else {
|
// inserBlock() already insert a new line, so eliminate one here.
|
||||||
if (!p_pasteBefore && !cursor.atBlockEnd()) {
|
text = text.left(text.size() - 1);
|
||||||
// Insert behind current cursor.
|
|
||||||
cursor.movePosition(QTextCursor::Right);
|
|
||||||
}
|
}
|
||||||
|
cursor.insertText(text);
|
||||||
|
} else { // no selection
|
||||||
|
if (isBlock) {
|
||||||
|
if (p_pasteBefore) {
|
||||||
|
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||||
|
cursor.insertBlock();
|
||||||
|
cursor.movePosition(QTextCursor::PreviousBlock);
|
||||||
|
} else if (!isVisualLine) {
|
||||||
|
cursor.movePosition(QTextCursor::EndOfBlock);
|
||||||
|
cursor.insertBlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
int nrBlock = text.count('\n');
|
||||||
|
if (nrBlock > 0) {
|
||||||
|
message(tr("%1 more %2").arg(nrBlock).arg(nrBlock > 1 ? tr("lines")
|
||||||
|
: tr("line")));
|
||||||
|
}
|
||||||
|
// inserBlock() already insert a new line, so eliminate one here.
|
||||||
|
text = text.left(text.size() - 1);
|
||||||
|
} else { // not a block
|
||||||
|
if (!p_pasteBefore && !cursor.atBlockEnd()) {
|
||||||
|
// Insert behind current cursor.
|
||||||
|
cursor.movePosition(QTextCursor::Right);
|
||||||
|
}
|
||||||
|
}
|
||||||
cursor.insertText(text);
|
cursor.insertText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor.endEditBlock();
|
cursor.endEditBlock();
|
||||||
m_editor->setTextCursor(cursor);
|
m_editor->setTextCursor(cursor);
|
||||||
|
|
||||||
|
setMode(VimMode::Normal);
|
||||||
qDebug() << "text pasted" << text;
|
qDebug() << "text pasted" << text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user