1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.java;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import java.util.List;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.contentassist.ICompletionProposal;
32 import org.eclipse.jface.text.contentassist.IContextInformation;
33 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
35 import org.eclipse.ui.IWorkbenchCommandConstants;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.keys.IBindingService;
39 import org.eclipse.jdt.core.CompletionProposal;
40 import org.eclipse.jdt.core.CompletionRequestor;
41 import org.eclipse.jdt.core.ICompilationUnit;
42 import org.eclipse.jdt.core.JavaModelException;
44 import org.eclipse.jdt.internal.corext.util.Messages;
46 import org.eclipse.jdt.ui.PreferenceConstants;
47 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector;
48 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
49 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
50 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
52 import org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner;
53 import org.eclipse.jdt.internal.ui.text.Symbols;
56 * Computes Java completion proposals and context infos.
60 public class JavaCompletionProposalComputer implements IJavaCompletionProposalComputer {
62 private static final class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
64 private final IContextInformation fContextInformation;
65 private int fPosition;
67 public ContextInformationWrapper(IContextInformation contextInformation) {
68 fContextInformation= contextInformation;
72 * @see IContextInformation#getContextDisplayString()
74 public String getContextDisplayString() {
75 return fContextInformation.getContextDisplayString();
79 * @see IContextInformation#getImage()
81 public Image getImage() {
82 return fContextInformation.getImage();
86 * @see IContextInformation#getInformationDisplayString()
88 public String getInformationDisplayString() {
89 return fContextInformation.getInformationDisplayString();
93 * @see IContextInformationExtension#getContextInformationPosition()
95 public int getContextInformationPosition() {
99 public void setContextInformationPosition(int position) {
104 * @see org.eclipse.jface.text.contentassist.IContextInformation#equals(java.lang.Object)
107 public boolean equals(Object object) {
108 if (object instanceof ContextInformationWrapper)
109 return fContextInformation.equals(((ContextInformationWrapper) object).fContextInformation);
111 return fContextInformation.equals(object);
115 * @see java.lang.Object#hashCode()
119 public int hashCode() {
120 return fContextInformation.hashCode();
124 private static final long JAVA_CODE_ASSIST_TIMEOUT= Long.getLong("org.eclipse.jdt.ui.codeAssistTimeout", 5000).longValue(); // ms //$NON-NLS-1$
126 private String fErrorMessage;
128 private final IProgressMonitor fTimeoutProgressMonitor;
130 public JavaCompletionProposalComputer() {
131 fTimeoutProgressMonitor= createTimeoutProgressMonitor(JAVA_CODE_ASSIST_TIMEOUT);
134 protected int guessContextInformationPosition(ContentAssistInvocationContext context) {
135 return context.getInvocationOffset();
138 protected final int guessMethodContextInformationPosition(ContentAssistInvocationContext context) {
139 final int contextPosition= context.getInvocationOffset();
141 IDocument document= context.getDocument();
142 JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
143 int bound= Math.max(-1, contextPosition - 200);
145 // try the innermost scope of parentheses that looks like a method call
146 int pos= contextPosition - 1;
148 int paren= scanner.findOpeningPeer(pos, bound, '(', ')');
149 if (paren == JavaHeuristicScanner.NOT_FOUND)
151 int token= scanner.previousToken(paren - 1, bound);
152 // next token must be a method name (identifier) or the closing angle of a
153 // constructor call of a parameterized type.
154 if (token == Symbols.TokenIDENT || token == Symbols.TokenGREATERTHAN)
159 return contextPosition;
162 private List<IContextInformation> addContextInformations(JavaContentAssistInvocationContext context, int offset) {
163 List<ICompletionProposal> proposals= internalComputeCompletionProposals(offset, context);
164 List<IContextInformation> result= new ArrayList<IContextInformation>(proposals.size());
165 List<IContextInformation> anonymousResult= new ArrayList<IContextInformation>(proposals.size());
167 for (Iterator<ICompletionProposal> it= proposals.iterator(); it.hasNext();) {
168 ICompletionProposal proposal= it.next();
169 IContextInformation contextInformation= proposal.getContextInformation();
170 if (contextInformation != null) {
171 ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation);
172 wrapper.setContextInformationPosition(offset);
173 if (proposal instanceof AnonymousTypeCompletionProposal)
174 anonymousResult.add(wrapper);
180 if (result.size() == 0)
181 return anonymousResult;
187 * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
189 public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
190 if (context instanceof JavaContentAssistInvocationContext) {
191 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
193 int contextInformationPosition= guessContextInformationPosition(javaContext);
194 List<IContextInformation> result= addContextInformations(javaContext, contextInformationPosition);
197 return Collections.emptyList();
201 * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
203 public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
204 if (context instanceof JavaContentAssistInvocationContext) {
205 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
206 return internalComputeCompletionProposals(context.getInvocationOffset(), javaContext);
208 return Collections.emptyList();
211 private List<ICompletionProposal> internalComputeCompletionProposals(int offset, JavaContentAssistInvocationContext context) {
212 ICompilationUnit unit= context.getCompilationUnit();
214 return Collections.emptyList();
216 ITextViewer viewer= context.getViewer();
218 CompletionProposalCollector collector= createCollector(context);
219 collector.setInvocationContext(context);
221 // Allow completions for unresolved types - since 3.3
222 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
223 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
224 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
226 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
227 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
228 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
230 collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
232 collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
233 collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);
235 collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
237 // Set the favorite list to propose static members - since 3.3
238 collector.setFavoriteReferences(getFavoriteStaticMembers());
241 Point selection= viewer.getSelectedRange();
243 collector.setReplacementLength(selection.y);
244 unit.codeComplete(offset, collector, fTimeoutProgressMonitor);
245 } catch (OperationCanceledException x) {
246 IBindingService bindingSvc= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
247 String keyBinding= bindingSvc.getBestActiveBindingFormattedFor(IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST);
248 fErrorMessage= Messages.format(JavaTextMessages.CompletionProcessor_error_javaCompletion_took_too_long_message, keyBinding);
249 } catch (JavaModelException x) {
250 Shell shell= viewer.getTextWidget().getShell();
251 if (x.isDoesNotExist() && !unit.getJavaProject().isOnClasspath(unit))
252 MessageDialog.openInformation(shell, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_title, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_message);
254 ErrorDialog.openError(shell, JavaTextMessages.CompletionProcessor_error_accessing_title, JavaTextMessages.CompletionProcessor_error_accessing_message, x.getStatus());
257 ICompletionProposal[] javaProposals= collector.getJavaCompletionProposals();
258 int contextInformationOffset= guessMethodContextInformationPosition(context);
259 if (contextInformationOffset != offset) {
260 for (int i= 0; i < javaProposals.length; i++) {
261 if (javaProposals[i] instanceof JavaMethodCompletionProposal) {
262 JavaMethodCompletionProposal jmcp= (JavaMethodCompletionProposal) javaProposals[i];
263 jmcp.setContextInformationPosition(contextInformationOffset);
268 List<ICompletionProposal> proposals= new ArrayList<ICompletionProposal>(Arrays.asList(javaProposals));
269 if (proposals.size() == 0) {
270 String error= collector.getErrorMessage();
271 if (error.length() > 0)
272 fErrorMessage= error;
278 * Returns a new progress monitor that get cancelled after the given timeout.
280 * @param timeout the timeout in ms
281 * @return the progress monitor
284 private IProgressMonitor createTimeoutProgressMonitor(final long timeout) {
285 return new IProgressMonitor() {
287 private long fEndTime;
289 public void beginTask(String name, int totalWork) {
290 fEndTime= System.currentTimeMillis() + timeout;
292 public boolean isCanceled() {
293 return fEndTime <= System.currentTimeMillis();
297 public void internalWorked(double work) {
299 public void setCanceled(boolean value) {
301 public void setTaskName(String name) {
303 public void subTask(String name) {
305 public void worked(int work) {
311 * Returns the array with favorite static members.
313 * @return the <code>String</code> array with with favorite static members
314 * @see CompletionRequestor#setFavoriteReferences(String[])
317 private String[] getFavoriteStaticMembers() {
318 String serializedFavorites= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
319 if (serializedFavorites != null && serializedFavorites.length() > 0)
320 return serializedFavorites.split(";"); //$NON-NLS-1$
321 return new String[0];
325 * Creates the collector used to get proposals from core.
327 * @param context the context
328 * @return the collector
330 protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
331 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES))
332 return new FillArgumentNamesCompletionProposalCollector(context);
334 return new CompletionProposalCollector(context.getCompilationUnit(), true);
338 * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
340 public String getErrorMessage() {
341 return fErrorMessage;
345 * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
347 public void sessionStarted() {
351 * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
353 public void sessionEnded() {