【问题标题】:Adding item to Eclipse text viewer context menu将项目添加到 Eclipse 文本查看器上下文菜单
【发布时间】:2010-12-14 06:20:23
【问题描述】:

我正在为 Eclipse 开发一个插件。在这个插件中,我需要能够在文本编辑器的上下文菜单中添加一个项目。到目前为止我一直没有成功,有没有人知道如何添加这个项目。

另外,我如何获得一个带有当前在编辑器中选择的文本的字符串。

非常感谢。

【问题讨论】:

    标签: java eclipse plugins swt


    【解决方案1】:

    关于选择部分,“Replace selected code from eclipse editor thru plugin comand”这个问题已经足够满足你的需要了:

    try {               
        IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        if ( part instanceof ITextEditor ) {
            final ITextEditor editor = (ITextEditor)part;
            IDocumentProvider prov = editor.getDocumentProvider();
            IDocument doc = prov.getDocument( editor.getEditorInput() );
            ISelection sel = editor.getSelectionProvider().getSelection();
            if ( sel instanceof TextSelection ) {
    
                // Here is your String
                final TextSelection textSel = (TextSelection)sel;
    
            }
        }
    } catch ( Exception ex ) {
        ex.printStackTrace();
    }
    

    然后,您可以将此选择与在弹出菜单中添加的项目相关联,如以下 SO 问题所示:
    "How do you contribute a command to an editor context menu in Eclipse"

    <command
          commandId="org.my.command.IdCommand"
          tooltip="My Command Tooltip"
          id="org.my.popup.IdCommand">
        <visibleWhen>
           <with variable="selection">
              <instanceof value="org.eclipse.jface.text.ITextSelection"/>
           </with>
        </visibleWhen>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2010-10-08
      • 2018-12-07
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      相关资源
      最近更新 更多