Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / HTMLCompletionProcessor.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
20 import net.sourceforge.phpdt.internal.ui.text.java.PHPCompletionProposalComparator;
21 import net.sourceforge.phpdt.internal.ui.text.template.contentassist.TemplateEngine;
22 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.TextPresentation;
28 import org.eclipse.jface.text.contentassist.ICompletionProposal;
29 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
30 import org.eclipse.jface.text.contentassist.IContextInformation;
31 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
32 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
33 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
34 import org.eclipse.jface.text.templates.TemplateContextType;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.ui.IEditorPart;
37
38 /**
39  * HTML completion processor.
40  */
41 public class HTMLCompletionProcessor implements IContentAssistProcessor {
42
43   /**
44    * Simple content assist tip closer. The tip is valid in a range
45    * of 5 characters around its popup location.
46    */
47   protected static class Validator implements IContextInformationValidator, IContextInformationPresenter {
48
49     protected int fInstallOffset;
50
51     /*
52      * @see IContextInformationValidator#isContextInformationValid(int)
53      */
54     public boolean isContextInformationValid(int offset) {
55       return Math.abs(fInstallOffset - offset) < 5;
56     }
57
58     /*
59      * @see IContextInformationValidator#install(IContextInformation, ITextViewer, int)
60      */
61     public void install(IContextInformation info, ITextViewer viewer, int offset) {
62       fInstallOffset = offset;
63     }
64
65     /*
66      * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int, TextPresentation)
67      */
68     public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
69       return false;
70     }
71   };
72
73   private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
74
75     private final IContextInformation fContextInformation;
76     private int fPosition;
77
78     public ContextInformationWrapper(IContextInformation contextInformation) {
79       fContextInformation = contextInformation;
80     }
81
82     /*
83      * @see IContextInformation#getContextDisplayString()
84      */
85     public String getContextDisplayString() {
86       return fContextInformation.getContextDisplayString();
87     }
88
89     /*
90     * @see IContextInformation#getImage()
91     */
92     public Image getImage() {
93       return fContextInformation.getImage();
94     }
95
96     /*
97      * @see IContextInformation#getInformationDisplayString()
98      */
99     public String getInformationDisplayString() {
100       return fContextInformation.getInformationDisplayString();
101     }
102
103     /*
104      * @see IContextInformationExtension#getContextInformationPosition()
105      */
106     public int getContextInformationPosition() {
107       return fPosition;
108     }
109
110     public void setContextInformationPosition(int position) {
111       fPosition = position;
112     }
113   };
114
115   protected IContextInformationValidator fValidator = new Validator();
116   private TemplateEngine fTemplateEngine;
117   private char[] fProposalAutoActivationSet;
118   private PHPCompletionProposalComparator fComparator;
119   private int fNumberOfComputedResults = 0;
120
121   private IEditorPart fEditor;
122
123   protected IWorkingCopyManager fManager;
124
125   public HTMLCompletionProcessor(IEditorPart editor) {
126       fEditor = editor;
127       fManager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
128       
129     TemplateContextType contextType = PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType("html"); //$NON-NLS-1$
130     if (contextType != null)
131       fTemplateEngine = new TemplateEngine(contextType);
132
133     fComparator = new PHPCompletionProposalComparator();
134   }
135   
136   /* (non-Javadoc)
137    * Method declared on IContentAssistProcessor
138    */
139   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
140     int contextInformationPosition = guessContextInformationPosition(viewer, documentOffset);
141     return internalComputeCompletionProposals(viewer, documentOffset, contextInformationPosition);
142   }
143
144   private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
145     IDocument document = viewer.getDocument();
146     ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
147     
148     if (fTemplateEngine != null) {
149       ICompletionProposal[] results;
150       //      try {
151       fTemplateEngine.reset();
152       fTemplateEngine.complete(viewer, offset, unit);
153       //      } catch (JavaModelException x) {
154       //        Shell shell= viewer.getTextWidget().getShell();
155       //        ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
156       //      }       
157
158       IPHPCompletionProposal[] templateResults = fTemplateEngine.getResults();
159
160       // concatenate arrays
161       IPHPCompletionProposal[] total;
162       total = new IPHPCompletionProposal[templateResults.length];
163       System.arraycopy(templateResults, 0, total, 0, templateResults.length);
164       results = total;
165
166       fNumberOfComputedResults = (results == null ? 0 : results.length);
167       /*
168        * Order here and not in result collector to make sure that the order
169        * applies to all proposals and not just those of the compilation unit. 
170        */
171       return order(results);
172     }
173     return new IPHPCompletionProposal[0];
174   }
175
176   private int guessContextInformationPosition(ITextViewer viewer, int offset) {
177     int contextPosition = offset;
178     IDocument document = viewer.getDocument();
179     return contextPosition;
180   }
181
182   /* (non-Javadoc)
183    * Method declared on IContentAssistProcessor
184    */
185   //  public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
186   //    IContextInformation[] result = new IContextInformation[5];
187   //    for (int i = 0; i < result.length; i++)
188   //      result[i] = new ContextInformation(MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.display.pattern"), new Object[] { new Integer(i), new Integer(documentOffset)}), //$NON-NLS-1$
189   //      MessageFormat.format(PHPEditorMessages.getString("CompletionProcessor.ContextInfo.value.pattern"), new Object[] { new Integer(i), new Integer(documentOffset - 5), new Integer(documentOffset + 5)})); //$NON-NLS-1$
190   //    return result;
191   //  }
192   /**
193    * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
194    */
195   public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
196     int contextInformationPosition = guessContextInformationPosition(viewer, offset);
197     List result = addContextInformations(viewer, contextInformationPosition);
198     return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
199   }
200
201   private List addContextInformations(ITextViewer viewer, int offset) {
202     ICompletionProposal[] proposals = internalComputeCompletionProposals(viewer, offset, -1);
203
204     List result = new ArrayList();
205     for (int i = 0; i < proposals.length; i++) {
206       IContextInformation contextInformation = proposals[i].getContextInformation();
207       if (contextInformation != null) {
208         ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
209         wrapper.setContextInformationPosition(offset);
210         result.add(wrapper);
211       }
212     }
213     return result;
214   }
215
216   /**
217    * Order the given proposals.
218    */
219   private ICompletionProposal[] order(ICompletionProposal[] proposals) {
220     Arrays.sort(proposals, fComparator);
221     return proposals;
222   }
223
224   /**
225    * Tells this processor to order the proposals alphabetically.
226    * 
227    * @param order <code>true</code> if proposals should be ordered.
228    */
229   public void orderProposalsAlphabetically(boolean order) {
230     fComparator.setOrderAlphabetically(order);
231   }
232   
233   /**
234    * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
235    */
236   public char[] getCompletionProposalAutoActivationCharacters() {
237     return fProposalAutoActivationSet;
238   }
239   
240   /**
241    * Sets this processor's set of characters triggering the activation of the
242    * completion proposal computation.
243    * 
244    * @param activationSet the activation set
245    */
246   public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
247     fProposalAutoActivationSet= activationSet;
248   }
249   
250   /* (non-Javadoc)
251    * Method declared on IContentAssistProcessor
252    */
253   public char[] getContextInformationAutoActivationCharacters() {
254     return new char[] {
255     };
256   }
257
258   /* (non-Javadoc)
259    * Method declared on IContentAssistProcessor
260    */
261   public IContextInformationValidator getContextInformationValidator() {
262     return fValidator;
263   }
264
265   /* (non-Javadoc)
266    * Method declared on IContentAssistProcessor
267    */
268   public String getErrorMessage() {
269     return null;
270   }
271 }