【问题标题】:How to add a command to an editor context menu in Eclipse only if text is selected仅在选择文本时如何将命令添加到 Eclipse 中的编辑器上下文菜单
【发布时间】:2022-01-10 21:42:59
【问题描述】:

我想在 Eclipse 上下文菜单中添加一个项目如果文本被标记为选中
以下是我的 XML 相关 sn-p:

<menuContribution
    allPopups="false"
    locationURI="popup:org.eclipse.ui.popup.any">
  <command
      commandId="com.test.ide.menu.commands.sampleCommand"
      icon="icons/sample.png"
      id="com.test.ide.menu.toolbars.sampleCommand"
      tooltip="Popup test">
    <visibleWhen>
        <with variable="selection">
            <instanceof value="org.eclipse.jface.text.ITextSelection"/>
        </with>
    </visibleWhen>
  </command>
</menuContribution>

我尝试了How do you contribute a command to an editor context menu in Eclipse 中的解决方案,但无法解决问题

我错过了什么?
你能帮忙吗?

谢谢
Avner

请原谅我的详细问题,我是 Eclipse 编程的新手
我试过了:

<extension
         point="org.eclipse.core.expressions.propertyTesters">
      <propertyTester
            class="com.test.ide.TextSelectedPropertyTester"
            id="com.test.ide.propertyTester1"
            namespace="tested"
            properties="textSelected"
            type="org.eclipse.jface.text.ITextSelection">
      </propertyTester>
</extension>

<menuContribution
    allPopups="false"
    locationURI="popup:org.eclipse.ui.popup.any">
  <command
      commandId="com.test.ide.menu.commands.sampleCommand"
      icon="icons/sample.png"
      id="com.test.ide.menu.toolbars.sampleCommand"
      tooltip="Popup test">
    <visibleWhen>
        <with variable="selection">
            <adapt type="org.eclipse.jface.text.ITextSelection">
                <test
                    property="tested.textSelected">
                </test>
            </adapt>
        </with>
    </visibleWhen>
  </command>
</menuContribution>

创建了一个新包 - com.test.ide 在其中创建了一个新类 TextSelectedPropertyTester
但是该类从未被调用

package com.test.ide;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.text.ITextSelection;


public class TextSelectedPropertyTester extends PropertyTester
{
    
    
    
  @Override
  public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
  {
      System.out.println("testing");
      if (receiver instanceof ITextSelection) {
          return ((ITextSelection)receiver).getLength() > 0;
      }

    return false;
  }
}

然而该类从未使用过

我错过了什么?

【问题讨论】:

  • 问题是编辑器通常总是有一个文本选择集 - 如果没有选择任何字符,它将只是零长度。
  • 谢谢,有没有办法检查它的长度?
  • 我尝试使用 public void setEnabled(Object context) 但是看起来这只有在第一次使用新菜单项后才会生效
  • 您是否在 Eclipse 启动时指定 -clean 以确保它始终查找更改的 plugin.xml 详细信息?只有在当前选择是文本选择时才会运行属性测试器 - 只有在文本编辑器中才会出现这种情况。
  • 我正在使用 Eclipse Committer 包(版本 2021-06 (4.20))进行开发,并通过右键单击 -> 调试为 -> Eclipse 应用程序重新启动 Eclipse,这就是我完成整个过程的方式开发我的功能并且从未遇到任何问题。在新打开的 Eclipse 中,鼠标在文本编辑器或 Java 编辑器中时,右键单击完成。无论是否标记了字符串,都会显示上下文菜单

标签: eclipse


【解决方案1】:

问题是编辑器通常总是有一个文本选择集——如果没有选择任何字符,它将只是零长度。

我看不到使用现有表达式对此进行测试的方法,因此可能需要使用 org.eclipse.core.expressions.propertyTester 扩展点定义您自己的属性测试器。

 <extension
         point="org.eclipse.core.expressions.propertyTesters">
      <propertyTester
            class="tested.TextSelectedPropertyTester"
            id="tested.propertyTester1"
            namespace="tested"
            properties="textSelected"
            type="org.eclipse.jface.text.ITextSelection">
      </propertyTester>
</extension>

这样使用:

<visibleWhen>
    <with variable="selection">
        <adapt type="org.eclipse.jface.text.ITextSelection">
            <test
                property="tested.textSelected"
                forcePluginActivation="true">
            </test>
       </adapt>
    </with>
</visibleWhen>

属性测试器很简单:

public class TextSelectedPropertyTester extends PropertyTester
{
  @Override
  public boolean test(Object receiver, String property, Object [] args, Object expectedValue)
  {
    if (receiver instanceof ITextSelection textSel) {
      return textSel.getLength() > 0;
    }

    return false;
  }
}

请注意,if 语句使用 Java 16 instanceof 类型模式,如果您需要使用较旧的 Java 运行:

    if (receiver instanceof ITextSelection) {
      return ((ITextSelection)receiver).getLength() > 0;
    }

【讨论】:

  • 抱歉,按照您的指示编辑了我的任务
  • 如果包含属性测试器的插件尚未激活,您可能需要test 上的forcePluginActivation - 请参阅更新的答案。
  • forcePluginActivation 解决了这个问题,谢谢!非常感谢您的帮助
猜你喜欢
  • 2014-07-07
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 2012-01-17
  • 1970-01-01
  • 2010-12-14
相关资源
最近更新 更多