Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#codeComplete()
The following examples show how to use
org.eclipse.jdt.core.ICompilationUnit#codeComplete() .
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: FullSourceWorkspaceCompletionTests.java From dacapobench with Apache License 2.0 | 6 votes |
private void complete(String projectName, String packageName, String unitName, String completeAt, String completeBehind, int[] ignoredKinds, int warmupCount, int iterationCount) throws CoreException { AbstractJavaModelTests.waitUntilIndexesReady(); TestCompletionRequestor requestor = new TestCompletionRequestor(); if (ignoredKinds != null) { for (int i = 0; i < ignoredKinds.length; i++) { requestor.setIgnored(ignoredKinds[i], true); } } ICompilationUnit unit = getCompilationUnit(projectName, packageName, unitName); String str = unit.getSource(); int completionIndex = str.indexOf(completeAt) + completeBehind.length(); if (DEBUG) System.out.print("Perform code assist inside " + unitName + "..."); for (int j = 0; j < iterationCount; j++) { unit.codeComplete(completionIndex, requestor); } if (DEBUG) System.out.println("done!"); }
Example 2
Source File: JavaContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private CompilationUnitCompletion getCompletion() { ICompilationUnit compilationUnit= getCompilationUnit(); if (fCompletion == null) { fCompletion= new CompilationUnitCompletion(compilationUnit); if (compilationUnit != null) { try { compilationUnit.codeComplete(getStart(), fCompletion); } catch (JavaModelException e) { // ignore } } } return fCompletion; }
Example 3
Source File: JavaZipModule.java From Pydev with Eclipse Public License 1.0 | 6 votes |
/** * 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 4
Source File: SimilarElementsRequestor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private SimilarElement[] process(ICompilationUnit cu, int pos) throws JavaModelException { try { cu.codeComplete(pos, this); processKeywords(); return fResult.toArray(new SimilarElement[fResult.size()]); } finally { fResult.clear(); } }
Example 5
Source File: JavaContentAssistInvocationContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Fallback to retrieve a core context and keyword proposals when no collector is available. * Runs code completion on the cu and collects keyword proposals. {@link #fKeywordProposals} is * non-<code>null</code> after this call. * * @since 3.3 */ private void computeKeywordsAndContext() { ICompilationUnit cu= getCompilationUnit(); if (cu == null) { if (fKeywordProposals == null) fKeywordProposals= new IJavaCompletionProposal[0]; return; } CompletionProposalCollector collector= new CompletionProposalCollector(cu, true); collector.setIgnored(CompletionProposal.KEYWORD, false); try { cu.codeComplete(getInvocationOffset(), collector); if (fCoreContext == null) fCoreContext= collector.getContext(); if (fKeywordProposals == null) fKeywordProposals= collector.getKeywordCompletionProposals(); if (fLabelProvider == null) fLabelProvider= collector.getLabelProvider(); } catch (JavaModelException x) { if (!x.isDoesNotExist() || cu.getJavaProject() == null || cu.getJavaProject().isOnClasspath(cu)) JavaPlugin.log(x); if (fKeywordProposals == null) fKeywordProposals= new IJavaCompletionProposal[0]; } }
Example 6
Source File: SimilarElementsRequestor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private SimilarElement[] process(ICompilationUnit cu, int pos) throws JavaModelException { try { cu.codeComplete(pos, this); processKeywords(); return fResult.toArray(new SimilarElement[fResult.size()]); } finally { fResult.clear(); } }
Example 7
Source File: SimilarElementsRequestor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
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 8
Source File: CompletionHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private CompletionList computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException { CompletionResponses.clear(); if (unit == null) { return null; } List<CompletionItem> proposals = new ArrayList<>(); final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset, manager); // Allow completions for unresolved types - since 3.3 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true); collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true); collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true); collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true); collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true); collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true); collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true); collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true); collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true); collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true); collector.setFavoriteReferences(getFavoriteStaticMembers()); if (offset >-1 && !monitor.isCanceled()) { IBuffer buffer = unit.getBuffer(); if (buffer != null && buffer.getLength() >= offset) { IProgressMonitor subMonitor = new ProgressMonitorWrapper(monitor) { private long timeLimit; private final long TIMEOUT = Long.getLong("completion.timeout", 5000); @Override public void beginTask(String name, int totalWork) { timeLimit = System.currentTimeMillis() + TIMEOUT; } @Override public boolean isCanceled() { return super.isCanceled() || timeLimit <= System.currentTimeMillis(); } }; try { if (isIndexEngineEnabled()) { unit.codeComplete(offset, collector, subMonitor); } else { ModelBasedCompletionEngine.codeComplete(unit, offset, collector, DefaultWorkingCopyOwner.PRIMARY, subMonitor); } proposals.addAll(collector.getCompletionItems()); if (isSnippetStringSupported() && !UNSUPPORTED_RESOURCES.contains(unit.getResource().getName())) { proposals.addAll(SnippetCompletionProposal.getSnippets(unit, collector.getContext(), subMonitor)); } proposals.addAll(new JavadocCompletionProposal().getProposals(unit, offset, collector, subMonitor)); } catch (OperationCanceledException e) { monitor.setCanceled(true); } } } proposals.sort(PROPOSAL_COMPARATOR); CompletionList list = new CompletionList(proposals); list.setIsIncomplete(!collector.isComplete()); return list; }
Example 9
Source File: SimilarElementsRequestor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
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(); } } }