Java Code Examples for org.eclipse.jdt.core.CompletionProposal#getReplaceStart()

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#getReplaceStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompletionProposalRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
	try {
		IJavaElement enclosingElement = null;
		if (response.getContext().isExtended()) {
			enclosingElement = response.getContext().getEnclosingElement();
		} else if (unit != null) {
			// kept for backward compatibility: CU is not reconciled at this moment, information is missing (bug 70005)
			enclosingElement = unit.getElementAt(proposal.getCompletionLocation() + 1);
		}
		if (enclosingElement == null) {
			return;
		}
		IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
		if (type != null) {
			String prefix = String.valueOf(proposal.getName());
			int completionStart = proposal.getReplaceStart();
			int completionEnd = proposal.getReplaceEnd();
			int relevance = proposal.getRelevance() + 6;

			GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, proposals);
		}
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Accept potential method declaration failed for completion ", e);
	}
}
 
Example 2
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param completionBuffer
 * @param proposal
 */
private void appendMethodOverrideReplacement(StringBuilder completionBuffer, CompletionProposal proposal) {
	IDocument document;
	try {
		document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer());
		String signature = String.valueOf(proposal.getSignature());
		String[] types = Stream.of(Signature.getParameterTypes(signature)).map(t -> Signature.toString(t))
				.toArray(String[]::new);
		String methodName = String.valueOf(proposal.getName());
		int offset = proposal.getReplaceStart();
		String completion = new String(proposal.getCompletion());
		OverrideCompletionProposal overrider = new OverrideCompletionProposal(compilationUnit, methodName, types,
				completion);
		String replacement = overrider.updateReplacementString(document, offset, importRewrite,
				client.isCompletionSnippetsSupported());
		completionBuffer.append(replacement);
	} catch (BadLocationException | CoreException e) {
		JavaLanguageServerPlugin.logException("Failed to compute override replacement", e);
	}
}
 
Example 3
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the replacement length of a given completion proposal. The
 * replacement length is usually the difference between the return values of
 * <code>proposal.getReplaceEnd</code> and
 * <code>proposal.getReplaceStart</code>, but this behavior may be
 * overridden by calling {@link #setReplacementLength(int)}.
 *
 * @param proposal the completion proposal to get the replacement length for
 * @return the replacement length for <code>proposal</code>
 */
protected final int getLength(CompletionProposal proposal) {
	int start= proposal.getReplaceStart();
	int end= proposal.getReplaceEnd();
	int length;
	if (fUserReplacementLength == -1) {
		length= end - start;
	} else {
		length= fUserReplacementLength;
		// extend length to begin at start
		int behindCompletion= proposal.getCompletionLocation() + 1;
		if (start < behindCompletion) {
			length+= behindCompletion - start;
		}
	}
	return length;
}
 
Example 4
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
	try {
		IJavaElement enclosingElement= null;
		if (getContext().isExtended()) {
			enclosingElement= getContext().getEnclosingElement();
		} else if (fCompilationUnit != null) {
			// kept for backward compatibility: CU is not reconciled at this moment, information is missing (bug 70005)
			enclosingElement= fCompilationUnit.getElementAt(proposal.getCompletionLocation() + 1);
		}
		if (enclosingElement == null)
			return;
		IType type= (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
		if (type != null) {
			String prefix= String.valueOf(proposal.getName());
			int completionStart= proposal.getReplaceStart();
			int completionEnd= proposal.getReplaceEnd();
			int relevance= computeRelevance(proposal);

			GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 2, fSuggestedMethodNames, fJavaProposals);
			MethodDeclarationCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, fSuggestedMethodNames, fJavaProposals);
		}
	} catch (CoreException e) {
		JavaPlugin.log(e);
	}
}
 
Example 5
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IJavaCompletionProposal createFieldProposal(CompletionProposal proposal) {
	String completion= String.valueOf(proposal.getCompletion());
	int start= proposal.getReplaceStart();
	int length= getLength(proposal);
	StyledString label= fLabelProvider.createStyledLabel(proposal);
	Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal));
	int relevance= computeRelevance(proposal);

	JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext());
	if (fJavaProject != null)
		javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));

	javaProposal.setTriggerCharacters(VAR_TRIGGER);

	return javaProposal;
}
 
Example 6
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates the Java completion proposal for the JDT Core
 * {@link CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal.
 *
 * @param proposal the JDT Core proposal
 * @return the Java completion proposal
 * @since 3.4
 */
private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) {
	String completion= String.valueOf(proposal.getCompletion());
	completion= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", fJavaProject); //$NON-NLS-1$
	int start= proposal.getReplaceStart();
	int length= getLength(proposal);
	StyledString label= fLabelProvider.createStyledLabel(proposal);
	Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal));
	int relevance= computeRelevance(proposal);

	JavaCompletionProposal javaProposal= new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal);
	if (fJavaProject != null)
		javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));

	javaProposal.setTriggerCharacters(VAR_TRIGGER);

	return javaProposal;
}
 
Example 7
Source File: CompletionProposalRequestor.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
    try {
        IJavaElement enclosingElement = null;
        if (response.getContext().isExtended()) {
            enclosingElement = response.getContext().getEnclosingElement();
        } else if (typeRoot != null) {
            // kept for backward compatibility: CU is not reconciled at this moment,
            // information is missing (bug 70005)
            enclosingElement = typeRoot.getElementAt(proposal.getCompletionLocation() + 1);
        }
        if (enclosingElement == null) {
            return;
        }
        IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
        if (type != null) {
            String prefix = String.valueOf(proposal.getName());
            int completionStart = proposal.getReplaceStart();
            int completionEnd = proposal.getReplaceEnd();
            int relevance = proposal.getRelevance() + 6;

            GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart,
                    completionEnd - completionStart, relevance, proposals);
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Accept potential method declaration failed for completion ", e);
    }
}
 
Example 8
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal createAnnotationAttributeReferenceProposal(CompletionProposal proposal) {
	StyledString displayString= fLabelProvider.createLabelWithTypeAndDeclaration(proposal);
	ImageDescriptor descriptor= fLabelProvider.createMethodImageDescriptor(proposal);
	String completion= String.valueOf(proposal.getCompletion());
	JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, proposal.getReplaceStart(), getLength(proposal), getImage(descriptor), displayString, computeRelevance(proposal));
	if (fJavaProject != null)
		javaProposal.setProposalInfo(new AnnotationAtttributeProposalInfo(fJavaProject, proposal));
	return javaProposal;
}
 
Example 9
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal createAnonymousTypeProposal(CompletionProposal proposal, JavaContentAssistInvocationContext invocationContext) {
	if (fCompilationUnit == null || fJavaProject == null)
		return null;

	char[] declarationKey= proposal.getDeclarationKey();
	if (declarationKey == null)
		return null;

	try {
		IJavaElement element= fJavaProject.findElement(new String(declarationKey), null);
		if (!(element instanceof IType))
			return null;

		IType type= (IType) element;

		String completion= String.valueOf(proposal.getCompletion());
		int start= proposal.getReplaceStart();
		int length= getLength(proposal);
		int relevance= computeRelevance(proposal);

		StyledString label= fLabelProvider.createAnonymousTypeLabel(proposal);

		JavaCompletionProposal javaProposal= new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, invocationContext, start, length, completion, label, String.valueOf(proposal
				.getDeclarationSignature()), type, relevance);
		javaProposal.setProposalInfo(new AnonymousTypeProposalInfo(fJavaProject, proposal));
		return javaProposal;
	} catch (JavaModelException e) {
		return null;
	}
}
 
Example 10
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal createKeywordProposal(CompletionProposal proposal) {
	String completion= String.valueOf(proposal.getCompletion());
	int start= proposal.getReplaceStart();
	int length= getLength(proposal);
	StyledString label= fLabelProvider.createSimpleLabel(proposal);
	int relevance= computeRelevance(proposal);
	return new JavaCompletionProposal(completion, start, length, null, label, relevance);
}
 
Example 11
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal createLabelProposal(CompletionProposal proposal) {
	String completion= String.valueOf(proposal.getCompletion());
	int start= proposal.getReplaceStart();
	int length= getLength(proposal);
	StyledString label= fLabelProvider.createSimpleLabel(proposal);
	int relevance= computeRelevance(proposal);

	return new JavaCompletionProposal(completion, start, length, null, label, relevance);
}
 
Example 12
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal createLocalVariableProposal(CompletionProposal proposal) {
	String completion= String.valueOf(proposal.getCompletion());
	int start= proposal.getReplaceStart();
	int length= getLength(proposal);
	Image image= getImage(fLabelProvider.createLocalImageDescriptor(proposal));
	StyledString label= fLabelProvider.createSimpleLabelWithType(proposal);
	int relevance= computeRelevance(proposal);
	final JavaCompletionProposal javaProposal= new JavaCompletionProposal(completion, start, length, image, label, relevance);
	javaProposal.setTriggerCharacters(VAR_TRIGGER);
	return javaProposal;
}