Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I add Content Assist to my language editor?"

 
m
Line 1: Line 1:
In[[FAQ_How_do_I_write_an_editor_for_my_own_language%3F]]
+
In [[FAQ How do I write an editor for my own language?]] we describe how Content Assist is installed  through our configuration class, as follows:
we describe how Content Assist is installed  through our configuration  
+
class, as follows:
+
 
<pre>
 
<pre>
 
   class Configuration extends SourceViewerConfiguration {
 
   class Configuration extends SourceViewerConfiguration {
 
       ...
 
       ...
       public IContentAssistant getContentAssistant(ISourceViewer  
+
       public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
      sourceViewer) {
+
 
         ContentAssistant ca = new ContentAssistant();
 
         ContentAssistant ca = new ContentAssistant();
         IContentAssistProcessor cap =  
+
         IContentAssistProcessor cap = '''new CompletionProcessor()''';
                            '''new CompletionProcessor()''';
+
         ca.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE);
         ca.setContentAssistProcessor(cap,  
+
         ca.setInformationControlCreator(getInformationControlCreator(sourceViewer));
            IDocument.DEFAULT_CONTENT_TYPE);
+
         ca.setInformationControlCreator(
+
            getInformationControlCreator(sourceViewer));
+
 
         return ca;
 
         return ca;
 
       }
 
       }
Line 20: Line 14:
 
</pre>
 
</pre>
  
A completion processor takes the current insertion point in the editor
+
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:
and figures out a list of continuation proposals for the user to choose from.
+
Our completion processor looks something like this:
+
 
<pre>
 
<pre>
   class CompletionProcessor implements  
+
   class CompletionProcessor implements IContentAssistProcessor {  
                                    IContentAssistProcessor {  
+
       private final IContextInformation[] NO_CONTEXTS = new IContextInformation[0];
       private final IContextInformation[] NO_CONTEXTS =  
+
       private final char[] PROPOSAL_ACTIVATION_CHARS = new char[] { 's','f','p','n','m', };
        new IContextInformation[0];
+
       private ICompletionProposal[] NO_COMPLETIONS = new ICompletionProposal[0];
       private final char[] PROPOSAL_ACTIVATION_CHARS =  
+
        new char[] { 's','f','p','n','m', };
+
       private ICompletionProposal[] NO_COMPLETIONS =  
+
        new ICompletionProposal[0];
+
  
       public ICompletionProposal[] computeCompletionProposals(
+
       public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
        ITextViewer viewer, int offset) {
+
 
         try {
 
         try {
 
             IDocument document = viewer.getDocument();
 
             IDocument document = viewer.getDocument();
Line 40: Line 27:
 
             String prefix = lastWord(document, offset);
 
             String prefix = lastWord(document, offset);
 
             String indent = lastIndent(document, offset);
 
             String indent = lastIndent(document, offset);
             EscriptModel model =  
+
             EscriptModel model = EscriptModel.getModel(document, null);
                        EscriptModel.getModel(document, null);
+
             model.getContentProposals(prefix, indent, offset, result);
             model.getContentProposals(prefix, indent,  
+
             return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]);
                                            offset, result);
+
             return (ICompletionProposal[]) result.toArray(
+
              new ICompletionProposal[result.size()]);
+
 
         } catch (Exception e) {
 
         } catch (Exception e) {
 
             // ... log the exception ...
 
             // ... log the exception ...
Line 66: Line 50:
 
         try {
 
         try {
 
             int start = offset-1;  
 
             int start = offset-1;  
             while (start &gt;= 0 &amp;&amp;  
+
             while (start &gt;= 0 &amp;&amp; doc.getChar(start)!= '\n') start--;
              doc.getChar(start)!= '\n') start--;
+
 
             int end = start;
 
             int end = start;
             while (end &lt; offset &amp;&amp;  
+
             while (end &lt; offset &amp;&amp; Character.isSpaceChar(doc.getChar(end))) end++;
              Character.isSpaceChar(doc.getChar(end))) end++;
+
 
             return doc.get(start+1, end-start-1);
 
             return doc.get(start+1, end-start-1);
 
         } catch (BadLocationException e) {
 
         } catch (BadLocationException e) {
Line 77: Line 59:
 
         return "";
 
         return "";
 
       }
 
       }
       public IContextInformation[] computeContextInformation(
+
       public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {  
        ITextViewer viewer, int offset) {  
+
 
         return NO_CONTEXTS;
 
         return NO_CONTEXTS;
 
       }
 
       }
Line 87: Line 68:
 
   }
 
   }
 
</pre>
 
</pre>
Basically, Content Assist completion has three steps. First, we  
+
Basically, Content Assist completion has three steps. First, we have to figure out what string has already been started by the user (see <tt>lastWord</tt>). 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 <tt>lastIndent</tt>).
have to figure out what string has already been started by the user
+
(see <tt>lastWord</tt>). 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 <tt>lastIndent</tt>).
+
 
+
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ How do I add Content Assist to my editor?]]
 +
*[[FAQ How do I add hover support to my text editor?]]
  
[[FAQ_How_do_I_add_Content_Assist_to_my_editor%3F]]
+
{{Template:FAQ_Tagline}}
 
+
[[FAQ_How_do_I_add_hover_support_to_my_text_editor%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Revision as of 00:28, 9 June 2006

In FAQ 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()''';
         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).

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