use of com.intellij.openapi.util.AsyncResult in project intellij-plugins by JetBrains.
the class DesignerApplicationManager method renderIfNeed.
public void renderIfNeed(@NotNull XmlFile psiFile, @Nullable final Consumer<DocumentInfo> handler, @Nullable ActionCallback renderRejectedCallback, final boolean debug) {
boolean needInitialRender = isApplicationClosed();
DocumentInfo documentInfo = null;
if (!needInitialRender) {
Document[] unsavedDocuments = FileDocumentManager.getInstance().getUnsavedDocuments();
if (unsavedDocuments.length > 0) {
renderDocumentsAndCheckLocalStyleModification(unsavedDocuments);
}
documentInfo = DocumentFactoryManager.getInstance().getNullableInfo(psiFile);
needInitialRender = documentInfo == null;
}
if (!needInitialRender) {
if (handler == null) {
return;
}
Application app = ApplicationManager.getApplication();
if (app.isDispatchThread()) {
final DocumentInfo finalDocumentInfo = documentInfo;
app.executeOnPooledThread(() -> handler.consume(finalDocumentInfo));
} else {
handler.consume(documentInfo);
}
return;
}
synchronized (initialRenderQueue) {
AsyncResult<DocumentInfo> renderResult = initialRenderQueue.findResult(psiFile);
if (renderResult == null) {
renderResult = new AsyncResult<>();
if (renderRejectedCallback != null) {
renderResult.notifyWhenRejected(renderRejectedCallback);
}
initialRenderQueue.add(new RenderAction<AsyncResult<DocumentInfo>>(psiFile.getProject(), psiFile.getViewProvider().getVirtualFile(), renderResult) {
@Override
protected boolean isNeedEdt() {
// ProgressManager requires dispatch thread
return true;
}
@Override
protected void doRun() {
assert project != null;
if (project.isDisposed()) {
return;
}
assert file != null;
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (!(psiFile instanceof XmlFile)) {
return;
}
Module module = ModuleUtilCore.findModuleForFile(file, project);
if (module != null) {
renderDocument(module, (XmlFile) psiFile, debug, result);
}
}
});
}
if (handler != null) {
renderResult.doWhenDone(handler);
}
renderResult.doWhenDone(createDocumentRenderedNotificationDoneHandler(false));
}
}
use of com.intellij.openapi.util.AsyncResult in project intellij-plugins by JetBrains.
the class Client method renderDocument.
/**
* final, full render document - responsible for handle problemsHolder and assetCounter - you must not do it
*/
public AsyncResult<DocumentInfo> renderDocument(Module module, XmlFile psiFile, ProblemsHolder problemsHolder) {
VirtualFile virtualFile = psiFile.getVirtualFile();
final int factoryId = registerDocumentFactoryIfNeed(module, psiFile, virtualFile, false, problemsHolder);
final AsyncResult<DocumentInfo> result = new AsyncResult<>();
if (factoryId == -1) {
result.setRejected();
return result;
}
FlexLibrarySet flexLibrarySet = registeredModules.getInfo(module).getFlexLibrarySet();
if (flexLibrarySet != null) {
fillAssetClassPoolIfNeed(flexLibrarySet);
}
if (!problemsHolder.isEmpty()) {
DocumentProblemManager.getInstance().report(module.getProject(), problemsHolder);
}
final ActionCallback callback = new ActionCallback("renderDocument");
boolean hasError = true;
try {
beginMessage(ClientMethod.renderDocument, callback, result, () -> result.setDone(DocumentFactoryManager.getInstance().getInfo(factoryId)));
out.writeShort(factoryId);
hasError = false;
} finally {
finalizeMessageAndFlush(hasError);
}
return result;
}
use of com.intellij.openapi.util.AsyncResult in project intellij-community by JetBrains.
the class WindowSystemPlaybackCall method printFocus.
public static AsyncResult<String> printFocus(final PlaybackContext context) {
final AsyncResult result = new AsyncResult<String>();
getUiReady(context).doWhenProcessed(() -> {
final LinkedHashMap<String, String> focusInfo = getFocusInfo();
if (focusInfo == null) {
result.setRejected("No component focused");
return;
}
StringBuffer text = new StringBuffer();
for (Iterator<String> iterator = focusInfo.keySet().iterator(); iterator.hasNext(); ) {
String key = iterator.next();
text.append(key + "=" + focusInfo.get(key));
if (iterator.hasNext()) {
text.append("|");
}
}
result.setDone(text.toString());
});
return result;
}
use of com.intellij.openapi.util.AsyncResult in project intellij-community by JetBrains.
the class EditorPlaybackCall method assertEditorLine.
public static AsyncResult<String> assertEditorLine(final PlaybackContext context, final String expected) {
final AsyncResult<String> result = new AsyncResult<>();
WindowSystemPlaybackCall.getUiReady(context).doWhenDone(() -> {
Editor editor = CommonDataKeys.EDITOR.getData(DataManager.getInstance().getDataContextFromFocus().getResult());
if (editor == null) {
editor = CommonDataKeys.EDITOR_EVEN_IF_INACTIVE.getData(DataManager.getInstance().getDataContextFromFocus().getResult());
}
if (editor == null) {
result.setRejected("Cannot find editor");
return;
}
final int line = editor.getCaretModel().getLogicalPosition().line;
final int caret = editor.getCaretModel().getOffset();
final int start = editor.getDocument().getLineStartOffset(line);
final int end = editor.getDocument().getLineEndOffset(line);
final StringBuffer actualText = new StringBuffer(editor.getDocument().getText(new TextRange(start, caret)));
actualText.append("<caret>").append(editor.getDocument().getText(new TextRange(caret, end)));
if (expected.equals(actualText.toString())) {
result.setDone();
} else {
result.setRejected("Expected:" + expected + " but was:" + actualText);
}
});
return result;
}
use of com.intellij.openapi.util.AsyncResult in project intellij-community by JetBrains.
the class NavBarPanel method getHintContainerShowPoint.
AsyncResult<RelativePoint> getHintContainerShowPoint() {
final AsyncResult<RelativePoint> result = new AsyncResult<>();
if (myLocationCache == null) {
if (myHintContainer != null) {
final Point p = AbstractPopup.getCenterOf(myHintContainer, this);
p.y -= myHintContainer.getVisibleRect().height / 4;
myLocationCache = RelativePoint.fromScreen(p);
} else {
if (myContextComponent != null) {
myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(DataManager.getInstance().getDataContext(myContextComponent));
} else {
DataManager.getInstance().getDataContextFromFocus().doWhenDone((Consumer<DataContext>) dataContext -> {
myContextComponent = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(DataManager.getInstance().getDataContext(myContextComponent));
});
}
}
}
final Component c = myLocationCache.getComponent();
if (!(c instanceof JComponent && c.isShowing())) {
//Yes. It happens sometimes.
// 1. Empty frame. call nav bar, select some package and open it in Project View
// 2. Call nav bar, then Esc
// 3. Hide all tool windows (Ctrl+Shift+F12), so we've got empty frame again
// 4. Call nav bar. NPE. ta da
final JComponent ideFrame = WindowManager.getInstance().getIdeFrame(getProject()).getComponent();
final JRootPane rootPane = UIUtil.getRootPane(ideFrame);
myLocationCache = JBPopupFactory.getInstance().guessBestPopupLocation(rootPane);
}
result.setDone(myLocationCache);
return result;
}
Aggregations