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