1 /*******************************************************************************
2 * Copyright (c) 2004 IBM Corporation 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
15 import org.eclipse.core.runtime.IProgressMonitor;
18 * Completion proposal.
20 * In typical usage, the user working in a Java code editor issues a code assist
21 * command. This command results in a call to
22 * <code>ICodeAssist.codeComplete(position, completionRequestor)</code>
23 * passing the current position in the source code. The code assist engine
24 * analyzes the code in the buffer, determines what kind of Java language
25 * construct is at that position, and proposes ways to complete that construct.
26 * These proposals are instances of subclasses of
27 * <code>CompletionProposal</code>. These proposals, perhaps after sorting
28 * and filtering, are presented to the user to make a choice.
31 * The proposal is as follows: insert the
32 * {@linkplain #getCompletion() completion string} into the source file buffer,
33 * replacing the characters between {@linkplain #getReplaceStart() the start}
34 * and {@linkplain #getReplaceEnd() end}. The string can be arbitrary; for
35 * example, it might include not only the name of a method but a set of
36 * parentheses. Moreover, the source range may include source positions before
37 * or after the source position where <code>ICodeAssist.codeComplete</code>
38 * was invoked. The rest of the information associated with the proposal is to
39 * provide context that may help a user to choose from among competing
43 * The completion engine creates instances of this class; it is not intended to
44 * be used by other clients.
47 * @see ICodeAssist#codeComplete(int, CompletionRequestor)
50 public final class CompletionProposal {
53 * Completion is a declaration of an anonymous class. This kind of
54 * completion might occur in a context like <code>"new List^;"</code> and
55 * complete it to <code>"new List() {}"</code>.
57 * The following additional context information is available for this kind
58 * of completion proposal at little extra cost:
60 * <li>{@link #getDeclarationSignature()} - the type signature of the type
61 * being implemented or subclassed </li>
63 * <li>{@link #getSignature()} - the method signature of the constructor
64 * that is referenced </li>
65 * <li>{@link #getFlags()} - the modifiers flags of the constructor that is
72 public static final int ANONYMOUS_CLASS_DECLARATION = 1;
75 * Completion is a reference to a field. This kind of completion might occur
76 * in a context like <code>"this.ref^ = 0;"</code> and complete it to
77 * <code>"this.refcount = 0;"</code>.
79 * The following additional context information is available for this kind
80 * of completion proposal at little extra cost:
82 * <li>{@link #getDeclarationSignature()} - the type signature of the type
83 * that declares the field that is referenced </li>
84 * <li>{@link #getFlags()} - the modifiers flags (including ACC_ENUM) of
85 * the field that is referenced </li>
86 * <li>{@link #getName()} - the simple name of the field that is referenced
88 * <li>{@link #getSignature()} - the type signature of the field's type (as
89 * opposed to the signature of the type in which the referenced field is
96 public static final int FIELD_REF = 2;
99 * Completion is a keyword. This kind of completion might occur in a context
100 * like <code>"public cl^ Foo {}"</code> and complete it to
101 * <code>"public class Foo {}"</code>.
103 * The following additional context information is available for this kind
104 * of completion proposal at little extra cost:
106 * <li>{@link #getName()} - the keyword token </li>
107 * <li>{@link #getFlags()} - the corresponding modifier flags if the
108 * keyword is a modifier </li>
114 public static final int KEYWORD = 3;
117 * Completion is a reference to a label. This kind of completion might occur
118 * in a context like <code>"break lo^;"</code> and complete it to
119 * <code>"break loop;"</code>.
121 * The following additional context information is available for this kind
122 * of completion proposal at little extra cost:
124 * <li>{@link #getName()} - the simple name of the label that is referenced
131 public static final int LABEL_REF = 4;
134 * Completion is a reference to a local variable. This kind of completion
135 * might occur in a context like <code>"ke^ = 4;"</code> and complete it
136 * to <code>"keys = 4;"</code>.
138 * The following additional context information is available for this kind
139 * of completion proposal at little extra cost:
141 * <li>{@link #getFlags()} - the modifiers flags of the local variable that
142 * is referenced </li>
143 * <li>{@link #getName()} - the simple name of the local variable that is
145 * <li>{@link #getSignature()} - the type signature of the local variable's
152 public static final int LOCAL_VARIABLE_REF = 5;
155 * Completion is a reference to a method. This kind of completion might
156 * occur in a context like <code>"System.out.pr^();"</code> and complete
157 * it to <code>""System.out.println();"</code>.
159 * The following additional context information is available for this kind
160 * of completion proposal at little extra cost:
162 * <li>{@link #getDeclarationSignature()} - the type signature of the type
163 * that declares the method that is referenced </li>
164 * <li>{@link #getFlags()} - the modifiers flags of the method that is
166 * <li>{@link #getName()} - the simple name of the method that is
168 * <li>{@link #getSignature()} - the method signature of the method that is
175 public static final int METHOD_REF = 6;
178 * Completion is a declaration of a method. This kind of completion might
179 * occur in a context like <code>"new List() {si^};"</code> and complete
180 * it to <code>"new List() {public int size() {} };"</code>.
182 * The following additional context information is available for this kind
183 * of completion proposal at little extra cost:
185 * <li>{@link #getDeclarationSignature()} - the type signature of the type
186 * that declares the method that is being overridden or implemented </li>
187 * <li>{@link #getName()} - the simple name of the method that is being
188 * overridden or implemented </li>
189 * <li>{@link #getSignature()} - the method signature of the method that is
190 * being overridden or implemented </li>
191 * <li>{@link #getFlags()} - the modifiers flags of the method that is
192 * being overridden or implemented </li>
198 public static final int METHOD_DECLARATION = 7;
201 * Completion is a reference to a package. This kind of completion might
202 * occur in a context like <code>"import java.u^.*;"</code> and complete
203 * it to <code>"import java.util.*;"</code>.
205 * The following additional context information is available for this kind
206 * of completion proposal at little extra cost:
208 * <li>{@link #getDeclarationSignature()} - the dot-based package signature
209 * of the package that is referenced </li>
215 public static final int PACKAGE_REF = 8;
218 * Completion is a reference to a type. Any kind of type is allowed,
219 * including primitive types, reference types, array types, parameterized
220 * types, and type variables. This kind of completion might occur in a
221 * context like <code>"public static Str^ key;"</code> and complete it to
222 * <code>"public static String key;"</code>.
224 * The following additional context information is available for this kind
225 * of completion proposal at little extra cost:
227 * <li>{@link #getDeclarationSignature()} - the dot-based package signature
228 * of the package that contains the type that is referenced </li>
229 * <li>{@link #getSignature()} - the type signature of the type that is
231 * <li>{@link #getFlags()} - the modifiers flags (including
232 * Flags.AccInterface, AccEnum, and AccAnnotation) of the type that is
239 public static final int TYPE_REF = 9;
242 * Completion is a declaration of a variable (locals, parameters, fields,
245 * The following additional context information is available for this kind
246 * of completion proposal at little extra cost:
248 * <li>{@link #getName()} - the simple name of the variable being declared
250 * <li>{@link #getSignature()} - the type signature of the type of the
251 * variable being declared </li>
252 * <li>{@link #getFlags()} - the modifiers flags of the variable being
259 public static final int VARIABLE_DECLARATION = 10;
262 * Kind of completion request.
264 private int completionKind;
267 * Offset in original buffer where ICodeAssist.codeComplete() was requested.
269 private int completionLocation;
272 * Start position (inclusive) of source range in original buffer containing
273 * the relevant token defaults to empty subrange at [0,0).
275 private int tokenStart = 0;
278 * End position (exclusive) of source range in original buffer containing
279 * the relevant token; defaults to empty subrange at [0,0).
281 private int tokenEnd = 0;
284 * Completion string; defaults to empty string.
286 private char[] completion = CharOperation.NO_CHAR;
289 * Start position (inclusive) of source range in original buffer to be
290 * replaced by completion string; defaults to empty subrange at [0,0).
292 private int replaceStart = 0;
295 * End position (exclusive) of source range in original buffer to be
296 * replaced by completion string; defaults to empty subrange at [0,0).
298 private int replaceEnd = 0;
301 * Relevance rating; positive; higher means better; defaults to minimum
304 private int relevance = 1;
307 * Signature of the relevant package or type declaration in the context, or
308 * <code>null</code> if none. Defaults to null.
310 private char[] declarationSignature = null;
313 * Simple name of the method, field, member, or variable relevant in the
314 * context, or <code>null</code> if none. Defaults to null.
316 private char[] name = null;
319 * Signature of the method, field type, member type, relevant in the
320 * context, or <code>null</code> if none. Defaults to null.
322 private char[] signature = null;
325 * Modifier flags relevant in the context, or <code>Flags.AccDefault</code>
326 * if none. Defaults to <code>Flags.AccDefault</code>.
328 private int flags = Flags.AccDefault;
331 * Parameter names (for method completions), or <code>null</code> if none.
332 * Lazily computed. Defaults to <code>null</code>.
334 private char[][] parameterNames = null;
337 * Indicates whether parameter names have been computed.
339 private boolean parameterNamesComputed = false;
342 * Creates a basic completion proposal. All instance field have plausible
343 * default values unless otherwise noted.
345 * Note that the constructors for this class are internal to the Java model
346 * implementation. Clients cannot directly create CompletionProposal
351 * one of the kind constants declared on this class
352 * @param completionOffset
353 * original offset of code completion request
354 * @return a new completion proposal
356 public static CompletionProposal create(int kind, int completionOffset) {
357 return new CompletionProposal(kind, completionOffset);
361 * Creates a basic completion proposal. All instance field have plausible
362 * default values unless otherwise noted.
364 * Note that the constructors for this class are internal to the Java model
365 * implementation. Clients cannot directly create CompletionProposal
370 * one of the kind constants declared on this class
371 * @param completionLocation
372 * original offset of code completion request
374 CompletionProposal(int kind, int completionLocation) {
375 if ((kind < CompletionProposal.ANONYMOUS_CLASS_DECLARATION)
376 || (kind > CompletionProposal.VARIABLE_DECLARATION)) {
377 throw new IllegalArgumentException();
379 if (this.completion == null || completionLocation < 0) {
380 throw new IllegalArgumentException();
382 this.completionKind = kind;
383 this.completionLocation = completionLocation;
387 * Returns the kind of completion being proposed.
389 * The set of different kinds of completion proposals is expected to change
390 * over time. It is strongly recommended that clients do <b>not</b> assume
391 * that the kind is one of the ones they know about, and code defensively
392 * for the possibility of unexpected future growth.
395 * @return the kind; one of the kind constants declared on this class, or
396 * possibly a kind unknown to the caller
398 public int getKind() {
399 return this.completionKind;
403 * Returns the character index in the source file buffer where source
404 * completion was requested (the <code>offset</code>parameter to
405 * <code>ICodeAssist.codeComplete</code>.
407 * @return character index in source file buffer
408 * @see ICodeAssist#codeComplete(int,CompletionRequestor)
410 public int getCompletionLocation() {
411 return this.completionLocation;
415 * Returns the character index of the start of the subrange in the source
416 * file buffer containing the relevant token being completed. This token is
417 * either the identifier or Java language keyword under, or immediately
418 * preceding, the original request offset. If the original request offset is
419 * not within or immediately after an identifier or keyword, then the
420 * position returned is original request offset and the token range is
423 * @return character index of token start position (inclusive)
425 public int getTokenStart() {
426 return this.tokenStart;
430 * Returns the character index of the end (exclusive) of the subrange in the
431 * source file buffer containing the relevant token. When there is no
432 * relevant token, the range is empty (<code>getEndToken() == getStartToken()</code>).
434 * @return character index of token end position (exclusive)
436 public int getTokenEnd() {
437 return this.tokenEnd;
441 * Sets the character indices of the subrange in the source file buffer
442 * containing the relevant token being completed. This token is either the
443 * identifier or Java language keyword under, or immediately preceding, the
444 * original request offset. If the original request offset is not within or
445 * immediately after an identifier or keyword, then the source range begins
446 * at original request offset and is empty.
448 * If not set, defaults to empty subrange at [0,0).
452 * character index of token start position (inclusive)
454 * character index of token end position (exclusive)
456 public void setTokenRange(int startIndex, int endIndex) {
457 if (startIndex < 0 || endIndex < startIndex) {
458 throw new IllegalArgumentException();
460 this.tokenStart = startIndex;
461 this.tokenEnd = endIndex;
465 * Returns the proposed sequence of characters to insert into the source
466 * file buffer, replacing the characters at the specified source range. The
467 * string can be arbitrary; for example, it might include not only the name
468 * of a method but a set of parentheses.
470 * The client must not modify the array returned.
473 * @return the completion string
475 public char[] getCompletion() {
476 return this.completion;
480 * Sets the proposed sequence of characters to insert into the source file
481 * buffer, replacing the characters at the specified source range. The
482 * string can be arbitrary; for example, it might include not only the name
483 * of a method but a set of parentheses.
485 * If not set, defaults to an empty character array.
488 * The completion engine creates instances of this class and sets its
489 * properties; this method is not intended to be used by other clients.
493 * the completion string
495 public void setCompletion(char[] completion) {
496 this.completion = completion;
500 * Returns the character index of the start of the subrange in the source
501 * file buffer to be replaced by the completion string. If the subrange is
502 * empty (<code>getReplaceEnd() == getReplaceStart()</code>), the
503 * completion string is to be inserted at this index.
505 * Note that while the token subrange is precisely specified, the
506 * replacement range is loosely constrained and may not bear any direct
507 * relation to the original request offset. For example, a it would be
508 * possible for a type completion to propose inserting an import declaration
509 * at the top of the compilation unit; or the completion might include
510 * trailing parentheses and punctuation for a method completion.
513 * @return replacement start position (inclusive)
515 public int getReplaceStart() {
516 return this.replaceStart;
520 * Returns the character index of the end of the subrange in the source file
521 * buffer to be replaced by the completion string. If the subrange is empty (<code>getReplaceEnd() == getReplaceStart()</code>),
522 * the completion string is to be inserted at this index.
524 * @return replacement end position (exclusive)
526 public int getReplaceEnd() {
527 return this.replaceEnd;
531 * Sets the character indices of the subrange in the source file buffer to
532 * be replaced by the completion string. If the subrange is empty (<code>startIndex == endIndex</code>),
533 * the completion string is to be inserted at this index.
535 * If not set, defaults to empty subrange at [0,0).
538 * The completion engine creates instances of this class and sets its
539 * properties; this method is not intended to be used by other clients.
543 * character index of replacement start position (inclusive)
545 * character index of replacement end position (exclusive)
547 public void setReplaceRange(int startIndex, int endIndex) {
548 if (startIndex < 0 || endIndex < startIndex) {
549 throw new IllegalArgumentException();
551 this.replaceStart = startIndex;
552 this.replaceEnd = endIndex;
556 * Returns the relative relevance rating of this proposal.
558 * @return relevance rating of this proposal; ratings are positive; higher
561 public int getRelevance() {
562 return this.relevance;
566 * Sets the relative relevance rating of this proposal.
568 * If not set, defaults to the lowest possible rating (1).
571 * The completion engine creates instances of this class and sets its
572 * properties; this method is not intended to be used by other clients.
576 * relevance rating of this proposal; ratings are positive;
577 * higher means better
579 public void setRelevance(int rating) {
581 throw new IllegalArgumentException();
583 this.relevance = rating;
587 * Returns the type or package signature of the relevant declaration in the
588 * context, or <code>null</code> if none.
590 * This field is available for the following kinds of completion proposals:
592 * <li><code>ANONYMOUS_CLASS_DECLARATION</code> - type signature of the
593 * type that is being subclassed or implemented</li>
594 * <li><code>FIELD_REF</code> - type signature of the type that declares
595 * the field that is referenced</li>
596 * <li><code>METHOD_REF</code> - type signature of the type that declares
597 * the method that is referenced</li>
598 * <li><code>METHOD_DECLARATION</code> - type signature of the type that
599 * declares the method that is being implemented or overridden</li>
600 * <li><code>PACKAGE_REF</code> - dot-based package signature of the
601 * package that is referenced</li>
602 * <li><code>TYPE_REF</code> - dot-based package signature of the package
603 * containing the type that is referenced</li>
605 * For kinds of completion proposals, this method returns <code>null</code>.
606 * Clients must not modify the array returned.
609 * @return the declaration signature, or <code>null</code> if none
612 public char[] getDeclarationSignature() {
613 return this.declarationSignature;
617 * Sets the type or package signature of the relevant declaration in the
618 * context, or <code>null</code> if none.
620 * If not set, defaults to none.
623 * The completion engine creates instances of this class and sets its
624 * properties; this method is not intended to be used by other clients.
628 * the type or package signature, or <code>null</code> if none
630 public void setDeclarationSignature(char[] signature) {
631 this.declarationSignature = signature;
635 * Returns the simple name of the method, field, member, or variable
636 * relevant in the context, or <code>null</code> if none.
638 * This field is available for the following kinds of completion proposals:
640 * <li><code>FIELD_REF</code> - the name of the field</li>
641 * <li><code>KEYWORD</code> - the keyword</li>
642 * <li><code>LABEL_REF</code> - the name of the label</li>
643 * <li><code>LOCAL_VARIABLE_REF</code> - the name of the local variable</li>
644 * <li><code>METHOD_REF</code> - the name of the method</li>
645 * <li><code>METHOD_DECLARATION</code> - the name of the method</li>
646 * <li><code>VARIABLE_DECLARATION</code> - the name of the variable</li>
648 * For kinds of completion proposals, this method returns <code>null</code>.
649 * Clients must not modify the array returned.
652 * @return the keyword, field, method, local variable, or member name, or
653 * <code>null</code> if none
655 public char[] getName() {
660 * Sets the simple name of the method, field, member, or variable relevant
661 * in the context, or <code>null</code> if none.
663 * If not set, defaults to none.
666 * The completion engine creates instances of this class and sets its
667 * properties; this method is not intended to be used by other clients.
671 * the keyword, field, method, local variable, or member name, or
672 * <code>null</code> if none
674 public void setName(char[] name) {
679 * Returns the signature of the method or type relevant in the context, or
680 * <code>null</code> if none.
682 * This field is available for the following kinds of completion proposals:
684 * <li><code>ANONYMOUS_CLASS_DECLARATION</code> - method signature of the
685 * constructor that is being invoked</li>
686 * <li><code>FIELD_REF</code> - the type signature of the referenced
688 * <li><code>LOCAL_VARIABLE_REF</code> - the type signature of the
689 * referenced local variable's type</li>
690 * <li><code>METHOD_REF</code> - method signature of the method that is
692 * <li><code>METHOD_DECLARATION</code> - method signature of the method
693 * that is being implemented or overridden</li>
694 * <li><code>TYPE_REF</code> - type signature of the type that is
696 * <li><code>VARIABLE_DECLARATION</code> - the type signature of the type
697 * of the variable being declared</li>
699 * For kinds of completion proposals, this method returns <code>null</code>.
700 * Clients must not modify the array returned.
703 * @return the signature, or <code>null</code> if none
706 public char[] getSignature() {
707 return this.signature;
711 * Sets the signature of the method, field type, member type, relevant in
712 * the context, or <code>null</code> if none.
714 * If not set, defaults to none.
717 * The completion engine creates instances of this class and sets its
718 * properties; this method is not intended to be used by other clients.
722 * the signature, or <code>null</code> if none
724 public void setSignature(char[] signature) {
725 this.signature = signature;
729 * Returns the modifier flags relevant in the context, or
730 * <code>Flags.AccDefault</code> if none.
732 * This field is available for the following kinds of completion proposals:
734 * <li><code>ANONYMOUS_CLASS_DECLARATION</code> - modifier flags of the
735 * constructor that is referenced</li>
736 * <li><code>FIELD_REF</code> - modifier flags of the field that is
737 * referenced; <code>Flags.AccEnum</code> can be used to recognize
738 * references to enum constants </li>
739 * <li><code>KEYWORD</code> - modifier flag corrresponding to the
740 * modifier keyword</li>
741 * <li><code>LOCAL_VARIABLE_REF</code> - modifier flags of the local
742 * variable that is referenced</li>
743 * <li><code>METHOD_REF</code> - modifier flags of the method that is
744 * referenced; <code>Flags.AccAnnotation</code> can be used to recognize
745 * references to annotation type members </li>
746 * <li><code>METHOD_DECLARATION</code> - modifier flags for the method
747 * that is being implemented or overridden</li>
748 * <li><code>TYPE_REF</code> - modifier flags of the type that is
749 * referenced; <code>Flags.AccInterface</code> can be used to recognize
750 * references to interfaces, <code>Flags.AccEnum</code> enum types, and
751 * <code>Flags.AccAnnotation</code> annotation types </li>
752 * <li><code>VARIABLE_DECLARATION</code> - modifier flags for the
753 * variable being declared</li>
755 * For kinds of completion proposals, this method returns
756 * <code>Flags.AccDefault</code>.
759 * @return the modifier flags, or <code>Flags.AccDefault</code> if none
762 public int getFlags() {
767 * Sets the modifier flags relevant in the context.
769 * If not set, defaults to none.
772 * The completion engine creates instances of this class and sets its
773 * properties; this method is not intended to be used by other clients.
777 * the modifier flags, or <code>Flags.AccDefault</code> if none
779 public void setFlags(int flags) {
784 * Finds the method parameter names. This information is relevant to method
785 * reference (and method declaration proposals). Returns <code>null</code>
786 * if not available or not relevant.
788 * The client must not modify the array returned.
791 * <b>Note that this is an expensive thing to compute, which may require
792 * parsing Java source files, etc. Use sparingly.
796 * the progress monitor, or <code>null</code> if none
797 * @return the parameter names, or <code>null</code> if none or not
798 * available or not relevant
800 public char[][] findParameterNames(IProgressMonitor monitor) {
801 if (!this.parameterNamesComputed) {
802 this.parameterNamesComputed = true;
803 // TODO (jerome) - Missing implementation
805 return this.parameterNames;
809 * Sets the method parameter names. This information is relevant to method
810 * reference (and method declaration proposals).
812 * The completion engine creates instances of this class and sets its
813 * properties; this method is not intended to be used by other clients.
816 * @param parameterNames
817 * the parameter names, or <code>null</code> if none
819 public void setParameterNames(char[][] parameterNames) {
820 this.parameterNames = parameterNames;
821 this.parameterNamesComputed = true;