KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > java > CompletionProposalCollector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.text.java;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.Status;
24
25 import org.eclipse.swt.graphics.Image;
26
27 import org.eclipse.jface.resource.ImageDescriptor;
28
29 import org.eclipse.jface.text.contentassist.IContextInformation;
30
31 import org.eclipse.jdt.core.CompletionContext;
32 import org.eclipse.jdt.core.CompletionProposal;
33 import org.eclipse.jdt.core.CompletionRequestor;
34 import org.eclipse.jdt.core.ICompilationUnit;
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.IType;
38 import org.eclipse.jdt.core.Signature;
39 import org.eclipse.jdt.core.compiler.IProblem;
40
41 import org.eclipse.jdt.internal.corext.util.TypeFilter;
42
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.text.java.AnonymousTypeCompletionProposal;
46 import org.eclipse.jdt.internal.ui.text.java.AnonymousTypeProposalInfo;
47 import org.eclipse.jdt.internal.ui.text.java.FieldProposalInfo;
48 import org.eclipse.jdt.internal.ui.text.java.GetterSetterCompletionProposal;
49 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
50 import org.eclipse.jdt.internal.ui.text.java.JavaMethodCompletionProposal;
51 import org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal;
52 import org.eclipse.jdt.internal.ui.text.java.LazyJavaTypeCompletionProposal;
53 import org.eclipse.jdt.internal.ui.text.java.MethodDeclarationCompletionProposal;
54 import org.eclipse.jdt.internal.ui.text.java.MethodProposalInfo;
55 import org.eclipse.jdt.internal.ui.text.java.OverrideCompletionProposal;
56 import org.eclipse.jdt.internal.ui.text.java.ProposalContextInformation;
57 import org.eclipse.jdt.internal.ui.text.javadoc.JavadocInlineTagCompletionProposal;
58 import org.eclipse.jdt.internal.ui.text.javadoc.JavadocLinkTypeCompletionProposal;
59 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
60
61 /**
62  * Java UI implementation of <code>CompletionRequestor</code>. Produces
63  * {@link IJavaCompletionProposal}s from the proposal descriptors received via
64  * the <code>CompletionRequestor</code> interface.
65  * <p>
66  * The lifecycle of a <code>CompletionProposalCollector</code> instance is very
67  * simple:
68  * <pre>
69  * ICompilationUnit unit= ...
70  * int offset= ...
71  *
72  * CompletionProposalCollector collector= new CompletionProposalCollector(unit);
73  * unit.codeComplete(offset, collector);
74  * IJavaCompletionProposal[] proposals= collector.getJavaCompletionProposals();
75  * String errorMessage= collector.getErrorMessage();
76  *
77  * &#x2f;&#x2f; display &#x2f; process proposals
78  * </pre>
79  * Note that after a code completion operation, the collector will store any
80  * received proposals, which may require a considerable amount of memory, so the
81  * collector should not be kept as a reference after a completion operation.
82  * </p>
83  * <p>
84  * Clients may instantiate or subclass.
85  * </p>
86  * @since 3.1
87  */

88 public class CompletionProposalCollector extends CompletionRequestor {
89
90     /** Tells whether this class is in debug mode. */
91     private static final boolean DEBUG= "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.jdt.ui/debug/ResultCollector")); //$NON-NLS-1$//$NON-NLS-2$
92

93     /** Triggers for method proposals without parameters. Do not modify. */
94     protected final static char[] METHOD_TRIGGERS= new char[] { ';', ',', '.', '\t', '[', ' ' };
95     /** Triggers for method proposals. Do not modify. */
96     protected final static char[] METHOD_WITH_ARGUMENTS_TRIGGERS= new char[] { '(', '-', ' ' };
97     /** Triggers for types. Do not modify. */
98     protected final static char[] TYPE_TRIGGERS= new char[] { '.', '\t', '[', '(', ' ' };
99     /** Triggers for variables. Do not modify. */
100     protected final static char[] VAR_TRIGGER= new char[] { '\t', ' ', '=', ';', '.' };
101
102     private final CompletionProposalLabelProvider fLabelProvider= new CompletionProposalLabelProvider();
103     private final ImageDescriptorRegistry fRegistry= JavaPlugin.getImageDescriptorRegistry();
104
105     private final List JavaDoc fJavaProposals= new ArrayList JavaDoc();
106     private final List JavaDoc fKeywords= new ArrayList JavaDoc();
107     private final Set JavaDoc fSuggestedMethodNames= new HashSet JavaDoc();
108
109     private final ICompilationUnit fCompilationUnit;
110     private final IJavaProject fJavaProject;
111     private int fUserReplacementLength;
112
113     private CompletionContext fContext;
114     private IProblem fLastProblem;
115
116     /* performance instrumentation */
117     private long fStartTime;
118     private long fUITime;
119
120     /**
121      * The UI invocation context or <code>null</code>.
122      *
123      * @since 3.2
124      */

125     private JavaContentAssistInvocationContext fInvocationContext;
126
127     /**
128      * Creates a new instance ready to collect proposals. If the passed
129      * <code>ICompilationUnit</code> is not contained in an
130      * {@link IJavaProject}, no javadoc will be available as
131      * {@link org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo() additional info}
132      * on the created proposals.
133      *
134      * @param cu the compilation unit that the result collector will operate on
135      */

136     public CompletionProposalCollector(ICompilationUnit cu) {
137         this(cu.getJavaProject(), cu);
138     }
139
140     /**
141      * Creates a new instance ready to collect proposals. Note that proposals
142      * for anonymous types and method declarations are not created when using
143      * this constructor, as those need to know the compilation unit that they
144      * are created on. Use
145      * {@link CompletionProposalCollector#CompletionProposalCollector(ICompilationUnit)}
146      * instead to get all proposals.
147      * <p>
148      * If the passed Java project is <code>null</code>, no javadoc will be
149      * available as
150      * {@link org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo() additional info}
151      * on the created (e.g. method and type) proposals.
152      * </p>
153      * @param project the project that the result collector will operate on, or
154      * <code>null</code>
155      */

156     public CompletionProposalCollector(IJavaProject project) {
157         this(project, null);
158     }
159
160     private CompletionProposalCollector(IJavaProject project, ICompilationUnit cu) {
161         fJavaProject= project;
162         fCompilationUnit= cu;
163
164         fUserReplacementLength= -1;
165     }
166     
167     /**
168      * Sets the invocation context.
169      * <p>
170      * Subclasses may extend.
171      * </p>
172      *
173      * @param context the invocation context
174      * @see #getInvocationContext()
175      * @since 3.2
176      */

177     public void setInvocationContext(JavaContentAssistInvocationContext context) {
178         Assert.isNotNull(context);
179         fInvocationContext= context;
180         context.setCollector(this);
181     }
182     
183     /**
184      * Returns the invocation context. If none has been set via
185      * {@link #setInvocationContext(JavaContentAssistInvocationContext)}, a new one is created.
186      *
187      * @return invocationContext the invocation context
188      * @since 3.2
189      */

190     protected final JavaContentAssistInvocationContext getInvocationContext() {
191         if (fInvocationContext == null)
192             setInvocationContext(new JavaContentAssistInvocationContext(getCompilationUnit()));
193         return fInvocationContext;
194     }
195
196     /**
197      * {@inheritDoc}
198      * <p>
199      * Subclasses may replace, but usually should not need to. Consider
200      * replacing
201      * {@linkplain #createJavaCompletionProposal(CompletionProposal) createJavaCompletionProposal}
202      * instead.
203      * </p>
204      */

205     public void accept(CompletionProposal proposal) {
206         long start= DEBUG ? System.currentTimeMillis() : 0;
207         try {
208             if (isFiltered(proposal))
209                 return;
210
211             if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION) {
212                 acceptPotentialMethodDeclaration(proposal);
213             } else {
214                 IJavaCompletionProposal javaProposal= createJavaCompletionProposal(proposal);
215                 if (javaProposal != null) {
216                     fJavaProposals.add(javaProposal);
217                     if (proposal.getKind() == CompletionProposal.KEYWORD)
218                         fKeywords.add(javaProposal);
219                 }
220             }
221         } catch (IllegalArgumentException JavaDoc e) {
222             // all signature processing method may throw IAEs
223
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=84657
224
// don't abort, but log and show all the valid proposals
225
JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "Exception when processing proposal for: " + String.valueOf(proposal.getCompletion()), e)); //$NON-NLS-1$
226
}
227
228         if (DEBUG) fUITime += System.currentTimeMillis() - start;
229     }
230
231     /**
232      * {@inheritDoc}
233      * <p>
234      * Subclasses may extend, but usually should not need to.
235      * </p>
236      * @see #getContext()
237      */

238     public void acceptContext(CompletionContext context) {
239         fContext= context;
240         fLabelProvider.setContext(context);
241     }
242
243     /**
244      * {@inheritDoc}
245      *
246      * Subclasses may extend, but must call the super implementation.
247      */

248     public void beginReporting() {
249         if (DEBUG) {
250             fStartTime= System.currentTimeMillis();
251             fUITime= 0;
252         }
253
254         fLastProblem= null;
255         fJavaProposals.clear();
256         fKeywords.clear();
257         fSuggestedMethodNames.clear();
258     }
259
260     /**
261      * {@inheritDoc}
262      *
263      * Subclasses may extend, but must call the super implementation.
264      */

265     public void completionFailure(IProblem problem) {
266         fLastProblem= problem;
267     }
268
269     /**
270      * {@inheritDoc}
271      *
272      * Subclasses may extend, but must call the super implementation.
273      */

274     public void endReporting() {
275         if (DEBUG) {
276             long total= System.currentTimeMillis() - fStartTime;
277             System.err.println("Core Collector (core):\t" + (total - fUITime)); //$NON-NLS-1$
278
System.err.println("Core Collector (ui):\t" + fUITime); //$NON-NLS-1$
279
}
280     }
281
282     /**
283      * Returns an error message about any error that may have occurred during
284      * code completion, or the empty string if none.
285      * <p>
286      * Subclasses may replace or extend.
287      * </p>
288      * @return an error message or the empty string
289      */

290     public String JavaDoc getErrorMessage() {
291         if (fLastProblem != null)
292             return fLastProblem.getMessage();
293         return ""; //$NON-NLS-1$
294
}
295
296     /**
297      * Returns the unsorted list of received proposals.
298      *
299      * @return the unsorted list of received proposals
300      */

301     public final IJavaCompletionProposal[] getJavaCompletionProposals() {
302         return (IJavaCompletionProposal[]) fJavaProposals.toArray(new IJavaCompletionProposal[fJavaProposals.size()]);
303     }
304
305     /**
306      * Returns the unsorted list of received keyword proposals.
307      *
308      * @return the unsorted list of received keyword proposals
309      */

310     public final IJavaCompletionProposal[] getKeywordCompletionProposals() {
311         return (JavaCompletionProposal[]) fKeywords.toArray(new JavaCompletionProposal[fKeywords.size()]);
312     }
313
314     /**
315      * If the replacement length is set, it overrides the length returned from
316      * the content assist infrastructure. Use this setting if code assist is
317      * called with a none empty selection.
318      *
319      * @param length the new replacement length, relative to the code assist
320      * offset. Must be equal to or greater than zero.
321      */

322     public final void setReplacementLength(int length) {
323         Assert.isLegal(length >= 0);
324         fUserReplacementLength= length;
325     }
326
327     /**
328      * Computes the relevance for a given <code>CompletionProposal</code>.
329      * <p>
330      * Subclasses may replace, but usually should not need to.
331      * </p>
332      * @param proposal the proposal to compute the relevance for
333      * @return the relevance for <code>proposal</code>
334      */

335     protected int computeRelevance(CompletionProposal proposal) {
336         final int baseRelevance= proposal.getRelevance() * 16;
337         switch (proposal.getKind()) {
338             case CompletionProposal.PACKAGE_REF:
339                 return baseRelevance + 0;
340             case CompletionProposal.LABEL_REF:
341                 return baseRelevance + 1;
342             case CompletionProposal.KEYWORD:
343                 return baseRelevance + 2;
344             case CompletionProposal.TYPE_REF:
345             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
346                 return baseRelevance + 3;
347             case CompletionProposal.METHOD_REF:
348             case CompletionProposal.METHOD_NAME_REFERENCE:
349             case CompletionProposal.METHOD_DECLARATION:
350             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
351                 return baseRelevance + 4;
352             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
353                 return baseRelevance + 4 /* + 99 */;
354             case CompletionProposal.FIELD_REF:
355                 return baseRelevance + 5;
356             case CompletionProposal.LOCAL_VARIABLE_REF:
357             case CompletionProposal.VARIABLE_DECLARATION:
358                 return baseRelevance + 6;
359             default:
360                 return baseRelevance;
361         }
362     }
363
364     /**
365      * Creates a new java completion proposal from a core proposal. This may
366      * involve computing the display label and setting up some context.
367      * <p>
368      * This method is called for every proposal that will be displayed to the
369      * user, which may be hundreds. Implementations should therefore defer as
370      * much work as possible: Labels should be computed lazily to leverage
371      * virtual table usage, and any information only needed when
372      * <em>applying</em> a proposal should not be computed yet.
373      * </p>
374      * <p>
375      * Implementations may return <code>null</code> if a proposal should not
376      * be included in the list presented to the user.
377      * </p>
378      * <p>
379      * Subclasses may extend or replace this method.
380      * </p>
381      *
382      * @param proposal the core completion proposal to create a UI proposal for
383      * @return the created java completion proposal, or <code>null</code> if
384      * no proposal should be displayed
385      */

386     protected IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
387         switch (proposal.getKind()) {
388             case CompletionProposal.KEYWORD:
389                 return createKeywordProposal(proposal);
390             case CompletionProposal.PACKAGE_REF:
391                 return createPackageProposal(proposal);
392             case CompletionProposal.TYPE_REF:
393                 return createTypeProposal(proposal);
394             case CompletionProposal.JAVADOC_TYPE_REF:
395                 return createJavadocLinkTypeProposal(proposal);
396             case CompletionProposal.FIELD_REF:
397             case CompletionProposal.JAVADOC_FIELD_REF:
398             case CompletionProposal.JAVADOC_VALUE_REF:
399                 return createFieldProposal(proposal);
400             case CompletionProposal.METHOD_REF:
401             case CompletionProposal.METHOD_NAME_REFERENCE:
402             case CompletionProposal.JAVADOC_METHOD_REF:
403                 return createMethodReferenceProposal(proposal);
404             case CompletionProposal.METHOD_DECLARATION:
405                 return createMethodDeclarationProposal(proposal);
406             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
407                 return createAnonymousTypeProposal(proposal);
408             case CompletionProposal.LABEL_REF:
409                 return createLabelProposal(proposal);
410             case CompletionProposal.LOCAL_VARIABLE_REF:
411             case CompletionProposal.VARIABLE_DECLARATION:
412                 return createLocalVariableProposal(proposal);
413             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
414                 return createAnnotationAttributeReferenceProposal(proposal);
415             case CompletionProposal.JAVADOC_BLOCK_TAG:
416             case CompletionProposal.JAVADOC_PARAM_REF:
417                 return createJavadocSimpleProposal(proposal);
418             case CompletionProposal.JAVADOC_INLINE_TAG:
419                 return createJavadocInlineTagProposal(proposal);
420             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
421             default:
422                 return null;
423         }
424     }
425
426     /**
427      * Creates the context information for a given method reference proposal.
428      * The passed proposal must be of kind {@link CompletionProposal#METHOD_REF}.
429      *
430      * @param methodProposal the method proposal for which to create context information
431      * @return the context information for <code>methodProposal</code>
432      */

433     protected final IContextInformation createMethodContextInformation(CompletionProposal methodProposal) {
434         Assert.isTrue(methodProposal.getKind() == CompletionProposal.METHOD_REF);
435         return new ProposalContextInformation(methodProposal);
436     }
437
438     /**
439      * Returns the compilation unit that the receiver operates on, or
440      * <code>null</code> if the <code>IJavaProject</code> constructor was
441      * used to create the receiver.
442      *
443      * @return the compilation unit that the receiver operates on, or
444      * <code>null</code>
445      */

446     protected final ICompilationUnit getCompilationUnit() {
447         return fCompilationUnit;
448     }
449
450     /**
451      * Returns the <code>CompletionContext</code> for this completion operation.
452
453      * @return the <code>CompletionContext</code> for this completion operation
454      * @see CompletionRequestor#acceptContext(CompletionContext)
455      */

456     protected final CompletionContext getContext() {
457         return fContext;
458     }
459
460     /**
461      * Returns a cached image for the given descriptor.
462      *
463      * @param descriptor the image descriptor to get an image for, may be
464      * <code>null</code>
465      * @return the image corresponding to <code>descriptor</code>
466      */

467     protected final Image getImage(ImageDescriptor descriptor) {
468         return (descriptor == null) ? null : fRegistry.get(descriptor);
469     }
470
471     /**
472      * Returns the proposal label provider used by the receiver.
473      *
474      * @return the proposal label provider used by the receiver
475      */

476     protected final CompletionProposalLabelProvider getLabelProvider() {
477         return fLabelProvider;
478     }
479
480     /**
481      * Returns the replacement length of a given completion proposal. The
482      * replacement length is usually the difference between the return values of
483      * <code>proposal.getReplaceEnd</code> and
484      * <code>proposal.getReplaceStart</code>, but this behavior may be
485      * overridden by calling {@link #setReplacementLength(int)}.
486      *
487      * @param proposal the completion proposal to get the replacement length for
488      * @return the replacement length for <code>proposal</code>
489      */

490     protected final int getLength(CompletionProposal proposal) {
491         int start= proposal.getReplaceStart();
492         int end= proposal.getReplaceEnd();
493         int length;
494         if (fUserReplacementLength == -1) {
495             length= end - start;
496         } else {
497             length= fUserReplacementLength;
498             // extend length to begin at start
499
int behindCompletion= proposal.getCompletionLocation() + 1;
500             if (start < behindCompletion) {
501                 length+= behindCompletion - start;
502             }
503         }
504         return length;
505     }
506
507     /**
508      * Returns <code>true</code> if <code>proposal</code> is filtered, e.g.
509      * should not be proposed to the user, <code>false</code> if it is valid.
510      * <p>
511      * Subclasses may extends this method. The default implementation filters
512      * proposals set to be ignored via
513      * {@linkplain CompletionRequestor#setIgnored(int, boolean) setIgnored} and
514      * types set to be ignored in the preferences.
515      * </p>
516      *
517      * @param proposal the proposal to filter
518      * @return <code>true</code> to filter <code>proposal</code>,
519      * <code>false</code> to let it pass
520      */

521     protected boolean isFiltered(CompletionProposal proposal) {
522         if (isIgnored(proposal.getKind()))
523             return true;
524         char[] declaringType= getDeclaringType(proposal);
525         return declaringType!= null && TypeFilter.isFiltered(declaringType);
526     }
527
528     /**
529      * Returns the type signature of the declaring type of a
530      * <code>CompletionProposal</code>, or <code>null</code> for proposals
531      * that do not have a declaring type. The return value is <em>not</em>
532      * <code>null</code> for proposals of the following kinds:
533      * <ul>
534      * <li>METHOD_DECLARATION</li>
535      * <li>METHOD_NAME_REFERENCE</li>
536      * <li>METHOD_REF</li>
537      * <li>ANNOTATION_ATTRIBUTE_REF</li>
538      * <li>POTENTIAL_METHOD_DECLARATION</li>
539      * <li>ANONYMOUS_CLASS_DECLARATION</li>
540      * <li>FIELD_REF</li>
541      * <li>PACKAGE_REF (returns the package, but no type)</li>
542      * <li>TYPE_REF</li>
543      * </ul>
544      *
545      * @param proposal the completion proposal to get the declaring type for
546      * @return the type signature of the declaring type, or <code>null</code> if there is none
547      * @see Signature#toCharArray(char[])
548      */

549     protected final char[] getDeclaringType(CompletionProposal proposal) {
550         switch (proposal.getKind()) {
551             case CompletionProposal.METHOD_DECLARATION:
552             case CompletionProposal.METHOD_NAME_REFERENCE:
553             case CompletionProposal.JAVADOC_METHOD_REF:
554             case CompletionProposal.METHOD_REF:
555             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
556             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
557             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
558             case CompletionProposal.FIELD_REF:
559             case CompletionProposal.JAVADOC_FIELD_REF:
560             case CompletionProposal.JAVADOC_VALUE_REF:
561                 char[] declaration= proposal.getDeclarationSignature();
562                 // special methods may not have a declaring type: methods defined on arrays etc.
563
// Currently known: class literals don't have a declaring type - use Object
564
if (declaration == null)
565                     return "java.lang.Object".toCharArray(); //$NON-NLS-1$
566
return Signature.toCharArray(declaration);
567             case CompletionProposal.PACKAGE_REF:
568                 return proposal.getDeclarationSignature();
569             case CompletionProposal.JAVADOC_TYPE_REF:
570             case CompletionProposal.TYPE_REF:
571                 return Signature.toCharArray(proposal.getSignature());
572             case CompletionProposal.LOCAL_VARIABLE_REF:
573             case CompletionProposal.VARIABLE_DECLARATION:
574             case CompletionProposal.KEYWORD:
575             case CompletionProposal.LABEL_REF:
576             case CompletionProposal.JAVADOC_BLOCK_TAG:
577             case CompletionProposal.JAVADOC_INLINE_TAG:
578             case CompletionProposal.JAVADOC_PARAM_REF:
579                 return null;
580             default:
581                 Assert.isTrue(false);
582                 return null;
583         }
584     }
585
586     private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
587         if (fCompilationUnit == null)
588             return;
589
590         String JavaDoc prefix= String.valueOf(proposal.getName());
591         int completionStart= proposal.getReplaceStart();
592         int completionEnd= proposal.getReplaceEnd();
593         int relevance= computeRelevance(proposal);
594
595         try {
596             IJavaElement element= fCompilationUnit.getElementAt(proposal.getCompletionLocation() + 1);
597             if (element != null) {
598                 IType type= (IType) element.getAncestor(IJavaElement.TYPE);
599                 if (type != null) {
600                     GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 1, fSuggestedMethodNames, fJavaProposals);
601                     MethodDeclarationCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, fSuggestedMethodNames, fJavaProposals);
602                 }
603             }
604         } catch (CoreException e) {
605             JavaPlugin.log(e);
606         }
607     }
608
609     private IJavaCompletionProposal createAnnotationAttributeReferenceProposal(CompletionProposal proposal) {
610         String JavaDoc displayString= fLabelProvider.createLabelWithTypeAndDeclaration(proposal);
611         ImageDescriptor descriptor= fLabelProvider.createMethodImageDescriptor(proposal);
612         String JavaDoc completion= String.valueOf(proposal.getCompletion());
613         return new JavaCompletionProposal(completion, proposal.getReplaceStart(), getLength(proposal), getImage(descriptor), displayString, computeRelevance(proposal));
614     }
615
616     private IJavaCompletionProposal createAnonymousTypeProposal(CompletionProposal proposal) {
617         if (fCompilationUnit == null || fJavaProject == null)
618             return null;
619
620         String JavaDoc completion= String.valueOf(proposal.getCompletion());
621         int start= proposal.getReplaceStart();
622         int length= getLength(proposal);
623         int relevance= computeRelevance(proposal);
624
625         String JavaDoc label= fLabelProvider.createAnonymousTypeLabel(proposal);
626
627         JavaCompletionProposal javaProposal= new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, start, length, completion, label, String.valueOf(proposal.getDeclarationSignature()), relevance);
628         javaProposal.setProposalInfo(new AnonymousTypeProposalInfo(fJavaProject, proposal));
629         return javaProposal;
630     }
631
632     private IJavaCompletionProposal createFieldProposal(CompletionProposal proposal) {
633         String JavaDoc completion= String.valueOf(proposal.getCompletion());
634         int start= proposal.getReplaceStart();
635         int length= getLength(proposal);
636         String JavaDoc label= fLabelProvider.createLabel(proposal);
637         Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal));
638         int relevance= computeRelevance(proposal);
639
640         JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext());
641         if (fJavaProject != null)
642             javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));
643
644         javaProposal.setTriggerCharacters(VAR_TRIGGER);
645
646         return javaProposal;
647     }
648
649     private IJavaCompletionProposal createJavadocSimpleProposal(CompletionProposal javadocProposal) {
650         // TODO do better with javadoc proposals
651
// String completion= String.valueOf(proposal.getCompletion());
652
// int start= proposal.getReplaceStart();
653
// int length= getLength(proposal);
654
// String label= fLabelProvider.createSimpleLabel(proposal);
655
// Image image= getImage(fLabelProvider.createImageDescriptor(proposal));
656
// int relevance= computeRelevance(proposal);
657
//
658
// JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, start, length, image, label, relevance);
659
// if (fJavaProject != null)
660
// javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));
661
//
662
// javaProposal.setTriggerCharacters(VAR_TRIGGER);
663
//
664
// return javaProposal;
665
LazyJavaCompletionProposal proposal = new LazyJavaCompletionProposal(javadocProposal, getInvocationContext());
666 // adaptLength(proposal, javadocProposal);
667
return proposal;
668     }
669     
670     private IJavaCompletionProposal createJavadocInlineTagProposal(CompletionProposal javadocProposal) {
671         LazyJavaCompletionProposal proposal= new JavadocInlineTagCompletionProposal(javadocProposal, getInvocationContext());
672         adaptLength(proposal, javadocProposal);
673         return proposal;
674     }
675
676     private IJavaCompletionProposal createKeywordProposal(CompletionProposal proposal) {
677         String JavaDoc completion= String.valueOf(proposal.getCompletion());
678         int start= proposal.getReplaceStart();
679         int length= getLength(proposal);
680         String JavaDoc label= fLabelProvider.createSimpleLabel(proposal);
681         int relevance= computeRelevance(proposal);
682         return new JavaCompletionProposal(completion, start, length, null, label, relevance);
683     }
684
685     private IJavaCompletionProposal createLabelProposal(CompletionProposal proposal) {
686         String JavaDoc completion= String.valueOf(proposal.getCompletion());
687         int start= proposal.getReplaceStart();
688         int length= getLength(proposal);
689         String JavaDoc label= fLabelProvider.createSimpleLabel(proposal);
690         int relevance= computeRelevance(proposal);
691
692         return new JavaCompletionProposal(completion, start, length, null, label, relevance);
693     }
694
695     private IJavaCompletionProposal createLocalVariableProposal(CompletionProposal proposal) {
696         String JavaDoc completion= String.valueOf(proposal.getCompletion());
697         int start= proposal.getReplaceStart();
698         int length= getLength(proposal);
699         Image image= getImage(fLabelProvider.createLocalImageDescriptor(proposal));
700         String JavaDoc label= fLabelProvider.createSimpleLabelWithType(proposal);
701         int relevance= computeRelevance(proposal);
702
703         final JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, start, length, image, label, relevance);
704         javaProposal.setTriggerCharacters(VAR_TRIGGER);
705         return javaProposal;
706     }
707
708     private IJavaCompletionProposal createMethodDeclarationProposal(CompletionProposal proposal) {
709         if (fCompilationUnit == null || fJavaProject == null)
710             return null;
711
712         String JavaDoc name= String.valueOf(proposal.getName());
713         String JavaDoc[] paramTypes= Signature.getParameterTypes(String.valueOf(proposal.getSignature()));
714         for (int index= 0; index < paramTypes.length; index++)
715             paramTypes[index]= Signature.toString(paramTypes[index]);
716         int start= proposal.getReplaceStart();
717         int length= getLength(proposal);
718
719         String JavaDoc label= fLabelProvider.createOverrideMethodProposalLabel(proposal);
720
721         JavaCompletionProposal javaProposal= new OverrideCompletionProposal(fJavaProject, fCompilationUnit, name, paramTypes, start, length, label, String.valueOf(proposal.getCompletion()));
722         javaProposal.setImage(getImage(fLabelProvider.createMethodImageDescriptor(proposal)));
723         javaProposal.setProposalInfo(new MethodProposalInfo(fJavaProject, proposal));
724         javaProposal.setRelevance(computeRelevance(proposal));
725
726         fSuggestedMethodNames.add(new String JavaDoc(name));
727         return javaProposal;
728     }
729
730     private IJavaCompletionProposal createMethodReferenceProposal(CompletionProposal methodProposal) {
731         LazyJavaCompletionProposal proposal= new JavaMethodCompletionProposal(methodProposal, getInvocationContext());
732         adaptLength(proposal, methodProposal);
733         return proposal;
734     }
735
736     private void adaptLength(LazyJavaCompletionProposal proposal, CompletionProposal coreProposal) {
737         if (fUserReplacementLength != -1) {
738             proposal.setReplacementLength(getLength(coreProposal));
739         }
740     }
741
742     private IJavaCompletionProposal createPackageProposal(CompletionProposal proposal) {
743         String JavaDoc completion= String.valueOf(proposal.getCompletion());
744         int start= proposal.getReplaceStart();
745         int length= getLength(proposal);
746         String JavaDoc label= fLabelProvider.createSimpleLabel(proposal);
747         Image image= getImage(fLabelProvider.createPackageImageDescriptor(proposal));
748         int relevance= computeRelevance(proposal);
749
750         return new JavaCompletionProposal(completion, start, length, image, label, relevance);
751     }
752
753     private IJavaCompletionProposal createTypeProposal(CompletionProposal typeProposal) {
754         LazyJavaCompletionProposal proposal= new LazyJavaTypeCompletionProposal(typeProposal, getInvocationContext());
755         adaptLength(proposal, typeProposal);
756         return proposal;
757     }
758     
759     private IJavaCompletionProposal createJavadocLinkTypeProposal(CompletionProposal typeProposal) {
760         LazyJavaCompletionProposal proposal= new JavadocLinkTypeCompletionProposal(typeProposal, getInvocationContext());
761         adaptLength(proposal, typeProposal);
762         return proposal;
763     }
764 }
765
Popular Tags