为 eclipse 插件中的内容提案设置帮助 text/doc

Setting help text/doc for content proposals in eclipse plugin

我正在开发一个 eclipse 插件,它使用 SimpleContentProposalProvider class 显示文本单元格编辑器的内容建议,并且工作正常。我的问题 - 我想为正在显示的内容提案显示帮助 text/doc,类似于 java 编辑器显示每种方法的文档。我怎样才能实现这个功能。

我是否需要编写 IContentProposalProvider 的自定义实现并使用 getDescription()?

代码:

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.fieldassist.ContentProposal;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IContentProposalListener2;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;



public class TextCellEditorWithContentProposal extends TextCellEditor {

    private ContentProposalAdapter contentProposalAdapter;
    private boolean popupOpen = false; // true, iff popup is currently open

    public TextCellEditorWithContentProposal(Composite parent,
            SimpleContentProposalProvider contentProposalProvider,
            KeyStroke keyStroke, char[] autoActivationCharacters) {
        super(parent);

        enableContentProposal(contentProposalProvider, keyStroke,
                autoActivationCharacters);
    }

    private void enableContentProposal(
            SimpleContentProposalProvider contentProposalProvider,
            KeyStroke keyStroke, char[] autoActivationCharacters) {

        try {
            keyStroke = KeyStroke.getInstance("Ctrl+Space");
        } catch (ParseException e) {
            // Logger.getLogger("").error("KeyStroke Parse Exception");
        }

        contentProposalAdapter = new ContentProposalAdapter(text,
                new TextContentAdapter(), contentProposalProvider, keyStroke,
                autoActivationCharacters);

        contentProposalAdapter
                .setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

        contentProposalProvider.setFiltering(true);

        contentProposalAdapter.setLabelProvider(new ILabelProvider() {

            public void addListener(ILabelProviderListener arg0) {

            }

            public void dispose() {

            }

            public boolean isLabelProperty(Object arg0, String arg1) {
                return false;
            }

            public void removeListener(ILabelProviderListener arg0) {

            }

            public Image getImage(Object element) {

                Image img=null;
                System.out.println("Content proposal>>>>>>>>>>>>>>>>>>>"+((ContentProposal)element).getLabel());
                 try {
                     img = Activator.getImageDescriptor("icons/blue-icon.png").createImage();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return img;
            }

            public String getText(Object element) {
                return ((ContentProposal)element).getLabel();
            }

        });

        // Listen for popup open/close events to be able to handle focus
        // events correctly
        contentProposalAdapter
                .addContentProposalListener(new IContentProposalListener2() {

                    public void proposalPopupClosed(
                            ContentProposalAdapter adapter) {
                        popupOpen = false;
                    }

                    public void proposalPopupOpened(
                            ContentProposalAdapter adapter) {
                        popupOpen = true;
                    }
                });
    }

    /**
     * Return the {@link ContentProposalAdapter} of this cell editor.
     * 
     * @return the {@link ContentProposalAdapter}
     */
    public ContentProposalAdapter getContentProposalAdapter() {
        return contentProposalAdapter;
    }

    protected void focusLost() {
        if (!popupOpen) {
            // Focus lost deactivates the cell editor.
            // This must not happen if focus lost was caused by activating
            // the completion proposal popup.
            super.focusLost();
        }
    }

    protected boolean dependsOnExternalFocusListener() {
        // Always return false;
        // Otherwise, the ColumnViewerEditor will install an additional
        // focus listener
        // that cancels cell editing on focus lost, even if focus gets lost
        // due to
        // activation of the completion proposal popup. See also bug 58777.
        return false;
    }

}

是的,使用您自己的 IContentProposalProvider 并为每个提案使用 ContentProposal

一个非常简单的提供程序可能如下所示:

class SpecialCharactersProposalProvider implements IContentProposalProvider
{
  @Override
  public IContentProposal [] getProposals(String contents, int position)
  {
    return new IContentProposal [] {
        new ContentProposal("text to insert", "label", "long description"), 
       };
  }
}

ContentProposal的第三个参数是你想要的长文本。