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