Package tool.editors.contentassist

Source Code of tool.editors.contentassist.ToolCompletionProcessor

package tool.editors.contentassist;

import java.text.MessageFormat;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

import tool.ToolPlugin;
import tool.model.ToolComponent;

public class ToolCompletionProcessor implements IContentAssistProcessor {
 
  private final char[] PROPOSAL_ACTIVATION_CHARS = new char[] {'.'};
  private ICompletionProposal[] NO_COMPLETIONS = new ICompletionProposal[0];
 
  ToolComponent context;
 
  public ToolCompletionProcessor(ToolComponent context){
    this.context = context;
  }
 
  @Override
  public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
      int offset) {
    try {
      IDocument document = viewer.getDocument();
      if (isLastCharDot(document, offset)){
        String identifyer = lastIdentifier(document, offset);
        ICompletionProposal[] proposals = ProposalCache.classProposals(identifyer, offset);
        return proposals;
      } else {
        String prefix = lastWord(document, offset);
        String indent = lastIndent(document, offset);
        ICompletionProposal[] proposals = ProposalCache.languageProposals(prefix, offset, indent);
        return proposals;
      }
    } catch (Exception e){
      ToolPlugin.showError("Error computing proposals", e);
      return NO_COMPLETIONS;
    }
  }


 
  @Override
  public IContextInformation[] computeContextInformation(ITextViewer viewer,
      int offset) {
    IContextInformation[] result= new IContextInformation[5];
    for (int i= 0; i < result.length; i++)
      result[i]= new ContextInformation(
        MessageFormat.format("{0} at position {1}", new Object[] { new Integer(i), new Integer(offset) }),
        MessageFormat.format("{0} valid from {1} to {2}", new Object[] { new Integer(i), new Integer(offset - 5), new Integer(offset + 5)}));
    return result;
  }

  @Override
  public char[] getCompletionProposalAutoActivationCharacters() {
    return PROPOSAL_ACTIVATION_CHARS;
  }
 
  @Override
  public char[] getContextInformationAutoActivationCharacters() {
    return PROPOSAL_ACTIVATION_CHARS;
  }

  @Override
  public IContextInformationValidator getContextInformationValidator() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public String getErrorMessage() {
    // TODO Auto-generated method stub
    return null;
  }
  /**
   * returns the last word in upper case
   * @param doc
   * @param offset
   * @return
   */
    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).toLowerCase();
           }
        } catch (BadLocationException e) {
           ToolPlugin.showError("Error in ToolCompletionProcessor", e);
        }
        return "";
     }
    private boolean isLastCharDot(IDocument doc, int offset){
      try {
      return doc.getChar(offset-1) == '.';
    } catch (BadLocationException e) {
      ToolPlugin.showError("Error in ToolCompletionProcessor", e);
    }
    return false;
    }
    private String lastIdentifier(IDocument doc, int offset) {
     
      return lastWord(doc, offset-1);
    }
     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.isWhitespace(doc.getChar(end))) end++;
           return doc.get(start+1, end-start-1);
        } catch (BadLocationException e) {
          ToolPlugin.showError("Error in ToolCompletionProcessor", e);
        }
        return "";
     }

}
TOP

Related Classes of tool.editors.contentassist.ToolCompletionProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.