Java Code Examples for org.eclipse.jdt.core.CompletionProposal#METHOD_REF

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#METHOD_REF . 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: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates and returns a parameter list of the given method or type proposal suitable for
 * display. The list does not include parentheses. The lower bound of parameter types is
 * returned.
 * <p>
 * Examples:
 * 
 * <pre>
 *   &quot;void method(int i, String s)&quot; -&gt; &quot;int i, String s&quot;
 *   &quot;? extends Number method(java.lang.String s, ? super Number n)&quot; -&gt; &quot;String s, Number n&quot;
 * </pre>
 * 
 * </p>
 * 
 * @param proposal the proposal to create the parameter list for
 * @return the list of comma-separated parameters suitable for display
 */
public String createParameterList(CompletionProposal proposal) {
	String paramList;
	int kind= proposal.getKind();
	switch (kind) {
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
			paramList= appendUnboundedParameterList(new StyledString(), proposal).getString();
			return Strings.markJavaElementLabelLTR(paramList);
		case CompletionProposal.TYPE_REF:
		case CompletionProposal.JAVADOC_TYPE_REF:
			paramList= appendTypeParameterList(new StyledString(), proposal).getString();
			return Strings.markJavaElementLabelLTR(paramList);
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			paramList= appendUnboundedParameterList(new StyledString(), proposal).getString();
			return Strings.markJavaElementLabelLTR(paramList);
		default:
			Assert.isLegal(false);
			return null; // dummy
	}
}
 
Example 2
Source File: JsniCompletionProposalCollector.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IJavaCompletionProposal createJavaCompletionProposal(
    CompletionProposal proposal) {
  IJavaCompletionProposal defaultProposal = super.createJavaCompletionProposal(proposal);

  // For members of inner classes, there's a bug in the JDT which results in
  // the declaration signature of the proposal missing the outer classes, so
  // we have to manually set the signature instead.
  if (proposal.getKind() == CompletionProposal.METHOD_REF
      || proposal.getKind() == CompletionProposal.FIELD_REF) {
    char[] typeSignature = Signature.createTypeSignature(qualifiedTypeName,
        true).toCharArray();
    proposal.setDeclarationSignature(typeSignature);
  }

  return JsniCompletionProposal.create(defaultProposal, proposal,
      getCompilationUnit().getJavaProject(), refOffset, refLength);
}
 
Example 3
Source File: JsniCompletionProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private String createReplaceString() {
  switch (wrappedProposal.getKind()) {
    case CompletionProposal.PACKAGE_REF:
      return computePackageCompletion();
    case CompletionProposal.TYPE_REF:
      return computeTypeCompletion();
    case CompletionProposal.FIELD_REF:
      return computeFieldCompletion();
    case CompletionProposal.METHOD_REF:
      if (wrappedProposal.isConstructor()) {
        return computeCtorCompletion();
      } else {
        return computeMethodCompletion();
      }
    default:
      return "";
  }
}
 
Example 4
Source File: JavaElExpressionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public void computeProposals(List<ICompletionProposal> proposals)
    throws UiBinderException {
  IEvaluationContext evalContext = createEvaluationContext();

  try {
    // Get proposals for instance methods and static methods by treating the
    // Java type as an instance.
    // Since the type does not necessarily have to have a zero-arg
    // constructor, we use a local variable in the completion request below
    JavaElExpressionCompletionRequestor requestor = new JavaElExpressionCompletionRequestor(
        CompletionProposal.METHOD_REF);
    String snippet = javaType.getFullyQualifiedName('.') + " tmpVar; tmpVar."
        + getEnteredText();
    evalContext.codeComplete(snippet, snippet.length(), requestor);
    proposals.addAll(requestor.getProposals());

  } catch (JavaModelException e) {
    throw new UiBinderException(e);
  }
}
 
Example 5
Source File: ProposalGeneratingCompletionRequestor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private void ignoreAll() {
  int[] ignoredKinds = new int[] {
      CompletionProposal.ANONYMOUS_CLASS_DECLARATION,
      CompletionProposal.FIELD_REF, CompletionProposal.KEYWORD,
      CompletionProposal.LABEL_REF, CompletionProposal.LOCAL_VARIABLE_REF,
      CompletionProposal.METHOD_REF, CompletionProposal.METHOD_DECLARATION,
      CompletionProposal.PACKAGE_REF, CompletionProposal.TYPE_REF,
      CompletionProposal.VARIABLE_DECLARATION,
      CompletionProposal.POTENTIAL_METHOD_DECLARATION,
      CompletionProposal.METHOD_NAME_REFERENCE,
      CompletionProposal.ANNOTATION_ATTRIBUTE_REF,
      CompletionProposal.JAVADOC_FIELD_REF,
      CompletionProposal.JAVADOC_METHOD_REF,
      CompletionProposal.JAVADOC_TYPE_REF,
      CompletionProposal.JAVADOC_VALUE_REF,
      CompletionProposal.JAVADOC_PARAM_REF,
      CompletionProposal.JAVADOC_BLOCK_TAG,
      CompletionProposal.JAVADOC_INLINE_TAG, CompletionProposal.FIELD_IMPORT,
      CompletionProposal.METHOD_IMPORT, CompletionProposal.TYPE_IMPORT};

  for (int kind : ignoredKinds) {
    setIgnored(kind, true);
  }
}
 
Example 6
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates and returns the method signature suitable for display.
 *
 * @param proposal
 *            the proposal to create the description for
 * @return the string of method signature suitable for display
 */
public StringBuilder createMethodProposalDescription(CompletionProposal proposal) {
	int kind = proposal.getKind();
	StringBuilder description = new StringBuilder();
	switch (kind) {
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:

			// method name
			description.append(proposal.getName());

			// parameters
			description.append('(');
			appendUnboundedParameterList(description, proposal);
			description.append(')');

			// return type
			if (!proposal.isConstructor()) {
				// TODO remove SignatureUtil.fix83600 call when bugs are fixed
				char[] returnType = createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(proposal.getSignature()))));
				description.append(RETURN_TYPE_SEPARATOR);
				description.append(returnType);
			}
	}
	return description; // dummy
}
 
Example 7
Source File: LazyJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected int computeRelevance() {
	final int baseRelevance= fProposal.getRelevance() * 16;
	switch (fProposal.getKind()) {
		case CompletionProposal.PACKAGE_REF:
			return baseRelevance + 0;
		case CompletionProposal.LABEL_REF:
			return baseRelevance + 1;
		case CompletionProposal.KEYWORD:
			return baseRelevance + 2;
		case CompletionProposal.TYPE_REF:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			return baseRelevance + 3;
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
			return baseRelevance + 4;
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
			return baseRelevance + 4 /* + 99 */;
		case CompletionProposal.FIELD_REF:
			return baseRelevance + 5;
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
			return baseRelevance + 6;
		default:
			return baseRelevance;
	}
}
 
Example 8
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the relevance for a given <code>CompletionProposal</code>.
 * <p>
 * Subclasses may replace, but usually should not need to.
 * </p>
 * @param proposal the proposal to compute the relevance for
 * @return the relevance for <code>proposal</code>
 */
protected int computeRelevance(CompletionProposal proposal) {
	final int baseRelevance= proposal.getRelevance() * 16;
	switch (proposal.getKind()) {
		case CompletionProposal.PACKAGE_REF:
			return baseRelevance + 0;
		case CompletionProposal.LABEL_REF:
			return baseRelevance + 1;
		case CompletionProposal.KEYWORD:
			return baseRelevance + 2;
		case CompletionProposal.TYPE_REF:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			return baseRelevance + 3;
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
			return baseRelevance + 4;
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
			return baseRelevance + 4 /* + 99 */;
		case CompletionProposal.FIELD_REF:
			return baseRelevance + 5;
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
			return baseRelevance + 6;
		default:
			return baseRelevance;
	}
}
 
Example 9
Source File: SortTextHelper.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Computes the relevance for a given <code>CompletionProposal</code>.
 *
 * @param proposal the proposal to compute the relevance for
 * @return the relevance for <code>proposal</code>
 */
public static String computeSortText(CompletionProposal proposal) {
	final int baseRelevance= proposal.getRelevance() * 16;
	switch (proposal.getKind()) {
	case CompletionProposal.LABEL_REF:
		return convertRelevance( baseRelevance + 1);
	case CompletionProposal.KEYWORD:
		return convertRelevance(baseRelevance + 2);
	case CompletionProposal.TYPE_REF:
	case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
	case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
		return convertRelevance(baseRelevance + 3);
	case CompletionProposal.METHOD_REF:
	case CompletionProposal.CONSTRUCTOR_INVOCATION:
	case CompletionProposal.METHOD_NAME_REFERENCE:
	case CompletionProposal.METHOD_DECLARATION:
	case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
	case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		return convertRelevance(baseRelevance + 4);
	case CompletionProposal.FIELD_REF:
		return convertRelevance(baseRelevance + 5);
	case CompletionProposal.LOCAL_VARIABLE_REF:
	case CompletionProposal.VARIABLE_DECLARATION:
		return convertRelevance(baseRelevance + 6);
	case CompletionProposal.PACKAGE_REF://intentional fall-through
	default:
		return convertRelevance(baseRelevance);
	}
}
 
Example 10
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isSupportingRequiredProposals(CompletionProposal proposal) {
	return proposal != null
			&& (proposal.getKind() == CompletionProposal.METHOD_REF
			|| proposal.getKind() == CompletionProposal.FIELD_REF
			|| proposal.getKind() == CompletionProposal.TYPE_REF
					|| proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION
					|| proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION);
}
 
Example 11
Source File: JsniCompletionProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return proposals only for references to a package, type, field, method or
 * constructor (and nothing else).
 */
public static IJavaCompletionProposal create(
    IJavaCompletionProposal jdtProposal, CompletionProposal wrappedProposal,
    IJavaProject javaProject, int replaceOffset, int replaceLength) {
  switch (wrappedProposal.getKind()) {
    case CompletionProposal.PACKAGE_REF:
    case CompletionProposal.TYPE_REF:
    case CompletionProposal.FIELD_REF:
    case CompletionProposal.METHOD_REF:
      return new JsniCompletionProposal(jdtProposal, wrappedProposal,
          javaProject, replaceOffset, replaceLength);
    default:
      return null;
  }
}
 
Example 12
Source File: JavaElExpressionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ICompletionProposal createProposal(CompletionProposal javaProposal) {
  char[] signature = javaProposal.getSignature();
  String replacementText = null;
  int relevanceAdjustment = 0;

  if (javaProposal.getKind() != CompletionProposal.METHOD_REF) {
    return null;
  }

  if (Signature.getParameterCount(signature) != 0) {
    // Only zero-arg methods are allowed
    return null;
  }

  String returnType = String.valueOf(Signature.getReturnType(signature));
  if (Signature.SIG_VOID.equals(returnType)) {
    // Methods with void return type are not allowed
    return null;
  }

  relevanceAdjustment += getRelevanceAdjustmentForMyTypeAndDeclarationType(
      returnType, javaProposal.getDeclarationSignature());
  replacementText = String.valueOf(javaProposal.getName());

  IJavaCompletionProposal jdtCompletionProposal = JavaContentAssistUtilities.getJavaCompletionProposal(
      javaProposal, getContext(), getJavaProject());
  ReplacementCompletionProposal proposal = ReplacementCompletionProposal.fromExistingCompletionProposal(
      replacementText, getReplaceOffset(), getReplaceLength(),
      jdtCompletionProposal);

  if (relevanceAdjustment != 0) {
    proposal.setRelevance(proposal.getRelevance() + relevanceAdjustment);
  }

  return proposal;
}
 
Example 13
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a display label with styles for a given <code>CompletionProposal</code>.
 *
 * @param proposal the completion proposal to create the display label for
 * @return the display label for <code>proposal</code>
 *
 * @since 3.4
 */
public StyledString createStyledLabel(CompletionProposal proposal) {
	switch (proposal.getKind()) {
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
			if (fContext != null && fContext.isInJavadoc())
				return createJavadocMethodProposalLabel(proposal);
			return createMethodProposalLabel(proposal);
		case CompletionProposal.METHOD_DECLARATION:
			return createOverrideMethodProposalLabel(proposal);
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			return createAnonymousTypeLabel(proposal);
		case CompletionProposal.TYPE_REF:
			return createTypeProposalLabel(proposal);
		case CompletionProposal.JAVADOC_TYPE_REF:
			return createJavadocTypeProposalLabel(proposal);
		case CompletionProposal.JAVADOC_FIELD_REF:
		case CompletionProposal.JAVADOC_VALUE_REF:
		case CompletionProposal.JAVADOC_BLOCK_TAG:
		case CompletionProposal.JAVADOC_INLINE_TAG:
		case CompletionProposal.JAVADOC_PARAM_REF:
			return createJavadocSimpleProposalLabel(proposal);
		case CompletionProposal.JAVADOC_METHOD_REF:
			return createJavadocMethodProposalLabel(proposal);
		case CompletionProposal.PACKAGE_REF:
			return createPackageProposalLabel(proposal);
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
		case CompletionProposal.FIELD_REF:
		case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
			return createLabelWithTypeAndDeclaration(proposal);
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
			return createSimpleLabelWithType(proposal);
		case CompletionProposal.KEYWORD:
		case CompletionProposal.LABEL_REF:
			return createSimpleLabel(proposal);
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 14
Source File: CompletionProposalRequestor.java    From java-debug with Eclipse Public License 1.0 4 votes vote down vote up
private CompletionItemKind mapKind(final int kind) {
    // When a new CompletionItemKind is added, don't forget to update
    // SUPPORTED_KINDS
    switch (kind) {
        case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
        case CompletionProposal.CONSTRUCTOR_INVOCATION:
            return CompletionItemKind.Constructor;
        case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
        case CompletionProposal.TYPE_REF:
            return CompletionItemKind.Class;
        case CompletionProposal.FIELD_IMPORT:
        case CompletionProposal.METHOD_IMPORT:
        case CompletionProposal.METHOD_NAME_REFERENCE:
        case CompletionProposal.PACKAGE_REF:
        case CompletionProposal.TYPE_IMPORT:
            return CompletionItemKind.Module;
        case CompletionProposal.FIELD_REF:
        case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
            return CompletionItemKind.Field;
        case CompletionProposal.KEYWORD:
            return CompletionItemKind.Keyword;
        case CompletionProposal.LABEL_REF:
            return CompletionItemKind.Reference;
        case CompletionProposal.LOCAL_VARIABLE_REF:
        case CompletionProposal.VARIABLE_DECLARATION:
            return CompletionItemKind.Variable;
        case CompletionProposal.METHOD_DECLARATION:
        case CompletionProposal.METHOD_REF:
        case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
        case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
            return CompletionItemKind.Function;
        // text
        case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
        case CompletionProposal.JAVADOC_BLOCK_TAG:
        case CompletionProposal.JAVADOC_FIELD_REF:
        case CompletionProposal.JAVADOC_INLINE_TAG:
        case CompletionProposal.JAVADOC_METHOD_REF:
        case CompletionProposal.JAVADOC_PARAM_REF:
        case CompletionProposal.JAVADOC_TYPE_REF:
        case CompletionProposal.JAVADOC_VALUE_REF:
        default:
            return CompletionItemKind.Text;
    }
}
 
Example 15
Source File: ExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 4 votes vote down vote up
public ExtensionMethodCompletionProposal(final int replacementOffset) {
	super(CompletionProposal.METHOD_REF, replacementOffset - 1);
}
 
Example 16
Source File: SignatureHelpRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isIgnored(int completionProposalKind) {
	return completionProposalKind != CompletionProposal.METHOD_REF;
}
 
Example 17
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private StringBuilder appendImportProposal(StringBuilder buffer, CompletionProposal proposal, int coreKind) {
	int proposalKind= proposal.getKind();
	String qualifiedTypeName= null;
	char[] qualifiedType= null;
	if (proposalKind == CompletionProposal.TYPE_IMPORT) {
		qualifiedType= proposal.getSignature();
		qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
	} else if (proposalKind == CompletionProposal.METHOD_IMPORT || proposalKind == CompletionProposal.FIELD_IMPORT) {
		qualifiedType= Signature.getTypeErasure(proposal.getDeclarationSignature());
		qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
	} else {
		/*
		 * In 3.3 we only support the above import proposals, see
		 * CompletionProposal#getRequiredProposals()
		 */
		Assert.isTrue(false);
	}

	/* Add imports if the preference is on. */
	if (importRewrite != null) {
		if (proposalKind == CompletionProposal.TYPE_IMPORT) {
			String simpleType= importRewrite.addImport(qualifiedTypeName, null);
			if (coreKind == CompletionProposal.METHOD_REF) {
				buffer.append(simpleType);
				buffer.append(COMMA);
				return buffer;
			}
		} else {
			String res= importRewrite.addStaticImport(qualifiedTypeName, String.valueOf(proposal.getName()), proposalKind == CompletionProposal.FIELD_IMPORT, null);
			int dot= res.lastIndexOf('.');
			if (dot != -1) {
				buffer.append(importRewrite.addImport(res.substring(0, dot), null));
				buffer.append('.');
				return buffer;
			}
		}
		return buffer;
	}

	// Case where we don't have an import rewrite (see allowAddingImports)

	if (compilationUnit != null && isImplicitImport(Signature.getQualifier(qualifiedTypeName), compilationUnit)) {
		/* No imports for implicit imports. */

		if (proposal.getKind() == CompletionProposal.TYPE_IMPORT && coreKind == CompletionProposal.FIELD_REF) {
			return buffer;
		}
		qualifiedTypeName= String.valueOf(Signature.getSignatureSimpleName(qualifiedType));
	}
	buffer.append(qualifiedTypeName);
	buffer.append('.');
	return buffer;
}
 
Example 18
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 19
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates and returns a decorated image descriptor for a completion proposal.
 *
 * @param proposal the proposal for which to create an image descriptor
 * @return the created image descriptor, or <code>null</code> if no image is available
 */
public ImageDescriptor createImageDescriptor(CompletionProposal proposal) {
	final int flags= proposal.getFlags();

	ImageDescriptor descriptor;
	switch (proposal.getKind()) {
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, flags);
			break;
		case CompletionProposal.TYPE_REF:
			switch (Signature.getTypeSignatureKind(proposal.getSignature())) {
				case Signature.CLASS_TYPE_SIGNATURE:
					descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, flags, false);
					break;
				case Signature.TYPE_VARIABLE_SIGNATURE:
					descriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
					break;
				default:
					descriptor= null;
			}
			break;
		case CompletionProposal.FIELD_REF:
		case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
			descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, flags);
			break;
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
			descriptor= JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;
			break;
		case CompletionProposal.PACKAGE_REF:
			descriptor= JavaPluginImages.DESC_OBJS_PACKAGE;
			break;
		case CompletionProposal.KEYWORD:
		case CompletionProposal.LABEL_REF:
			descriptor= null;
			break;
		case CompletionProposal.JAVADOC_METHOD_REF:
		case CompletionProposal.JAVADOC_TYPE_REF:
		case CompletionProposal.JAVADOC_FIELD_REF:
		case CompletionProposal.JAVADOC_VALUE_REF:
		case CompletionProposal.JAVADOC_BLOCK_TAG:
		case CompletionProposal.JAVADOC_INLINE_TAG:
		case CompletionProposal.JAVADOC_PARAM_REF:
			descriptor = JavaPluginImages.DESC_OBJS_JAVADOCTAG;
			break;
		default:
			descriptor= null;
			Assert.isTrue(false);
	}

	if (descriptor == null)
		return null;
	return decorateImageDescriptor(descriptor, proposal);
}
 
Example 20
Source File: ImportCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes the replacement string.
 *
 * @return the replacement string
 */
private String computeReplacementString() {
	int proposalKind= fProposal.getKind();
	String qualifiedTypeName= null;
	char[] qualifiedType= null;
		if (proposalKind == CompletionProposal.TYPE_IMPORT) {
			qualifiedType= fProposal.getSignature();
	 		qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
		} else if (proposalKind == CompletionProposal.METHOD_IMPORT || proposalKind == CompletionProposal.FIELD_IMPORT) {
           qualifiedType= Signature.getTypeErasure(fProposal.getDeclarationSignature());
           qualifiedTypeName= String.valueOf(Signature.toCharArray(qualifiedType));
	} else {
		/*
		 * In 3.3 we only support the above import proposals, see
		 * CompletionProposal#getRequiredProposals()
		 */
		 Assert.isTrue(false);
	}

		/* Add imports if the preference is on. */
		fImportRewrite= createImportRewrite();
		if (fImportRewrite != null) {
 		if (proposalKind == CompletionProposal.TYPE_IMPORT) {
 			String simpleType= fImportRewrite.addImport(qualifiedTypeName, fImportContext);
	 		if (fParentProposalKind == CompletionProposal.METHOD_REF)
	 			return simpleType + "."; //$NON-NLS-1$
			} else {
			String res= fImportRewrite.addStaticImport(qualifiedTypeName, String.valueOf(fProposal.getName()), proposalKind == CompletionProposal.FIELD_IMPORT, fImportContext);
			int dot= res.lastIndexOf('.');
			if (dot != -1) {
				String typeName= fImportRewrite.addImport(res.substring(0, dot), fImportContext);
				return typeName + '.';
			}
		}
 		return ""; //$NON-NLS-1$
 	}

	// Case where we don't have an import rewrite (see allowAddingImports)

	if (fCompilationUnit != null && JavaModelUtil.isImplicitImport(Signature.getQualifier(qualifiedTypeName), fCompilationUnit)) {
		/* No imports for implicit imports. */

		if (fProposal.getKind() == CompletionProposal.TYPE_IMPORT && fParentProposalKind == CompletionProposal.FIELD_REF)
			return ""; //$NON-NLS-1$
		qualifiedTypeName= String.valueOf(Signature.getSignatureSimpleName(qualifiedType));
	}

	return qualifiedTypeName + "."; //$NON-NLS-1$
}