Search in sources :

Example 1 with ProgressMonitorWrapper

use of org.eclipse.core.runtime.ProgressMonitorWrapper 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)

Example 2 with ProgressMonitorWrapper

use of org.eclipse.core.runtime.ProgressMonitorWrapper in project eclipse.jdt.ls by eclipse.

the class CompletionHandler method computeContentAssist.

private List<CompletionItem> computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException {
    CompletionResponses.clear();
    if (unit == null) {
        return Collections.emptyList();
    }
    List<CompletionItem> proposals = new ArrayList<>();
    final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
    CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset);
    // 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 static final long 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 {
                unit.codeComplete(offset, collector, subMonitor);
                proposals.addAll(collector.getCompletionItems());
            } catch (OperationCanceledException e) {
                monitor.setCanceled(true);
            }
        }
    }
    return proposals;
}
Also used : ProgressMonitorWrapper(org.eclipse.core.runtime.ProgressMonitorWrapper) CompletionProposalRequestor(org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CompletionItem(org.eclipse.lsp4j.CompletionItem) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) IBuffer(org.eclipse.jdt.core.IBuffer)

Example 3 with ProgressMonitorWrapper

use of org.eclipse.core.runtime.ProgressMonitorWrapper in project xtext-eclipse by eclipse.

the class XtextBuilder method build.

@SuppressWarnings("rawtypes")
@Override
protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
    ensureBuilderStateLoaded();
    boolean wasFullBuildRequested = wasFullBuildRequested();
    if (!wasFullBuildRequested && IBuildFlag.FORGET_BUILD_STATE_ONLY.isSet(args)) {
        forgetLastBuiltState();
        return getReferencedProjects();
    }
    int effectiveKind = wasFullBuildRequested ? FULL_BUILD : kind;
    long startTime = System.currentTimeMillis();
    StoppedTask task = Stopwatches.forTask(String.format("XtextBuilder.build[%s]", getKindAsString(effectiveKind)));
    try {
        queuedBuildData.createCheckpoint();
        if (shouldCancelBuild(effectiveKind)) {
            buildLogger.log("interrupted");
            throw new OperationCanceledException("Build has been interrupted");
        }
        task.start();
        if (monitor != null) {
            // $NON-NLS-1$
            final String taskName = Messages.XtextBuilder_Building + getProject().getName() + ": ";
            monitor = new ProgressMonitorWrapper(monitor) {

                @Override
                public void subTask(String name) {
                    super.subTask(taskName + name);
                }

                @Override
                public boolean isCanceled() {
                    boolean shouldCancelBuild = shouldCancelBuild(effectiveKind);
                    if (shouldCancelBuild)
                        buildLogger.log("interrupted");
                    return shouldCancelBuild || super.isCanceled();
                }
            };
        }
        SubMonitor progress = SubMonitor.convert(monitor, 1);
        if (effectiveKind == FULL_BUILD) {
            fullBuild(progress.split(1), !wasFullBuildRequested && IBuildFlag.RECOVERY_BUILD.isSet(args));
        } else {
            IResourceDelta delta = getDelta(getProject());
            if (delta == null || isOpened(delta)) {
                fullBuild(progress.split(1), IBuildFlag.RECOVERY_BUILD.isSet(args));
            } else {
                incrementalBuild(delta, progress.split(1));
            }
        }
        // If no exceptions have been thrown, fix the information about the requested full builds
        unsetWasFullBuildRequested(wasFullBuildRequested);
    } catch (CoreException e) {
        log.error(e.getMessage(), e);
        throw e;
    } catch (OperationCanceledException e) {
        handleCanceled(e);
    } catch (OperationCanceledError err) {
        handleCanceled(err);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        buildLogger.log(e.getClass().getSimpleName() + " while building " + getProject().getName() + ": " + e.getMessage() + " (see logs for details)");
        forgetLastBuiltState();
    } finally {
        queuedBuildData.discardCheckpoint();
        if (monitor != null)
            monitor.done();
        String message = "Built " + getProject().getName() + " in " + (System.currentTimeMillis() - startTime) + " ms";
        log.info(message);
        buildLogger.log(message);
        task.stop();
    }
    return getReferencedProjects();
}
Also used : ProgressMonitorWrapper(org.eclipse.core.runtime.ProgressMonitorWrapper) OperationCanceledError(org.eclipse.xtext.service.OperationCanceledError) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) StoppedTask(org.eclipse.xtext.util.internal.Stopwatches.StoppedTask) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CoreException(org.eclipse.core.runtime.CoreException) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Aggregations

OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 ProgressMonitorWrapper (org.eclipse.core.runtime.ProgressMonitorWrapper)3 ArrayList (java.util.ArrayList)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 CompletionProposalRequestor (org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor)2 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 IResourceDelta (org.eclipse.core.resources.IResourceDelta)1 CoreException (org.eclipse.core.runtime.CoreException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 JavadocCompletionProposal (org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 OperationCanceledError (org.eclipse.xtext.service.OperationCanceledError)1 StoppedTask (org.eclipse.xtext.util.internal.Stopwatches.StoppedTask)1