Home > Articles > Programming > Java

📄 Contents

  1. FAQ 319: What is eScript?
  2. FAQ 320: Language integration phase 1: How do I compile and build programs?
  3. FAQ 321: How do I load source files edited outside Eclipse?
  4. FAQ 322: How do I run an external builder on my source files?
  5. FAQ 323: How do I implement a compiler that runs inside Eclipse?
  6. FAQ 324: How do I react to changes in source files?
  7. FAQ 325: How do I implement an Eclipse builder?
  8. FAQ 326: Where are project build specifications stored?
  9. FAQ 327: How do I add a builder to a given project?
  10. FAQ 328: How do I implement an incremental project builder?
  11. FAQ 329: How do I handle setup problems for a given builder?
  12. FAQ 330: How do I make my compiler incremental?
  13. FAQ 331: Language integration phase 2: How do I implement a DOM?
  14. FAQ 332: How do I implement a DOM for my language?
  15. FAQ 333: How can I ensure that my model is scalable?
  16. FAQ 334: Language integration phase 3: How do I edit programs?
  17. FAQ 335: How do I write an editor for my own language?
  18. FAQ 336: How do I add Content Assist to my language editor?
  19. FAQ 337: How do I add hover support to my text editor?
  20. FAQ 338: How do I create problem markers for my compiler?
  21. FAQ 339: How do I implement Quick Fixes for my own language?
  22. FAQ 340: How do I support refactoring for my own language?
  23. FAQ 341: How do I create an Outline view for my own language editor?
  24. FAQ 342: Language integration phase 4: What are the finishing touches?
  25. FAQ 343: What wizards do I define for my own language?
  26. FAQ 344: When does my language need its own nature?
  27. FAQ 345: When does my language need its own perspective?
  28. FAQ 346: How do I add documentation and help for my own language?
  29. FAQ 347: How do I support source-level debugging for my own language?
  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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

In FAQ 335 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();
   ca.setContentAssistProcessor(cap,
   IDocument.DEFAULT_CONTENT_TYPE);
   ca.setInformationControlCreator(
   getInformationControlCreator(sourceViewer));
   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) {
         e.printStackTrace();
      }
      return "";
   }
   public IContextInformation[] computeContextInformation(
      ITextViewer viewer, int offset) {
      return NO_CONTEXTS;
   }
   char[] getCompletionProposalAutoActivationCharacters() {
      return PROPOSAL_ACTIVATION_CHARS;
   }
   // ... 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).

Note

FAQ 267 How do I add Content Assist to my editor?
FAQ 337 How do I add hover support to my text editor?

  • + Share This
  • 🔖 Save To Your Account