You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在自定义Plugin.xml中移除Eclipse插件的上下文菜单项?

在自定义plugin.xml中移除已添加的Eclipse上下文菜单项?

你添加的上下文菜单项配置如下:

<menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
    <menu id="sourceMenuId" label="%Source">
        <command
              commandId="org.eclipse.wst.sse.ui.format"
              id="sourceFormat"
              style="push">
            <visibleWhen checkEnabled="true" />
        </command>
    </menu>
</menuContribution>

问题

能否在我自己的plugin.xml中移除该菜单项?我未找到可覆盖其可见状态的配置选项。


解决方案

Eclipse的plugin.xml没有直接删除其他插件贡献菜单项的原生配置,但可以通过以下几种方式实现隐藏或移除:

  • 利用活动(Activities)禁用命令
    通过org.eclipse.ui.activities扩展点,将目标命令关联到一个永远禁用的活动,从而隐藏对应的菜单项:

    <extension point="org.eclipse.ui.activities">
        <activity id="hideSourceFormatMenu" name="Hide Source Format Menu">
            <enabledWhen>
                <!-- 设置永远不启用,直接隐藏菜单项 -->
                <with variable="activeWorkbenchWindow">
                    <test property="org.eclipse.core.runtime.propertyTester" value="false"/>
                </with>
            </enabledWhen>
        </activity>
        <activityPatternBinding activityId="hideSourceFormatMenu">
            <!-- 绑定目标命令的ID -->
            <pattern value="org.eclipse.wst.sse.ui.format"/>
        </activityPatternBinding>
    </extension>
    
  • 通过代码动态移除菜单项
    在你的插件的WorkbenchWindowAdvisor的postWindowOpen()方法中,找到目标菜单并移除对应命令:

    @Override
    public void postWindowOpen() {
        super.postWindowOpen();
        IMenuManager popupMenu = getWindowConfigurer().getActionBarConfigurer().getMenuManager();
        // 找到目标子菜单
        IMenuManager sourceMenu = popupMenu.findMenuUsingPath("sourceMenuId");
        if (sourceMenu != null) {
            // 移除指定ID的命令项
            sourceMenu.remove("sourceFormat");
        }
    }
    
  • 覆盖原有菜单项的可见性配置
    在你的plugin.xml中添加相同位置和ID的菜单贡献,通过<visibleWhen>设置永远不可见,注意要确保你的插件加载顺序在目标插件之后:

    <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
        <menu id="sourceMenuId" label="%Source">
            <command
                  commandId="org.eclipse.wst.sse.ui.format"
                  id="sourceFormat"
                  style="push">
                <visibleWhen>
                    <test property="org.eclipse.core.runtime.propertyTester" value="false"/>
                </visibleWhen>
            </command>
        </menu>
    </menuContribution>
    

内容的提问来源于stack exchange,提问作者dawge4qd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 02:20:17