use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class AbstractTemplateCompletionProposalComputer method computeCompletionProposals.
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
*/
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
if (!(context instanceof JavaContentAssistInvocationContext))
return Collections.emptyList();
JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
ICompilationUnit unit = javaContext.getCompilationUnit();
if (unit == null)
return Collections.emptyList();
fEngine = computeCompletionEngine(javaContext);
if (fEngine == null)
return Collections.emptyList();
fEngine.reset();
fEngine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit);
TemplateProposal[] templateProposals = fEngine.getResults();
List<ICompletionProposal> result = new ArrayList<ICompletionProposal>(Arrays.asList(templateProposals));
IJavaCompletionProposal[] keyWordResults = javaContext.getKeywordProposals();
if (keyWordResults.length == 0)
return result;
/* Update relevance of template proposals that match with a keyword
* give those templates slightly more relevance than the keyword to
* sort them first.
*/
for (int k = 0; k < templateProposals.length; k++) {
TemplateProposal curr = templateProposals[k];
String name = curr.getTemplate().getPattern();
for (int i = 0; i < keyWordResults.length; i++) {
String keyword = keyWordResults[i].getDisplayString();
if (name.startsWith(keyword)) {
String content = curr.getTemplate().getPattern();
if (content.startsWith(keyword)) {
curr.setRelevance(keyWordResults[i].getRelevance() + 1);
break;
}
}
}
}
return result;
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class JavaCompletionProposalComputer method addContextInformations.
private List<IContextInformation> addContextInformations(JavaContentAssistInvocationContext context, int offset) {
List<ICompletionProposal> proposals = internalComputeCompletionProposals(offset, context);
List<IContextInformation> result = new ArrayList<IContextInformation>(proposals.size());
List<IContextInformation> anonymousResult = new ArrayList<IContextInformation>(proposals.size());
for (Iterator<ICompletionProposal> it = proposals.iterator(); it.hasNext(); ) {
ICompletionProposal proposal = it.next();
IContextInformation contextInformation = proposal.getContextInformation();
if (contextInformation != null) {
ContextInformationWrapper wrapper = new ContextInformationWrapper(contextInformation);
wrapper.setContextInformationPosition(offset);
if (proposal instanceof AnonymousTypeCompletionProposal)
anonymousResult.add(wrapper);
else
result.add(wrapper);
}
}
if (result.size() == 0)
return anonymousResult;
return result;
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class JavaCompletionProposalComputer method internalComputeCompletionProposals.
private List<ICompletionProposal> internalComputeCompletionProposals(int offset, JavaContentAssistInvocationContext context) {
ICompilationUnit unit = context.getCompilationUnit();
if (unit == null)
return Collections.emptyList();
ITextViewer viewer = context.getViewer();
CompletionProposalCollector collector = createCollector(context);
collector.setInvocationContext(context);
// Allow completions for unresolved types - since 3.3
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
// Set the favorite list to propose static members - since 3.3
collector.setFavoriteReferences(getFavoriteStaticMembers());
try {
Point selection = viewer.getSelectedRange();
if (selection.y > 0)
collector.setReplacementLength(selection.y);
unit.codeComplete(offset, collector, fTimeoutProgressMonitor);
} catch (OperationCanceledException x) {
// IBindingService bindingSvc= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
// String keyBinding= bindingSvc.getBestActiveBindingFormattedFor(IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST);
// fErrorMessage= Messages.format(JavaTextMessages.CompletionProcessor_error_javaCompletion_took_too_long_message, keyBinding);
JavaPlugin.log(x);
} catch (JavaModelException x) {
// Shell shell= viewer.getTextWidget().getShell();
// if (x.isDoesNotExist() && !unit.getJavaProject().isOnClasspath(unit))
// MessageDialog.openInformation(shell, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_title, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_message);
// else
// ErrorDialog.openError(shell, JavaTextMessages.CompletionProcessor_error_accessing_title, JavaTextMessages.CompletionProcessor_error_accessing_message, x.getStatus());
JavaPlugin.log(x);
}
ICompletionProposal[] javaProposals = collector.getJavaCompletionProposals();
int contextInformationOffset = guessMethodContextInformationPosition(context);
if (contextInformationOffset != offset) {
for (int i = 0; i < javaProposals.length; i++) {
if (javaProposals[i] instanceof JavaMethodCompletionProposal) {
JavaMethodCompletionProposal jmcp = (JavaMethodCompletionProposal) javaProposals[i];
jmcp.setContextInformationPosition(contextInformationOffset);
}
}
}
List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>(Arrays.asList(javaProposals));
if (proposals.size() == 0) {
String error = collector.getErrorMessage();
if (error.length() > 0)
fErrorMessage = error;
}
return proposals;
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class ParameterGuessingProposal method guessParameters.
// /**
// * Returns the currently active java editor, or <code>null</code> if it
// * cannot be determined.
// *
// * @return the currently active java editor, or <code>null</code>
// */
// private JavaEditor getJavaEditor() {
// IEditorPart part= JavaPlugin.getActivePage().getActiveEditor();
// if (part instanceof JavaEditor)
// return (JavaEditor) part;
// else
// return null;
// }
private ICompletionProposal[][] guessParameters(char[][] parameterNames) throws JavaModelException {
// find matches in reverse order. Do this because people tend to declare the variable meant for the last
// parameter last. That is, local variables for the last parameter in the method completion are more
// likely to be closer to the point of code completion. As an example consider a "delegation" completion:
//
// public void myMethod(int param1, int param2, int param3) {
// someOtherObject.yourMethod(param1, param2, param3);
// }
//
// The other consideration is giving preference to variables that have not previously been used in this
// code completion (which avoids "someOtherObject.yourMethod(param1, param1, param1)";
int count = parameterNames.length;
fPositions = new Position[count];
fChoices = new ICompletionProposal[count][];
String[] parameterTypes = getParameterTypes();
ParameterGuesser guesser = new ParameterGuesser(getEnclosingElement());
IJavaElement[][] assignableElements = getAssignableElements();
for (int i = count - 1; i >= 0; i--) {
String paramName = new String(parameterNames[i]);
Position position = new Position(0, 0);
boolean isLastParameter = i == count - 1;
ICompletionProposal[] argumentProposals = guesser.parameterProposals(parameterTypes[i], paramName, position, assignableElements[i], fFillBestGuess, isLastParameter);
if (argumentProposals.length == 0) {
JavaCompletionProposal proposal = new JavaCompletionProposal(paramName, 0, paramName.length(), null, paramName, 0);
if (isLastParameter)
proposal.setTriggerCharacters(new char[] { ',' });
argumentProposals = new ICompletionProposal[] { proposal };
}
fPositions[i] = position;
fChoices[i] = argumentProposals;
}
return fChoices;
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class ParameterGuessingProposal method computeGuessingCompletion.
/**
* Creates the completion string. Offsets and Lengths are set to the offsets and lengths of the
* parameters.
*
* @return the completion string
* @throws org.eclipse.jdt.core.JavaModelException if parameter guessing failed
*/
private String computeGuessingCompletion() throws JavaModelException {
StringBuffer buffer = new StringBuffer();
appendMethodNameReplacement(buffer);
FormatterPrefs prefs = getFormatterPrefs();
setCursorPosition(buffer.length());
if (prefs.afterOpeningParen)
buffer.append(SPACE);
char[][] parameterNames = fProposal.findParameterNames(null);
fChoices = guessParameters(parameterNames);
int count = fChoices.length;
int replacementOffset = getReplacementOffset();
for (int i = 0; i < count; i++) {
if (i != 0) {
if (prefs.beforeComma)
buffer.append(SPACE);
buffer.append(COMMA);
if (prefs.afterComma)
buffer.append(SPACE);
}
ICompletionProposal proposal = fChoices[i][0];
String argument = proposal.getDisplayString();
Position position = fPositions[i];
position.setOffset(replacementOffset + buffer.length());
position.setLength(argument.length());
if (// handle the "unknown" case where we only insert a proposal.
proposal instanceof JavaCompletionProposal)
((JavaCompletionProposal) proposal).setReplacementOffset(replacementOffset + buffer.length());
buffer.append(argument);
}
if (prefs.beforeClosingParen)
buffer.append(SPACE);
buffer.append(RPAREN);
if (canAutomaticallyAppendSemicolon())
buffer.append(SEMICOLON);
return buffer.toString();
}
Aggregations