forked from qt-creator/qt-creator
CppEditor: Refactor CppEditorWidget::contextMenuEvent
No behavior change. * Extract some functions * Add some clarifying comments here and there * Add QTC_CHECK() * Remove QLatin1String * foreach -> for * Const-correctness Change-Id: Ie6ccc987ef959c5295a4d3225aac0dbad144f91d Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -743,51 +743,69 @@ void CppEditorWidget::processKeyNormally(QKeyEvent *e)
|
|||||||
TextEditorWidget::keyPressEvent(e);
|
TextEditorWidget::keyPressEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
static void addRefactoringActions(QMenu *menu, AssistInterface *iface)
|
||||||
{
|
{
|
||||||
|
if (!iface || !menu)
|
||||||
|
return;
|
||||||
|
|
||||||
|
using Processor = QScopedPointer<IAssistProcessor>;
|
||||||
|
using Proposal = QScopedPointer<IAssistProposal>;
|
||||||
|
using Model = QScopedPointer<GenericProposalModel>;
|
||||||
|
|
||||||
|
const Processor processor(CppEditorPlugin::instance()->quickFixProvider()->createProcessor());
|
||||||
|
const Proposal proposal(processor->perform(iface)); // OK, perform() takes ownership of iface.
|
||||||
|
if (proposal) {
|
||||||
|
Model model(static_cast<GenericProposalModel *>(proposal->model()));
|
||||||
|
for (int index = 0; index < model->size(); ++index) {
|
||||||
|
const auto item = static_cast<AssistProposalItem *>(model->proposalItem(index));
|
||||||
|
const QuickFixOperation::Ptr op = item->data().value<QuickFixOperation::Ptr>();
|
||||||
|
const QAction *action = menu->addAction(op->description());
|
||||||
|
QObject::connect(action, &QAction::triggered, menu, [op] { op->perform(); });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QMenu *CppEditorWidget::createRefactorMenu(QWidget *parent) const
|
||||||
|
{
|
||||||
|
auto *menu = new QMenu(tr("&Refactor"), parent);
|
||||||
|
menu->addAction(ActionManager::command(Constants::RENAME_SYMBOL_UNDER_CURSOR)->action());
|
||||||
|
|
||||||
// ### enable
|
// ### enable
|
||||||
// updateSemanticInfo(m_semanticHighlighter->semanticInfo(currentSource()));
|
// updateSemanticInfo(m_semanticHighlighter->semanticInfo(currentSource()));
|
||||||
|
|
||||||
QPointer<QMenu> menu(new QMenu(this));
|
|
||||||
|
|
||||||
ActionContainer *mcontext = ActionManager::actionContainer(Constants::M_CONTEXT);
|
|
||||||
QMenu *contextMenu = mcontext->menu();
|
|
||||||
|
|
||||||
QMenu *quickFixMenu = new QMenu(tr("&Refactor"), menu);
|
|
||||||
quickFixMenu->addAction(ActionManager::command(Constants::RENAME_SYMBOL_UNDER_CURSOR)->action());
|
|
||||||
|
|
||||||
if (isSemanticInfoValidExceptLocalUses()) {
|
if (isSemanticInfoValidExceptLocalUses()) {
|
||||||
d->m_useSelectionsUpdater.update(CppUseSelectionsUpdater::Synchronous);
|
d->m_useSelectionsUpdater.update(CppUseSelectionsUpdater::Synchronous);
|
||||||
AssistInterface *interface = createAssistInterface(QuickFix, ExplicitlyInvoked);
|
addRefactoringActions(menu, createAssistInterface(QuickFix, ExplicitlyInvoked));
|
||||||
if (interface) {
|
}
|
||||||
QScopedPointer<IAssistProcessor> processor(
|
|
||||||
CppEditorPlugin::instance()->quickFixProvider()->createProcessor());
|
return menu;
|
||||||
QScopedPointer<IAssistProposal> proposal(processor->perform(interface));
|
}
|
||||||
if (!proposal.isNull()) {
|
|
||||||
auto model = static_cast<GenericProposalModel *>(proposal->model());
|
static void appendCustomContextMenuActionsAndMenus(QMenu *menu, QMenu *refactorMenu)
|
||||||
for (int index = 0; index < model->size(); ++index) {
|
{
|
||||||
auto item = static_cast<AssistProposalItem *>(model->proposalItem(index));
|
bool isRefactoringMenuAdded = false;
|
||||||
QuickFixOperation::Ptr op = item->data().value<QuickFixOperation::Ptr>();
|
const QMenu *contextMenu = ActionManager::actionContainer(Constants::M_CONTEXT)->menu();
|
||||||
QAction *action = quickFixMenu->addAction(op->description());
|
for (QAction *action : contextMenu->actions()) {
|
||||||
connect(action, &QAction::triggered, this, [op] { op->perform(); });
|
menu->addAction(action);
|
||||||
}
|
if (action->objectName() == Constants::M_REFACTORING_MENU_INSERTION_POINT) {
|
||||||
delete model;
|
isRefactoringMenuAdded = true;
|
||||||
}
|
menu->addMenu(refactorMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (QAction *action, contextMenu->actions()) {
|
QTC_CHECK(isRefactoringMenuAdded);
|
||||||
menu->addAction(action);
|
}
|
||||||
if (action->objectName() == QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT))
|
|
||||||
menu->addMenu(quickFixMenu);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
void CppEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
||||||
|
{
|
||||||
|
const QPointer<QMenu> menu(new QMenu(this));
|
||||||
|
|
||||||
|
appendCustomContextMenuActionsAndMenus(menu, createRefactorMenu(menu));
|
||||||
appendStandardContextMenuActions(menu);
|
appendStandardContextMenuActions(menu);
|
||||||
|
|
||||||
menu->exec(e->globalPos());
|
menu->exec(e->globalPos());
|
||||||
if (!menu)
|
if (menu)
|
||||||
return;
|
delete menu; // OK, menu was not already deleted by closed editor widget.
|
||||||
delete menu;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEditorWidget::keyPressEvent(QKeyEvent *e)
|
void CppEditorWidget::keyPressEvent(QKeyEvent *e)
|
||||||
|
@@ -129,6 +129,8 @@ private:
|
|||||||
|
|
||||||
unsigned documentRevision() const;
|
unsigned documentRevision() const;
|
||||||
|
|
||||||
|
QMenu *createRefactorMenu(QWidget *parent) const;
|
||||||
|
|
||||||
TextEditor::RefactorMarkers refactorMarkersWithoutClangMarkers() const;
|
TextEditor::RefactorMarkers refactorMarkersWithoutClangMarkers() const;
|
||||||
|
|
||||||
CppTools::RefactoringEngineInterface *refactoringEngine() const;
|
CppTools::RefactoringEngineInterface *refactoringEngine() const;
|
||||||
|
Reference in New Issue
Block a user