1 /**********************************************************************
 
   2 Copyright (c) 2000, 2002 IBM Corp. and others.
 
   3 All rights reserved. This program and the accompanying materials
 
   4 are made available under the terms of the Common Public License v1.0
 
   5 which accompanies this distribution, and is available at
 
   6 http://www.eclipse.org/legal/cpl-v10.html
 
   9     IBM Corporation - Initial implementation
 
  10     Klaus Hartlage - www.eclipseproject.de
 
  11 **********************************************************************/
 
  12 package net.sourceforge.phpeclipse.phpeditor.php;
 
  14 import java.util.ArrayList;
 
  15 import java.util.Arrays;
 
  16 import java.util.List;
 
  18 import net.sourceforge.phpdt.internal.corext.template.ContextType;
 
  19 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
 
  20 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
 
  21 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
 
  22 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
 
  24 import org.eclipse.jface.text.IDocument;
 
  25 import org.eclipse.jface.text.ITextViewer;
 
  26 import org.eclipse.jface.text.TextPresentation;
 
  27 import org.eclipse.jface.text.contentassist.ICompletionProposal;
 
  28 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 
  29 import org.eclipse.jface.text.contentassist.IContextInformation;
 
  30 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
 
  31 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
 
  32 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
 
  33 import org.eclipse.swt.graphics.Image;
 
  36  * HTML completion processor.
 
  38 public class HTMLCompletionProcessor implements IContentAssistProcessor {
 
  41    * Simple content assist tip closer. The tip is valid in a range
 
  42    * of 5 characters around its popup location.
 
  44   protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
 
  46     protected int fInstallOffset;
 
  49      * @see IContextInformationValidator#isContextInformationValid(int)
 
  51     public boolean isContextInformationValid(int offset) {
 
  52       return Math.abs(fInstallOffset - offset) < 5;
 
  56      * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
 
  58     public void install(IContextInformation info, ITextViewer viewer, int offset) {
 
  59       fInstallOffset = offset;
 
  63      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
 
  65     public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
 
  70   private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
 
  72     private final IContextInformation fContextInformation;
 
  73     private int fPosition;
 
  75     public ContextInformationWrapper(IContextInformation contextInformation) {
 
  76       fContextInformation = contextInformation;
 
  80      * @see IContextInformation#getContextDisplayString()
 
  82     public String getContextDisplayString() {
 
  83       return fContextInformation.getContextDisplayString();
 
  87     * @see IContextInformation#getImage()
 
  89     public Image getImage() {
 
  90       return fContextInformation.getImage();
 
  94      * @see IContextInformation#getInformationDisplayString()
 
  96     public String getInformationDisplayString() {
 
  97       return fContextInformation.getInformationDisplayString();
 
 101      * @see IContextInformationExtension#getContextInformationPosition()
 
 103     public int getContextInformationPosition() {
 
 107     public void setContextInformationPosition(int position) {
 
 108       fPosition = position;
 
 112   protected IContextInformationValidator fValidator = new Validator();
 
 113   private TemplateEngine fTemplateEngine;
 
 114   private char[] fProposalAutoActivationSet;
 
 115   private PHPCompletionProposalComparator fComparator;
 
 116   private int fNumberOfComputedResults = 0;
 
 118   public HTMLCompletionProcessor() {
 
 120     ContextType contextType = ContextTypeRegistry.getInstance().getContextType("html"); //$NON-NLS-1$
 
 121     if (contextType != null)
 
 122       fTemplateEngine = new TemplateEngine(contextType);
 
 124     fComparator = new PHPCompletionProposalComparator();
 
 128    * Method declared on IContentAssistProcessor
 
 130   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
 
 131     int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
 
 132     return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
 
 135   private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
 
 136     IDocument document = viewer.getDocument();
 
 138     if (fTemplateEngine != null) {
 
 139       ICompletionProposal[] results;
 
 141       fTemplateEngine.reset();
 
 142       fTemplateEngine.complete(viewer, offset); //, unit);
 
 143       //      } catch (JavaModelException x) {
 
 144       //        Shell shell= viewer.getTextWidget().getShell();
 
 145       //        ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
 
 148       IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
 
 150       // concatenate arrays
 
 151       IPHPCompletionProposal[] total;
 
 152       total = new IPHPCompletionProposal[templateResults.length];
 
 153       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
 
 156       fNumberOfComputedResults = (results == null ? 0 : results.length);
 
 158        * Order here and not in result collector to make sure that the order
 
 159        * applies to all proposals and not just those of the compilation unit. 
 
 161       return order(results);
 
 163     return new IPHPCompletionProposal[0];
 
 166   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
 
 167     int contextPosition = offset;
 
 168     IDocument document = viewer.getDocument();
 
 169     return contextPosition;
 
 173    * Method declared on IContentAssistProcessor
 
 175   //  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
 
 176   //    IContextInformation[] result = new IContextInformation[5];
 
 177   //    for (int i = 0; i < result.length; i++)
 
 178   //      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
 
 179   //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
 
 183    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
 
 185   public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
 
 186     int contextInformationPosition = guessContextInformationPosition(viewer, offset);
 
 187     List result = addContextInformations(viewer, contextInformationPosition);
 
 188     return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
 
 191   private List addContextInformations(ITextViewer viewer, int offset) {
 
 192     ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
 
 194     List result = new ArrayList();
 
 195     for (int i = 0; i < proposals.length; i++) {
 
 196       IContextInformation contextInformation = proposals[i].getContextInformation();
 
 197       if (contextInformation != null) {
 
 198         ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
 
 199         wrapper.setContextInformationPosition(offset);
 
 207    * Order the given proposals.
 
 209   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
 
 210     Arrays.sort(proposals, fComparator);
 
 215    * Tells this processor to order the proposals alphabetically.
 
 217    * @param order <code>true</code> if proposals should be ordered.
 
 219   public void orderProposalsAlphabetically(boolean order) {
 
 220     fComparator.setOrderAlphabetically(order);
 
 224    * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
 
 226   public char[] getCompletionProposalAutoActivationCharacters() {
 
 227     return fProposalAutoActivationSet;
 
 231    * Sets this processor's set of characters triggering the activation of the
 
 232    * completion proposal computation.
 
 234    * @param activationSet the activation set
 
 236   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
 
 237     fProposalAutoActivationSet= activationSet;
 
 241    * Method declared on IContentAssistProcessor
 
 243   public char[] getContextInformationAutoActivationCharacters() {
 
 249    * Method declared on IContentAssistProcessor
 
 251   public IContextInformationValidator getContextInformationValidator() {
 
 256    * Method declared on IContentAssistProcessor
 
 258   public String getErrorMessage() {