Promise<Object> (final @NotNull PlaybackContext context) { final ActionCallback actionCallback = new ActionCallbackProfilerStopper(); Disposable listenerDisposable = Disposer.newDisposable(); Ref<Span> span = new Ref<>(); Ref<Scope> scope = new Ref<>(); Ref<Long> completionTimeStarted = new Ref<>(); AtomicBoolean lookupListenerInited = new AtomicBoolean(false); ApplicationManager.getApplication().getMessageBus().connect(listenerDisposable) .subscribe(CompletionPhaseListener.TOPIC, new CompletionPhaseListener() { @Override public void completionPhaseChanged(boolean isCompletionRunning) { Editor editor = getEditor(context); LookupEx lookup = LookupManager.getActiveLookup(editor); if (lookup != null && !lookupListenerInited.get()) { lookup.addLookupListener(new LookupListener() { @Override public void firstElementShown() { span.get().setAttribute("firstElementShown", System.currentTimeMillis() - completionTimeStarted.get()); } }); lookupListenerInited.set(true); } if (!isCompletionRunning && !CompletionServiceImpl.isPhase(CompletionPhase.CommittingDocuments.class) && !span.isNull()) { if (CompletionServiceImpl.getCurrentCompletionProgressIndicator() == null) { String description = "CompletionServiceImpl.getCurrentCompletionProgressIndicator() is null on " + CompletionServiceImpl.getCompletionPhase(); span.get().setStatus(StatusCode.ERROR, description); actionCallback.reject(description); } else { List<LookupElement> items = CompletionServiceImpl.getCurrentCompletionProgressIndicator().getLookup().getItems(); int size = items.size(); span.get().setAttribute("number", size); span.get().end(); scope.get().close(); context.message("Number of elements: " + size, getLine()); Path dir = getCompletionItemsDir(); if (dir != null) { dumpCompletionVariants(items, dir); } actionCallback.setDone(); } Disposer.dispose(listenerDisposable); } } }); ApplicationManager.getApplication().invokeLater(Context.current().wrap(() -> { Project project = context.getProject(); Editor editor = getEditor(context); span.set(startSpan(SPAN_NAME)); scope.set(span.get().makeCurrent()); completionTimeStarted.set(System.currentTimeMillis()); new CodeCompletionHandlerBase(getCompletionType(), true, false, true).invokeCompletion(project, editor); })); return Promises.toPromise(actionCallback); }