package com.jetbrains.python.debugger;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


public class PyDebuggerEditorsProvider extends XDebuggerEditorsProvider {

  @NotNull
  @Override
  public FileType getFileType() {
    return PythonFileType.INSTANCE;
  }

  @NotNull
  @Override
  public Document createDocument(@NotNull final Project project, @NotNull String text, @Nullable final XSourcePosition sourcePosition) {
    text = text.trim();
    final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(project, "fragment.py", text, true);

    /*
    final PsiElement element = getContextElement(project, sourcePosition);
    System.out.println("element:" + element);
    fragment.setContext(element);
    */

    // todo: bind to context
    return PsiDocumentManager.getInstance(project).getDocument(fragment);
  }

  /*
  @Nullable
  private static PsiElement getContextElement(Project project, XSourcePosition sourcePosition) {
    if (sourcePosition != null) {
      final Document document = FileDocumentManager.getInstance().getDocument(sourcePosition.getFile());
      final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
      if (psiFile != null) {
        int offset = sourcePosition.getOffset();
        final int lineEndOffset = document.getLineEndOffset(document.getLineNumber(offset));
        do {
          PsiElement element = psiFile.findElementAt(offset);
          if (element != null && !(element instanceof PsiWhiteSpace || element instanceof PsiComment)) {
            PsiElement e = PsiTreeUtil.getParentOfType(element, PyElement.class);
            while (e != null) {
              if (e instanceof PyPrintStatement) {
                return e;
              }
              e = e.getParent();
            }
            //return RControlFlowBuilder.getControlFlowNodeElement(element);
          }
          offset = element.getTextRange().getEndOffset() + 1;
        }
        while (offset < lineEndOffset);
      }
    }

    return null;
  }
  */

}

@@ -78,16 +78,76 @@
 cument;%0A
+import com.intellij.openapi.fileEditor.FileDocumentManager;%0A
 import c
@@ -255,34 +255,17 @@
 lij.psi.
-PsiDocumentManager
+*
 ;%0Aimport
@@ -483,16 +483,65 @@
 ntImpl;%0A
+import com.jetbrains.python.psi.impl.PyPsiUtils;%0A
 import o
@@ -1102,17 +1102,33 @@
 ;%0A%0A    /
-*
+/ Bind to context
 %0A    fin
@@ -1203,120 +1203,38 @@
     
-System.out.println(%22element:%22 + element);%0A    fragment.setContext(element);%0A    */%0A%0A    // todo: bind to context
+fragment.setContext(element);%0A
 %0A   
@@ -1313,13 +1313,8 @@
  %7D%0A%0A
-  /*%0A
   @N
@@ -1367,16 +1367,22 @@
 Element(
+final 
 Project 
@@ -2022,311 +2022,33 @@
     
-PsiElement e = PsiTreeUtil.getParentOfType(element, PyElement.class);%0A            while (e != null) %7B%0A              if (e instanceof PyPrintStatement) %7B%0A                return e;%0A              %7D%0A              e = e.getParent();%0A            %7D%0A            //return RControlFlowBuilder.getControlFlowNodeEl
+return PyPsiUtils.getStat
 emen
@@ -2197,17 +2197,16 @@
 %7D%0A    %7D%0A
-%0A
     retu
@@ -2222,12 +2222,6 @@
   %7D%0A
-  */%0A%0A
 %7D%0A

Make PyExpressionCodeFragment context aware