public void evaluate()

in flutter-idea/src/io/flutter/vmService/frame/DartVmServiceEvaluator.java [48:160]


  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<>();
    // Turn off pausing on exceptions as it is confusing to mouse over an expression
    // 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);
      }
    });
  }