3

如何在eclipse编辑器中替换选定的代码部分(通过鼠标选择)并仅/* selected text */通过插件将其替换为相同的代码?我已经设计了一个插件来在工具栏中创建一个按钮。当我单击它时,我需要它来更改选定的文本并将其放入/* */.

4

1 回答 1

8

试试这个片段,它应该给你足够的提示来完成你的工作:

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();
}    
于 2009-08-26T06:21:42.160 回答