Search in sources :

Example 1 with Promise

use of org.jetbrains.concurrency.Promise in project intellij-community by JetBrains.

the class XEvaluateInConsoleFromEditorActionHandler method perform.

@Override
protected void perform(@NotNull XDebugSession session, DataContext dataContext) {
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (editor == null || !(editor instanceof EditorEx)) {
        return;
    }
    int selectionStart = editor.getSelectionModel().getSelectionStart();
    int selectionEnd = editor.getSelectionModel().getSelectionEnd();
    Promise<Pair<TextRange, String>> rangeAndText = null;
    if (selectionStart != selectionEnd) {
        TextRange textRange = new TextRange(selectionStart, selectionEnd);
        rangeAndText = Promise.resolve(Pair.create(textRange, editor.getDocument().getText(textRange)));
    } else {
        XDebuggerEvaluator evaluator = session.getDebugProcess().getEvaluator();
        if (evaluator != null) {
            Promise<ExpressionInfo> expressionInfoPromise = evaluator.getExpressionInfoAtOffsetAsync(session.getProject(), editor.getDocument(), selectionStart, true);
            rangeAndText = expressionInfoPromise.then(expressionInfo -> {
                if (expressionInfo == null) {
                    return null;
                }
                return Pair.create(expressionInfo.getTextRange(), XDebuggerEvaluateActionHandler.getExpressionText(expressionInfo, editor.getDocument()));
            });
        } else {
            return;
        }
    }
    rangeAndText.done(textRangeStringPair -> {
        ApplicationManager.getApplication().invokeLater(() -> {
            TextRange range = textRangeStringPair.getFirst();
            String text = textRangeStringPair.getSecond();
            if (text == null)
                return;
            ConsoleExecuteAction action = getConsoleExecuteAction(session);
            if (action != null) {
                action.execute(range, text, (EditorEx) editor);
            }
        });
    });
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) ActionUtil(com.intellij.openapi.actionSystem.ex.ActionUtil) AnAction(com.intellij.openapi.actionSystem.AnAction) TextRange(com.intellij.openapi.util.TextRange) ContainerUtil(com.intellij.util.containers.ContainerUtil) Editor(com.intellij.openapi.editor.Editor) ExpressionInfo(com.intellij.xdebugger.evaluation.ExpressionInfo) Nullable(org.jetbrains.annotations.Nullable) Promise(org.jetbrains.concurrency.Promise) List(java.util.List) ConsoleView(com.intellij.execution.ui.ConsoleView) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) ConsoleExecuteAction(com.intellij.execution.console.ConsoleExecuteAction) LanguageConsoleView(com.intellij.execution.console.LanguageConsoleView) NotNull(org.jetbrains.annotations.NotNull) XDebugSession(com.intellij.xdebugger.XDebugSession) XDebuggerEvaluator(com.intellij.xdebugger.evaluation.XDebuggerEvaluator) EditorEx(com.intellij.openapi.editor.ex.EditorEx) XDebuggerEvaluator(com.intellij.xdebugger.evaluation.XDebuggerEvaluator) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) ExpressionInfo(com.intellij.xdebugger.evaluation.ExpressionInfo) ConsoleExecuteAction(com.intellij.execution.console.ConsoleExecuteAction) Pair(com.intellij.openapi.util.Pair)

Example 2 with Promise

use of org.jetbrains.concurrency.Promise in project intellij-community by JetBrains.

the class AbstractTreeUi method queueBackgroundUpdate.

private boolean queueBackgroundUpdate(@NotNull final UpdateInfo updateInfo, @NotNull final DefaultMutableTreeNode node) {
    assertIsDispatchThread();
    final Object oldElementFromDescriptor = getElementFromDescriptor(updateInfo.getDescriptor());
    UpdateInfo loaded = getLoadedInBackground(oldElementFromDescriptor);
    if (loaded != null) {
        loaded.apply(updateInfo);
        return false;
    }
    addToLoadedInBackground(oldElementFromDescriptor, updateInfo);
    maybeSetBusyAndScheduleWaiterForReady(true, oldElementFromDescriptor);
    if (!isNodeBeingBuilt(node)) {
        LoadingNode loadingNode = new LoadingNode(getLoadingNodeText());
        myTreeModel.insertNodeInto(loadingNode, node, node.getChildCount());
    }
    removeFromUnbuilt(node);
    final Ref<LoadedChildren> children = new Ref<>();
    final Ref<Object> elementFromDescriptor = new Ref<>();
    final DefaultMutableTreeNode[] nodeToProcessActions = new DefaultMutableTreeNode[1];
    final TreeConsumer<Void> finalizeRunnable = new TreeConsumer<Void>("AbstractTreeUi.queueBackgroundUpdate: finalize") {

        @Override
        public void perform() {
            invokeLaterIfNeeded(false, new TreeRunnable("AbstractTreeUi.queueBackgroundUpdate: finalize later") {

                @Override
                public void perform() {
                    if (isReleased())
                        return;
                    removeLoading(node, false);
                    removeFromLoadedInBackground(elementFromDescriptor.get());
                    removeFromLoadedInBackground(oldElementFromDescriptor);
                    if (nodeToProcessActions[0] != null) {
                        processNodeActionsIfReady(nodeToProcessActions[0]);
                    }
                }
            });
        }
    };
    Runnable buildRunnable = new TreeRunnable("AbstractTreeUi.queueBackgroundUpdate: build") {

        @Override
        public void perform() {
            if (updateInfo.getPass().isExpired()) {
                finalizeRunnable.run();
                return;
            }
            if (!updateInfo.isDescriptorIsUpToDate()) {
                update(updateInfo.getDescriptor(), true);
            }
            if (!updateInfo.isUpdateChildren()) {
                nodeToProcessActions[0] = node;
                return;
            }
            Object element = getElementFromDescriptor(updateInfo.getDescriptor());
            if (element == null) {
                removeFromLoadedInBackground(oldElementFromDescriptor);
                finalizeRunnable.run();
                return;
            }
            elementFromDescriptor.set(element);
            Object[] loadedElements = getChildrenFor(element);
            final LoadedChildren loaded = new LoadedChildren(loadedElements);
            for (final Object each : loadedElements) {
                NodeDescriptor existingDesc = getDescriptorFrom(getNodeForElement(each, true));
                final NodeDescriptor eachChildDescriptor = isValid(existingDesc) ? existingDesc : getTreeStructure().createDescriptor(each, updateInfo.getDescriptor());
                execute(new TreeRunnable("AbstractTreeUi.queueBackgroundUpdate") {

                    @Override
                    public void perform() {
                        Promise<Boolean> promise = update(eachChildDescriptor, true);
                        LOG.assertTrue(promise instanceof Getter);
                        //noinspection unchecked
                        loaded.putDescriptor(each, eachChildDescriptor, ((Getter<Boolean>) promise).get());
                    }
                });
            }
            children.set(loaded);
        }

        @NotNull
        @NonNls
        @Override
        public String toString() {
            return "runnable=" + oldElementFromDescriptor;
        }
    };
    Runnable updateRunnable = new TreeRunnable("AbstractTreeUi.queueBackgroundUpdate: update") {

        @Override
        public void perform() {
            if (updateInfo.getPass().isExpired()) {
                finalizeRunnable.run();
                return;
            }
            if (children.get() == null) {
                finalizeRunnable.run();
                return;
            }
            if (isRerunNeeded(updateInfo.getPass())) {
                removeFromLoadedInBackground(elementFromDescriptor.get());
                getUpdater().requeue(updateInfo.getPass());
                return;
            }
            removeFromLoadedInBackground(elementFromDescriptor.get());
            if (myUnbuiltNodes.contains(node)) {
                Pair<Boolean, LoadedChildren> unbuilt = processUnbuilt(node, updateInfo.getDescriptor(), updateInfo.getPass(), isExpanded(node, updateInfo.isWasExpanded()), children.get());
                if (unbuilt.getFirst()) {
                    nodeToProcessActions[0] = node;
                    return;
                }
            }
            ActionCallback callback = updateNodeChildren(node, updateInfo.getPass(), children.get(), true, updateInfo.isCanSmartExpand(), updateInfo.isForceUpdate(), true, true);
            callback.doWhenDone(new TreeRunnable("AbstractTreeUi.queueBackgroundUpdate: on done updateNodeChildren") {

                @Override
                public void perform() {
                    if (isRerunNeeded(updateInfo.getPass())) {
                        getUpdater().requeue(updateInfo.getPass());
                        return;
                    }
                    Object element = elementFromDescriptor.get();
                    if (element != null) {
                        removeLoading(node, false);
                        nodeToProcessActions[0] = node;
                    }
                }
            });
        }
    };
    queueToBackground(buildRunnable, updateRunnable).done(finalizeRunnable).rejected(new TreeConsumer<Throwable>("AbstractTreeUi.queueBackgroundUpdate: on rejected") {

        @Override
        public void perform() {
            updateInfo.getPass().expire();
        }
    });
    return true;
}
Also used : LoadingNode(com.intellij.ui.LoadingNode) TreeConsumer(com.intellij.ide.util.treeView.TreeRunnable.TreeConsumer) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) Promise(org.jetbrains.concurrency.Promise) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

Promise (org.jetbrains.concurrency.Promise)2 ConsoleExecuteAction (com.intellij.execution.console.ConsoleExecuteAction)1 LanguageConsoleView (com.intellij.execution.console.LanguageConsoleView)1 ConsoleView (com.intellij.execution.ui.ConsoleView)1 TreeConsumer (com.intellij.ide.util.treeView.TreeRunnable.TreeConsumer)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 ActionUtil (com.intellij.openapi.actionSystem.ex.ActionUtil)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 LoadingNode (com.intellij.ui.LoadingNode)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 ExpressionInfo (com.intellij.xdebugger.evaluation.ExpressionInfo)1 XDebuggerEvaluator (com.intellij.xdebugger.evaluation.XDebuggerEvaluator)1 List (java.util.List)1