Search in sources :

Example 1 with AsyncPromise

use of org.jetbrains.concurrency.AsyncPromise in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoBuildingRunner method prepare.

@NotNull
@Override
protected Promise<RunProfileStarter> prepare(@NotNull ExecutionEnvironment environment, @NotNull RunProfileState state) throws ExecutionException {
    File outputFile = getOutputFile(environment, (GoApplicationRunningState) state);
    FileDocumentManager.getInstance().saveAllDocuments();
    AsyncPromise<RunProfileStarter> buildingPromise = new AsyncPromise<>();
    GoHistoryProcessListener historyProcessListener = new GoHistoryProcessListener();
    ((GoApplicationRunningState) state).createCommonExecutor().withParameters("build").withParameterString(((GoApplicationRunningState) state).getGoBuildParams()).withParameters("-o", outputFile.getAbsolutePath()).withParameters(((GoApplicationRunningState) state).isDebug() ? new String[] { "-gcflags", "-N -l" } : ArrayUtil.EMPTY_STRING_ARRAY).withParameters(((GoApplicationRunningState) state).getTarget()).disablePty().withPresentableName("go build").withProcessListener(historyProcessListener).withProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            super.processTerminated(event);
            boolean compilationFailed = event.getExitCode() != 0;
            if (((GoApplicationRunningState) state).isDebug()) {
                buildingPromise.setResult(new MyDebugStarter(outputFile.getAbsolutePath(), historyProcessListener, compilationFailed));
            } else {
                buildingPromise.setResult(new MyRunStarter(outputFile.getAbsolutePath(), historyProcessListener, compilationFailed));
            }
        }
    }).executeWithProgress(false);
    return buildingPromise;
}
Also used : RunProfileStarter(com.intellij.execution.RunProfileStarter) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) GoApplicationRunningState(com.goide.runconfig.application.GoApplicationRunningState) GoHistoryProcessListener(com.goide.util.GoHistoryProcessListener) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AsyncPromise

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

the class JavaValue method calculateEvaluationExpression.

@NotNull
@Override
public Promise<XExpression> calculateEvaluationExpression() {
    if (evaluationExpression != null) {
        return Promise.resolve(evaluationExpression);
    } else {
        final AsyncPromise<XExpression> res = new AsyncPromise<>();
        myEvaluationContext.getManagerThread().schedule(new SuspendContextCommandImpl(myEvaluationContext.getSuspendContext()) {

            @Override
            public Priority getPriority() {
                return Priority.HIGH;
            }

            @Override
            public void contextAction() throws Exception {
                evaluationExpression = ApplicationManager.getApplication().runReadAction(new Computable<XExpression>() {

                    @Override
                    public XExpression compute() {
                        try {
                            PsiElement psiExpression = getDescriptor().getTreeEvaluation(JavaValue.this, getDebuggerContext());
                            if (psiExpression != null) {
                                XExpression res = TextWithImportsImpl.toXExpression(new TextWithImportsImpl(psiExpression));
                                // add runtime imports if any
                                Set<String> imports = psiExpression.getUserData(DebuggerTreeNodeExpression.ADDITIONAL_IMPORTS_KEY);
                                if (imports != null && res != null) {
                                    if (res.getCustomInfo() != null) {
                                        imports.add(res.getCustomInfo());
                                    }
                                    res = new XExpressionImpl(res.getExpression(), res.getLanguage(), StringUtil.join(imports, ","), res.getMode());
                                }
                                return res;
                            }
                        } catch (EvaluateException e) {
                            LOG.info(e);
                        }
                        return null;
                    }
                });
                res.setResult(evaluationExpression);
            }
        });
        return res;
    }
}
Also used : Set(java.util.Set) XExpressionImpl(com.intellij.xdebugger.impl.breakpoints.XExpressionImpl) SuspendContextCommandImpl(com.intellij.debugger.engine.events.SuspendContextCommandImpl) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) XExpression(com.intellij.xdebugger.XExpression) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with AsyncPromise

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

the class AbstractTreeUi method processExistingNode.

@NotNull
private Promise<?> processExistingNode(@NotNull final DefaultMutableTreeNode childNode, final NodeDescriptor childDescriptor, @NotNull final DefaultMutableTreeNode parentNode, @NotNull final MutualMap<Object, Integer> elementToIndexMap, @NotNull final TreeUpdatePass pass, final boolean canSmartExpand, final boolean forceUpdate, @Nullable LoadedChildren parentPreloadedChildren) {
    if (pass.isExpired()) {
        return Promises.<Void>rejectedPromise();
    }
    if (childDescriptor == null) {
        pass.expire();
        return Promises.<Void>rejectedPromise();
    }
    final Object oldElement = getElementFromDescriptor(childDescriptor);
    if (oldElement == null) {
        // if a tree node with removed element was not properly removed from a tree model
        // we must not ignore this situation and should remove a wrong node
        removeNodeFromParent(childNode, true);
        return Promises.<Void>resolvedPromise();
    }
    Promise<Boolean> update;
    if (parentPreloadedChildren != null && parentPreloadedChildren.getDescriptor(oldElement) == childDescriptor) {
        update = Promise.resolve(parentPreloadedChildren.isUpdated(oldElement));
    } else {
        update = update(childDescriptor, false);
    }
    final AsyncPromise<Void> result = new AsyncPromise<>();
    final Ref<NodeDescriptor> childDesc = new Ref<>(childDescriptor);
    update.done(isChanged -> {
        final AtomicBoolean changes = new AtomicBoolean(isChanged);
        final AtomicBoolean forceRemapping = new AtomicBoolean();
        final Ref<Object> newElement = new Ref<>(getElementFromDescriptor(childDesc.get()));
        final Integer index = newElement.get() == null ? null : elementToIndexMap.getValue(getElementFromDescriptor(childDesc.get()));
        Promise<Boolean> promise;
        if (index == null) {
            promise = Promise.resolve(false);
        } else {
            final Object elementFromMap = elementToIndexMap.getKey(index);
            if (elementFromMap != newElement.get() && elementFromMap.equals(newElement.get())) {
                if (isInStructure(elementFromMap) && isInStructure(newElement.get())) {
                    final AsyncPromise<Boolean> updateIndexDone = new AsyncPromise<>();
                    promise = updateIndexDone;
                    NodeDescriptor parentDescriptor = getDescriptorFrom(parentNode);
                    if (parentDescriptor != null) {
                        childDesc.set(getTreeStructure().createDescriptor(elementFromMap, parentDescriptor));
                        NodeDescriptor oldDesc = getDescriptorFrom(childNode);
                        if (isValid(oldDesc)) {
                            childDesc.get().applyFrom(oldDesc);
                        }
                        childNode.setUserObject(childDesc.get());
                        newElement.set(elementFromMap);
                        forceRemapping.set(true);
                        update(childDesc.get(), false).done(isChanged1 -> {
                            changes.set(isChanged1);
                            updateIndexDone.setResult(isChanged1);
                        });
                    }
                } else {
                    promise = Promise.resolve(changes.get());
                }
            } else {
                promise = Promise.resolve(changes.get());
            }
            promise.done(new TreeConsumer<Boolean>("AbstractTreeUi.processExistingNode: on done index updating after update") {

                @Override
                public void perform() {
                    if (childDesc.get().getIndex() != index.intValue()) {
                        changes.set(true);
                    }
                    childDesc.get().setIndex(index.intValue());
                }
            });
        }
        promise.done(new TreeConsumer<Boolean>("AbstractTreeUi.processExistingNode: on done index updating") {

            @Override
            public void perform() {
                if (!oldElement.equals(newElement.get()) || forceRemapping.get()) {
                    removeMapping(oldElement, childNode, newElement.get());
                    Object newE = newElement.get();
                    if (newE != null) {
                        createMapping(newE, childNode);
                    }
                    NodeDescriptor parentDescriptor = getDescriptorFrom(parentNode);
                    if (parentDescriptor != null) {
                        parentDescriptor.setChildrenSortingStamp(-1);
                    }
                }
                if (index == null) {
                    int selectedIndex = -1;
                    if (TreeBuilderUtil.isNodeOrChildSelected(myTree, childNode)) {
                        selectedIndex = parentNode.getIndex(childNode);
                    }
                    if (childNode.getParent() instanceof DefaultMutableTreeNode) {
                        final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) childNode.getParent();
                        if (myTree.isExpanded(new TreePath(parent.getPath()))) {
                            if (parent.getChildCount() == 1 && parent.getChildAt(0) == childNode) {
                                insertLoadingNode(parent, false);
                            }
                        }
                    }
                    Object disposedElement = getElementFor(childNode);
                    removeNodeFromParent(childNode, selectedIndex >= 0);
                    disposeNode(childNode);
                    adjustSelectionOnChildRemove(parentNode, selectedIndex, disposedElement);
                    result.setResult(null);
                } else {
                    elementToIndexMap.remove(getElementFromDescriptor(childDesc.get()));
                    updateNodeChildren(childNode, pass, null, false, canSmartExpand, forceUpdate, true, true).doWhenDone(() -> result.setResult(null));
                }
            }
        });
    });
    return result;
}
Also used : AsyncPromise(org.jetbrains.concurrency.AsyncPromise) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with AsyncPromise

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

the class AbstractTreeUi method update.

@NotNull
private Promise<Boolean> update(@NotNull final NodeDescriptor nodeDescriptor, boolean now) {
    Promise<Boolean> promise;
    if (now || isPassthroughMode()) {
        promise = Promise.resolve(update(nodeDescriptor));
    } else {
        final AsyncPromise<Boolean> result = new AsyncPromise<>();
        promise = result;
        boolean bgLoading = isToBuildInBackground(nodeDescriptor);
        boolean edt = isEdt();
        if (bgLoading) {
            if (edt) {
                final AtomicBoolean changes = new AtomicBoolean();
                queueToBackground(new TreeRunnable("AbstractTreeUi.update: build") {

                    @Override
                    public void perform() {
                        changes.set(update(nodeDescriptor));
                    }
                }, new TreeRunnable("AbstractTreeUi.update: post") {

                    @Override
                    public void perform() {
                        result.setResult(changes.get());
                    }
                });
            } else {
                result.setResult(update(nodeDescriptor));
            }
        } else {
            if (edt || !myWasEverShown) {
                result.setResult(update(nodeDescriptor));
            } else {
                invokeLaterIfNeeded(false, new TreeRunnable("AbstractTreeUi.update: later") {

                    @Override
                    public void perform() {
                        execute(new TreeRunnable("AbstractTreeUi.update: later execute") {

                            @Override
                            public void perform() {
                                result.setResult(update(nodeDescriptor));
                            }
                        });
                    }
                });
            }
        }
    }
    promise.done(changes -> {
        if (!changes) {
            return;
        }
        invokeLaterIfNeeded(false, new TreeRunnable("AbstractTreeUi.update: on done result") {

            @Override
            public void perform() {
                Object element = nodeDescriptor.getElement();
                DefaultMutableTreeNode node = getNodeForElement(element, false);
                if (node != null) {
                    TreePath path = getPathFor(node);
                    if (myTree.isVisible(path)) {
                        updateNodeImageAndPosition(node, false, true);
                    }
                }
            }
        });
    });
    return promise;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AsyncPromise

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

the class XQuickEvaluateHandler method createValueHintAsync.

@NotNull
@Override
public Promise<AbstractValueHint> createValueHintAsync(@NotNull final Project project, @NotNull final Editor editor, @NotNull final Point point, final ValueHintType type) {
    final XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
    if (session == null) {
        return Promise.resolve(null);
    }
    final XDebuggerEvaluator evaluator = session.getDebugProcess().getEvaluator();
    if (evaluator == null) {
        return Promise.resolve(null);
    }
    int offset = AbstractValueHint.calculateOffset(editor, point);
    return getExpressionInfo(evaluator, project, type, editor, offset).thenAsync(expressionInfo -> {
        AsyncPromise<AbstractValueHint> resultPromise = new AsyncPromise<>();
        UIUtil.invokeLaterIfNeeded(() -> {
            int textLength = editor.getDocument().getTextLength();
            if (expressionInfo == null) {
                resultPromise.setResult(null);
                return;
            }
            TextRange range = expressionInfo.getTextRange();
            if (range.getStartOffset() > range.getEndOffset() || range.getStartOffset() < 0 || range.getEndOffset() > textLength) {
                LOG.error("invalid range: " + range + ", text length = " + textLength + ", evaluator: " + evaluator);
                resultPromise.setResult(null);
                return;
            }
            resultPromise.setResult(new XValueHint(project, editor, point, type, expressionInfo, evaluator, session));
        });
        return resultPromise;
    });
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) AbstractValueHint(com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint) XDebuggerEvaluator(com.intellij.xdebugger.evaluation.XDebuggerEvaluator) TextRange(com.intellij.openapi.util.TextRange) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) AbstractValueHint(com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AsyncPromise (org.jetbrains.concurrency.AsyncPromise)8 NotNull (org.jetbrains.annotations.NotNull)7 TextRange (com.intellij.openapi.util.TextRange)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 GoApplicationRunningState (com.goide.runconfig.application.GoApplicationRunningState)1 GoHistoryProcessListener (com.goide.util.GoHistoryProcessListener)1 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)1 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)1 SuspendContextCommandImpl (com.intellij.debugger.engine.events.SuspendContextCommandImpl)1 RunProfileStarter (com.intellij.execution.RunProfileStarter)1 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)1 ProcessEvent (com.intellij.execution.process.ProcessEvent)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 FileChooser (com.intellij.openapi.fileChooser.FileChooser)1