CppEditor: Use ActionBuilder

Change-Id: I2590d77b73ebbcd3856f259201b874a112e12900
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2023-12-15 07:54:18 +01:00
parent 508a961c1c
commit 3771eb196b

View File

@@ -270,9 +270,12 @@ static void insertIntoMenus(const QList<ActionContainer *> &menus,
func(menu); func(menu);
} }
static void addActionToMenus(const QList<ActionContainer *> &menus, Command *cmd, Id id) static void addActionToMenus(const QList<Id> &menuIds, Id actionId, Id groupId)
{ {
insertIntoMenus(menus, [cmd, id](ActionContainer *menu) { menu->addAction(cmd, id); }); for (const Id menuId : menuIds) {
ActionContainer * const menu = ActionManager::actionContainer(menuId);
menu->addAction(ActionManager::command(actionId), groupId);
}
} }
void CppEditorPlugin::setupMenus() void CppEditorPlugin::setupMenus()
@@ -298,220 +301,181 @@ void CppEditorPlugin::setupMenus()
addPerFileActions(); addPerFileActions();
addGlobalActions(); addGlobalActions();
ActionContainer * const toolsDebug ActionBuilder inspectCppCodeModel(this, Constants::INSPECT_CPP_CODEMODEL);
= ActionManager::actionContainer(Core::Constants::M_TOOLS_DEBUG); inspectCppCodeModel.setText(Tr::tr("Inspect C++ Code Model..."));
QAction * const inspectCppCodeModel = new QAction(Tr::tr("Inspect C++ Code Model..."), this); inspectCppCodeModel.setDefaultKeySequence(Tr::tr("Meta+Shift+F12"), Tr::tr("Ctrl+Shift+F12"));
Command * const cmd = ActionManager::registerAction(inspectCppCodeModel, inspectCppCodeModel.addToContainer(Core::Constants::M_TOOLS_DEBUG);
Constants::INSPECT_CPP_CODEMODEL); inspectCppCodeModel.addOnTriggered(d, &CppEditorPluginPrivate::inspectCppCodeModel);
cmd->setDefaultKeySequence({useMacShortcuts ? Tr::tr("Meta+Shift+F12")
: Tr::tr("Ctrl+Shift+F12")});
connect(inspectCppCodeModel, &QAction::triggered,
d, &CppEditorPluginPrivate::inspectCppCodeModel);
toolsDebug->addAction(cmd);
} }
void CppEditorPlugin::addPerSymbolActions() void CppEditorPlugin::addPerSymbolActions()
{ {
const QList<ActionContainer *> menus{ActionManager::actionContainer(Constants::M_TOOLS_CPP), const QList<Id> menus{Constants::M_TOOLS_CPP, Constants::M_CONTEXT};
ActionManager::actionContainer(Constants::M_CONTEXT)}; const auto addSymbolActionToMenus = [&menus](Id id) {
ActionContainer * const touchBar = ActionManager::actionContainer(Core::Constants::TOUCH_BAR); addActionToMenus(menus, id, Constants::G_SYMBOL);
const Context context(Constants::CPPEDITOR_ID);
const auto addSymbolActionToMenus = [&menus](Command *cmd) {
addActionToMenus(menus, cmd, Constants::G_SYMBOL);
}; };
const Context context(Constants::CPPEDITOR_ID);
addSymbolActionToMenus(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
Command *cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR); Command *cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
cmd->setTouchBarText(Tr::tr("Follow", "text on macOS touch bar")); cmd->setTouchBarText(Tr::tr("Follow", "text on macOS touch bar"));
addSymbolActionToMenus(cmd); ActionContainer * const touchBar = ActionManager::actionContainer(Core::Constants::TOUCH_BAR);
touchBar->addAction(cmd, Core::Constants::G_TOUCHBAR_NAVIGATION);
addSymbolActionToMenus(ActionManager::command(
TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT));
addSymbolActionToMenus(ActionManager::command(
TextEditor::Constants::FOLLOW_SYMBOL_TO_TYPE));
addSymbolActionToMenus(ActionManager::command(
TextEditor::Constants::FOLLOW_SYMBOL_TO_TYPE_IN_NEXT_SPLIT));
QAction * const switchDeclarationDefinition
= new QAction(Tr::tr("Switch Between Function Declaration/Definition"), this);
cmd = ActionManager::registerAction(switchDeclarationDefinition,
Constants::SWITCH_DECLARATION_DEFINITION, context, true);
cmd->setDefaultKeySequence(QKeySequence(Tr::tr("Shift+F2")));
cmd->setTouchBarText(Tr::tr("Decl/Def", "text on macOS touch bar"));
connect(switchDeclarationDefinition, &QAction::triggered,
this, &CppEditorPlugin::switchDeclarationDefinition);
addSymbolActionToMenus(cmd);
touchBar->addAction(cmd, Core::Constants::G_TOUCHBAR_NAVIGATION); touchBar->addAction(cmd, Core::Constants::G_TOUCHBAR_NAVIGATION);
QAction * const openDeclarationDefinitionInNextSplit = addSymbolActionToMenus(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
new QAction(Tr::tr("Open Function Declaration/Definition in Next Split"), this); addSymbolActionToMenus(TextEditor::Constants::FOLLOW_SYMBOL_TO_TYPE);
cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit, addSymbolActionToMenus(TextEditor::Constants::FOLLOW_SYMBOL_TO_TYPE_IN_NEXT_SPLIT);
Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT,
context, true);
cmd->setDefaultKeySequence(QKeySequence(HostOsInfo::isMacHost()
? Tr::tr("Meta+E, Shift+F2")
: Tr::tr("Ctrl+E, Shift+F2")));
connect(openDeclarationDefinitionInNextSplit, &QAction::triggered,
this, &CppEditorPlugin::openDeclarationDefinitionInNextSplit);
addSymbolActionToMenus(cmd);
addSymbolActionToMenus(ActionManager::command(TextEditor::Constants::FIND_USAGES)); ActionBuilder switchDeclDef(this, Constants::SWITCH_DECLARATION_DEFINITION);
switchDeclDef.setText(Tr::tr("Switch Between Function Declaration/Definition"));
switchDeclDef.setContext(context);
switchDeclDef.setScriptable(true);
switchDeclDef.setDefaultKeySequence(Tr::tr("Shift+F2"));
switchDeclDef.setTouchBarText(Tr::tr("Decl/Def", "text on macOS touch bar"));
switchDeclDef.addToContainers(menus, Constants::G_SYMBOL);
switchDeclDef.addToContainer(Core::Constants::TOUCH_BAR, Core::Constants::G_TOUCHBAR_NAVIGATION);
d->m_findRefsCategorizedAction = new QAction(Tr::tr("Find References With Access Type"), this); ActionBuilder openDeclDefSplit(this, Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT);
cmd = ActionManager::registerAction(d->m_findRefsCategorizedAction, openDeclDefSplit.setText(Tr::tr("Open Function Declaration/Definition in Next Split"));
"CppEditor.FindRefsCategorized", context); openDeclDefSplit.setContext(context);
connect(d->m_findRefsCategorizedAction, &QAction::triggered, this, [this] { openDeclDefSplit.setScriptable(true);
openDeclDefSplit.setDefaultKeySequence(Tr::tr("Meta+E, Shift+F2"), Tr::tr("Ctrl+E, Shift+F2"));
openDeclDefSplit.addToContainers(menus, Constants::G_SYMBOL);
openDeclDefSplit.addOnTriggered(this, &CppEditorPlugin::openDeclarationDefinitionInNextSplit);
addSymbolActionToMenus(TextEditor::Constants::FIND_USAGES);
ActionBuilder findRefsCategorized(this, "CppEditor.FindRefsCategorized");
findRefsCategorized.setText(Tr::tr("Find References With Access Type"));
findRefsCategorized.setContext(context);
findRefsCategorized.bindContextAction(&d->m_findRefsCategorizedAction);
findRefsCategorized.addToContainers(menus, Constants::G_SYMBOL);
findRefsCategorized.addOnTriggered(this, [this] {
if (const auto w = currentCppEditorWidget()) { if (const auto w = currentCppEditorWidget()) {
codeModelSettings()->setCategorizeFindReferences(true); codeModelSettings()->setCategorizeFindReferences(true);
w->findUsages(); w->findUsages();
codeModelSettings()->setCategorizeFindReferences(false); codeModelSettings()->setCategorizeFindReferences(false);
} }
}); });
addSymbolActionToMenus(cmd);
addSymbolActionToMenus(ActionManager::command(TextEditor::Constants::RENAME_SYMBOL)); addSymbolActionToMenus(TextEditor::Constants::RENAME_SYMBOL);
d->m_openTypeHierarchyAction = new QAction(Tr::tr("Open Type Hierarchy"), this); ActionBuilder openTypeHierarchy(this, Constants::OPEN_TYPE_HIERARCHY);
cmd = ActionManager::registerAction(d->m_openTypeHierarchyAction, openTypeHierarchy.setText(Tr::tr("Open Type Hierarchy"));
Constants::OPEN_TYPE_HIERARCHY, context); openTypeHierarchy.setContext(context);
cmd->setDefaultKeySequence(QKeySequence(useMacShortcuts openTypeHierarchy.bindContextAction(&d->m_openTypeHierarchyAction);
? Tr::tr("Meta+Shift+T") : Tr::tr("Ctrl+Shift+T"))); openTypeHierarchy.setDefaultKeySequence(Tr::tr("Meta+Shift+T"), Tr::tr("Ctrl+Shift+T"));
connect(d->m_openTypeHierarchyAction, &QAction::triggered, openTypeHierarchy.addToContainers(menus, Constants::G_SYMBOL);
this, &CppEditorPlugin::openTypeHierarchy); openTypeHierarchy.addOnTriggered(this, &CppEditorPlugin::openTypeHierarchy);
addSymbolActionToMenus(cmd);
addSymbolActionToMenus(ActionManager::command(TextEditor::Constants::OPEN_CALL_HIERARCHY)); addSymbolActionToMenus(TextEditor::Constants::OPEN_CALL_HIERARCHY);
// Refactoring sub-menu // Refactoring sub-menu
Command *sep = menus.last()->addSeparator(Constants::G_SYMBOL); Command *sep = ActionManager::actionContainer(Constants::M_CONTEXT)
->addSeparator(Constants::G_SYMBOL);
sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT)); sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
} }
void CppEditorPlugin::addActionsForSelections() void CppEditorPlugin::addActionsForSelections()
{ {
const QList<ActionContainer *> menus{ActionManager::actionContainer(Constants::M_TOOLS_CPP), const QList<Id> menus{Constants::M_TOOLS_CPP, Constants::M_CONTEXT};
ActionManager::actionContainer(Constants::M_CONTEXT)};
const auto addSelectionActionToMenus = [&menus](Command *cmd) { addActionToMenus(menus, TextEditor::Constants::AUTO_INDENT_SELECTION, Constants::G_SELECTION);
addActionToMenus(menus, cmd, Constants::G_SELECTION); addActionToMenus(menus, TextEditor::Constants::UN_COMMENT_SELECTION, Constants::G_SELECTION);
};
addSelectionActionToMenus(ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION));
addSelectionActionToMenus(ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION));
} }
void CppEditorPlugin::addPerFileActions() void CppEditorPlugin::addPerFileActions()
{ {
const QList<ActionContainer *> menus{ActionManager::actionContainer(Constants::M_TOOLS_CPP), const QList<Id> menus{Constants::M_TOOLS_CPP, Constants::M_CONTEXT};
ActionManager::actionContainer(Constants::M_CONTEXT)};
ActionContainer * const touchBar = ActionManager::actionContainer(Core::Constants::TOUCH_BAR);
const auto addFileActionToMenus = [&menus](Command *cmd) {
addActionToMenus(menus, cmd, Constants::G_FILE);
};
const Context context(Constants::CPPEDITOR_ID); const Context context(Constants::CPPEDITOR_ID);
QAction * const switchAction = new QAction(Tr::tr("Switch Header/Source"), this); ActionBuilder switchAction(this, Constants::SWITCH_HEADER_SOURCE);
Command *cmd = ActionManager::registerAction(switchAction, Constants::SWITCH_HEADER_SOURCE, switchAction.setText(Tr::tr("Switch Header/Source"));
context, true); switchAction.setContext(context);
cmd->setTouchBarText(Tr::tr("Header/Source", "text on macOS touch bar")); switchAction.setScriptable(true);
addFileActionToMenus(cmd); switchAction.setTouchBarText(Tr::tr("Header/Source", "text on macOS touch bar"));
touchBar->addAction(cmd, Core::Constants::G_TOUCHBAR_NAVIGATION); switchAction.addToContainers(menus, Constants::G_FILE);
cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F4)); switchAction.addToContainer(Core::Constants::TOUCH_BAR, Core::Constants::G_TOUCHBAR_NAVIGATION);
connect(switchAction, &QAction::triggered, switchAction.setDefaultKeySequence(Qt::Key_F4);
this, [] { CppModelManager::switchHeaderSource(false); }); switchAction.addOnTriggered([] { CppModelManager::switchHeaderSource(false); });
QAction * const switchInNextSplitAction ActionBuilder switchInNextSplit(this, Constants::OPEN_HEADER_SOURCE_IN_NEXT_SPLIT);
= new QAction(Tr::tr("Open Corresponding Header/Source in Next Split"), this); switchInNextSplit.setText(Tr::tr("Open Corresponding Header/Source in Next Split"));
cmd = ActionManager::registerAction( switchInNextSplit.setContext(context);
switchInNextSplitAction, Constants::OPEN_HEADER_SOURCE_IN_NEXT_SPLIT, context, true); switchInNextSplit.setScriptable(true);
cmd->setDefaultKeySequence(QKeySequence(HostOsInfo::isMacHost() switchInNextSplit.setDefaultKeySequence(Tr::tr("Meta+E, F4"), Tr::tr("Ctrl+E, F4"));
? Tr::tr("Meta+E, F4") switchInNextSplit.addToContainers(menus, Constants::G_FILE);
: Tr::tr("Ctrl+E, F4"))); switchInNextSplit.addOnTriggered([] { CppModelManager::switchHeaderSource(true); });
addFileActionToMenus(cmd);
connect(switchInNextSplitAction, &QAction::triggered,
this, [] { CppModelManager::switchHeaderSource(true); });
QAction * const openPreprocessorDialog ActionBuilder openPreprocessor(this, Constants::OPEN_PREPROCESSOR_DIALOG);
= new QAction(Tr::tr("Additional Preprocessor Directives..."), this); openPreprocessor.setText(Tr::tr("Additional Preprocessor Directives..."));
cmd = ActionManager::registerAction(openPreprocessorDialog, openPreprocessor.setContext(context);
Constants::OPEN_PREPROCESSOR_DIALOG, context); openPreprocessor.setDefaultKeySequence({});
cmd->setDefaultKeySequence(QKeySequence()); openPreprocessor.addToContainers(menus, Constants::G_FILE);
connect(openPreprocessorDialog, &QAction::triggered, openPreprocessor.addOnTriggered(this, &CppEditorPlugin::showPreProcessorDialog);
this, &CppEditorPlugin::showPreProcessorDialog);
addFileActionToMenus(cmd);
QAction * const showPreprocessedAction = new QAction(Tr::tr("Show Preprocessed Source"), this); ActionBuilder showPreprocessed(this, Constants::SHOW_PREPROCESSED_FILE);
cmd = ActionManager::registerAction(showPreprocessedAction, showPreprocessed.setText(Tr::tr("Show Preprocessed Source"));
Constants::SHOW_PREPROCESSED_FILE, context); showPreprocessed.setContext(context);
addFileActionToMenus(cmd); showPreprocessed.addToContainers(menus, Constants::G_FILE);
connect(showPreprocessedAction, &QAction::triggered, showPreprocessed.addOnTriggered(this, [] { CppModelManager::showPreprocessedFile(false); });
this, [] { CppModelManager::showPreprocessedFile(false); });
QAction * const showPreprocessedInSplitAction = new QAction ActionBuilder showPreprocessedInSplit(this, Constants::SHOW_PREPROCESSED_FILE_SPLIT);
(Tr::tr("Show Preprocessed Source in Next Split"), this); showPreprocessedInSplit.setText(Tr::tr("Show Preprocessed Source in Next Split"));
cmd = ActionManager::registerAction(showPreprocessedInSplitAction, showPreprocessedInSplit.setContext(context);
Constants::SHOW_PREPROCESSED_FILE_SPLIT, context); showPreprocessedInSplit.addToContainers(menus, Constants::G_FILE);
addFileActionToMenus(cmd); showPreprocessedInSplit.addOnTriggered([] { CppModelManager::showPreprocessedFile(true); });
connect(showPreprocessedInSplitAction, &QAction::triggered,
this, [] { CppModelManager::showPreprocessedFile(true); });
QAction * const foldCommentsAction = new QAction(Tr::tr("Fold All Comment Blocks"), this); ActionBuilder foldComments(this, "CppTools.FoldCommentBlocks");
cmd = ActionManager::registerAction(foldCommentsAction, foldComments.setText(Tr::tr("Fold All Comment Blocks"));
"CppTools.FoldCommentBlocks", context); foldComments.setContext(context);
addFileActionToMenus(cmd); foldComments.addToContainers(menus, Constants::G_FILE);
connect(foldCommentsAction, &QAction::triggered, this, [] { CppModelManager::foldComments(); }); foldComments.addOnTriggered(this, [] { CppModelManager::foldComments(); });
QAction * const unfoldCommentsAction = new QAction(Tr::tr("Unfold All Comment Blocks"), this);
cmd = ActionManager::registerAction(unfoldCommentsAction,
"CppTools.UnfoldCommentBlocks", context);
addFileActionToMenus(cmd);
connect(unfoldCommentsAction, &QAction::triggered,
this, [] { CppModelManager::unfoldComments(); });
d->m_openIncludeHierarchyAction = new QAction(Tr::tr("Open Include Hierarchy"), this); ActionBuilder unfoldComments(this, "CppTools.UnfoldCommentBlocks");
cmd = ActionManager::registerAction(d->m_openIncludeHierarchyAction, unfoldComments.setText(Tr::tr("Unfold All Comment Blocks"));
Constants::OPEN_INCLUDE_HIERARCHY, context); unfoldComments.setContext(context);
cmd->setDefaultKeySequence(QKeySequence(useMacShortcuts unfoldComments.addToContainers(menus, Constants::G_FILE);
? Tr::tr("Meta+Shift+I") : Tr::tr("Ctrl+Shift+I"))); unfoldComments.addOnTriggered(this, [] { CppModelManager::unfoldComments(); });
connect(d->m_openIncludeHierarchyAction, &QAction::triggered,
this, &CppEditorPlugin::openIncludeHierarchy); ActionBuilder openIncludeHierarchy(this, Constants::OPEN_INCLUDE_HIERARCHY);
addFileActionToMenus(cmd); openIncludeHierarchy.setText(Tr::tr("Open Include Hierarchy"));
openIncludeHierarchy.bindContextAction(&d->m_openIncludeHierarchyAction);
openIncludeHierarchy.setContext(context);
openIncludeHierarchy.setDefaultKeySequence(Tr::tr("Meta+Shift+I"), Tr::tr("Ctrl+Shift+I"));
openIncludeHierarchy.addToContainers(menus, Constants::G_FILE);
openIncludeHierarchy.addOnTriggered(this, &CppEditorPlugin::openIncludeHierarchy);
} }
void CppEditorPlugin::addGlobalActions() void CppEditorPlugin::addGlobalActions()
{ {
const QList<ActionContainer *> menus{ActionManager::actionContainer(Constants::M_TOOLS_CPP), const QList<Id> menus{Constants::M_TOOLS_CPP, Constants::M_CONTEXT};
ActionManager::actionContainer(Constants::M_CONTEXT)};
const auto addGlobalActionToMenus = [&menus](Command *cmd) {
addActionToMenus(menus, cmd, Constants::G_GLOBAL);
};
QAction * const findUnusedFunctionsAction = new QAction(Tr::tr("Find Unused Functions"), this); ActionBuilder findUnusedFunctions(this, "CppTools.FindUnusedFunctions");
Command *cmd = ActionManager::registerAction(findUnusedFunctionsAction, findUnusedFunctions.setText(Tr::tr("Find Unused Functions"));
"CppTools.FindUnusedFunctions"); findUnusedFunctions.addToContainers(menus, Constants::G_GLOBAL);
addGlobalActionToMenus(cmd); findUnusedFunctions.addOnTriggered(this, [] { CppModelManager::findUnusedFunctions({}); });
connect(findUnusedFunctionsAction, &QAction::triggered, this, [] {
CppModelManager::findUnusedFunctions({});
});
QAction * const findUnusedFunctionsInSubProjectAction ActionBuilder findUnusedFunctionsSubProject(this, "CppTools.FindUnusedFunctionsInSubProject");
= new QAction(Tr::tr("Find Unused C/C++ Functions"), this); findUnusedFunctionsSubProject.setText(Tr::tr("Find Unused C/C++ Functions"));
cmd = ActionManager::registerAction(findUnusedFunctionsInSubProjectAction,
"CppTools.FindUnusedFunctionsInSubProject");
for (ActionContainer *const projectContextMenu : for (ActionContainer *const projectContextMenu :
{ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT), {ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT),
ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT)}) { ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT)}) {
projectContextMenu->addSeparator(ProjectExplorer::Constants::G_PROJECT_TREE); projectContextMenu->addSeparator(ProjectExplorer::Constants::G_PROJECT_TREE);
projectContextMenu->addAction(cmd, ProjectExplorer::Constants::G_PROJECT_TREE); projectContextMenu->addAction(findUnusedFunctionsSubProject.command(),
ProjectExplorer::Constants::G_PROJECT_TREE);
} }
connect(findUnusedFunctionsInSubProjectAction, &QAction::triggered, this, [] { findUnusedFunctionsSubProject.addOnTriggered(this, [] {
if (const Node *const node = ProjectTree::currentNode(); node && node->asFolderNode()) if (const Node *const node = ProjectTree::currentNode(); node && node->asFolderNode())
CppModelManager::findUnusedFunctions(node->directory()); CppModelManager::findUnusedFunctions(node->directory());
}); });
d->m_reparseExternallyChangedFiles = new QAction(Tr::tr("Reparse Externally Changed Files"), ActionBuilder reparseChangedFiles(this, Constants::UPDATE_CODEMODEL);
this); reparseChangedFiles.setText(Tr::tr("Reparse Externally Changed Files"));
cmd = ActionManager::registerAction(d->m_reparseExternallyChangedFiles, reparseChangedFiles.bindContextAction(&d->m_reparseExternallyChangedFiles);
Constants::UPDATE_CODEMODEL); reparseChangedFiles.addToContainers(menus, Constants::G_GLOBAL);
connect(d->m_reparseExternallyChangedFiles, &QAction::triggered, reparseChangedFiles.addOnTriggered(CppModelManager::instance(),
CppModelManager::instance(), &CppModelManager::updateModifiedSourceFiles); &CppModelManager::updateModifiedSourceFiles);
addGlobalActionToMenus(cmd);
} }
void CppEditorPlugin::setupProjectPanels() void CppEditorPlugin::setupProjectPanels()