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