EDU-475 Step over in debugger doesn't work for unresolved reference
[idea/community.git] / python / educational-python / src / com / jetbrains / python / edu / debugger / PyEduDebugProcess.java
1 package com.jetbrains.python.edu.debugger;
2
3 import com.google.common.base.Predicate;
4 import com.google.common.collect.Collections2;
5 import com.intellij.execution.process.ProcessHandler;
6 import com.intellij.execution.ui.ExecutionConsole;
7 import com.intellij.openapi.util.io.FileUtil;
8 import com.intellij.xdebugger.XDebugSession;
9 import com.jetbrains.python.PythonHelpersLocator;
10 import com.jetbrains.python.debugger.*;
11 import org.jetbrains.annotations.NotNull;
12 import org.jetbrains.annotations.Nullable;
13
14 import java.net.ServerSocket;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19
20 class PyEduDebugProcess extends PyDebugProcess {
21
22   private final String myScriptName;
23   private final int myLine;
24
25   public PyEduDebugProcess(@NotNull XDebugSession session,
26                            @NotNull ServerSocket serverSocket,
27                            @NotNull ExecutionConsole executionConsole,
28                            @Nullable ProcessHandler processHandler, boolean multiProcess,
29                            String scriptName,
30                            int line) {
31     super(session, serverSocket, executionConsole, processHandler, multiProcess);
32     myScriptName = scriptName;
33     myLine = line;
34   }
35
36   @Override
37   public PyStackFrame createStackFrame(PyStackFrameInfo frameInfo) {
38     return new PyEduStackFrame(getSession().getProject(), this, frameInfo,
39                                getPositionConverter().convertFromPython(frameInfo.getPosition()));
40   }
41
42   @Override
43   public void init() {
44     super.init();
45     addTemporaryBreakpoint(PyLineBreakpointType.ID, myScriptName, myLine);
46   }
47
48   @NotNull
49   @Override
50   protected PySuspendContext createSuspendContext(PyThreadInfo threadInfo) {
51     threadInfo.updateState(threadInfo.getState(), new ArrayList<PyStackFrameInfo>(filterFrames(threadInfo.getFrames())));
52     return new PySuspendContext(this, threadInfo);
53   }
54
55   public Collection<PyStackFrameInfo> filterFrames(@Nullable List<PyStackFrameInfo> frames) {
56     if (frames == null) {
57       return Collections.emptyList();
58     }
59     final String helpersPath = PythonHelpersLocator.getHelpersRoot().getPath();
60     Collection<PyStackFrameInfo> filteredFrames = Collections2.filter(frames, new Predicate<PyStackFrameInfo>() {
61       @Override
62       public boolean apply(PyStackFrameInfo frame) {
63         String file = frame.getPosition().getFile();
64         return !FileUtil.isAncestor(helpersPath, file, false);
65       }
66     });
67     return !filteredFrames.isEmpty() ? filteredFrames : frames;
68   }
69 }