javaeclipse-plugineclipse-rcpcontent-assist

Add style to CompletionProposal in Eclipse RCP (Content assist)


I'm creating a custom content assist for an editor, this is how I'm creating the proposals:

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    String test = "Test";
    ContextInformation contextInfo = new ContextInformation("Context display test", "information display test");
    CompletionProposal proposal = new CompletionProposal(test,
            offset, 
            0, 
            test.length(),
            Activator.getImage("icons/sample.png"), 
            test,
            contextInfo,
            "Additional info");
    return new ICompletionProposal[] {proposal}; 
}

This is the result:

enter image description here

Which is fine, but for example, in the content assist of the Java editor, they are using colors such as blue and gray:

enter image description here

I know there is a class called StyledText that could help but I can't find a good example to use it in combination with CompletionProposal.


Solution

  • The extension interface ICompletionProposalExtension6 supports styled display strings. Its sole method getStyledDisplayString() must return a StyledString that is used for display.

    Instead of creating an instance of CompletionProposal you would have to implement your own ICompletionProposal that also implements the above mentioned extension. For example:

    class StyledCompletionProposal 
        implements ICompletionProposal, ICompletionProposalExtension6 
    {
    
      ...
    
      @Override
      public StyledString getStyledDisplayString() {
        return new StyledString("test").append(" [10%]", Styler.QUALIFIER_STYLER);
      }
    
    }
    

    In addition, the content assistant must be configured to enable coloured labels. For editors, this is usually done in SourceViewerConfiguration::getContentAssistant:

    ContentAssistant contentAssistant = new ContentAssistant();
    contentAssistant.enableColoredLabels(true);