简体   繁体   English

将项添加到Eclipse文本查看器上下文菜单

[英]Adding item to Eclipse text viewer context menu

I am developing a plugin for eclipse. 我正在为eclipse开发一个插件。 In this plugin I need to be able to add an item to the context menu in the text editor. 在这个插件中,我需要能够在文本编辑器中的上下文菜单中添加一个项目。 So far I have been unsuccessful in this, does anyone know how to add this item. 到目前为止,我一直没有成功,有没有人知道如何添加这个项目。

Also, how do I get a string with the text currently selected in the editor. 另外,如何获取当前在编辑器中选择的文本的字符串。

Thank you so much. 非常感谢。

Regarding the selection part, the question " Replace selected code from eclipse editor thru plugin comand " is quite adequate for your need: 关于选择部分,“ 通过插件命令从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();
}

You can then link this selection with the addition of an item in the popup menu, as in this SO question: 然后,您可以将此选择与弹出菜单中的项目添加相关联,如此SO问题:
" How do you contribute a command to an editor context menu in Eclipse " 你如何为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM