Search in sources :

Example 1 with TextEditorLocation

use of com.intellij.openapi.fileEditor.TextEditorLocation 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)

Aggregations

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 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XValue (com.intellij.xdebugger.frame.XValue)1 VmServiceWrapper (io.flutter.vmService.VmServiceWrapper)1 ArrayList (java.util.ArrayList)1 GetObjectConsumer (org.dartlang.vm.service.consumer.GetObjectConsumer)1 NotNull (org.jetbrains.annotations.NotNull)1