Replace selected code in eclipse editor via plugin command

How to replace the selected code section (selected by mouse selection) in the eclipse editor and replace it with the same code only in /* selected text */ through the plugin? I have already developed a plugin for creating a toolbar button. When I click on it, I need to change the text that is selected and put it in /* */ .

+3
eclipse selected plugins replace text
source share
1 answer

try this snippet that should give you enough hints to do your job:

 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(); } 
+7
source share

All Articles