forked from dpo/atom-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-debugger-view.coffee
More file actions
317 lines (266 loc) · 10 KB
/
python-debugger-view.coffee
File metadata and controls
317 lines (266 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{Point, Disposable, CompositeDisposable} = require "atom"
{$, $$, View, TextEditorView} = require "atom-space-pen-views"
Breakpoint = require "./breakpoint"
BreakpointStore = require "./breakpoint-store"
spawn = require("child_process").spawn
path = require "path"
fs = require "fs"
module.exports =
class PythonDebuggerView extends View
debuggedFileName: null
debuggedFileArgs: []
backendDebuggerPath: null
backendDebuggerName: "atom_pdb.py"
getCurrentFilePath: ->
editor = atom.workspace.getActivePaneItem()
file = editor?.buffer.file
return file?.path
getDebuggerPath: ->
pkgs = atom.packages.getPackageDirPaths()[0]
debuggerPath = path.join(pkgs, "python-debugger", "resources")
return debuggerPath
@content: ->
@div class: "pythonDebuggerView", =>
@subview "argsEntryView", new TextEditorView
mini: true,
placeholderText: "> Enter input arguments here"
@subview "commandEntryView", new TextEditorView
mini: true,
placeholderText: "> Enter debugger commands here"
@button outlet: "breakpointBtn", click: "toggleBreak", class: "btn", =>
@span "break point"
@button class: "btn", =>
@span " "
@button outlet: "runBtn", click: "runApp", class: "btn", =>
@span "run"
@button outlet: "stopBtn", click: "stopApp", class: "btn", =>
@span "stop"
@button class: "btn", =>
@span " "
@button outlet: "stepOverBtn", click: "stepOverBtnPressed", class: "btn", =>
@span "next"
@button outlet: "stepInBtn", click: "stepInBtnPressed", class: "btn", =>
@span "step"
@button outlet: "varBtn", click: "varBtnPressed", class: "btn", =>
@span "variables"
@button class: "btn", =>
@span " "
@button outlet: "returnBtn", click: "returnBtnPressed", class: "btn", =>
@span "return"
@button outlet: "continueBtn", click: "continueBtnPressed", class: "btn", =>
@span "continue"
@button class: "btn", =>
@span " "
@button outlet: "upBtn", click: "upBtnPressed", class: "btn", =>
@span "up"
@button outlet: "callstackBtn", click: "callstackBtnPressed", class: "btn", =>
@span "callstack"
@button outlet: "downBtn", click: "downBtnPressed", class: "btn", =>
@span "down"
@button class: "btn", =>
@span " "
@button outlet: "clearBtn", click: "clearOutput", class: "btn", =>
@span "clear"
@div class: "panel-body", outlet: "outputContainer", =>
@pre class: "command-output", outlet: "output"
toggleBreak: ->
editor = atom.workspace.getActiveTextEditor()
filename = editor.getTitle()
lineNumber = editor.getCursorBufferPosition().row + 1
breakpoint = new Breakpoint(filename, lineNumber)
cmd = @breakpointStore.toggle(breakpoint)
if @backendDebugger
@backendDebugger.stdin.write(cmd + " " + @getCurrentFilePath() + ":" + lineNumber + "\n")
@output.empty()
for breakpoint in @breakpointStore.breakpoints
@output.append(breakpoint.toCommand() + "\n")
upBtnPressed: ->
@output.empty()
@backendDebugger?.stdin.write("up\nbt\n")
callstackBtnPressed: ->
@output.empty()
@backendDebugger?.stdin.write("bt\n")
downBtnPressed: ->
@output.empty()
@backendDebugger?.stdin.write("down\nbt\n")
stepOverBtnPressed: ->
@backendDebugger?.stdin.write("n\n")
stepInBtnPressed: ->
@backendDebugger?.stdin.write("s\n")
continueBtnPressed: ->
@backendDebugger?.stdin.write("c\n")
returnBtnPressed: ->
@backendDebugger?.stdin.write("r\n")
workspacePath: ->
editor = atom.workspace.getActiveTextEditor()
activePath = editor.getPath()
relative = atom.project.relativizePath(activePath)
pathToWorkspace = relative[0] || (path.dirname(activePath) if activePath?)
pathToWorkspace
runApp: ->
@stopApp() if @backendDebugger
@debuggedFileArgs = @getInputArguments()
console.log @debuggedFileArgs
@debuggedFileName = @getCurrentFilePath()
if @pathsNotSet()
@askForPaths()
return
@runBackendDebugger()
varBtnPressed: ->
@output.empty()
@backendDebugger?.stdin.write("for (__k, __v) in [(__k, __v) for __k, __v in globals().items() if not __k.startswith('__')]: print __k, '=', __v\n")
@backendDebugger?.stdin.write("print '-------------'\n")
@backendDebugger?.stdin.write("for (__k, __v) in [(__k, __v) for __k, __v in locals().items() if __k != 'self' and not __k.startswith('__')]: print __k, '=', __v\n")
@backendDebugger?.stdin.write("for (__k, __v) in [(__k, __v) for __k, __v in (self.__dict__ if 'self' in locals().keys() else {}).items()]: print 'self.{0}'.format(__k), '=', __v\n")
# Extract the file name and line number output by the debugger.
processDebuggerOutput: (data) ->
data_str = data.toString().trim()
lineNumber = null
fileName = null
call_stack_str = "Call stack: \n"
m = /[^-]> (.*[.]py)[(]([0-9]*)[)].*/.exec(data_str)
if m
[fileName, lineNumber] = [m[1], m[2]]
`
re = /[\n](>*)[ \t]*(.*[.]py)[(]([0-9]*)[)]([^\n]*)[\n]([^\n]*)/gi;
while ((match = re.exec(data_str)))
{
if (match[1].includes('>'))
call_stack_str += '--> ';
else
call_stack_str += ' ';
call_stack_str += match[5].replace("->", "") + " in " + match[4] + " @ " + match [2] + ": " + match[3] + "\n";
}
`
data_str = call_stack_str
[data_str, tail] = data_str.split("line:: ")
if tail
[lineNumber, tail] = tail.split("\n")
data_str = data_str + tail if tail
[data_str, tail] = data_str.split("file:: ")
if tail
[fileName, tail] = tail.split("\n")
data_str = data_str + tail if tail
fileName = fileName.trim() if fileName
fileName = null if fileName == "<string>"
if lineNumber && fileName
lineNumber = parseInt(lineNumber)
editor = atom.workspace.getActiveTextEditor()
if fileName.toLowerCase() == editor.getPath().toLowerCase()
position = Point(lineNumber-1, 0)
editor.setCursorBufferPosition(position)
editor.unfoldBufferRow(lineNumber)
editor.scrollToBufferPosition(position)
else
options = {initialLine: lineNumber-1, initialColumn:0}
atom.workspace.open(fileName, options) if fs.existsSync(fileName)
# TODO: add decoration to current line?
@addOutput(data_str.trim())
runBackendDebugger: ->
args = [path.join(@backendDebuggerPath, @backendDebuggerName)]
args.push(@debuggedFileName)
args.push(arg) for arg in @debuggedFileArgs
python = atom.config.get "python-debugger.pythonExecutable"
console.log("python-debugger: using", python)
@backendDebugger = spawn python, args
for breakpoint in @breakpointStore.breakpoints
@backendDebugger.stdin.write(breakpoint.toCommand() + "\n")
# Move to first breakpoint if there are any.
if @breakpointStore.breakpoints.length > 0
@backendDebugger.stdin.write("c\n")
@backendDebugger.stdout.on "data", (data) =>
@processDebuggerOutput(data)
@backendDebugger.stderr.on "data", (data) =>
@processDebuggerOutput(data)
@backendDebugger.on "exit", (code) =>
@addOutput("debugger exits with code: " + code.toString().trim()) if code?
stopApp: ->
@backendDebugger?.stdin.write("\nexit()\n")
@backendDebugger = null
console.log "debugger stopped"
clearOutput: ->
@output.empty()
createOutputNode: (text) ->
node = $("<span />").text(text)
parent = $("<span />").append(node)
addOutput: (data) ->
atBottom = @atBottomOfOutput()
node = @createOutputNode(data)
@output.append(node)
@output.append("\n")
if atBottom
@scrollToBottomOfOutput()
pathsNotSet: ->
!@debuggedFileName
askForPaths: ->
@addOutput("To use a different entry point, set file to debug using e=fileName")
initialize: (breakpointStore) ->
@breakpointStore = breakpointStore
@debuggedFileName = @getCurrentFilePath()
@backendDebuggerPath = @getDebuggerPath()
@addOutput("Welcome to Python Debugger for Atom!")
@addOutput("The file being debugged is: " + @debuggedFileName)
@askForPaths()
@subscriptions = atom.commands.add @element,
"core:confirm": (event) =>
if @parseAndSetPaths()
@clearInputText()
else
@confirmBackendDebuggerCommand()
event.stopPropagation()
"core:cancel": (event) =>
@cancelBackendDebuggerCommand()
event.stopPropagation()
parseAndSetPaths:() ->
command = @getCommand()
return false if !command
if /e=(.*)/.test command
match = /e=(.*)/.exec command
@debuggedFileName = match[1]
@addOutput("The file being debugged is: " + @debuggedFileName)
return true
return false
stringIsBlank: (str) ->
!str or /^\s*$/.test str
escapeString: (str) ->
!str or str.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0')
getInputArguments: ->
args = @argsEntryView.getModel().getText()
return if !@stringIsBlank(args) then args.split(" ") else []
getCommand: ->
command = @commandEntryView.getModel().getText()
command if !@stringIsBlank(command)
cancelBackendDebuggerCommand: ->
@commandEntryView.getModel().setText("")
confirmBackendDebuggerCommand: ->
if !@backendDebugger
@addOutput("Program not running")
return
command = @getCommand()
if command
@backendDebugger.stdin.write(command + "\n")
@clearInputText()
clearInputText: ->
@commandEntryView.getModel().setText("")
serialize: ->
attached: @panel?.isVisible()
destroy: ->
@detach()
toggle: ->
if @panel?.isVisible()
@detach()
else
@attach()
atBottomOfOutput: ->
@output[0].scrollHeight <= @output.scrollTop() + @output.outerHeight()
scrollToBottomOfOutput: ->
@output.scrollToBottom()
attach: ->
console.log "attached"
@panel = atom.workspace.addBottomPanel(item: this)
@panel.show()
@scrollToBottomOfOutput()
detach: ->
console.log "detached"
@panel.destroy()
@panel = null