org.eclipse.jdt.ui.text.java.CompletionProposalCollector Java Examples

The following examples show how to use org.eclipse.jdt.ui.text.java.CompletionProposalCollector. 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: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static IJavaCompletionProposal[] getJavaCompletionProposals(IJavaCompletionProposal[] javaCompletionProposals,
		CompletionProposalCollector completionProposalCollector) {
	
	List<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>(Arrays.asList(javaCompletionProposals));
	if (canExtendCodeAssist(proposals)) {
		IJavaCompletionProposal firstProposal = proposals.get(0);
		int replacementOffset = getReplacementOffset(firstProposal);
		for (Extension extension : getExtensionMethods(completionProposalCollector)) {
			for (MethodBinding method : extension.extensionMethods) {
				ExtensionMethodCompletionProposal newProposal = new ExtensionMethodCompletionProposal(replacementOffset);
				copyNameLookupAndCompletionEngine(completionProposalCollector, firstProposal, newProposal);
				ASTNode node = getAssistNode(completionProposalCollector);
				newProposal.setMethodBinding(method, node);
				createAndAddJavaCompletionProposal(completionProposalCollector, newProposal, proposals);
			}
		}
	}
	return proposals.toArray(new IJavaCompletionProposal[proposals.size()]);
}
 
Example #2
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 6 votes vote down vote up
static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) {
		TypeBinding firstParameterType = null;
		ASTNode node = getAssistNode(completionProposalCollector);
		if (node == null) return null;
		if (!(node instanceof CompletionOnQualifiedNameReference) && !(node instanceof CompletionOnSingleNameReference) && !(node instanceof CompletionOnMemberAccess)) return null;
		
		// Never offer on 'super.<autocomplete>'.
		if (node instanceof FieldReference && ((FieldReference)node).receiver instanceof SuperReference) return null;
		
		if (node instanceof NameReference) {
			Binding binding = ((NameReference) node).binding;
			// Unremark next block to allow a 'blank' autocomplete to list any extensions that apply to the current scope, but make sure we're not in a static context first, which this doesn't do.
			// Lacking good use cases, and having this particular concept be a little tricky on javac, means for now we don't support extension methods like this. this.X() will be fine, though.
			
/*			if ((node instanceof SingleNameReference) && (((SingleNameReference) node).token.length == 0)) {
				firstParameterType = decl.binding;
			} else */if (binding instanceof VariableBinding) {
				firstParameterType = ((VariableBinding) binding).type;
			}
		} else if (node instanceof FieldReference) {
			firstParameterType = ((FieldReference) node).actualReceiverType;
		}
		return firstParameterType;
	}
 
Example #3
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 6 votes vote down vote up
private static ClassScope getClassScope(CompletionProposalCollector completionProposalCollector) {
	ClassScope scope = null;
	try {
		InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector);
		InternalExtendedCompletionContext extendedContext = (InternalExtendedCompletionContext) Reflection.extendedContextField.get(context);
		if (extendedContext != null) {
			Scope assistScope = ((Scope) Reflection.assistScopeField.get(extendedContext));
			if (assistScope != null) {
				scope = assistScope.classScope();
			}
		}
	} catch (IllegalAccessException ignore) {
		// ignore
	}
	return scope;
}
 
Example #4
Source File: JavaZipModule.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets tuples with the java element and the corresponding completion proposal for that element.
 *
 * @param contents the contents that should be set for doing the code-completion
 * @param completionOffset the offset where the code completion should be requested
 * @param filterCompletionName if specified, only return matches from elements that have the name passed (otherwise it should be null)
 * @return a list of tuples corresponding to the element and the proposal for the gotten elements
 * @throws JavaModelException
 */
@Override
protected List<Tuple<IJavaElement, CompletionProposal>> getJavaCompletionProposals(String contents,
        int completionOffset, final String filterCompletionName) throws JavaModelException {

    final List<Tuple<IJavaElement, CompletionProposal>> ret = new ArrayList<Tuple<IJavaElement, CompletionProposal>>();

    IClasspathEntry entries[] = getClasspathEntries();
    //Using old version for compatibility with eclipse 3.2
    ICompilationUnit unit = new WorkingCopyOwner() {
    }.newWorkingCopy(name, entries, null, new NullProgressMonitor());
    unit.getBuffer().setContents(contents);
    CompletionProposalCollector collector = createCollector(filterCompletionName, ret, unit);

    unit.codeComplete(completionOffset, collector); //fill the completions while searching it
    return ret;
}
 
Example #5
Source File: JavaAllCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	CompletionProposalCollector collector= super.createCollector(context);
	collector.setIgnored(CompletionProposal.ANNOTATION_ATTRIBUTE_REF, false);
	collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, false);
	collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, false);
	collector.setIgnored(CompletionProposal.FIELD_REF, false);
	collector.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, false);
	collector.setIgnored(CompletionProposal.KEYWORD, false);
	collector.setIgnored(CompletionProposal.LABEL_REF, false);
	collector.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, false);
	collector.setIgnored(CompletionProposal.METHOD_DECLARATION, false);
	collector.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, false);
	collector.setIgnored(CompletionProposal.METHOD_REF, false);
	collector.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, false);
	collector.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, false);
	collector.setIgnored(CompletionProposal.PACKAGE_REF, false);
	collector.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, false);
	collector.setIgnored(CompletionProposal.VARIABLE_DECLARATION, false);
	collector.setIgnored(CompletionProposal.TYPE_REF, false);
	return collector;
}
 
Example #6
Source File: JsniCompletionProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Collects the proposals generated at the end of the specified snippet of
 * Java code, scoped to the specific type.
 */
private IJavaCompletionProposal[] codeComplete(String qualifiedTypeName,
    String snippet, CompletionProposalCollector requestor)
    throws JavaModelException {
  IJavaCompletionProposal[] proposals = new IJavaCompletionProposal[0];

  IType type = cu.getJavaProject().findType(qualifiedTypeName);
  if (type != null) {
    // This can always be false, since the set of available completions
    // (static vs. instance) depends on the JSNI ref itself, not the modifiers
    // on the method in which it is defined.
    boolean isStatic = false;

    // Have the JDT generate completions in the context of the type, but at
    // an unspecified location (source offset -1), since the real position
    // is inside a Java comment block, which is not allowed by codeComplete.
    type.codeComplete(snippet.toCharArray(), -1, snippet.length(),
        new char[0][0], new char[0][0], new int[0], isStatic, requestor);
    proposals = requestor.getJavaCompletionProposals();
  }

  return proposals;
}
 
Example #7
Source File: JavaNoTypeCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	CompletionProposalCollector collector= super.createCollector(context);
	collector.setIgnored(CompletionProposal.ANNOTATION_ATTRIBUTE_REF, false);
	collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, false);
	collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, false);
	collector.setIgnored(CompletionProposal.FIELD_REF, false);
	collector.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, false);
	collector.setIgnored(CompletionProposal.KEYWORD, false);
	collector.setIgnored(CompletionProposal.LABEL_REF, false);
	collector.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, false);
	collector.setIgnored(CompletionProposal.METHOD_DECLARATION, false);
	collector.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, false);
	collector.setIgnored(CompletionProposal.METHOD_REF, false);
	collector.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, false);
	collector.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, false);
	collector.setIgnored(CompletionProposal.PACKAGE_REF, false);
	collector.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, false);
	collector.setIgnored(CompletionProposal.VARIABLE_DECLARATION, false);
	return collector;
}
 
Example #8
Source File: JsniCompletionProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private IJavaCompletionProposal[] computePackageAndTypeProposals(String js,
    int lineStartOffset, int cursorOffset) throws JavaModelException {
  Matcher matcher = PACKAGE_OR_TYPE_REF_START.matcher(js);
  if (!matcher.find()) {
    // Bail if we're not inside a JSNI Java package/type reference
    return null;
  }

  // Extract from the match the (maybe partial) package/type reference
  int refOffset = matcher.start(1) + lineStartOffset;
  int refLength = cursorOffset - refOffset;
  String partialRef = matcher.group(1);

  CompletionProposalCollector requestor = JsniCompletionProposalCollector.createPackageAndTypeProposalCollector(
      cu, refOffset, refLength);
  IEvaluationContext evalContext = createEvaluationContext();
  evalContext.codeComplete(partialRef, partialRef.length(), requestor);
  return requestor.getJavaCompletionProposals();
}
 
Example #9
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static List<Extension> getExtensionMethods(CompletionProposalCollector completionProposalCollector) {
	List<Extension> extensions = new ArrayList<Extension>();
	ClassScope classScope = getClassScope(completionProposalCollector);
	if (classScope != null) {
		TypeDeclaration decl = classScope.referenceContext;
		TypeBinding firstParameterType = getFirstParameterType(decl, completionProposalCollector);
		for (EclipseNode typeNode = getTypeNode(decl); typeNode != null; typeNode = upToType(typeNode)) {
			Annotation ann = getAnnotation(ExtensionMethod.class, typeNode);
			extensions.addAll(0, getApplicableExtensionMethods(typeNode, ann, firstParameterType));
		}
	}
	return extensions;
}
 
Example #10
Source File: AbstractJavaClassModule.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a proposal collector that's able to gather the passed completions/related java elements and adds
 * them to the passed 'ret' parameter
 *
 * @param filterCompletionName may be null or a name to which we want to match the java element name (so, only
 * a java element with an exact match of its name to filterCompletionName is added).
 *
 * @param ret the placeholder for the found java elements and completion proposals
 * @param unit the ICompilationUnit that's used (must be passed in the CompletionProposalCollector constructor).
 * @return the collector that will gather the completions (note that it'll keep the 'ret' placeholder alive unti it's
 * garbage-collected.
 */
protected CompletionProposalCollector createCollector(final String filterCompletionName,
        final List<Tuple<IJavaElement, CompletionProposal>> ret, ICompilationUnit unit) {
    CompletionProposalCollector collector = new CompletionProposalCollector(unit) {

        /**
         * Override the java proposal creation to always return null, as we'll keep just what we actually need.
         */
        @SuppressWarnings("restriction")
        @Override
        public IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
            IJavaCompletionProposal javaCompletionProposal = super.createJavaCompletionProposal(proposal);
            if (javaCompletionProposal instanceof AbstractJavaCompletionProposal) {
                AbstractJavaCompletionProposal prop = (AbstractJavaCompletionProposal) javaCompletionProposal;
                IJavaElement javaElement = prop.getJavaElement();
                if (javaElement != null) {

                    if (filterCompletionName == null) {
                        ret.add(new Tuple<IJavaElement, CompletionProposal>(javaElement, proposal));
                        return null;
                    }

                    if (javaElement.getElementName().equals(filterCompletionName)) {
                        ret.add(new Tuple<IJavaElement, CompletionProposal>(javaElement, proposal));
                        return null;
                    }

                }
            }
            return null;
        }
    };
    return collector;
}
 
Example #11
Source File: JavaModuleInProject.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tries to get completions for a given element.
 */
private void getCompletionsForType(String contents, String filterCompletionName, IType type,
        final List<Tuple<IJavaElement, CompletionProposal>> ret) throws JavaModelException {
    ICompilationUnit unit = type.getCompilationUnit();
    if (unit == null) {
        return;
    }
    CompletionProposalCollector collector = createCollector(filterCompletionName, ret, unit);
    type.codeComplete(StringUtils.format(contents, name).toCharArray(), -1, 0, new char[0][0], new char[0][0],
            new int[0], false, collector);
}
 
Example #12
Source File: JavadocCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	CompletionProposalCollector collector= super.createCollector(context);
	collector.setIgnored(CompletionProposal.JAVADOC_TYPE_REF, false);
	collector.setIgnored(CompletionProposal.JAVADOC_FIELD_REF, false);
	collector.setIgnored(CompletionProposal.JAVADOC_METHOD_REF, false);
	collector.setIgnored(CompletionProposal.JAVADOC_PARAM_REF, false);
	collector.setIgnored(CompletionProposal.JAVADOC_VALUE_REF, false);
	collector.setIgnored(CompletionProposal.JAVADOC_BLOCK_TAG, false);
	collector.setIgnored(CompletionProposal.JAVADOC_INLINE_TAG, false);
	collector.setIgnored(CompletionProposal.TYPE_REF, false);
	collector.setIgnored(CompletionProposal.FIELD_REF, false);
	collector.setIgnored(CompletionProposal.METHOD_REF, false);
	return collector;
}
 
Example #13
Source File: JavaCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the collector used to get proposals from core.
 *
 * @param context the context
 * @return the collector
 */
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES))
		return new FillArgumentNamesCompletionProposalCollector(context);
	else
		return new CompletionProposalCollector(context.getCompilationUnit(), true);
}
 
Example #14
Source File: JsniCompletionProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private IJavaCompletionProposal[] computeMethodAndFieldProposals(
    String qualifiedTypeName, String memberNameFragment, boolean isStatic,
    CompletionProposalCollector requestor) throws JavaModelException {
  String javaMethodOrFieldFragment = getJavaMethodOrFieldRefFragment(
      qualifiedTypeName, memberNameFragment, isStatic);

  // Compute the proposals as if we were inside the type we're referencing,
  // since JSNI allows access to even private Java members (violator pattern).
  return codeComplete(qualifiedTypeName, javaMethodOrFieldFragment, requestor);
}
 
Example #15
Source File: JsniCompletionProposalCollector.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public static CompletionProposalCollector createPackageAndTypeProposalCollector(
    ICompilationUnit cu, int refOffset, int refLength) {
  CompletionProposalCollector collector = new JsniCompletionProposalCollector(
      cu, refOffset, refLength);
  collector.setIgnored(CompletionProposal.METHOD_REF, true);
  collector.setIgnored(CompletionProposal.FIELD_REF, true);
  return collector;
}
 
Example #16
Source File: JsniCompletionProposalCollector.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public static CompletionProposalCollector createMemberProposalCollector(
    ICompilationUnit cu, int refOffset, int refLength,
    String refQualifiedTypeName) {
  JsniCompletionProposalCollector collector = new JsniCompletionProposalCollector(
      cu, refOffset, refLength);
  collector.setIgnored(CompletionProposal.PACKAGE_REF, true);
  collector.setIgnored(CompletionProposal.TYPE_REF, true);
  collector.setQualifiedTypeName(refQualifiedTypeName);

  return collector;
}
 
Example #17
Source File: JavaContentAssistUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Generates an {@link IJavaCompletionProposal} from Java's
 * {@link CompletionProposal}.
 * 
 * @param completionProposal the {@link CompletionProposal}
 * @param completionContext the context of the {@link CompletionProposal}
 * @param javaProject the java project for the given completion proposal
 * @return a {@link IJavaCompletionProposal}, or null
 */
public static IJavaCompletionProposal getJavaCompletionProposal(
    CompletionProposal completionProposal,
    CompletionContext completionContext, IJavaProject javaProject) {
  CompletionProposalCollector collector = new CompletionProposalCollector(
      javaProject);
  collector.acceptContext(completionContext);
  collector.accept(completionProposal);
  IJavaCompletionProposal[] javaCompletionProposals = collector.getJavaCompletionProposals();
  return javaCompletionProposals.length > 0 ? javaCompletionProposals[0]
      : null;
}
 
Example #18
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static void createAndAddJavaCompletionProposal(CompletionProposalCollector completionProposalCollector, CompletionProposal newProposal,
		List<IJavaCompletionProposal> proposals) {
	
	try {
		proposals.add((IJavaCompletionProposal) Reflection.createJavaCompletionProposalMethod.invoke(completionProposalCollector, newProposal));
	} catch (Exception ignore) {
		// ignore
	}
}
 
Example #19
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static void copyNameLookupAndCompletionEngine(CompletionProposalCollector completionProposalCollector, IJavaCompletionProposal proposal,
		InternalCompletionProposal newProposal) {
	
	try {
		InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector);
		InternalExtendedCompletionContext extendedContext = (InternalExtendedCompletionContext) Reflection.extendedContextField.get(context);
		LookupEnvironment lookupEnvironment = (LookupEnvironment) Reflection.lookupEnvironmentField.get(extendedContext);
		Reflection.nameLookupField.set(newProposal, ((SearchableEnvironment) lookupEnvironment.nameEnvironment).nameLookup);
		Reflection.completionEngineField.set(newProposal, lookupEnvironment.typeRequestor);
	} catch (IllegalAccessException ignore) {
		// ignore
	}
}
 
Example #20
Source File: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static ASTNode getAssistNode(CompletionProposalCollector completionProposalCollector) {
	try {
		InternalCompletionContext context = (InternalCompletionContext) Reflection.contextField.get(completionProposalCollector);
		InternalExtendedCompletionContext extendedContext = (InternalExtendedCompletionContext) Reflection.extendedContextField.get(context);
		if (extendedContext == null) return null;
		return (ASTNode) Reflection.assistNodeField.get(extendedContext);
	} catch (Exception ignore) {
		return null;
	}
}
 
Example #21
Source File: JsniCompletionProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private IJavaCompletionProposal[] computeCtorProposals(
    String qualifiedTypeName, CompletionProposalCollector requestor)
    throws JavaModelException {
  String javaCtorFragment = getJavaCtorRefFragment(qualifiedTypeName);
  return codeComplete(qualifiedTypeName, javaCtorFragment, requestor);
}
 
Example #22
Source File: JavaTypeCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	CompletionProposalCollector collector= super.createCollector(context);
	collector.setIgnored(CompletionProposal.TYPE_REF, false);
	return collector;
}