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;
}
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;
}
}
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;
}
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;
}
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;
});
}
Aggregations