6d168b57763fe38a1a62fdbbc462b98f11604a84
[phpeclipse.git] /
1 package net.sourceforge.phpdt.internal.ui.text.java;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Hashtable;
13 import java.util.List;
14
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Shell;
18
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
26 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
27
28 import org.eclipse.ui.IEditorPart;
29
30 //import org.eclipse.jdt.core.ICompilationUnit;
31 //import org.eclipse.jdt.core.JavaCore;
32 //import org.eclipse.jdt.core.JavaModelException;
33 //
34 //import org.eclipse.jdt.ui.IWorkingCopyManager;
35 //import org.eclipse.jdt.ui.PreferenceConstants;
36
37 import net.sourceforge.phpdt.internal.corext.template.ContextType;
38 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
39 //import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import net.sourceforge.phpdt.internal.ui.JavaUIMessages;
41 //import org.eclipse.jdt.internal.ui.text.JavaCodeReader;
42 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
43
44
45 /**
46  * Java completion processor.
47  */
48 public class JavaCompletionProcessor_NotInUseVersion implements IContentAssistProcessor {
49                 
50         private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
51                 
52                 private final IContextInformation fContextInformation;
53                 private int fPosition;
54                 
55                 public ContextInformationWrapper(IContextInformation contextInformation) {
56                         fContextInformation= contextInformation;
57                 }
58                 
59                 /*
60                  * @see IContextInformation#getContextDisplayString()
61                  */
62                 public String getContextDisplayString() {
63                         return fContextInformation.getContextDisplayString();
64                 }
65
66                         /*
67                  * @see IContextInformation#getImage()
68                  */
69                 public Image getImage() {
70                         return fContextInformation.getImage();
71                 }
72
73                 /*
74                  * @see IContextInformation#getInformationDisplayString()
75                  */
76                 public String getInformationDisplayString() {
77                         return fContextInformation.getInformationDisplayString();
78                 }
79
80                 /*
81                  * @see IContextInformationExtension#getContextInformationPosition()
82                  */
83                 public int getContextInformationPosition() {
84                         return fPosition;
85                 }
86                 
87                 public void setContextInformationPosition(int position) {
88                         fPosition= position;    
89                 }
90         };
91         
92         
93 //      private final static String VISIBILITY= JavaCore.CODEASSIST_VISIBILITY_CHECK;
94         private final static String ENABLED= "enabled"; //$NON-NLS-1$
95         private final static String DISABLED= "disabled"; //$NON-NLS-1$
96         
97         
98         
99         private IEditorPart fEditor;
100 //      private ResultCollector fCollector;
101 //      private IWorkingCopyManager fManager;
102         private IContextInformationValidator fValidator;
103         
104         private char[] fProposalAutoActivationSet;
105         private JavaCompletionProposalComparator fComparator;
106         private boolean fAllowAddImports;
107          
108         private TemplateEngine fTemplateEngine;
109 //      private ExperimentalResultCollector fExperimentalCollector;
110         
111         private int fNumberOfComputedResults= 0;
112         
113         
114         public JavaCompletionProcessor_NotInUseVersion(IEditorPart editor) {
115                 fEditor= editor;
116 //              fCollector= new ResultCollector();
117 //              fManager= JavaPlugin.getDefault().getWorkingCopyManager();
118                 ContextType contextType= ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
119                 if (contextType != null)
120                         fTemplateEngine= new TemplateEngine(contextType);
121 //              fExperimentalCollector= new ExperimentalResultCollector();
122                 fAllowAddImports= true;
123                 
124                 fComparator= new JavaCompletionProposalComparator();
125         }
126         
127         /**
128          * Sets this processor's set of characters triggering the activation of the
129          * completion proposal computation.
130          * 
131          * @param activationSet the activation set
132          */
133         public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
134                 fProposalAutoActivationSet= activationSet;
135         }
136         
137         /**
138          * Tells this processor to restrict its proposal to those element
139          * visible in the actual invocation context.
140          * 
141          * @param restrict <code>true</code> if proposals should be restricted
142          */
143         public void restrictProposalsToVisibility(boolean restrict) {
144 //              Hashtable options= JavaCore.getOptions();
145 //              Object value= options.get(VISIBILITY);
146 //              if (value instanceof String) {
147 //                      String newValue= restrict ? ENABLED : DISABLED;
148 //                      if ( !newValue.equals((String) value)) {
149 //                              options.put(VISIBILITY, newValue);
150 //                              JavaCore.setOptions(options);
151 //                      }
152 //              }
153         }
154         
155         /**
156          * Tells this processor to order the proposals alphabetically.
157          * 
158          * @param order <code>true</code> if proposals should be ordered.
159          */
160         public void orderProposalsAlphabetically(boolean order) {
161                 fComparator.setOrderAlphabetically(order);
162         }
163         
164         /**
165          * Tells this processor to restrict is proposals to those
166          * starting with matching cases.
167          * 
168          * @param restrict <code>true</code> if proposals should be restricted
169          */
170         public void restrictProposalsToMatchingCases(boolean restrict) {
171                 // not yet supported
172         }
173         
174         /**
175          * Tells this processor to add import statement for proposals that have
176          * a fully qualified type name
177          * 
178          * @param restrict <code>true</code> if import can be added
179          */
180         public void allowAddingImports(boolean allowAddingImports) {
181                 fAllowAddImports= allowAddingImports;
182         }       
183                 
184         /**
185          * @see IContentAssistProcessor#getErrorMessage()
186          */  
187         public String getErrorMessage() {
188         //      if (fNumberOfComputedResults == 0)
189                         return JavaUIMessages.getString("JavaEditor.codeassist.noCompletions"); //$NON-NLS-1$
190 //              return fCollector.getErrorMessage();
191         }
192
193         /**
194          * @see IContentAssistProcessor#getContextInformationValidator()
195          */
196         public IContextInformationValidator getContextInformationValidator() {
197 //              if (fValidator == null)
198 //                      fValidator= new JavaParameterListValidator();
199                 return fValidator;
200         }
201
202         /**
203          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
204          */
205         public char[] getContextInformationAutoActivationCharacters() {
206                 return null;
207         }
208
209         /**
210          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
211          */
212         public char[] getCompletionProposalAutoActivationCharacters() {
213                 return fProposalAutoActivationSet;
214         }
215         
216 //      private boolean looksLikeMethod(JavaCodeReader reader) throws IOException {
217 //              int curr= reader.read();
218 //              while (curr != JavaCodeReader.EOF && Character.isWhitespace((char) curr))
219 //                      curr= reader.read();
220 //                      
221 //              if (curr == JavaCodeReader.EOF)
222 //                      return false;
223 //
224 //              return Character.isJavaIdentifierPart((char) curr) || Character.isJavaIdentifierStart((char) curr);
225 //      }
226         
227         private int guessContextInformationPosition(ITextViewer viewer, int offset) {
228                 int contextPosition= offset;
229                         
230                 IDocument document= viewer.getDocument();
231                 
232 //              try {
233 //
234 //                      JavaCodeReader reader= new JavaCodeReader();
235 //                      reader.configureBackwardReader(document, offset, true, true);
236 //      
237 //                      int nestingLevel= 0;
238 //
239 //                      int curr= reader.read();                
240 //                      while (curr != JavaCodeReader.EOF) {
241 //
242 //                              if (')' == (char) curr)
243 //                                      ++ nestingLevel;
244 //
245 //                              else if ('(' == (char) curr) {
246 //                                      -- nestingLevel;
247 //                              
248 //                                      if (nestingLevel < 0) {
249 //                                              int start= reader.getOffset();
250 //                                              if (looksLikeMethod(reader))
251 //                                                      return start + 1;
252 //                                      }       
253 //                              }
254 //
255 //                              curr= reader.read();                                    
256 //                      }
257 //              } catch (IOException e) {
258 //              }
259                 
260                 return contextPosition;
261         }               
262         
263         private List addContextInformations(ITextViewer viewer, int offset) {
264                 ICompletionProposal[] proposals= internalComputeCompletionProposals(viewer, offset, -1);
265
266                 List result= new ArrayList();
267                 for (int i= 0; i < proposals.length; i++) {
268                         IContextInformation contextInformation= proposals[i].getContextInformation();
269                         if (contextInformation != null) {
270                                 ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation);
271                                 wrapper.setContextInformationPosition(offset);
272                                 result.add(wrapper);                            
273                         }
274                 }
275                 return result;
276         }
277         
278         /**
279          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
280          */
281         public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
282                 int contextInformationPosition= guessContextInformationPosition(viewer, offset);
283                 List result= addContextInformations(viewer, contextInformationPosition);
284                 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
285         }
286         
287         /**
288          * Order the given proposals.
289          */
290         private ICompletionProposal[] order(ICompletionProposal[] proposals) {
291                 Arrays.sort(proposals, fComparator);
292                 return proposals;       
293         }
294         
295         /**
296          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
297          */
298         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
299                 int contextInformationPosition= guessContextInformationPosition(viewer, offset);
300                 return internalComputeCompletionProposals(viewer, offset, contextInformationPosition);
301         }
302         
303         private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
304                 
305 //              ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
306 //              IJavaCompletionProposal[] results;
307 //
308 //              if (JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES)) {
309 //                              
310 //                      try {
311 //                              if (unit != null) {
312 //      
313 //                                      fExperimentalCollector.reset(offset, contextOffset, unit.getJavaProject(), fAllowAddImports ? unit : null);
314 //                                      fExperimentalCollector.setViewer(viewer);
315 //                                      
316 //                                      Point selection= viewer.getSelectedRange();
317 //                                      if (selection.y > 0)
318 //                                              fExperimentalCollector.setReplacementLength(selection.y);
319 //                                      
320 //                                      unit.codeComplete(offset, fExperimentalCollector);
321 //                              }
322 //                      } catch (JavaModelException x) {
323 //                              Shell shell= viewer.getTextWidget().getShell();
324 //                              ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
325 //                      }                               
326 //
327 //                      results= fExperimentalCollector.getResults();
328 //
329 //              } else {
330 //
331 //                      try {
332 //                              if (unit != null) {
333 //      
334 //                                      fCollector.reset(offset, contextOffset, unit.getJavaProject(), fAllowAddImports ? unit : null);
335 //                                      Point selection= viewer.getSelectedRange();
336 //                                      if (selection.y > 0)
337 //                                              fCollector.setReplacementLength(selection.y);
338 //                                      
339 //                                      unit.codeComplete(offset, fCollector);
340 //                              }
341 //                      } catch (JavaModelException x) {
342 //                              Shell shell= viewer.getTextWidget().getShell();
343 //                              ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
344 //                      }
345 //                      
346 //                      results= fCollector.getResults();
347 //              }
348
349                 if (fTemplateEngine != null) {
350           IJavaCompletionProposal[] results;
351 //                      try {
352                                 fTemplateEngine.reset();
353                                 fTemplateEngine.complete(viewer, offset); //, unit);
354 //                      } catch (JavaModelException x) {
355 //                              Shell shell= viewer.getTextWidget().getShell();
356 //                              ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
357 //                      }                               
358                          
359                         IJavaCompletionProposal[] templateResults= fTemplateEngine.getResults();
360         
361                         // concatenate arrays
362                         IJavaCompletionProposal[] total= new IJavaCompletionProposal[templateResults.length]; // +results.length ];
363                         System.arraycopy(templateResults, 0, total, 0, templateResults.length);
364 //                      System.arraycopy(results, 0, total, templateResults.length, results.length);
365                         results= total;
366 //              }
367                 
368                 fNumberOfComputedResults= (results == null ? 0 : results.length);
369                 
370                 /*
371                  * Order here and not in result collector to make sure that the order
372                  * applies to all proposals and not just those of the compilation unit. 
373                  */
374                 return order(results);
375     }
376     return new IJavaCompletionProposal[0];
377         }
378 }