Class CompletionProposalCollector
CompletionRequestor
. Produces
IJavaCompletionProposal
s from the proposal descriptors received via
the CompletionRequestor
interface.
The lifecycle of a CompletionProposalCollector
instance is very
simple:
ICompilationUnit unit= ... int offset= ... CompletionProposalCollector collector= new CompletionProposalCollector(unit); unit.codeComplete(offset, collector); IJavaCompletionProposal[] proposals= collector.getJavaCompletionProposals(); String errorMessage= collector.getErrorMessage(); // display / process proposals
Note that after a code completion operation, the collector will store any received proposals, which may require a considerable amount of memory, so the collector should not be kept as a reference after a completion operation.
Clients may instantiate or subclass.
- Since:
- 3.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final char[]
Triggers for method proposals without parameters.protected static final char[]
Triggers for method proposals.protected static final char[]
Triggers for types.protected static final char[]
Triggers for variables. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ready to collect proposals.CompletionProposalCollector
(ICompilationUnit cu, boolean ignoreAll) Creates a new instance ready to collect proposals.CompletionProposalCollector
(IJavaProject project) Creates a new instance ready to collect proposals. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(CompletionProposal proposal) Proposes a completion.void
acceptContext
(CompletionContext context) Propose the context in which the completion occurs.void
Pro forma notification sent before reporting a batch of completion proposals.void
completionFailure
(IProblem problem) Notification of failure to produce any completions.protected int
computeRelevance
(CompletionProposal proposal) Computes the relevance for a givenCompletionProposal
.protected IJavaCompletionProposal
Creates a new java completion proposal from a core proposal.protected final IContextInformation
createMethodContextInformation
(CompletionProposal methodProposal) Creates the context information for a given method reference proposal.void
Pro forma notification sent after reporting a batch of completion proposals.protected final ICompilationUnit
Returns the compilation unit that the receiver operates on, ornull
if theIJavaProject
constructor was used to create the receiver.protected final CompletionContext
Returns theCompletionContext
for this completion operation.protected final char[]
getDeclaringType
(CompletionProposal proposal) Returns the type signature of the declaring type of aCompletionProposal
, ornull
for proposals that do not have a declaring type.Returns an error message about any error that may have occurred during code completion, or the empty string if none.protected final Image
getImage
(ImageDescriptor descriptor) Returns a cached image for the given descriptor.protected final JavaContentAssistInvocationContext
Returns the invocation context.final IJavaCompletionProposal[]
Returns the unsorted list of received proposals.final IJavaCompletionProposal[]
Returns the unsorted list of received keyword proposals.protected final CompletionProposalLabelProvider
Returns the proposal label provider used by the receiver.protected final int
getLength
(CompletionProposal proposal) Returns the replacement length of a given completion proposal.protected boolean
isFiltered
(CompletionProposal proposal) Returnstrue
ifproposal
is filtered, e.g.boolean
isIgnored
(char[] fullTypeName) Returns whether the given type proposal is ignored.boolean
If this returns true, exclude test sources and dependencies.void
setIgnored
(int completionProposalKind, boolean ignore) Sets whether the given kind of completion proposal is ignored.void
Sets the invocation context.final void
setReplacementLength
(int length) If the replacement length is set, it overrides the length returned from the content assist infrastructure.Methods inherited from class org.eclipse.jdt.core.CompletionRequestor
getFavoriteReferences, isAllowingRequiredProposals, isExtendedContextRequired, isIgnored, setAllowsRequiredProposals, setFavoriteReferences, setRequireExtendedContext
-
Field Details
-
METHOD_TRIGGERS
protected static final char[] METHOD_TRIGGERSTriggers for method proposals without parameters. Do not modify. -
METHOD_WITH_ARGUMENTS_TRIGGERS
protected static final char[] METHOD_WITH_ARGUMENTS_TRIGGERSTriggers for method proposals. Do not modify. -
TYPE_TRIGGERS
protected static final char[] TYPE_TRIGGERSTriggers for types. Do not modify. -
VAR_TRIGGER
protected static final char[] VAR_TRIGGERTriggers for variables. Do not modify.
-
-
Constructor Details
-
CompletionProposalCollector
Creates a new instance ready to collect proposals. If the passedICompilationUnit
is not contained in anIJavaProject
, no javadoc will be available asadditional info
on the created proposals.- Parameters:
cu
- the compilation unit that the result collector will operate on
-
CompletionProposalCollector
Creates a new instance ready to collect proposals. Note that proposals for anonymous types and method declarations are not created when using this constructor, as those need to know the compilation unit that they are created on. UseCompletionProposalCollector(ICompilationUnit)
instead to get all proposals.If the passed Java project is
null
, no javadoc will be available asadditional info
on the created (e.g. method and type) proposals.- Parameters:
project
- the project that the result collector will operate on, ornull
-
CompletionProposalCollector
Creates a new instance ready to collect proposals. If the passedICompilationUnit
is not contained in anIJavaProject
, no javadoc will be available asadditional info
on the created proposals.- Parameters:
cu
- the compilation unit that the result collector will operate onignoreAll
-true
to ignore all kinds of completion proposals- Since:
- 3.4
-
-
Method Details
-
isTestCodeExcluded
public boolean isTestCodeExcluded()Description copied from class:CompletionRequestor
If this returns true, exclude test sources and dependencies.- Overrides:
isTestCodeExcluded
in classCompletionRequestor
- Returns:
true
if this requestor does not want to get any completions from test code.- Since:
- 3.14
- See Also:
-
setIgnored
public void setIgnored(int completionProposalKind, boolean ignore) Description copied from class:CompletionRequestor
Sets whether the given kind of completion proposal is ignored.- Overrides:
setIgnored
in classCompletionRequestor
- Parameters:
completionProposalKind
- one of the kind constants declared onCompletionProposal
ignore
-true
if the given kind of completion proposal is ignored by this requestor, andfalse
if it is of interest- See Also:
-
setInvocationContext
Sets the invocation context.Subclasses may extend.
- Parameters:
context
- the invocation context- Since:
- 3.2
- See Also:
-
getInvocationContext
Returns the invocation context. If none has been set viasetInvocationContext(JavaContentAssistInvocationContext)
, a new one is created.- Returns:
- invocationContext the invocation context
- Since:
- 3.2
-
accept
Proposes a completion. Has no effect if the kind of proposal is being ignored by this requestor. Callers should consider checkingCompletionRequestor.isIgnored(int)
before avoid creating proposal objects that would only be ignored.Similarly, implementers should check
isIgnored(proposal.getKind())
and ignore proposals that have been declared as uninteresting. The proposal object passed is only valid for the duration of completion operation.Subclasses may replace, but usually should not need to. Consider replacing createJavaCompletionProposal instead.
- Specified by:
accept
in classCompletionRequestor
- Parameters:
proposal
- the completion proposal
-
acceptContext
Propose the context in which the completion occurs.This method is called one and only one time before any call to
CompletionRequestor.accept(CompletionProposal)
. The default implementation of this method does nothing. Clients may override.Subclasses may extend, but usually should not need to.
- Overrides:
acceptContext
in classCompletionRequestor
- Parameters:
context
- the completion context- See Also:
-
beginReporting
public void beginReporting()Pro forma notification sent before reporting a batch of completion proposals.The default implementation of this method does nothing. Clients may override.
Subclasses may extend, but must call the super implementation.- Overrides:
beginReporting
in classCompletionRequestor
-
completionFailure
Notification of failure to produce any completions. The problem object explains what prevented completing.The default implementation of this method does nothing. Clients may override to receive this kind of notice.
Subclasses may extend, but must call the super implementation.- Overrides:
completionFailure
in classCompletionRequestor
- Parameters:
problem
- the problem object
-
endReporting
public void endReporting()Pro forma notification sent after reporting a batch of completion proposals.The default implementation of this method does nothing. Clients may override.
Subclasses may extend, but must call the super implementation.- Overrides:
endReporting
in classCompletionRequestor
-
getErrorMessage
Returns an error message about any error that may have occurred during code completion, or the empty string if none.Subclasses may replace or extend.
- Returns:
- an error message or the empty string
-
getJavaCompletionProposals
Returns the unsorted list of received proposals.- Returns:
- the unsorted list of received proposals
-
getKeywordCompletionProposals
Returns the unsorted list of received keyword proposals.- Returns:
- the unsorted list of received keyword proposals
-
setReplacementLength
public final void setReplacementLength(int length) If the replacement length is set, it overrides the length returned from the content assist infrastructure. Use this setting if code assist is called with a none empty selection.- Parameters:
length
- the new replacement length, relative to the code assist offset. Must be equal to or greater than zero.
-
computeRelevance
Computes the relevance for a givenCompletionProposal
.Subclasses may replace, but usually should not need to.
- Parameters:
proposal
- the proposal to compute the relevance for- Returns:
- the relevance for
proposal
-
createJavaCompletionProposal
Creates a new java completion proposal from a core proposal. This may involve computing the display label and setting up some context.This method is called for every proposal that will be displayed to the user, which may be hundreds. Implementations should therefore defer as much work as possible: Labels should be computed lazily to leverage virtual table usage, and any information only needed when applying a proposal should not be computed yet.
Implementations may return
null
if a proposal should not be included in the list presented to the user.Subclasses may extend or replace this method.
- Parameters:
proposal
- the core completion proposal to create a UI proposal for- Returns:
- the created java completion proposal, or
null
if no proposal should be displayed
-
createMethodContextInformation
protected final IContextInformation createMethodContextInformation(CompletionProposal methodProposal) Creates the context information for a given method reference proposal. The passed proposal must be of kindCompletionProposal.METHOD_REF
.- Parameters:
methodProposal
- the method proposal for which to create context information- Returns:
- the context information for
methodProposal
-
getCompilationUnit
Returns the compilation unit that the receiver operates on, ornull
if theIJavaProject
constructor was used to create the receiver.- Returns:
- the compilation unit that the receiver operates on, or
null
-
getContext
Returns theCompletionContext
for this completion operation.- Returns:
- the
CompletionContext
for this completion operation - See Also:
-
getImage
Returns a cached image for the given descriptor.- Parameters:
descriptor
- the image descriptor to get an image for, may benull
- Returns:
- the image corresponding to
descriptor
-
getLabelProvider
Returns the proposal label provider used by the receiver.- Returns:
- the proposal label provider used by the receiver
-
getLength
Returns the replacement length of a given completion proposal. The replacement length is usually the difference between the return values ofproposal.getReplaceEnd
andproposal.getReplaceStart
, but this behavior may be overridden by callingsetReplacementLength(int)
.- Parameters:
proposal
- the completion proposal to get the replacement length for- Returns:
- the replacement length for
proposal
-
isFiltered
Returnstrue
ifproposal
is filtered, e.g. should not be proposed to the user,false
if it is valid.Subclasses may extends this method. The default implementation filters proposals set to be ignored via setIgnored and types set to be ignored in the preferences.
- Parameters:
proposal
- the proposal to filter- Returns:
true
to filterproposal
,false
to let it pass
-
isIgnored
public boolean isIgnored(char[] fullTypeName) Description copied from class:CompletionRequestor
Returns whether the given type proposal is ignored.- Overrides:
isIgnored
in classCompletionRequestor
- Parameters:
fullTypeName
- the proposed type name of CompletionProposal- Returns:
true
if the given type name is ignored by this requestor, andfalse
if it is of interest
-
getDeclaringType
Returns the type signature of the declaring type of aCompletionProposal
, ornull
for proposals that do not have a declaring type. The return value is notnull
for proposals of the following kinds:- METHOD_DECLARATION
- METHOD_NAME_REFERENCE
- METHOD_REF
- ANNOTATION_ATTRIBUTE_REF
- POTENTIAL_METHOD_DECLARATION
- ANONYMOUS_CLASS_DECLARATION
- FIELD_REF
- PACKAGE_REF (returns the package name, but no type)
- MODULE_REF (returns the module name, but no type)
- MODULE_DECLARATION (returns the possible name of the module that is being declared, but no type)
- TYPE_REF
- Parameters:
proposal
- the completion proposal to get the declaring type for- Returns:
- the type signature of the declaring type, or
null
if there is none - See Also:
-