向Eclipse文本查看器上下文菜单添加项目

3
我正在开发一款针对Eclipse的插件。在这个插件中,我需要能够在文本编辑器的上下文菜单中添加一个项目。到目前为止,我还没有成功,有人知道如何添加此项目吗?
另外,我如何获取当前在编辑器中选定的文本字符串?
非常感谢。
1个回答

6

关于选择部分,问题“通过插件命令从Eclipse编辑器替换所选代码”非常适合您的需求:

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问题中一样:
"如何在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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接