Replace selected code from eclipse editor thru plugin command

前端 未结 1 830
故里飘歌
故里飘歌 2020-12-30 17:08

How do I replace the selected section of code (selected by mouse selection) in eclipse editor and replace it with the same code only in /* selected text */ thro

相关标签:
1条回答
  • 2020-12-30 17:29

    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();
    }    
    
    0 讨论(0)
提交回复
热议问题