private IProgressMonitor getSubMonitor(IProgressMonitor monitor) { // we need to use the parent SubMonitor otherwise SubMonitor#worked, etc doesn't work? // See article // https://medium.com/@jgwest/debugging-eclipse-subprogressmonitor-and-submonitor-0-work-completed-or-work-not-reported-f482c71cc85c if (monitor instanceof ProgressMonitorWrapper) { return getSubMonitor(((ProgressMonitorWrapper) monitor).getWrappedProgressMonitor()); } return monitor; }
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); 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; }