【发布时间】:2010-11-17 00:20:28
【问题描述】:
如何在 Eclipse 编辑器中替换选定的代码段(通过鼠标选择)并通过插件将其替换为仅在 /* selected text */ 中的相同代码?
我已经设计了一个插件来在工具栏中创建一个按钮。当我点击它时,我需要它来更改选中的文本并将其放入/* */。
【问题讨论】:
标签: eclipse text replace plugins selected
如何在 Eclipse 编辑器中替换选定的代码段(通过鼠标选择)并通过插件将其替换为仅在 /* selected text */ 中的相同代码?
我已经设计了一个插件来在工具栏中创建一个按钮。当我点击它时,我需要它来更改选中的文本并将其放入/* */。
【问题讨论】:
标签: eclipse text replace plugins selected
试试这个 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();
}
【讨论】: