forked from qt-creator/qt-creator
Fixed in-place renaming when text is selected
This handles the case with the selection inside the name correctly, and aborts when part of the selection is outside of the name. Previously, a selection could cause the text to get inserted backwards. Task-number: QTCREATORBUG-302 Reviewed-by: Roberto Raggi
This commit is contained in:
@@ -658,26 +658,30 @@ void CPPEditor::inAllRenameSelections(EditOperation operation,
|
|||||||
m_inRename = true;
|
m_inRename = true;
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
|
|
||||||
const int offset = cursor.position() - currentRenameSelection.cursor.anchor();
|
const int startOffset = cursor.selectionStart() - currentRenameSelection.cursor.anchor();
|
||||||
|
const int endOffset = cursor.selectionEnd() - currentRenameSelection.cursor.anchor();
|
||||||
|
const int length = endOffset - startOffset;
|
||||||
|
|
||||||
for (int i = 0; i < m_renameSelections.size(); ++i) {
|
for (int i = 0; i < m_renameSelections.size(); ++i) {
|
||||||
QTextEdit::ExtraSelection &s = m_renameSelections[i];
|
QTextEdit::ExtraSelection &s = m_renameSelections[i];
|
||||||
int pos = s.cursor.anchor();
|
int pos = s.cursor.anchor();
|
||||||
int endPos = s.cursor.position();
|
int endPos = s.cursor.position();
|
||||||
s.cursor.setPosition(s.cursor.anchor() + offset);
|
|
||||||
|
s.cursor.setPosition(pos + startOffset);
|
||||||
|
s.cursor.setPosition(pos + endOffset, QTextCursor::KeepAnchor);
|
||||||
|
|
||||||
switch (operation) {
|
switch (operation) {
|
||||||
case DeletePreviousChar:
|
case DeletePreviousChar:
|
||||||
s.cursor.deletePreviousChar();
|
s.cursor.deletePreviousChar();
|
||||||
--endPos;
|
endPos -= qMax(1, length);
|
||||||
break;
|
break;
|
||||||
case DeleteChar:
|
case DeleteChar:
|
||||||
s.cursor.deleteChar();
|
s.cursor.deleteChar();
|
||||||
--endPos;
|
endPos -= qMax(1, length);
|
||||||
break;
|
break;
|
||||||
case InsertText:
|
case InsertText:
|
||||||
s.cursor.insertText(text);
|
s.cursor.insertText(text);
|
||||||
endPos += text.length();
|
endPos += text.length() - length;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1733,8 +1737,8 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
// Eat backspace at start of name
|
// Eat backspace at start of name
|
||||||
e->accept();
|
e->accept();
|
||||||
return;
|
return;
|
||||||
} else if (cursor.position() > currentRenameSelection.cursor.anchor()
|
} else if (cursor.selectionStart() > currentRenameSelection.cursor.anchor()
|
||||||
&& cursor.position() <= currentRenameSelection.cursor.position()) {
|
&& cursor.selectionEnd() <= currentRenameSelection.cursor.position()) {
|
||||||
|
|
||||||
inAllRenameSelections(DeletePreviousChar, currentRenameSelection, cursor);
|
inAllRenameSelections(DeletePreviousChar, currentRenameSelection, cursor);
|
||||||
e->accept();
|
e->accept();
|
||||||
@@ -1747,8 +1751,8 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
// Eat delete at end of name
|
// Eat delete at end of name
|
||||||
e->accept();
|
e->accept();
|
||||||
return;
|
return;
|
||||||
} else if (cursor.position() >= currentRenameSelection.cursor.anchor()
|
} else if (cursor.selectionStart() >= currentRenameSelection.cursor.anchor()
|
||||||
&& cursor.position() < currentRenameSelection.cursor.position()) {
|
&& cursor.selectionEnd() < currentRenameSelection.cursor.position()) {
|
||||||
|
|
||||||
inAllRenameSelections(DeleteChar, currentRenameSelection, cursor);
|
inAllRenameSelections(DeleteChar, currentRenameSelection, cursor);
|
||||||
e->accept();
|
e->accept();
|
||||||
@@ -1759,8 +1763,8 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
default: {
|
default: {
|
||||||
QString text = e->text();
|
QString text = e->text();
|
||||||
if (! text.isEmpty() && text.at(0).isPrint()) {
|
if (! text.isEmpty() && text.at(0).isPrint()) {
|
||||||
if (cursor.position() >= currentRenameSelection.cursor.anchor()
|
if (cursor.selectionStart() >= currentRenameSelection.cursor.anchor()
|
||||||
&& cursor.position() <= currentRenameSelection.cursor.position()) {
|
&& cursor.selectionEnd() <= currentRenameSelection.cursor.position()) {
|
||||||
|
|
||||||
inAllRenameSelections(InsertText, currentRenameSelection, cursor, text);
|
inAllRenameSelections(InsertText, currentRenameSelection, cursor, text);
|
||||||
e->accept();
|
e->accept();
|
||||||
|
Reference in New Issue
Block a user