2009-08-05 66 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(); 
}