且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 08:48:16

关于选择部分,问题从eclipse编辑器通过插件comand替换所选代码足够满足您的需要:

  try {
IEditorPart part = PlatformUI.getWorkbench()。getActiveWorkbenchWindow ().getActivePage()getActiveEditor();
if(part instance of 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){

//这里是你的String
final TextSelection textSel =(TextSelection)sel;

}
}
} catch(Exception ex){
ex.printStackTrace();
}






然后可以链接到在弹出式菜单中添加项目进行选择,如此SO问题所示:

如何在Eclipse中为编辑器上下文菜单提供命令




tooltip =我的命令工具提示
id =org。 my.popup.IdCommand>
< visibleWhen>
< with variable =selection>
< instanceof value =org.eclipse.jface.text.ITextSelection/>
< / with>
< / visibleWhen>


I am developing a plugin for 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:

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:
"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>