通过plugin命令替换eclipse编辑器中选定的代码
如何在eclipse编辑器中替换选定的代码部分(通过鼠标选择),并通过插件仅在通过plugin命令替换eclipse编辑器中选定的代码,eclipse,text,replace,plugins,selected,Eclipse,Text,Replace,Plugins,Selected,如何在eclipse编辑器中替换选定的代码部分(通过鼠标选择),并通过插件仅在/*selected text*/中替换为相同的代码? 我已经设计了一个插件来在工具栏中创建一个按钮。当我单击它时,我需要它来更改所选的文本并将其放入/**/尝试此代码段,它将为您的工作提供足够的提示: try { IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePag
/*selected text*/
中替换为相同的代码?
我已经设计了一个插件来在工具栏中创建一个按钮。当我单击它时,我需要它来更改所选的文本并将其放入
/**/
尝试此代码段,它将为您的工作提供足够的提示:
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();
}