从eclipse编辑器替换选择的代码通过插件命令 [英] Replace selected code from eclipse editor thru plugin command

查看:231
本文介绍了从eclipse编辑器替换选择的代码通过插件命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 */ through a plugin? I have already designed a plugin to create a button in the toolbar. When I click it, I need it to change the text that is selected and put it into /* */.

推荐答案

尝试这个片段,这应该给你足够的提示来做你的工作:

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

这篇关于从eclipse编辑器替换选择的代码通过插件命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆