2009-08-05 65 views
3

如何在eclipse編輯器中替換選定的代碼段(通過鼠標選擇選擇),並通過插件僅用/* selected text */替換爲相同的代碼? 我已經設計了一個插件來在工具欄中創建一個按鈕。當我點擊它時,我需要它來更改選中的文本並將其放入/* */從eclipse編輯器中通過插件命令替換選定的代碼

回答

7

試試這個片段中,應該給你足夠的提示做你的工作:

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) { 
      final TextSelection textSel = (TextSelection)sel; 
      String newText = "/*" + textSel.getText() + "*/"; 
      doc.replace(textSel.getOffset(), textSel.getLength(), newText); 
     } 
    } 
} catch (Exception ex) { 
    ex.printStackTrace(); 
}