Skip to main content
Jump to: navigation, search

FAQ How do I add Content Assist to my language editor?

Revision as of 16:36, 14 March 2006 by Claffra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

InFAQ_How_do_I_write_an_editor_for_my_own_language? we describe how Content Assist is installed through our configuration class, as follows:

   class Configuration extends SourceViewerConfiguration {
      public IContentAssistant getContentAssistant(ISourceViewer 
       sourceViewer) {
         ContentAssistant ca = new ContentAssistant();
         IContentAssistProcessor cap = 
                             '''new CompletionProcessor()''';
         return ca;

A completion processor takes the current insertion point in the editor and figures out a list of continuation proposals for the user to choose from. Our completion processor looks something like this:

   class CompletionProcessor implements 
                                     IContentAssistProcessor { 
      private final IContextInformation[] NO_CONTEXTS = 
         new IContextInformation[0];
      private final char[] PROPOSAL_ACTIVATION_CHARS = 
         new char[] { 's','f','p','n','m', };
      private ICompletionProposal[] NO_COMPLETIONS = 
         new ICompletionProposal[0];

      public ICompletionProposal[] computeCompletionProposals(
         ITextViewer viewer, int offset) {
         try {
            IDocument document = viewer.getDocument();
            ArrayList result = new ArrayList();
            String prefix = lastWord(document, offset);
            String indent = lastIndent(document, offset);
            EscriptModel model = 
                        EscriptModel.getModel(document, null);
            model.getContentProposals(prefix, indent, 
                                             offset, result);
            return (ICompletionProposal[]) result.toArray(
               new ICompletionProposal[result.size()]);
         } catch (Exception e) {
            // ... log the exception ...
            return NO_COMPLETIONS;
      private String lastWord(IDocument doc, int offset) {
         try {
            for (int n = offset-1; n >= 0; n--) {
              char c = doc.getChar(n);
              if (!Character.isJavaIdentifierPart(c))
                return doc.get(n + 1, offset-n-1);
         } catch (BadLocationException e) {
            // ... log the exception ...
         return "";
      private String lastIndent(IDocument doc, int offset) {
         try {
            int start = offset-1; 
            while (start >= 0 && 
               doc.getChar(start)!= '\n') start--;
            int end = start;
            while (end < offset && 
               Character.isSpaceChar(doc.getChar(end))) end++;
            return doc.get(start+1, end-start-1);
         } catch (BadLocationException e) {
         return "";
      public IContextInformation[] computeContextInformation(
         ITextViewer viewer, int offset) { 
         return NO_CONTEXTS;
      char[] getCompletionProposalAutoActivationCharacters() {
      // ... remaining methods are optional ...

Basically, Content Assist completion has three steps. First, we have to figure out what string has already been started by the user (see lastWord). Second, we have to find appropriate completions. Third, we have to return strings so that when they are inserted, they lay out acceptably (see the use of lastIndent).

See Also:



This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top