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

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#getRequiredProposals() . 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: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void createAnonymousTypeLabel(CompletionProposal proposal, CompletionItem item) {
	char[] declaringTypeSignature= proposal.getDeclarationSignature();
	declaringTypeSignature= Signature.getTypeErasure(declaringTypeSignature);
	String name = new String(Signature.getSignatureSimpleName(declaringTypeSignature));
	item.setInsertText(name);
	StringBuilder buf= new StringBuilder();
	buf.append(name);
	buf.append('(');
	appendUnboundedParameterList(buf, proposal);
	buf.append(')');
	buf.append("  "); //$NON-NLS-1$
	buf.append("Anonymous Inner Type"); //TODO: consider externalization
	item.setLabel(buf.toString());

	if (proposal.getRequiredProposals() != null) {
		char[] signatureQualifier= Signature.getSignatureQualifier(declaringTypeSignature);
		if (signatureQualifier.length > 0) {
			item.setDetail(String.valueOf(signatureQualifier) + "." + name);
		}
	}
	setDeclarationSignature(item, String.valueOf(declaringTypeSignature));
}
 
Example 2
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
StyledString createAnonymousTypeLabel(CompletionProposal proposal) {
	char[] declaringTypeSignature= proposal.getDeclarationSignature();
	declaringTypeSignature= Signature.getTypeErasure(declaringTypeSignature);

	StyledString buffer= new StyledString();
	buffer.append(Signature.getSignatureSimpleName(declaringTypeSignature));
	buffer.append('(');
	appendUnboundedParameterList(buffer, proposal);
	buffer.append(')');
	buffer.append("  "); //$NON-NLS-1$
	buffer.append(JavaTextMessages.ResultCollector_anonymous_type);

	if (proposal.getRequiredProposals() != null) {
		char[] signatureQualifier= Signature.getSignatureQualifier(declaringTypeSignature);
		if (signatureQualifier.length > 0) {
			buffer.append(JavaElementLabels.CONCAT_STRING, StyledString.QUALIFIER_STYLER);
			buffer.append(signatureQualifier, StyledString.QUALIFIER_STYLER);
		}
	}

	return Strings.markJavaElementLabelLTR(buffer);
}
 
Example 3
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Updates a display label for the given method proposal to item. The display label
 * consists of:
 * <ul>
 *   <li>the method name</li>
 *   <li>the parameter list (see {@link #createParameterList(CompletionProposal)})</li>
 *   <li>the upper bound of the return type (see {@link SignatureUtil#getUpperBound(String)})</li>
 *   <li>the raw simple name of the declaring type</li>
 * </ul>
 * <p>
 * Examples:
 * For the <code>get(int)</code> method of a variable of type <code>List<? extends Number></code>, the following
 * display name is returned: <code>get(int index)  Number - List</code>.<br>
 * For the <code>add(E)</code> method of a variable of type <code>List<? super Number></code>, the following
 * display name is returned: <code>add(Number o)  void - List</code>.<br>
 * </p>
 *
 * @param methodProposal the method proposal to display
 * @param item to update
 */
private void createMethodProposalLabel(CompletionProposal methodProposal, CompletionItem item) {
	StringBuilder description = this.createMethodProposalDescription(methodProposal);
	item.setLabel(description.toString());
	item.setInsertText(String.valueOf(methodProposal.getName()));

	// declaring type
	StringBuilder typeInfo = new StringBuilder();
	String declaringType= extractDeclaringTypeFQN(methodProposal);

	if (methodProposal.getRequiredProposals() != null) {
		String qualifier= Signature.getQualifier(declaringType);
		if (qualifier.length() > 0) {
			typeInfo.append(qualifier);
			typeInfo.append('.');
		}
	}

	declaringType= Signature.getSimpleName(declaringType);
	typeInfo.append(declaringType);
	StringBuilder detail = new StringBuilder();
	if (typeInfo.length() > 0) {
		detail.append(typeInfo);
		detail.append('.');
	}
	detail.append(description);
	item.setDetail(detail.toString());

	setSignature(item, String.valueOf(methodProposal.getSignature()));
	setDeclarationSignature(item, String.valueOf(methodProposal.getDeclarationSignature()));
	setName(item, String.valueOf(methodProposal.getName()));

}
 
Example 4
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void createLabelWithTypeAndDeclaration(CompletionProposal proposal, CompletionItem item) {
	char[] name= proposal.getCompletion();
	if (!isThisPrefix(name)) {
		name= proposal.getName();
	}
	StringBuilder buf= new StringBuilder();

	buf.append(name);
	item.setInsertText(buf.toString());
	char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
	if (typeName.length > 0) {
		buf.append(VAR_TYPE_SEPARATOR);
		buf.append(typeName);
	}
	item.setLabel(buf.toString());

	char[] declaration= proposal.getDeclarationSignature();
	StringBuilder detailBuf = new StringBuilder();
	if (declaration != null) {
		setDeclarationSignature(item, String.valueOf(declaration));
		declaration= Signature.getSignatureSimpleName(declaration);
		if (declaration.length > 0) {
			if (proposal.getRequiredProposals() != null) {
				String declaringType= extractDeclaringTypeFQN(proposal);
				String qualifier= Signature.getQualifier(declaringType);
				if (qualifier.length() > 0) {
					detailBuf.append(qualifier);
					detailBuf.append('.');
				}
			}
			detailBuf.append(declaration);
		}
	}
	if (detailBuf.length() > 0) {
		detailBuf.append('.');
	}
	detailBuf.append(buf);
	item.setDetail(detailBuf.toString());
	setName(item,String.valueOf(name));
}
 
Example 5
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a display label for the given method proposal. The display label
 * consists of:
 * <ul>
 *   <li>the method name</li>
 *   <li>the parameter list (see {@link #createParameterList(CompletionProposal)})</li>
 *   <li>the upper bound of the return type (see {@link SignatureUtil#getUpperBound(String)})</li>
 *   <li>the raw simple name of the declaring type</li>
 * </ul>
 * <p>
 * Examples:
 * For the <code>get(int)</code> method of a variable of type <code>List<? extends Number></code>, the following
 * display name is returned: <code>get(int index)  Number - List</code>.<br>
 * For the <code>add(E)</code> method of a variable of type <code>List<? super Number></code>, the following
 * display name is returned: <code>add(Number o)  void - List</code>.<br>
 * </p>
 *
 * @param methodProposal the method proposal to display
 * @return the display label for the given method proposal
 */
StyledString createMethodProposalLabel(CompletionProposal methodProposal) {
	StyledString nameBuffer= new StyledString();

	// method name
	nameBuffer.append(methodProposal.getName());

	// parameters
	nameBuffer.append('(');
	appendUnboundedParameterList(nameBuffer, methodProposal);
	nameBuffer.append(')');

	// return type
	if (!methodProposal.isConstructor()) {
		// TODO remove SignatureUtil.fix83600 call when bugs are fixed
		char[] returnType= createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(methodProposal.getSignature()))));
		nameBuffer.append(RETURN_TYPE_SEPARATOR);
		nameBuffer.append(returnType);
	}

	// declaring type
	nameBuffer.append(QUALIFIER_SEPARATOR, StyledString.QUALIFIER_STYLER);
	String declaringType= extractDeclaringTypeFQN(methodProposal);

	if (methodProposal.getRequiredProposals() != null) {
		String qualifier= Signature.getQualifier(declaringType);
		if (qualifier.length() > 0) {
			nameBuffer.append(qualifier, StyledString.QUALIFIER_STYLER);
			nameBuffer.append('.', StyledString.QUALIFIER_STYLER);
		}
	}

	declaringType= Signature.getSimpleName(declaringType);
	nameBuffer.append(declaringType, StyledString.QUALIFIER_STYLER);
	return Strings.markJavaElementLabelLTR(nameBuffer);
}
 
Example 6
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
StyledString createLabelWithTypeAndDeclaration(CompletionProposal proposal) {
	char[] name= proposal.getCompletion();
	if (!isThisPrefix(name))
		name= proposal.getName();

	StyledString buf= new StyledString();
	buf.append(name);
	char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
	if (typeName.length > 0) {
		buf.append(VAR_TYPE_SEPARATOR);
		buf.append(typeName);
	}
	char[] declaration= proposal.getDeclarationSignature();
	if (declaration != null) {
		declaration= Signature.getSignatureSimpleName(declaration);
		if (declaration.length > 0) {
			buf.append(QUALIFIER_SEPARATOR, StyledString.QUALIFIER_STYLER);
			if (proposal.getRequiredProposals() != null) {
				String declaringType= extractDeclaringTypeFQN(proposal);
				String qualifier= Signature.getQualifier(declaringType);
				if (qualifier.length() > 0) {
					buf.append(qualifier, StyledString.QUALIFIER_STYLER);
					buf.append('.', StyledString.QUALIFIER_STYLER);
				}
			}
			buf.append(declaration, StyledString.QUALIFIER_STYLER);
		}
	}

	return Strings.markJavaElementLabelLTR(buf);
}
 
Example 7
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void updateReplacement(CompletionProposal proposal, CompletionItem item, char trigger, boolean isResolving) {
	// reset importRewrite
	this.importRewrite = TypeProposalUtils.createImportRewrite(compilationUnit);

	List<org.eclipse.lsp4j.TextEdit> additionalTextEdits = new ArrayList<>();

	StringBuilder completionBuffer = new StringBuilder();
	Range range = null;
	if (isSupportingRequiredProposals(proposal)) {
		CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
		if (requiredProposals != null) {
			for (CompletionProposal requiredProposal : requiredProposals) {
				switch (requiredProposal.getKind()) {
				case CompletionProposal.TYPE_IMPORT:
				case CompletionProposal.METHOD_IMPORT:
				case CompletionProposal.FIELD_IMPORT:
					appendImportProposal(completionBuffer, requiredProposal, proposal.getKind());
					break;
				case CompletionProposal.TYPE_REF:
					org.eclipse.lsp4j.TextEdit edit = toRequiredTypeEdit(requiredProposal, trigger, proposal.canUseDiamond(context));
					if (proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION
						|| proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION
						|| proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
							completionBuffer.append(edit.getNewText());
							range = edit.getRange();
					} else {
						additionalTextEdits.add(edit);
					}
					break;
				default:
					/*
					 * In 3.3 we only support the above required proposals, see
					 * CompletionProposal#getRequiredProposals()
					 */
					Assert.isTrue(false);
				}
			}
		}
	}

	if (range == null) {
		boolean completionOverwrite = preferences.isCompletionOverwrite();
		if (!completionOverwrite && (proposal.getKind() == CompletionProposal.METHOD_REF || proposal.getKind() == CompletionProposal.LOCAL_VARIABLE_REF || proposal.getKind() == CompletionProposal.FIELD_REF)) {
			// See https://github.com/redhat-developer/vscode-java/issues/462
			int end = proposal.getReplaceEnd();
			if (end > offset) {
				proposal.setReplaceRange(proposal.getReplaceStart(), offset);
			}
		}
		range = toReplacementRange(proposal);
	}
	if (proposal.getKind() == CompletionProposal.METHOD_DECLARATION) {
		appendMethodOverrideReplacement(completionBuffer, proposal);
	} else if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION && proposal instanceof GetterSetterCompletionProposal) {
		appendMethodPotentialReplacement(completionBuffer, (GetterSetterCompletionProposal) proposal);
	} else if (proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
		appendAnonymousClass(completionBuffer, proposal, range);
	} else {
		appendReplacementString(completionBuffer, proposal);
	}
	//select insertTextFormat.
	if (client.isCompletionSnippetsSupported()) {
		item.setInsertTextFormat(InsertTextFormat.Snippet);
	} else {
		item.setInsertTextFormat(InsertTextFormat.PlainText);
	}
	String text = completionBuffer.toString();
	if (range != null) {
		item.setTextEdit(new org.eclipse.lsp4j.TextEdit(range, text));
	} else {
		// fallback
		item.setInsertText(text);
	}

	if (!client.isResolveAdditionalTextEditsSupport() || isResolving) {
		addImports(additionalTextEdits);
		if(!additionalTextEdits.isEmpty()){
			item.setAdditionalTextEdits(additionalTextEdits);
		}
	}
}
 
Example 8
Source File: SimilarElementsRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
	StringBuffer dummyCU= new StringBuffer();
	String packName= cu.getParent().getElementName();
	IType type= cu.findPrimaryType();
	if (type == null) {
		return new String[0];
	}

	if (packName.length() > 0) {
		dummyCU.append("package ").append(packName).append(';'); //$NON-NLS-1$
	}
	dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName); // static initializer  //$NON-NLS-1$//$NON-NLS-2$
	int offset= dummyCU.length();
	dummyCU.append("\n}\n }"); //$NON-NLS-1$

	ICompilationUnit newCU= null;
	try {
		newCU= cu.getWorkingCopy(null);
		newCU.getBuffer().setContents(dummyCU.toString());

		final HashSet<String> result= new HashSet<>();

		CompletionRequestor requestor= new CompletionRequestor(true) {
			@Override
			public void accept(CompletionProposal proposal) {
				if (elementName.equals(new String(proposal.getName()))) {
					CompletionProposal[] requiredProposals= proposal.getRequiredProposals();
					for (int i= 0; i < requiredProposals.length; i++) {
						CompletionProposal curr= requiredProposals[i];
						if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
							result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
						}
					}
				}
			}
		};

		if (isMethod) {
			requestor.setIgnored(CompletionProposal.METHOD_REF, false);
			requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
		} else {
			requestor.setIgnored(CompletionProposal.FIELD_REF, false);
			requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
		}
		requestor.setFavoriteReferences(favorites);

		newCU.codeComplete(offset, requestor);

		return result.toArray(new String[result.size()]);
	} finally {
		if (newCU != null) {
			newCU.discardWorkingCopy();
		}
	}
}
 
Example 9
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns a version of <code>descriptor</code> decorated according to
 * the passed <code>modifier</code> flags.
 *
 * @param descriptor the image descriptor to decorate
 * @param proposal the proposal
 * @return an image descriptor for a method proposal
 * @see Flags
 */
private ImageDescriptor decorateImageDescriptor(ImageDescriptor descriptor, CompletionProposal proposal) {
	int adornments= 0;
	int flags= proposal.getFlags();
	int kind= proposal.getKind();

	boolean deprecated= Flags.isDeprecated(flags);
	if (!deprecated) {
		CompletionProposal[] requiredProposals= proposal.getRequiredProposals();
		if (requiredProposals != null) {
			for (int i= 0; i < requiredProposals.length; i++) {
				CompletionProposal requiredProposal= requiredProposals[i];
				if (requiredProposal.getKind() == CompletionProposal.TYPE_REF) {
					deprecated |= Flags.isDeprecated(requiredProposal.getFlags());
				}
			}
		}
	}
	if (deprecated)
		adornments |= JavaElementImageDescriptor.DEPRECATED;

	if (kind == CompletionProposal.FIELD_REF || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE
			|| kind == CompletionProposal.METHOD_REF || kind == CompletionProposal.CONSTRUCTOR_INVOCATION)
		if (Flags.isStatic(flags))
			adornments |= JavaElementImageDescriptor.STATIC;

	if (kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF
			|| kind == CompletionProposal.CONSTRUCTOR_INVOCATION)
		if (Flags.isSynchronized(flags))
			adornments |= JavaElementImageDescriptor.SYNCHRONIZED;
	if (kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF)
		if (Flags.isDefaultMethod(flags))
			adornments|= JavaElementImageDescriptor.DEFAULT_METHOD;
	if (kind == CompletionProposal.ANNOTATION_ATTRIBUTE_REF)
		if (Flags.isAnnnotationDefault(flags))
			adornments|= JavaElementImageDescriptor.ANNOTATION_DEFAULT;

	if (kind == CompletionProposal.TYPE_REF && Flags.isAbstract(flags) && !Flags.isInterface(flags))
		adornments |= JavaElementImageDescriptor.ABSTRACT;

	if (kind == CompletionProposal.FIELD_REF) {
		if (Flags.isFinal(flags))
			adornments |= JavaElementImageDescriptor.FINAL;
		if (Flags.isTransient(flags))
			adornments |= JavaElementImageDescriptor.TRANSIENT;
		if (Flags.isVolatile(flags))
			adornments |= JavaElementImageDescriptor.VOLATILE;
	}

	return new JavaElementImageDescriptor(descriptor, adornments, JavaElementImageProvider.SMALL_SIZE);
}
 
Example 10
Source File: SimilarElementsRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
	StringBuffer dummyCU= new StringBuffer();
	String packName= cu.getParent().getElementName();
	IType type= cu.findPrimaryType();
	if (type == null)
		return new String[0];
	
	if (packName.length() > 0) {
		dummyCU.append("package ").append(packName).append(';'); //$NON-NLS-1$
	}		
	dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName); // static initializer  //$NON-NLS-1$//$NON-NLS-2$
	int offset= dummyCU.length();
	dummyCU.append("\n}\n }"); //$NON-NLS-1$
	
	ICompilationUnit newCU= null;
	try {
		newCU= cu.getWorkingCopy(null);
		newCU.getBuffer().setContents(dummyCU.toString());
		
		final HashSet<String> result= new HashSet<String>();
		
		CompletionRequestor requestor= new CompletionRequestor(true) {
			@Override
			public void accept(CompletionProposal proposal) {
				if (elementName.equals(new String(proposal.getName()))) {
					CompletionProposal[] requiredProposals= proposal.getRequiredProposals();
					for (int i= 0; i < requiredProposals.length; i++) {
						CompletionProposal curr= requiredProposals[i];
						if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
							result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
						}
					}
				}
			}
		};
		
		if (isMethod) {
			requestor.setIgnored(CompletionProposal.METHOD_REF, false);
			requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
		} else {
			requestor.setIgnored(CompletionProposal.FIELD_REF, false);
			requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
		}
		requestor.setFavoriteReferences(favorites);
		
		newCU.codeComplete(offset, requestor);
		
		return result.toArray(new String[result.size()]);
	} finally {
		if (newCU != null) {
			newCU.discardWorkingCopy();
		}	
	}	
}