Search in sources :

Example 1 with JavadocCompletionProposal

use of org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal in project eclipse.jdt.ls by eclipse.

the class CompletionHandler method computeContentAssist.

private CompletionList computeContentAssist(ICompilationUnit unit, CompletionParams params, IProgressMonitor monitor) throws JavaModelException {
    CompletionResponses.clear();
    if (unit == null) {
        return null;
    }
    boolean completionForConstructor = false;
    if (params.getContext() != null && " ".equals(params.getContext().getTriggerCharacter())) {
        completionForConstructor = isCompletionForConstructor(params, unit, monitor);
        if (!completionForConstructor) {
            return null;
        }
    }
    List<CompletionItem> proposals = new ArrayList<>();
    final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), params.getPosition().getLine(), params.getPosition().getCharacter());
    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() || completionForConstructor);
    return list;
}
Also used : ProgressMonitorWrapper(org.eclipse.core.runtime.ProgressMonitorWrapper) CompletionProposalRequestor(org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor) CompletionList(org.eclipse.lsp4j.CompletionList) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) IBuffer(org.eclipse.jdt.core.IBuffer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JavadocCompletionProposal(org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Aggregations

ArrayList (java.util.ArrayList)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 ProgressMonitorWrapper (org.eclipse.core.runtime.ProgressMonitorWrapper)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 CompletionProposalRequestor (org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor)1 JavadocCompletionProposal (org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal)1 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 CompletionList (org.eclipse.lsp4j.CompletionList)1