2009-11-07 34 views
3

我正在開發一個用於eclipse的插件。在這個插件中,我需要能夠添加一個項目到文本編輯器的上下文菜單中。到目前爲止,我一直沒有成功,有沒有人知道如何添加這個項目。將項目添加到Eclipse文本查看器上下文菜單

另外,如何獲取當前在編輯器中選擇的文本的字符串。

非常感謝。

回答

6

關於選擇部分問題,「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> 
相關問題