Search in sources :

Example 6 with ICompletionProposal

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;
}
Also used : TemplateProposal(org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 7 with ICompletionProposal

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;
}
Also used : ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IContextInformation(org.eclipse.che.jface.text.contentassist.IContextInformation)

Example 8 with ICompletionProposal

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.che.jface.text.ITextViewer) CompletionProposalCollector(org.eclipse.jdt.ui.text.java.CompletionProposalCollector) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal)

Example 9 with ICompletionProposal

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;
}
Also used : Position(org.eclipse.jface.text.Position) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Point(org.eclipse.swt.graphics.Point)

Example 10 with ICompletionProposal

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();
}
Also used : Position(org.eclipse.jface.text.Position) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Point(org.eclipse.swt.graphics.Point)

Aggregations

ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)19 Point (org.eclipse.swt.graphics.Point)9 ArrayList (java.util.ArrayList)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)4 IDocument (org.eclipse.jface.text.IDocument)4 Position (org.eclipse.jface.text.Position)3 Test (org.junit.Test)3 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)2 ICompletionProposalExtension5 (org.eclipse.che.jface.text.contentassist.ICompletionProposalExtension5)2 LinkedDataImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl)2 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)2 LinkedPositionGroupImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl)2 RegionImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)2 QuickFixTest (org.eclipse.che.plugin.java.server.jdt.quickfix.QuickFixTest)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 IType (org.eclipse.jdt.core.IType)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2