Search in sources :

Example 1 with GetObjectConsumer

use of org.dartlang.vm.service.consumer.GetObjectConsumer in project intellij-plugins by JetBrains.

the class DartStaticFieldsGroup method computeChildren.

@Override
public void computeChildren(@NotNull final XCompositeNode node) {
    final AtomicInteger counter = new AtomicInteger(myFieldRefs.size());
    final XValueChildrenList list = new XValueChildrenList(myFieldRefs.size());
    for (final FieldRef fieldRef : myFieldRefs) {
        myDebugProcess.getVmServiceWrapper().getObject(myIsolateId, fieldRef.getId(), new GetObjectConsumer() {

            @Override
            public void received(Obj field) {
                final InstanceRef instanceRef = ((Field) field).getStaticValue();
                // static field may be not initialized yet, in this case this instanceRef is in fact a Sentinel
                if ("@Instance".equals(instanceRef.getType())) {
                    list.add(new DartVmServiceValue(myDebugProcess, myIsolateId, ((Field) field).getName(), instanceRef, null, fieldRef, false));
                } else if ("Sentinel".equals(instanceRef.getType())) {
                    list.add(new XNamedValue(((Field) field).getName()) {

                        @Override
                        public void computeSourcePosition(@NotNull XNavigatable navigatable) {
                            DartVmServiceValue.doComputeSourcePosition(myDebugProcess, navigatable, myIsolateId, fieldRef);
                        }

                        @Override
                        public void computePresentation(@NotNull XValueNode node, @NotNull XValuePlace place) {
                            final JsonElement valueAsString = instanceRef.getJson().get("valueAsString");
                            final String value = valueAsString == null ? "not initialized" : valueAsString.getAsString();
                            node.setPresentation(AllIcons.Nodes.Field, null, value, false);
                        }
                    });
                }
                if (counter.decrementAndGet() == 0) {
                    if (list.size() == 0) {
                        node.setErrorMessage("Static fields not initialized yet");
                    } else {
                        node.addChildren(list, true);
                    }
                }
            }

            @Override
            public void received(Sentinel sentinel) {
                node.setErrorMessage(sentinel.getValueAsString());
            }

            @Override
            public void onError(RPCError error) {
                node.setErrorMessage(error.getMessage());
            }
        });
    }
}
Also used : GetObjectConsumer(org.dartlang.vm.service.consumer.GetObjectConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonElement(com.google.gson.JsonElement)

Example 2 with GetObjectConsumer

use of org.dartlang.vm.service.consumer.GetObjectConsumer in project flutter-intellij by flutter.

the class EvalOnDartLibrary method getObjectHelper.

@SuppressWarnings("unchecked")
public <T extends Obj> CompletableFuture<T> getObjectHelper(ObjRef instance, InspectorService.ObjectGroup isAlive) {
    return addRequest(isAlive, "getObject", () -> {
        final CompletableFuture<T> future = new CompletableFuture<>();
        vmService.getObject(getIsolateId(), instance.getId(), new GetObjectConsumer() {

            @Override
            public void onError(RPCError error) {
                future.completeExceptionally(new RuntimeException("RPCError calling getObject: " + error.toString()));
            }

            @Override
            public void received(Obj response) {
                future.complete((T) response);
            }

            @Override
            public void received(Sentinel response) {
                future.completeExceptionally(new RuntimeException("Sentinel calling getObject: " + response.toString()));
            }
        });
        return future;
    });
}
Also used : GetObjectConsumer(org.dartlang.vm.service.consumer.GetObjectConsumer)

Example 3 with GetObjectConsumer

use of org.dartlang.vm.service.consumer.GetObjectConsumer in project flutter-intellij by flutter.

the class DartVmServiceDebugProcess method onOpenSourceLocationRequest.

// TODO(devoncarew): Re-implement this in terms of the generated vm service protocol library.
private void onOpenSourceLocationRequest(@NotNull String isolateId, @NotNull String scriptId, int tokenPos) {
    myVmServiceWrapper.getObject(isolateId, scriptId, new GetObjectConsumer() {

        @Override
        public void received(Obj response) {
            if (response instanceof Script) {
                ApplicationManager.getApplication().executeOnPooledThread(() -> {
                    final XSourcePosition source = getSourcePosition(isolateId, toScriptRef((Script) response), tokenPos);
                    if (source != null) {
                        final Project project = getSession().getProject();
                        final OpenFileHyperlinkInfo info = new OpenFileHyperlinkInfo(project, source.getFile(), source.getLine());
                        ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
                            info.navigate(project);
                            ProjectUtil.focusProjectWindow(project, true);
                        }));
                    }
                });
            }
        }

        @Override
        public void received(Sentinel response) {
        // ignore
        }

        @Override
        public void onError(RPCError error) {
        // ignore
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) OpenFileHyperlinkInfo(com.intellij.execution.filters.OpenFileHyperlinkInfo) GetObjectConsumer(org.dartlang.vm.service.consumer.GetObjectConsumer)

Example 4 with GetObjectConsumer

use of org.dartlang.vm.service.consumer.GetObjectConsumer in project flutter-intellij by flutter.

the class DartVmServiceEvaluator method evaluate.

@Override
public void evaluate(@NotNull final String expression, @NotNull final XEvaluationCallback callback, @Nullable final XSourcePosition expressionPosition) {
    final String isolateId = myDebugProcess.getCurrentIsolateId();
    final Project project = myDebugProcess.getSession().getProject();
    final FileEditorManager manager = FileEditorManager.getInstance(project);
    PsiElement element = null;
    PsiFile psiFile = null;
    final List<VirtualFile> libraryFiles = new ArrayList<>();
    // and to have that trigger pausing at an exception.
    if (myDebugProcess.getVmServiceWrapper() == null) {
        callback.errorOccurred("Device disconnected");
        return;
    }
    final VmServiceWrapper vmService = myDebugProcess.getVmServiceWrapper();
    if (vmService == null) {
        // Not connected to the VM yet.
        callback.errorOccurred("No connection to the Dart VM");
        return;
    }
    myDebugProcess.getVmServiceWrapper().setExceptionPauseMode(ExceptionPauseMode.None);
    final XEvaluationCallback wrappedCallback = new XEvaluationCallback() {

        @Override
        public void evaluated(@NotNull XValue result) {
            vmService.setExceptionPauseMode(myDebugProcess.getBreakOnExceptionMode());
            callback.evaluated(result);
        }

        @Override
        public void errorOccurred(@NotNull String errorMessage) {
            vmService.setExceptionPauseMode(myDebugProcess.getBreakOnExceptionMode());
            callback.errorOccurred(errorMessage);
        }
    };
    if (expressionPosition != null) {
        psiFile = PsiManager.getInstance(project).findFile(expressionPosition.getFile());
        if (psiFile != null) {
            element = psiFile.findElementAt(expressionPosition.getOffset());
        }
    } else {
        // TODO(jacobr): we could use the most recently selected Dart file instead
        // of using the selected file.
        final Editor editor = manager.getSelectedTextEditor();
        if (editor instanceof TextEditor) {
            final TextEditor textEditor = (TextEditor) editor;
            final FileEditorLocation fileEditorLocation = textEditor.getCurrentLocation();
            final VirtualFile virtualFile = textEditor.getFile();
            if (virtualFile != null) {
                psiFile = PsiManager.getInstance(project).findFile(virtualFile);
                if (psiFile != null && fileEditorLocation instanceof TextEditorLocation) {
                    final TextEditorLocation textEditorLocation = (TextEditorLocation) fileEditorLocation;
                    element = psiFile.findElementAt(textEditor.getEditor().logicalPositionToOffset(textEditorLocation.getPosition()));
                }
            }
        }
    }
    if (psiFile != null) {
        libraryFiles.addAll(DartResolveUtil.findLibrary(psiFile));
    }
    if (isolateId == null) {
        wrappedCallback.errorOccurred("No running isolate.");
        return;
    }
    final DartClass dartClass = element != null ? PsiTreeUtil.getParentOfType(element, DartClass.class) : null;
    final String dartClassName = dartClass != null ? dartClass.getName() : null;
    vmService.getCachedIsolate(isolateId).whenComplete((isolate, error) -> {
        if (error != null) {
            wrappedCallback.errorOccurred(error.getMessage());
            return;
        }
        if (isolate == null) {
            wrappedCallback.errorOccurred("No running isolate.");
            return;
        }
        final LibraryRef libraryRef = findMatchingLibrary(isolate, libraryFiles);
        if (dartClassName != null) {
            vmService.getObject(isolateId, libraryRef.getId(), new GetObjectConsumer() {

                @Override
                public void onError(RPCError error) {
                    wrappedCallback.errorOccurred(error.getMessage());
                }

                @Override
                public void received(Obj response) {
                    final Library library = (Library) response;
                    for (ClassRef classRef : library.getClasses()) {
                        if (classRef.getName().equals(dartClassName)) {
                            vmService.evaluateInTargetContext(isolateId, classRef.getId(), expression, wrappedCallback);
                            return;
                        }
                    }
                    // Class not found so just use the library.
                    vmService.evaluateInTargetContext(isolateId, libraryRef.getId(), expression, wrappedCallback);
                }

                @Override
                public void received(Sentinel response) {
                    wrappedCallback.errorOccurred(response.getValueAsString());
                }
            });
        } else {
            myDebugProcess.getVmServiceWrapper().evaluateInTargetContext(isolateId, libraryRef.getId(), expression, wrappedCallback);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) GetObjectConsumer(org.dartlang.vm.service.consumer.GetObjectConsumer) NotNull(org.jetbrains.annotations.NotNull) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) VmServiceWrapper(io.flutter.vmService.VmServiceWrapper) FileEditorLocation(com.intellij.openapi.fileEditor.FileEditorLocation) XValue(com.intellij.xdebugger.frame.XValue) Project(com.intellij.openapi.project.Project) TextEditorLocation(com.intellij.openapi.fileEditor.TextEditorLocation) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor)

Example 5 with GetObjectConsumer

use of org.dartlang.vm.service.consumer.GetObjectConsumer in project flutter-intellij by flutter.

the class DartVmServiceValue method computeCollectionChildren.

private void computeCollectionChildren(@NotNull InstanceRef instanceRef, int offset, @NotNull final XCompositeNode node) {
    final int count = Math.min(instanceRef.getLength() - offset, XCompositeNode.MAX_CHILDREN_TO_SHOW);
    myDebugProcess.getVmServiceWrapper().getCollectionObject(myIsolateId, myInstanceRef.getId(), offset, count, new GetObjectConsumer() {

        @Override
        public void received(Obj instance) {
            if (isListKind(instanceRef.getKind())) {
                addListChildren(offset, node, (Instance) instance);
            } else if (instanceRef.getKind() == InstanceKind.Map) {
                addMapChildren(offset, node, ((Instance) instance).getAssociations());
            } else {
                assert false : instanceRef.getKind();
            }
            if (offset + count < instanceRef.getLength()) {
                node.tooManyChildren(instanceRef.getLength() - offset - count, () -> computeCollectionChildren(instanceRef, offset + count, node));
            }
        }

        @Override
        public void received(Sentinel sentinel) {
            node.setErrorMessage(sentinel.getValueAsString());
        }

        @Override
        public void onError(RPCError error) {
            node.setErrorMessage(error.getMessage());
        }
    });
}
Also used : GetObjectConsumer(org.dartlang.vm.service.consumer.GetObjectConsumer)

Aggregations

GetObjectConsumer (org.dartlang.vm.service.consumer.GetObjectConsumer)11 JsonElement (com.google.gson.JsonElement)2 Project (com.intellij.openapi.project.Project)2 Semaphore (com.intellij.util.concurrency.Semaphore)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OpenFileHyperlinkInfo (com.intellij.execution.filters.OpenFileHyperlinkInfo)1 Editor (com.intellij.openapi.editor.Editor)1 FileEditorLocation (com.intellij.openapi.fileEditor.FileEditorLocation)1 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 TextEditorLocation (com.intellij.openapi.fileEditor.TextEditorLocation)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 SmartList (com.intellij.util.SmartList)1 XValue (com.intellij.xdebugger.frame.XValue)1 XValueChildrenList (com.intellij.xdebugger.frame.XValueChildrenList)1 VmServiceWrapper (io.flutter.vmService.VmServiceWrapper)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1