forked from qt-creator/qt-creator
Abort rename when text is changed due to unsupported actions
Done with Roberto Raggi and mae.
This commit is contained in:
@@ -656,6 +656,7 @@ CPPEditor::CPPEditor(QWidget *parent)
|
|||||||
, m_mouseNavigationEnabled(true)
|
, m_mouseNavigationEnabled(true)
|
||||||
, m_showingLink(false)
|
, m_showingLink(false)
|
||||||
, m_currentRenameSelection(-1)
|
, m_currentRenameSelection(-1)
|
||||||
|
, m_inRename(false)
|
||||||
{
|
{
|
||||||
setParenthesesMatchingEnabled(true);
|
setParenthesesMatchingEnabled(true);
|
||||||
setMarksVisible(true);
|
setMarksVisible(true);
|
||||||
@@ -748,6 +749,7 @@ void CPPEditor::createToolBar(CPPEditorEditable *editable)
|
|||||||
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateMethodBoxIndex()));
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateMethodBoxIndex()));
|
||||||
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
|
||||||
connect(m_methodCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMethodBoxToolTip()));
|
connect(m_methodCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMethodBoxToolTip()));
|
||||||
|
connect(document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(onContentsChanged(int,int,int)));
|
||||||
|
|
||||||
connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
|
connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
|
||||||
|
|
||||||
@@ -757,6 +759,13 @@ void CPPEditor::createToolBar(CPPEditorEditable *editable)
|
|||||||
static_cast<QHBoxLayout*>(w->layout())->insertWidget(0, m_methodCombo, 1);
|
static_cast<QHBoxLayout*>(w->layout())->insertWidget(0, m_methodCombo, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CPPEditor::abortRename()
|
||||||
|
{
|
||||||
|
m_currentRenameSelection = -1;
|
||||||
|
m_renameSelections.clear();
|
||||||
|
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
||||||
|
}
|
||||||
|
|
||||||
int CPPEditor::previousBlockState(QTextBlock block) const
|
int CPPEditor::previousBlockState(QTextBlock block) const
|
||||||
{
|
{
|
||||||
block = block.previous();
|
block = block.previous();
|
||||||
@@ -905,6 +914,8 @@ void CPPEditor::simplifyDeclarations()
|
|||||||
|
|
||||||
void CPPEditor::renameInPlace()
|
void CPPEditor::renameInPlace()
|
||||||
{
|
{
|
||||||
|
updateUsesNow();
|
||||||
|
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
m_currentRenameSelection = -1;
|
m_currentRenameSelection = -1;
|
||||||
|
|
||||||
@@ -921,6 +932,15 @@ void CPPEditor::renameInPlace()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CPPEditor::onContentsChanged(int position, int charsRemoved, int charsAdded)
|
||||||
|
{
|
||||||
|
if (!m_inRename)
|
||||||
|
abortRename();
|
||||||
|
|
||||||
|
if (charsRemoved > 0)
|
||||||
|
updateUses();
|
||||||
|
}
|
||||||
|
|
||||||
void CPPEditor::updateFileName()
|
void CPPEditor::updateFileName()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
@@ -1442,11 +1462,7 @@ bool CPPEditor::event(QEvent *e)
|
|||||||
{
|
{
|
||||||
switch (e->type()) {
|
switch (e->type()) {
|
||||||
case QEvent::ShortcutOverride:
|
case QEvent::ShortcutOverride:
|
||||||
qDebug () << "Override?";
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_currentRenameSelection != -1) {
|
||||||
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape
|
|
||||||
&& m_currentRenameSelection != -1) {
|
|
||||||
|
|
||||||
qDebug() << "Accept!";
|
|
||||||
e->accept();
|
e->accept();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1560,7 +1576,6 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString text = e->text();
|
|
||||||
QTextEdit::ExtraSelection currentRenameSelection = m_renameSelections.at(m_currentRenameSelection);
|
QTextEdit::ExtraSelection currentRenameSelection = m_renameSelections.at(m_currentRenameSelection);
|
||||||
|
|
||||||
QTextCursor::MoveMode moveMode = (e->modifiers() & Qt::ShiftModifier) ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;
|
QTextCursor::MoveMode moveMode = (e->modifiers() & Qt::ShiftModifier) ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;
|
||||||
@@ -1569,47 +1584,37 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
case Qt::Key_Enter:
|
case Qt::Key_Enter:
|
||||||
case Qt::Key_Return:
|
case Qt::Key_Return:
|
||||||
case Qt::Key_Escape:
|
case Qt::Key_Escape:
|
||||||
m_currentRenameSelection = -1;
|
abortRename();
|
||||||
m_renameSelections.clear();
|
|
||||||
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
|
||||||
e->accept();
|
e->accept();
|
||||||
break;
|
return;
|
||||||
case Qt::Key_Home: {
|
case Qt::Key_Home: {
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
c.setPosition(currentRenameSelection.cursor.anchor(), moveMode);
|
c.setPosition(currentRenameSelection.cursor.anchor(), moveMode);
|
||||||
setTextCursor(c);
|
setTextCursor(c);
|
||||||
e->accept();
|
e->accept();
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
case Qt::Key_End: {
|
case Qt::Key_End: {
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
c.setPosition(currentRenameSelection.cursor.position(), moveMode);
|
c.setPosition(currentRenameSelection.cursor.position(), moveMode);
|
||||||
setTextCursor(c);
|
setTextCursor(c);
|
||||||
e->accept();
|
e->accept();
|
||||||
break;
|
return;
|
||||||
}
|
|
||||||
case Qt::Key_Left: {
|
|
||||||
QTextCursor c = textCursor();
|
|
||||||
c.setPosition(qMax(c.position() - 1, currentRenameSelection.cursor.anchor()), moveMode);
|
|
||||||
setTextCursor(c);
|
|
||||||
e->accept();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Qt::Key_Right: {
|
|
||||||
QTextCursor c = textCursor();
|
|
||||||
c.setPosition(qMin(c.position() + 1, currentRenameSelection.cursor.position()), moveMode);
|
|
||||||
setTextCursor(c);
|
|
||||||
e->accept();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Qt::Key_Backspace: {
|
case Qt::Key_Backspace: {
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
|
|
||||||
if (c.position() > currentRenameSelection.cursor.anchor()
|
if (c.position() == currentRenameSelection.cursor.anchor()) {
|
||||||
|
// Eat
|
||||||
|
e->accept();
|
||||||
|
return;
|
||||||
|
} else if (c.position() > currentRenameSelection.cursor.anchor()
|
||||||
&& c.position() <= currentRenameSelection.cursor.position()) {
|
&& c.position() <= currentRenameSelection.cursor.position()) {
|
||||||
|
|
||||||
int offset = c.position() - currentRenameSelection.cursor.anchor();
|
int offset = c.position() - currentRenameSelection.cursor.anchor();
|
||||||
|
|
||||||
|
m_inRename = true;
|
||||||
|
|
||||||
c.beginEditBlock();
|
c.beginEditBlock();
|
||||||
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];
|
||||||
@@ -1622,17 +1627,55 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
}
|
}
|
||||||
c.endEditBlock();
|
c.endEditBlock();
|
||||||
|
|
||||||
|
m_inRename = false;
|
||||||
|
|
||||||
setTextCursor(c);
|
setTextCursor(c);
|
||||||
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
||||||
|
|
||||||
e->accept();
|
e->accept();
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Qt::Key_Delete: {
|
||||||
|
QTextCursor c = textCursor();
|
||||||
|
|
||||||
|
if (c.position() == currentRenameSelection.cursor.position()) {
|
||||||
|
// Eat
|
||||||
e->accept();
|
e->accept();
|
||||||
qWarning() << "Backspace outside of where you can be";
|
return;
|
||||||
|
} else if (c.position() >= currentRenameSelection.cursor.anchor()
|
||||||
|
&& c.position() < currentRenameSelection.cursor.position()) {
|
||||||
|
|
||||||
|
int offset = c.position() - currentRenameSelection.cursor.anchor();
|
||||||
|
|
||||||
|
m_inRename = true;
|
||||||
|
|
||||||
|
c.beginEditBlock();
|
||||||
|
for (int i = 0; i < m_renameSelections.size(); ++i) {
|
||||||
|
QTextEdit::ExtraSelection &s = m_renameSelections[i];
|
||||||
|
int pos = s.cursor.anchor();
|
||||||
|
int endPos = s.cursor.position();
|
||||||
|
s.cursor.setPosition(s.cursor.anchor() + offset);
|
||||||
|
s.cursor.deleteChar();
|
||||||
|
s.cursor.setPosition(pos);
|
||||||
|
s.cursor.setPosition(endPos - 1, QTextCursor::KeepAnchor);
|
||||||
|
}
|
||||||
|
c.endEditBlock();
|
||||||
|
|
||||||
|
m_inRename = false;
|
||||||
|
|
||||||
|
setTextCursor(c);
|
||||||
|
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
||||||
|
|
||||||
|
e->accept();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
QString text = e->text();
|
||||||
|
|
||||||
if (! text.isEmpty() && text.at(0).isPrint()) {
|
if (! text.isEmpty() && text.at(0).isPrint()) {
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
|
|
||||||
@@ -1641,6 +1684,8 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
|
|
||||||
int offset = c.position() - currentRenameSelection.cursor.anchor();
|
int offset = c.position() - currentRenameSelection.cursor.anchor();
|
||||||
|
|
||||||
|
m_inRename = true;
|
||||||
|
|
||||||
c.beginEditBlock();
|
c.beginEditBlock();
|
||||||
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];
|
||||||
@@ -1653,20 +1698,20 @@ void CPPEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
}
|
}
|
||||||
c.endEditBlock();
|
c.endEditBlock();
|
||||||
|
|
||||||
|
m_inRename = false;
|
||||||
|
|
||||||
setTextCursor(c);
|
setTextCursor(c);
|
||||||
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
setExtraSelections(CodeSemanticsSelection, m_renameSelections);
|
||||||
|
|
||||||
e->accept();
|
e->accept();
|
||||||
} else {
|
return;
|
||||||
e->accept();
|
|
||||||
qWarning() << "Whaa!";
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
e->accept();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextEditor::BaseTextEditor::keyPressEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CPPEditor::showLink(const Link &link)
|
void CPPEditor::showLink(const Link &link)
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ private slots:
|
|||||||
void reformatDocument();
|
void reformatDocument();
|
||||||
void simplifyDeclarations();
|
void simplifyDeclarations();
|
||||||
void renameInPlace();
|
void renameInPlace();
|
||||||
|
void onContentsChanged(int position, int charsRemoved, int charsAdded);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool sortedMethodOverview() const;
|
bool sortedMethodOverview() const;
|
||||||
@@ -143,6 +144,8 @@ private:
|
|||||||
|
|
||||||
void createToolBar(CPPEditorEditable *editable);
|
void createToolBar(CPPEditorEditable *editable);
|
||||||
|
|
||||||
|
void abortRename();
|
||||||
|
|
||||||
struct Link
|
struct Link
|
||||||
{
|
{
|
||||||
Link(const QString &fileName = QString(),
|
Link(const QString &fileName = QString(),
|
||||||
@@ -186,6 +189,7 @@ private:
|
|||||||
|
|
||||||
QList<QTextEdit::ExtraSelection> m_renameSelections;
|
QList<QTextEdit::ExtraSelection> m_renameSelections;
|
||||||
int m_currentRenameSelection;
|
int m_currentRenameSelection;
|
||||||
|
bool m_inRename;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
Reference in New Issue
Block a user