-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpython-debugger-view.coffee
More file actions
462 lines (400 loc) · 14.9 KB
/
python-debugger-view.coffee
File metadata and controls
462 lines (400 loc) · 14.9 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
{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"
flagActionStarted: false
flagVarsNeedUpdate: false
flagCallstackNeedsUpdate: false
# 0 - normal output, 1 - print variables, 2 - print call stack
currentState: 0
varScrollTop: 0
callStackScrollTop: 0
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"
@div class: "btn-toolbar", =>
@div class: "btn-group", =>
@button outlet: "breakpointBtn", click: "toggleBreak", class: "btn", =>
@span "break point"
@div class: "btn-group", =>
@button outlet: "runBtn", click: "runApp", class: "btn", =>
@span "run"
@button outlet: "stopBtn", click: "stopApp", class: "btn", =>
@span "stop"
@div class: "btn-group", =>
@button outlet: "stepOverBtn", click: "stepOverBtnPressed", class: "btn", =>
@span "next"
@button outlet: "stepInBtn", click: "stepInBtnPressed", class: "btn", =>
@span "step"
@button outlet: "returnBtn", click: "returnBtnPressed", class: "btn", =>
@span "return"
@button outlet: "continueBtn", click: "continueBtnPressed", class: "btn", =>
@span "continue"
@div class: "btn-group", =>
@button outlet: "upBtn", click: "upBtnPressed", class: "btn", =>
@span "up"
@button outlet: "downBtn", click: "downBtnPressed", class: "btn", =>
@span "down"
@div class: "btn-group", =>
@button outlet: "clearBtn", click: "clearOutput", class: "btn", =>
@span "clear"
@input class : "input-checkbox", type: "checkbox", id: "ck_input", outlet: "showInput", click: "toggleInput"
@label class : "label", for: "ck_input", =>
@span "Input"
@input class : "input-checkbox", type: "checkbox", id: "ck_vars", outlet: "showVars", click: "toggleVars"
@label class : "label", for: "ck_vars", =>
@span "Variables"
@input class : "input-checkbox", type: "checkbox", id: "ck_callstack", outlet: "showCallstack", click: "toggleCallstack"
@label class : "label", for: "ck_callstack", =>
@span "Call stack"
@div class: "block", outlet: "bottomPane", =>
@div class: "inline-block panel", id: "outputPane", outlet: "outputPane", =>
@pre class: "command-output", outlet: "output"
@div class: "inline-block panel", id: "variablesPane", outlet: "variablesPane", =>
@pre class: "command-output", outlet: "variables"
@div class: "inline-block panel", id: "callstackPane", outlet: "callstackPane", =>
@pre class: "command-output", outlet: "callstack"
toggleInput: ->
if @backendDebugger
@argsEntryView.hide()
if @showInput.prop('checked')
@commandEntryView.show()
else
@commandEntryView.hide()
else
if @showInput.prop('checked')
@argsEntryView.show()
else
@argsEntryView.hide()
@commandEntryView.hide()
toggleVars: ->
@togglePanes()
toggleCallstack: ->
@togglePanes()
togglePanes: ->
n = 1
if @showVars.prop('checked')
@variablesPane.show()
n = n+1
else
@variablesPane.hide()
if @showCallstack.prop('checked')
@callstackPane.show()
n = n+1
else
@callstackPane.hide()
width = ''+(100/n)+'%'
@outputPane.css('width', width)
if @showVars.prop('checked')
@variablesPane.css('width', width)
if @showCallstack.prop('checked')
@callstackPane.css('width', width)
# the following statements are used to update the information in the variables/callstack
@setFlags()
@backendDebugger?.stdin.write("print 'display option changed.'\n")
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")
stepOverBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("n\n")
stepInBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("s\n")
continueBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("c\n")
returnBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("r\n")
upBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("up\n")
downBtnPressed: ->
@setFlags()
@backendDebugger?.stdin.write("down\n")
printVars: ->
@variables.empty()
@backendDebugger?.stdin.write("print ('@{variables_start}')\n")
@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")
@backendDebugger?.stdin.write("print ('@{variables_end}')\n")
printCallstack: ->
@callstack.empty()
@backendDebugger?.stdin.write("print ('@{callstack_start}')\n")
@backendDebugger?.stdin.write("bt\n")
@backendDebugger?.stdin.write("print ('@{callstack_end}')\n")
setFlags: ->
@flagActionStarted = true
if @showVars.prop('checked')
@varScrollTop = @variables.prop('scrollTop')
@flagVarsNeedUpdate = true
if @showCallstack.prop('checked')
@callStackScrollTop = @callstack.prop('scrollTop')
@flagCallstackNeedsUpdate = true
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
@setFlags()
@runBackendDebugger()
@toggleInput()
highlightLineInEditor: (fileName, lineNumber) ->
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?
processNormalOutput: (data_str) ->
lineNumber = null
fileName = null
# print the action_end string
if @flagActionStarted
@backendDebugger?.stdin.write("print ('@{action_end}')\n")
@flagActionStarted = false
# detect predefined flag strings
isActionEnd = data_str.includes('@{action_end}')
isVarsStart = data_str.includes('@{variables_start}')
isCallstackStart = data_str.includes('@{callstack_start}')
# variables print started
if isVarsStart
@currentState = 1
@processVariables(data_str)
return
# call stack print started
if isCallstackStart
@currentState = 2
@processCallstack(data_str)
return
# handle normal output
[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>"
# highlight the current line
if lineNumber && fileName
@highlightLineInEditor(fileName, lineNumber)
# print the output
@addOutput(data_str.trim().replace('@{action_end}', ''))
# if action end, trigger the follow up actions
if isActionEnd
if @flagVarsNeedUpdate
@printVars()
@flagVarsNeedUpdate = false
else
if @flagCallstackNeedsUpdate
@printCallstack()
@flagCallstackNeedsUpdate = false
processVariables: (data_str) ->
isVarsEnd = data_str.includes('@{variables_end}')
for line in data_str.split '\n'
if ! line.includes("@{variable")
@variables.append(@createOutputNode(line))
@variables.append('\n')
if isVarsEnd
@variables.prop('scrollTop', @varScrollTop)
@currentState = 0
if @flagCallstackNeedsUpdate
@printCallstack()
@flagCallstackNeedsUpdate = false
processCallstack: (data_str) ->
lineNumber = null
fileName = null
isCallstackEnd = data_str.includes('@{callstack_end}')
m = /[^-]> (.*[.]py)[(]([0-9]*)[)].*/.exec(data_str)
if m
[fileName, lineNumber] = [m[1], m[2]]
callstack_pre = @callstack
`
re = /[\n](>*)[ \t]*(.*[.]py)[(]([0-9]*)[)]([^\n]*)[\n]([^\n]*)/gi;
while ((match = re.exec(data_str)))
{
if (match[5].includes('exec cmd in globals, locals')) continue;
if (match[1].includes('>'))
item = "<b><u>"+match[5].replace("->", "")+"</u></b>";
else
item = match[5].replace("->", "");
callstack_pre.append(item);
callstack_pre.append('\n');
}
`
if lineNumber && fileName
@highlightLineInEditor(fileName, lineNumber)
if isCallstackEnd
@currentState = 0
@callstack.prop('scrollTop', @callStackScrollTop)
# Extract the file name and line number output by the debugger.
processDebuggerOutput: (data) ->
data_str = data.toString().trim()
if @currentState == 1
@processVariables(data_str)
else if @currentState == 2
@processCallstack(data_str)
else
@processNormalOutput(data_str)
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"
@toggleInput()
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()
@toggleInput()
@togglePanes()
@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