org.eclipse.jdt.core.CompletionContext Java Examples

The following examples show how to use org.eclipse.jdt.core.CompletionContext. 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: TemplateCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	try {
		String partition= TextUtilities.getContentType(context.getDocument(), IJavaPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true);
		if (partition.equals(IJavaPartitions.JAVA_DOC))
			return fJavadocTemplateEngine;
		else {
			CompletionContext coreContext= context.getCoreContext();
			if (coreContext != null) {
				int tokenLocation= coreContext.getTokenLocation();
				if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
					return fJavaMembersTemplateEngine;
				}
				if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
					return fJavaStatementsTemplateEngine;
				}
			}
			return fJavaTemplateEngine;
		}
	} catch (BadLocationException x) {
		return null;
	}
}
 
Example #2
Source File: SWTTemplateCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	ICompilationUnit unit= context.getCompilationUnit();
	if (unit == null)
		return null;

	IJavaProject javaProject= unit.getJavaProject();
	if (javaProject == null)
		return null;

	if (isSWTOnClasspath(javaProject)) {
		CompletionContext coreContext= context.getCoreContext();
		if (coreContext != null) {
			int tokenLocation= coreContext.getTokenLocation();
			if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
				return fSWTMembersTemplateEngine;
			}
			if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
				return fSWTStatementsTemplateEngine;
			}
		}
		return fSWTTemplateEngine;
	}

	return null;
}
 
Example #3
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean accept(ICompilationUnit cu, CompletionContext completionContext, boolean acceptClass) {
	if (completionContext != null && completionContext.isExtended()) {
		if (completionContext.isInJavadoc()) {
			return false;
		}
		if (completionContext instanceof InternalCompletionContext) {
			InternalCompletionContext internalCompletionContext = (InternalCompletionContext) completionContext;
			ASTNode node = internalCompletionContext.getCompletionNode();
			if (node instanceof CompletionOnKeyword2) {
				return true;
			}
			if (node instanceof CompletionOnFieldType) {
				return true;
			}
			if (acceptClass && node instanceof CompletionOnSingleNameReference) {
				if (completionContext.getEnclosingElement() instanceof IMethod) {
					CompilationUnit ast = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
					org.eclipse.jdt.core.dom.ASTNode astNode = ASTNodeSearchUtil.getAstNode(ast, completionContext.getTokenStart(), completionContext.getTokenEnd() - completionContext.getTokenStart() + 1);
					return (astNode == null || (astNode.getParent() instanceof ExpressionStatement));
				}
				return true;
			}
		}
	}
	return false;
}
 
Example #4
Source File: JavaContentAssistInvocationContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the expected type if any, <code>null</code> otherwise.
 * <p>
 * <strong>Note:</strong> This method may run
 * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
 * on the compilation unit.
 * </p>
 *
 * @return the expected type if any, <code>null</code> otherwise
 */
public IType getExpectedType() {
	if (fType == null && getCompilationUnit() != null) {
		CompletionContext context= getCoreContext();
		if (context != null) {
			char[][] expectedTypes= context.getExpectedTypesSignatures();
			if (expectedTypes != null && expectedTypes.length > 0) {
				IJavaProject project= getCompilationUnit().getJavaProject();
				if (project != null) {
					try {
						fType= project.findType(SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0])));
					} catch (JavaModelException x) {
						JavaPlugin.log(x);
					}
				}
			}
		}
	}
	return fType;
}
 
Example #5
Source File: JavaContentAssistInvocationContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the content assist type history for the expected type.
 *
 * @return the content assist type history for the expected type
 */
private RHSHistory getRHSHistory() {
	if (fRHSHistory == null) {
		CompletionContext context= getCoreContext();
		if (context != null) {
			char[][] expectedTypes= context.getExpectedTypesSignatures();
			if (expectedTypes != null && expectedTypes.length > 0) {
				String expected= SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0]));
				fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(expected);
			}
		}
		if (fRHSHistory == null)
			fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(null);
	}
	return fRHSHistory;
}
 
Example #6
Source File: JavaContentAssistInvocationContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the {@link CompletionContext core completion context} if available, <code>null</code>
 * otherwise.
 * <p>
 * <strong>Note:</strong> This method may run
 * {@linkplain ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor) codeComplete}
 * on the compilation unit.
 * </p>
 *
 * @return the core completion context if available, <code>null</code> otherwise
 */
public CompletionContext getCoreContext() {
	if (fCollector != null) {
		CompletionContext context= fCollector.getContext();
		if (context != null) {
			if (fCoreContext == null)
				fCoreContext= context;
			return context;
		}
	}

	if (fCoreContext == null)
		computeKeywordsAndContext(); // Retrieve the context ourselves

	return fCoreContext;
}
 
Example #7
Source File: JFaceCompletionProposalComputer.java    From saneclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	final ICompilationUnit unit = context.getCompilationUnit();
	if (unit == null) {
		return null;
	}

	final IJavaProject javaProject = unit.getJavaProject();
	if (javaProject == null) {
		return null;
	}

	if (isJFaceOnClasspath(javaProject)) {
		final CompletionContext coreContext = context.getCoreContext();
		if (coreContext != null) {
			final int tokenLocation = coreContext.getTokenLocation();
			if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
				return jFaceMembersTemplateEngine;
			}
			if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
				return jFaceStatementsTemplateEngine;
			}
		}
		return jFaceTemplateEngine;
	}

	return null;
}
 
Example #8
Source File: CompletionProposalRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptContext(CompletionContext context) {
	super.acceptContext(context);
	this.context = context;
	response.setContext(context);
	this.descriptionProvider = new CompletionProposalDescriptionProvider(context);
	this.proposalProvider = new CompletionProposalReplacementProvider(unit, context, response.getOffset(), preferenceManager.getPreferences(), preferenceManager.getClientPreferences());
}
 
Example #9
Source File: InternalCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean canUseDiamond(CompletionContext coreContext) {
	if (this.getKind() != CONSTRUCTOR_INVOCATION) return false;
	if (coreContext instanceof InternalCompletionContext) {
		InternalCompletionContext internalCompletionContext = (InternalCompletionContext) coreContext;
		if (internalCompletionContext.extendedContext == null) return false;
		char[] name1 = this.declarationPackageName;
		char[] name2 = this.declarationTypeName;
		char[] declarationType = CharOperation.concat(name1, name2, '.');  // fully qualified name
		// even if the type arguments used in the method have been substituted,
		// extract the original type arguments only, since thats what we want to compare with the class
		// type variables (Substitution might have happened when the constructor is coming from another
		// CU and not the current one).
		char[] sign = (this.originalSignature != null)? this.originalSignature : getSignature();
		if (!(sign == null || sign.length < 2)) {
			sign = Signature.removeCapture(sign);
		}
		char[][] types= Signature.getParameterTypes(sign);
		String[] paramTypeNames= new String[types.length];
		for (int i= 0; i < types.length; i++) {
			paramTypeNames[i]= new String(Signature.toCharArray(types[i]));
		}
		return internalCompletionContext.extendedContext.canUseDiamond(paramTypeNames,declarationType);
	}
	else {
		return false;
	}
}
 
Example #10
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static List<CompletionItem> getSnippets(ICompilationUnit cu, CompletionContext completionContext, IProgressMonitor monitor) throws JavaModelException {
	if (cu == null) {
		throw new IllegalArgumentException("Compilation unit must not be null"); //$NON-NLS-1$
	}

	List<CompletionItem> res = new ArrayList<>();
	SnippetCompletionContext scc = new SnippetCompletionContext(cu, completionContext);
	res.addAll(getGenericSnippets(scc));
	res.addAll(getTypeDefinitionSnippets(scc, monitor));

	return res;
}
 
Example #11
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public CompletionProposalReplacementProvider(ICompilationUnit compilationUnit, CompletionContext context, int offset, Preferences preferences, ClientPreferences clientPrefs) {
	super();
	this.compilationUnit = compilationUnit;
	this.context = context;
	this.offset = offset;
	this.preferences = preferences == null ? new Preferences() : preferences;
	this.client = clientPrefs;
}
 
Example #12
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 #13
Source File: CompletionProposalRequestor.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void acceptContext(CompletionContext context) {
    super.acceptContext(context);
    this.context = context;
    response.setContext(context);
    this.descriptionProvider = new CompletionProposalDescriptionProvider(context);
}
 
Example #14
Source File: ProposalGeneratingCompletionRequestor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void acceptContext(CompletionContext context) {
  this.context = context;
}
 
Example #15
Source File: PostfixCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	ICompilationUnit unit = context.getCompilationUnit();
	if (unit == null)
		return null;

	IJavaProject javaProject = unit.getJavaProject();
	if (javaProject == null)
		return null;

	CompletionContext coreContext = context.getCoreContext();
	if (coreContext != null) {
		int tokenLocation= coreContext.getTokenLocation();
		int tokenStart= coreContext.getTokenStart();
		int tokenKind= coreContext.getTokenKind();
		
		/*
		// XXX print out tokenlocation stuff (debugging)
		System.out.println("All Tokens: " + CompletionContext.TL_CONSTRUCTOR_START + " " + CompletionContext.TL_MEMBER_START + " " + CompletionContext.TL_STATEMENT_START);
		System.out.println("Token Start: " + coreContext.getTokenStart());
		System.out.println("Token End: " + coreContext.getTokenEnd());
		System.out.println("Token Kind: " + coreContext.getTokenKind());
		System.out.println("Token Location: " + coreContext.getTokenLocation());
		System.out.println("Enclosing Element: " + coreContext.getEnclosingElement());
		System.out.println("Offset: " + coreContext.getOffset());
		System.out.println("Token Array: " + Arrays.toString(coreContext.getToken()));
		System.out.println("Kind Tokens: " + CompletionContext.TOKEN_KIND_NAME + ", " + CompletionContext.TOKEN_KIND_STRING_LITERAL + ", " + CompletionContext.TOKEN_KIND_UNKNOWN);
		*/
		if (context.getViewer().getSelectedRange().y > 0) { // If there is an active selection we do not want to contribute to the CA
			return null;
		}
		
		if ((tokenLocation == 0 && tokenStart > -1)
				|| ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0 && tokenKind == CompletionContext.TOKEN_KIND_NAME && tokenStart > -1)
				|| (tokenLocation == 0 && isAfterDot(context.getDocument(), context.getInvocationOffset()))) {
			
			analyzeCoreContext(context, coreContext);

			return postfixCompletionTemplateEngine;
		}
	}
	return null;
}
 
Example #16
Source File: ParameterGuessingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public ParameterGuessingProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context, CompletionContext coreContext, boolean fillBestGuess) {
	super(proposal, context);
fCoreContext= coreContext;
fFillBestGuess= fillBestGuess;
}
 
Example #17
Source File: ProposalGeneratingCompletionRequestor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public CompletionContext getContext() {
  return context;
}
 
Example #18
Source File: CompletionResponse.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param context the context to set
 */
public void setContext(CompletionContext context) {
	this.context = context;
}
 
Example #19
Source File: CompletionResponse.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @return the context
 */
public CompletionContext getContext() {
	return context;
}
 
Example #20
Source File: SignatureHelpRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void acceptContext(CompletionContext context) {
	super.acceptContext(context);
	response.setContext(context);
	this.descriptionProvider = new CompletionProposalDescriptionProvider(context);
}
 
Example #21
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext scc) throws JavaModelException {
	List<CompletionItem> res = new ArrayList<>();
	CompletionContext completionContext = scc.getCompletionContext();
	char[] completionToken = completionContext.getToken();
	if (completionToken == null) {
		return Collections.emptyList();
	}
	int tokenLocation = completionContext.getTokenLocation();
	JavaContextType contextType = (JavaContextType) JavaLanguageServerPlugin.getInstance().getTemplateContextRegistry().getContextType(JavaContextType.ID_STATEMENTS);
	if (contextType == null) {
		return Collections.emptyList();
	}
	ICompilationUnit cu = scc.getCompilationUnit();
	IDocument document = new Document(cu.getSource());
	DocumentTemplateContext javaContext = contextType.createContext(document, completionContext.getOffset(), completionToken.length, cu);
	Template[] templates = null;
	if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
		templates = JavaLanguageServerPlugin.getInstance().getTemplateStore().getTemplates(JavaContextType.ID_STATEMENTS);
	} else {
		// We only support statement templates for now.
	}

	if (templates == null || templates.length == 0) {
		return Collections.emptyList();
	}

	for (Template template : templates) {
		if (!javaContext.canEvaluate(template)) {
			continue;
		}
		TemplateBuffer buffer = null;
		try {
			buffer = javaContext.evaluate(template);
		} catch (BadLocationException | TemplateException e) {
			JavaLanguageServerPlugin.logException(e.getMessage(), e);
			continue;
		}
		if (buffer == null) {
			continue;
		}
		String content = buffer.getString();
		if (Strings.containsOnlyWhitespaces(content)) {
			continue;
		}
		final CompletionItem item = new CompletionItem();
		item.setLabel(template.getName());
		item.setInsertText(content);
		item.setDetail(template.getDescription());
		setFields(item, cu);
		res.add(item);
	}

	return res;
}
 
Example #22
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
CompletionContext getCompletionContext() {
	return completionContext;
}
 
Example #23
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
SnippetCompletionContext(ICompilationUnit cu, CompletionContext completionContext) {
	this.cu = cu;
	this.completionContext = completionContext;
}
 
Example #24
Source File: CompletionProposalRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public CompletionContext getContext() {
	return context;
}
 
Example #25
Source File: CompletionProposalDescriptionProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Creates a new label provider.
 * @param iCompilationUnit
 */
public CompletionProposalDescriptionProvider(CompletionContext context) {
	super();
	fContext = context;
}
 
Example #26
Source File: CompletionProposalRequestor.java    From java-debug with Eclipse Public License 1.0 4 votes vote down vote up
public CompletionContext getContext() {
    return context;
}
 
Example #27
Source File: ParameterGuessingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Creates a {@link ParameterGuessingProposal} or <code>null</code> if the core context isn't available or extended.
 *
 * @param proposal the original completion proposal
 * @param context the currrent context
 * @param fillBestGuess if set, the best guess will be filled in
 *
 * @return a proposal or <code>null</code>
 */
public static ParameterGuessingProposal createProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context, boolean fillBestGuess) {
	CompletionContext coreContext= context.getCoreContext();
		if (coreContext != null && coreContext.isExtended()) {
		return new ParameterGuessingProposal(proposal, context, coreContext, fillBestGuess);
		}
		return null;
}
 
Example #28
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Sets the completion context.
 *
 * @param context the completion context
 * @since 3.2
 */
void setContext(CompletionContext context) {
	fContext= context;
}
 
Example #29
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Subclasses may extend, but usually should not need to.
 * </p>
 * @see #getContext()
 */
@Override
public void acceptContext(CompletionContext context) {
	fContext= context;
	fLabelProvider.setContext(context);
}
 
Example #30
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 votes vote down vote up
/**
 * Returns the <code>CompletionContext</code> for this completion operation.

 * @return the <code>CompletionContext</code> for this completion operation
 * @see CompletionRequestor#acceptContext(CompletionContext)
 */
protected final CompletionContext getContext() {
	return fContext;
}