![](/img/trans.png)
[英]How to set the minimum height and icon with of a text menu or context menu item?
[英]Adding item to Eclipse text viewer context menu
我正在為eclipse開發一個插件。 在這個插件中,我需要能夠在文本編輯器中的上下文菜單中添加一個項目。 到目前為止,我一直沒有成功,有沒有人知道如何添加這個項目。
另外,如何獲取當前在編輯器中選擇的文本的字符串。
非常感謝。
關於選擇部分,“ 通過插件命令從eclipse編輯器中替換所選代碼 ”這一問題足以滿足您的需求:
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問題:
“ 你如何為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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.