【问题标题】:Replace selected code from eclipse editor thru plugin command通过插件命令从 Eclipse 编辑器替换选定的代码
【发布时间】:2010-11-17 00:20:28
【问题描述】:

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

【问题讨论】:

    标签: eclipse text replace plugins selected


    【解决方案1】:

    试试这个 sn-p,它应该会给你足够的提示来完成你的工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多