我正在为 eclipse 开发一个插件。在这个插件中,我需要能够将一个项目添加到文本编辑器的上下文菜单中。到目前为止我一直没有成功,有谁知道如何添加这个项目。
此外,我如何获取包含当前在编辑器中选择的文本的字符串。
非常感谢。
最佳答案
关于选择部分,问题“Replace selected code from eclipse editor thru plugin comand”已经足够满足您的需求:
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 ) {
// Here is your String
final TextSelection textSel = (TextSelection)sel;
}
}
} catch ( Exception ex ) {
ex.printStackTrace();
}
然后您可以将此选择与弹出菜单中添加的项目相关联,如以下 SO 问题:
"How do you contribute a command to an editor context menu in Eclipse "
<command
commandId="org.my.command.IdCommand"
tooltip="My Command Tooltip"
id="org.my.popup.IdCommand">
<visibleWhen>
<with variable="selection">
<instanceof value="org.eclipse.jface.text.ITextSelection"/>
</with>
</visibleWhen>
关于java - 将项目添加到 Eclipse 文本查看器上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1694748/