-
Notifications
You must be signed in to change notification settings - Fork 92
/
swi.py
1489 lines (1143 loc) · 55 KB
/
swi.py
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hashlib
import functools
import glob
import sublime
import sublime_plugin
import urllib.request, urllib.parse, urllib.error
import threading
import json
import types
import os
import re
import time
import sys
import imp
import re
import logging
swi_folder = os.path.dirname(os.path.realpath(__file__))
if not swi_folder in sys.path:
sys.path.append(swi_folder)
import utils
import webkit
import projectsystem
import protocol
import views
import styles
from webkit import Console
from webkit import Runtime
from webkit import Debugger
from webkit import Network
from webkit import Page
from projectsystem import DocumentMapping
imp.reload(sys.modules['webkit.wkutils'])
imp.reload(sys.modules['webkit.Console'])
imp.reload(sys.modules['webkit.Runtime'])
imp.reload(sys.modules['webkit.Debugger'])
imp.reload(sys.modules['webkit.Network'])
imp.reload(sys.modules['webkit.Page'])
brk_object = {}
channel = None
original_layout = None
window = None
file_to_scriptId = []
paused = False
current_line = None
set_script_source = False
current_call_frame = None
current_call_frame_position = None
source_map_state = None
hostname = utils.get_setting('hostname', 'localhost')
port = str(utils.get_setting('chrome_remote_port'))
breakpoint_active_icon = 'Packages/Web Inspector/icons/breakpoint_active.png'
breakpoint_inactive_icon = 'Packages/Web Inspector/icons/breakpoint_inactive.png'
breakpoint_current_icon = 'Packages/Web Inspector/icons/breakpoint_current.png'
logger = logging.getLogger("SWI")
logger.propagate = False
def plugin_loaded():
if not logger.handlers and utils.get_setting('debug_mode'):
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
close_all_our_windows()
clear_all_views()
####################################################################################
# COMMANDS
####################################################################################
class SwiDebugCommand(sublime_plugin.WindowCommand):
""" The SWIdebug main quick panel menu
"""
def run(self):
""" Called by Sublime to display the quick panel entries """
mapping = []
if chrome_launched():
spacer = " " * 7
if paused:
mapping.append(['swi_debug_step_into', 'Step in' + spacer + ' F11'])
mapping.append(['swi_debug_step_out', 'Step out' + spacer + ' Shift+F11'])
mapping.append(['swi_debug_step_over', 'Step over' + spacer + 'F10'])
mapping.append(['swi_debug_pause_resume', 'Resume' + spacer + ' F8'])
elif channel:
mapping.append(['swi_debug_pause_resume', 'Pause' + spacer + ' F8'])
if channel:
mapping.append(['swi_debug_evaluate', 'Evaluate selection'])
mapping.append(['swi_debug_clear_console', 'Clear console'])
mapping.append(['swi_debug_stop', 'Stop debugging'])
mapping.append(['swi_debug_reload', 'Reload page'])
else:
mapping.append(['swi_debug_start', 'Start debugging'])
mapping.append(['swi_debug_toggle_breakpoint', 'Toggle Breakpoint'])
if channel:
if is_source_map_enabled:
mapping.append(['swi_toggle_authored_code', 'Toggle authored code'])
mapping.append(['swi_debug_clear_breakpoints', 'Clear all Breakpoints'])
mapping.append(['swi_dump_file_mappings', 'Dump file mappings'])
else:
mapping.append(['swi_debug_start_chrome', 'Start Google Chrome with remote debug port ' + port])
self.cmds = [entry[0] for entry in mapping]
self.items = [entry[1] for entry in mapping]
self.window.show_quick_panel(self.items, self.command_selected)
def command_selected(self, index):
""" Called by Sublime when a quick panel entry is selected """
utils.assert_main_thread()
if index == -1:
return
command = self.cmds[index]
if command == 'swi_dump_file_mappings':
# we wrap this command so we can use the correct view
print(command)
v = views.find_or_create_view('mapping')
v.run_command('swi_dump_file_mappings_internal')
return
self.window.run_command(command)
def chrome_launched():
if channel:
return True
try:
proxy = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
urllib.request.urlopen('http://' + hostname + ':' + port + '/json')
return True
except:
pass
return False
class SwiDebugStartChromeCommand(sublime_plugin.WindowCommand):
""" Represents the start chrome command """
def run(self):
utils.assert_main_thread()
window = sublime.active_window()
key = sublime.platform()
# sublime.arch() is x86 on x64 Windows, presumably because it's a 32 bit app
if key == "windows" and (sublime.arch() == "x64" or sublime.executable_path().find('(x86)') >= 0):
key += "_x64"
cmd = [os.getenv('GOOGLE_CHROME_PATH', '') + utils.get_setting('chrome_path')[key], '--remote-debugging-port=' + port]
profile = utils.get_setting('chrome_profile') or ''
if profile:
profile = '--user-data-dir=' + profile
cmd.append(profile)
url = utils.get_setting('chrome_url') or ''
cmd.append(url)
self.window.run_command('exec', {
"cmd": cmd
})
class SwiDebugStartCommand(sublime_plugin.WindowCommand):
""" Connect to the socket. """
def run(self):
utils.assert_main_thread()
proxy = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('http://' + hostname + ':' + port + '/json')
pages = json.loads(response.read().decode('utf-8'))
mapping = {}
for page in pages:
if 'webSocketDebuggerUrl' in page:
url = page['url']
if (url.find('chrome-extension://') == 0 or
url.find('about:blank') == 0 or
url.find('res:') == 0 or
url.find('x-mvwit:') == 0 or # seen this on a Windows machine
url.lower().find('windows/inetcache') != -1): # some Outlook goo exposed by IE adapter
continue
mapping[page['webSocketDebuggerUrl']] = page['url']
self.urls = list(mapping.keys())
items = list(mapping.values())
if len(self.urls) == 0:
print('No urls proferred by debuggee. Cannot start debugging')
elif len(self.urls) == 1:
# just one URL - pick it automatically
print('Connecting to ' + str(items[0]) + " as it's the only URL offered by the debuggee")
self.remote_debug_url_selected(0)
else:
self.window.show_quick_panel(items, self.remote_debug_url_selected)
def remote_debug_url_selected(self, index):
utils.assert_main_thread()
if index == -1:
return
url = self.urls[index]
global window
window = sublime.active_window()
global original_layout
original_layout = window.get_layout()
window.set_layout(utils.get_setting('console_layout'))
load_breaks()
global debugger_enabled
debugger_enabled = False
global file_to_scriptId
file_to_scriptId = []
# Look in the folders opened in Sublime first
folders = [s.lower() for s in self.window.folders()]
[logger.info('====Currently open folder is %s, will search there' % s) for s in folders]
# Then also look at the folders containing currently open files
for v in window.views():
file = v.file_name()
if file and os.path.isfile(file):
dir = os.path.dirname(file).lower()
logger.info('====Currently open file is in folder %s, will search there' % dir)
folders.append(dir)
# Remove redundant folders. Eg., c:\a\b would be redundant if we also have c:\a
folders.sort(key=len) # now c:\a is before (and adjacent to) c:\a\b
for i, s in reversed(list(enumerate(folders))): # go from long to short entries
if i - 1 >= 0:
if folders[i].find(folders[i-1]) == 0: # if current contains (or matches) immediately previous
folders[i] = None # remove current
folders = [s for s in folders if s and len(s) > 3] # filter out removed entries, and drive roots like "c:\" (we're going to recurse..)
[logger.info('====Using folder %s' % s) for s in folders]
self.project_folders = folders
self.url = url
global channel
if channel:
print ('SWI: Socket closed')
channel.socket.close()
else:
channel = protocol.Protocol()
channel.connect(self.url, self.connected, self.disconnected)
global set_script_source
set_script_source = utils.get_setting('set_script_source')
def connected(self):
""" Callback when socket connects """
utils.assert_main_thread()
channel.subscribe(webkit.Console.messageAdded(), self.messageAdded)
channel.subscribe(webkit.Console.messageRepeatCountUpdated(), self.messageRepeatCountUpdated)
channel.subscribe(webkit.Console.messagesCleared(), self.messagesCleared)
channel.subscribe(webkit.Debugger.scriptParsed(), self.scriptParsed)
channel.subscribe(webkit.Debugger.paused(), self.paused)
channel.subscribe(webkit.Debugger.resumed(), self.resumed)
channel.subscribe(webkit.Debugger.globalObjectCleared(), self.globalObjectCleared)
channel.send(webkit.Debugger.enable(), self.enabled)
channel.send(webkit.Debugger.setPauseOnExceptions(utils.get_setting('pause_on_exceptions')))
channel.send(webkit.Console.enable())
channel.send(webkit.Debugger.canSetScriptSource(), self.canSetScriptSource)
#self.window.run_command('swi_styles_window')
if utils.get_setting('user_agent') is not "":
channel.send(webkit.Network.setUserAgentOverride(utils.get_setting('user_agent')))
if utils.get_setting('reload_on_start'):
channel.send(webkit.Network.clearBrowserCache())
channel.send(webkit.Page.reload(), on_reload)
def disconnected(self):
""" Notification when socket disconnects """
utils.assert_main_thread()
self.window.run_command('swi_debug_stop')
def messageAdded(self, data, notification):
""" Notification when console message """
utils.assert_main_thread()
console_add_message(data)
def messageRepeatCountUpdated(self, data, notification):
""" Notification when repeated messages """
utils.assert_main_thread()
console_repeat_message(data['count'])
def messagesCleared(self, data, notification):
""" Notification when console cleared (by navigate or on request) """
utils.assert_main_thread()
views.clear_view('console')
# build table of mappings from local to server
def scriptParsed(self, data, notification):
""" Notification when a script is parsed (loaded).
Attempts to map it to a local file.
"""
utils.assert_main_thread()
url = data['url']
if url != '':
url_parts = url.split("/")
url_parts = list(filter(None, url_parts)) # remove empty entries so we have eg ['http:', 'foo.com', 'bar', 'baz.biz']
scriptId = str(data['scriptId'])
file_name = ''
script = get_script(data['url'])
logger.info('====Notified of url %s====' % url)
if script:
if int(scriptId) > int(script['scriptId']):
script['scriptId'] = str(scriptId)
file_name = script['file']
# Create a file mapping to look for mapped source code
projectsystem.DocumentMapping.MappingsManager.create_mapping(file_name)
else:
del url_parts[0:2] # remove protocol and domain
while len(url_parts) > 0:
for folder in self.project_folders:
if sublime.platform() == "windows":
# eg., folder is c:\site and url is http://localhost/app.js
# glob for c:\site\app.js (primary) and c:\site\*\app.js (fallback only - there may be a c:\site\foo\app.js)
try:
glob1 = folder + "\\" + "\\".join(url_parts)
if os.path.exists(glob1) and os.path.isfile(glob1): # literal match (url is directly relative to project folder root)
files = [ glob1 ]
else:
glob2 = folder + "\\*\\" + "\\".join(url_parts)
logger.info(' Glob in %s and %s' % (glob1, glob2))
files = glob.glob(glob1) + glob.glob(glob2)
except:
pass
else:
glob1 = folder + "/" + "/".join(url_parts)
glob2 = folder + "/*/" + "/".join(url_parts)
logger.info(' Glob in %s and %s' % (glob1, glob2))
files = glob.glob(glob1) + glob.glob(glob2)
if len(files) > 0 and files[0] != '':
file_name = files[0]
if (file_name):
logger.info(' Matched %s' % file_name)
# Create a file mapping to look for mapped source code
projectsystem.DocumentMapping.MappingsManager.create_mapping(file_name)
file_to_scriptId.append({'file': file_name, 'scriptId': str(scriptId), 'url': data['url']})
# don't try to match shorter fragments, we already found a match
url_parts = []
break
if len(url_parts) > 0:
del url_parts[0]
if not file_name:
logger.info(' Found no local match')
if debugger_enabled and file_name:
self.add_breakpoints_to_file(file_name)
def paused(self, data, notification):
""" Notification that a break was hit.
Draw an overlay, display the callstack
and locals, and navigate to the break.
"""
utils.assert_main_thread()
global paused
paused = True
update_stack(data)
def resumed(self, data, notification):
""" Notification that execution resumed.
Clear the overlay, callstack, and locals,
and remove the highlight.
"""
utils.assert_main_thread()
views.clear_view('stack')
views.clear_view('scope')
views.clear_view('styles')
channel.send(webkit.Debugger.setOverlayMessage())
global current_file
current_file = None
global current_line
current_line = None
global current_call_frame
current_call_frame = None
global current_call_frame_position
current_call_frame_position = None
global paused
paused = False
update_overlays()
def globalObjectCleared(self, data, notification):
projectsystem.DocumentMapping.MappingsManager.delete_all_mappings()
def enabled(self, command):
""" Notification that debugging was enabled """
utils.assert_main_thread()
global debugger_enabled
debugger_enabled = True
for file_to_script_object in file_to_scriptId:
self.add_breakpoints_to_file(file_to_script_object['file'])
def add_breakpoints_to_file(self, file):
""" Apply any existing breakpoints.
Called when debugging starts, and when a new script
is loaded.
"""
if not file:
return
found_authored_file = False
scriptId = find_script(file)
# Authored files preferred
if is_source_map_enabled():
mapping = projectsystem.DocumentMapping.MappingsManager.get_mapping(file)
authored_files = mapping.get_authored_files()
if authored_files:
found_authored_file = True
for file_name in authored_files:
breakpoints = get_breakpoints_by_full_path(file_name)
if breakpoints:
for line in list(breakpoints.keys()):
column = 0
if 'column' in breakpoints[line] and int(breakpoints[line]['column']) >= 0:
column = int(breakpoints[line]['column'])
position = mapping.get_generated_position(file_name, int(line), column)
if position:
location = webkit.Debugger.Location({'lineNumber': position.zero_based_line(), 'columnNumber': position.zero_based_column(), 'scriptId': scriptId})
params = {'authoredLocation': { 'lineNumber': line, 'columnNumber': column, 'file': file_name }}
logger.info('Setting breakpoint in %s at %s,%s mapped from %s at %s,%s' % (scriptId, position.file_name, location.lineNumber, location.columnNumber, file_name, line, column))
channel.send(webkit.Debugger.setBreakpoint(location), self.breakpointAdded, params)
# Fall back to raw file
if not found_authored_file:
breakpoints = get_breakpoints_by_full_path(file)
if breakpoints:
for line in list(breakpoints.keys()):
column = 0
if 'column' in breakpoints[line] and int(breakpoints[line]['column']) >= 0:
column = int(breakpoints[line]['column'])
location = webkit.Debugger.Location({'lineNumber': int(line), 'columnNumber': int(column), 'scriptId': scriptId})
logger.info('Setting breakpoint in %s %s at %s,%s' % (scriptId, file, line, column))
channel.send(webkit.Debugger.setBreakpoint(location), self.breakpointAdded)
def updateAuthoredDocument(self, command):
save_breaks()
update_overlays()
def breakpointAdded(self, command):
""" Notification that a breakpoint was set.
Gives us the ID and specific location.
"""
utils.assert_main_thread()
breakpointId = command.data['breakpointId']
scriptId = command.data['actualLocation'].scriptId
file = find_script(str(scriptId))
lineNumberActual = str(command.data['actualLocation'].lineNumber)
columnNumberActual = str(command.data['actualLocation'].columnNumber)
# we persist in terms of the authored file if any, so translate
positionActual = get_authored_position_if_necessary(file, lineNumberActual, columnNumberActual)
if positionActual:
file = positionActual.file_name()
lineNumberActual = str(positionActual.zero_based_line())
columnNumberActual = str(positionActual.zero_based_column())
# the breakpoint might have been set before debugging starts
# in that case it might be stored under a different line number
# to the true actual number we get back. check both.
lineNumberSent = str(command.params['location']['lineNumber'])
columnNumberSent = str(command.params['location']['columnNumber'])
# again, prefer the authored location.
# we could use the source maps again to get it, but those don't
# always round trip to the original location. instead, pass them through
if command.options and 'authoredLocation' in command.options:
assert(file == command.options['authoredLocation']['file'])
lineNumberSent = str(command.options['authoredLocation']['lineNumber'])
columnNumberSent = str(command.options['authoredLocation']['columnNumber'])
breakpoints = get_breakpoints_by_full_path(file)
# delete anything persisted under the original line number and
# move it to the correct line number
if lineNumberSent != lineNumberActual:
if lineNumberSent in breakpoints:
breakpoints[lineNumberActual] = breakpoints[lineNumberSent].copy()
del breakpoints[lineNumberSent]
# finish updating the persisted breakpoint
breakpoints[lineNumberActual]['status'] = 'enabled'
breakpoints[lineNumberActual]['breakpointId'] = str(breakpointId)
save_breaks()
update_overlays()
def canSetScriptSource(self, command):
""" Notification that script can be edited
during debugging
"""
utils.assert_main_thread()
global set_script_source
set_script_source = False
if 'result' in command.data:
set_script_source = command.data['result']
class SwiDebugPauseResumeCommand(sublime_plugin.WindowCommand):
def run(self):
utils.assert_main_thread()
# As a convenience, we'll set up the connection
# if there isn't one. So F5 (etc) can be hit
# to get started.
if not channel:
if not chrome_launched():
SwiDebugStartChromeCommand.run(self)
else:
self.window.run_command('swi_debug_start')
elif paused:
logger.info('Resuming...')
channel.send(webkit.Debugger.resume())
else:
logger.info('Pausing...')
channel.send(webkit.Debugger.setSkipAllPauses(False))
channel.send(webkit.Debugger.pause())
class SwiDebugStepIntoCommand(sublime_plugin.WindowCommand):
def run(self):
if paused:
channel.send(webkit.Debugger.stepInto())
class SwiDebugStepOutCommand(sublime_plugin.WindowCommand):
def run(self):
if paused:
channel.send(webkit.Debugger.stepOut())
class SwiDebugStepOverCommand(sublime_plugin.WindowCommand):
def run(self):
if paused:
channel.send(webkit.Debugger.stepOver())
class SwiDebugClearConsoleCommand(sublime_plugin.WindowCommand):
def run(self):
views.clear_view('console')
class SwiDebugEvaluateCommand(sublime_plugin.WindowCommand):
def run(self):
utils.assert_main_thread()
active_view = self.window.active_view()
regions = active_view.sel()
for i in range(len(regions)):
title = active_view.substr(regions[i])
if paused:
if current_call_frame_position:
title = "%s on %s" % (active_view.substr(regions[i]), current_call_frame_position)
channel.send(webkit.Debugger.evaluateOnCallFrame(current_call_frame, active_view.substr(regions[i])), self.evaluated, {'name': title})
else:
channel.send(webkit.Runtime.evaluate(active_view.substr(regions[i])), self.evaluated, {'name': title})
def evaluated(self, command):
if command.data.type == 'object':
channel.send(webkit.Runtime.getProperties(command.data.objectId, True), console_add_properties, command.options)
else:
console_add_evaluate(command.data)
class SwiDebugClearBreakpointsCommand(sublime_plugin.WindowCommand):
def run(self):
# we choose to remove breakpoints only for active files, so not for unrelated sites
# so we need to be debugging a site
for file_to_script_object in file_to_scriptId:
file_name = file_to_script_object['file']
breaks = get_breakpoints_by_full_path(file_name)
if breaks:
for row in breaks:
if 'breakpointId' in breaks[row]:
channel.send(webkit.Debugger.removeBreakpoint(breaks[row]['breakpointId']))
del brk_object[file_name.lower()];
save_breaks()
update_overlays()
class SwiDebugToggleBreakpointCommand(sublime_plugin.WindowCommand):
def run(self):
utils.assert_main_thread()
active_view = self.window.active_view()
v = views.wrap_view(active_view)
view_name = v.file_name()
if not view_name: # eg file mapping pane
return
row = str(v.rows(v.lines())[0])
init_breakpoint_for_file(view_name)
breaks = get_breakpoints_by_full_path(view_name)
if row in breaks:
if channel:
if row in breaks:
try:
logger.info('Removing breakpoint in %s at %s' % (view_name, row))
channel.send(webkit.Debugger.removeBreakpoint(breaks[row]['breakpointId']))
except KeyError:
print("SWI: A key error occurred while removing the breakpoint")
del_breakpoint_by_full_path(view_name, row)
else:
if channel:
scriptUrl = ''
if projectsystem.DocumentMapping.MappingsManager.is_authored_file(view_name):
mapping = projectsystem.DocumentMapping.MappingsManager.get_mapping(view_name)
sel = active_view.sel()[0]
start = active_view.rowcol(sel.begin())
position = mapping.get_generated_position(view_name, start[0], start[1])
scriptUrl = find_script_url(position.file_name())
row = str(position.zero_based_line())
if not scriptUrl:
scriptUrl = find_script_url(view_name)
if scriptUrl:
logger.info('Setting breakpoint by url for %s at %s' % (scriptUrl, row))
channel.send(webkit.Debugger.setBreakpointByUrl(int(row), scriptUrl), self.breakpointAdded, view_name)
else:
logger.info('Pending breakpoint for %s at %s' % (view_name, row))
record_breakpoint_by_full_path(view_name, row)
update_overlays()
def breakpointAdded(self, command):
""" Notification that a breakpoint was added successfully """
utils.assert_main_thread()
active_view = self.window.active_view()
breakpointId = command.data['breakpointId']
init_breakpoint_for_file(command.options)
locations = command.data['locations']
for location in locations:
scriptId = location.scriptId
lineNumber = location.lineNumber
columnNumber = location.columnNumber
file_name = find_script(str(scriptId))
if projectsystem.DocumentMapping.MappingsManager.is_generated_file(file_name):
position = get_authored_position_if_necessary(file_name, lineNumber, columnNumber)
if position:
lineNumber = position.zero_based_line()
columnNumber = position.zero_based_column()
file_name = position.file_name()
init_breakpoint_for_file(file_name)
# If this breakpoint is in TS file, then store the column number as well for future restoration
if projectsystem.DocumentMapping.MappingsManager.is_generated_file(file_name):
record_breakpoint_by_full_path(file_name, str(lineNumber), -1, 'enabled', breakpointId)
else:
record_breakpoint_by_full_path(file_name, str(lineNumber), columnNumber, 'enabled', breakpointId)
logger.info('Breakpoint set in %s %s at (%s,%s)' % (scriptId, file_name, lineNumber, columnNumber))
update_overlays()
class SwiDebugStopCommand(sublime_plugin.WindowCommand):
def run(self):
active_view = self.window.active_view()
close_all_our_windows()
clear_all_views()
disable_all_breakpoints()
global paused
paused = False
global debugger_enabled
debugger_enabled = False
global current_file
current_file = None
global current_line
current_line = None
update_overlays()
global channel
if channel:
try:
channel.socket.close()
except:
print ('SWI: Can\'t close socket')
finally:
channel = None
class SwiDebugReloadCommand(sublime_plugin.WindowCommand):
def run(self):
if channel:
logger.info('Reloading page')
channel.send(webkit.Network.clearBrowserCache())
channel.send(webkit.Page.reload(), on_reload)
class SwiDumpFileMappingsInternalCommand(sublime_plugin.TextCommand):
""" Called internally on the file mapping view """
def run(self, edit):
views.clear_view('mapping')
dump = lambda obj: json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
text = "File to URL mappings:\n\n"
text += dump(file_to_scriptId) + "\n\n"
text += "Authored to source mappings:\n\n"
text += dump(projectsystem.DocumentMapping.MappingsManager.get_all_source_file_mappings()) + "\n\n"
text += "Breakpoints:\n\n"
text += dump(brk_object) + "\n\n"
self.view.insert(edit, 0, text)
class SwiToggleAuthoredCodeCommand(sublime_plugin.TextCommand):
""" This is a TextCommand because it specifically applies only
to the active view
"""
def run(self, edit):
utils.assert_main_thread()
view = views.wrap_view(self.view)
view_name = view.file_name();
if not view_name: # eg file mapping pane
return
file_mapping = projectsystem.DocumentMapping.MappingsManager.get_mapping(view_name)
if file_mapping:
is_authored_file = projectsystem.DocumentMapping.MappingsManager.is_authored_file(view_name)
sel = view.sel()[0]
start = view.rowcol(sel.begin())
end = view.rowcol(sel.end())
mapped_start = file_mapping.get_generated_position(view_name, start[0], start[1]) \
if is_authored_file \
else file_mapping.get_authored_position(start[0], start[1])
if (start != end):
mapped_end = file_mapping.get_generated_position(view_name, end[0], end[1]) \
if is_authored_file \
else file_mapping.get_authored_position(end[0], end[1])
else:
mapped_end = mapped_start
window = sublime.active_window()
window.focus_group(0)
view = window.open_file(mapped_start.file_name())
do_when(lambda: not view.is_loading(), lambda: set_selection(view,
mapped_start.zero_based_line(),
mapped_start.zero_based_column(),
mapped_end.zero_based_line(),
mapped_end.zero_based_column()))
def update_overlays():
# loop over all views, identifying the files
# we need to draw into
for v in window.views():
v = views.wrap_view(v)
if not v.file_name():
continue
v.erase_regions('swi_breakpoint_inactive')
v.erase_regions('swi_breakpoint_active')
v.erase_regions('swi_breakpoint_current')
breaks = get_breakpoints_by_full_path(v.file_name()) or {}
enabled = []
disabled = []
for key in list(breaks.keys()):
if breaks[key]['status'] == 'enabled':
enabled.append(key)
if breaks[key]['status'] == 'disabled':
disabled.append(key)
v.add_regions('swi_breakpoint_active', v.lines(enabled), utils.get_setting('breakpoint_scope'), icon=breakpoint_active_icon, flags=sublime.HIDDEN)
v.add_regions('swi_breakpoint_inactive', v.lines(disabled), utils.get_setting('breakpoint_scope'), icon=breakpoint_inactive_icon, flags=sublime.HIDDEN)
if current_line:
if v.file_name().lower() == current_file.lower():
if (str(current_line) in breaks and breaks[str(current_line)]['status'] == 'enabled'): # always draw current line region, but selectively draw icon
current_icon = breakpoint_current_icon
else:
current_icon = ''
v.add_regions('swi_breakpoint_current', v.lines([current_line]), utils.get_setting('current_line_scope'), current_icon, flags=sublime.DRAW_EMPTY)
####################################################################################
# EventListener
####################################################################################
class EventListener(sublime_plugin.EventListener):
def __init__(self):
self.timing = time.time()
def on_new(self, v):
views.wrap_view(v).on_new()
def on_clone(self, v):
views.wrap_view(v).on_clone()
def on_load(self, v):
update_overlays()
views.wrap_view(v).on_load()
def on_close(self, v):
views.wrap_view(v).on_close()
def on_pre_save(self, v):
views.wrap_view(v).on_pre_save()
def reload_styles(self):
channel.send(webkit.Runtime.evaluate("var files = document.getElementsByTagName('link');var links = [];for (var a = 0, l = files.length; a < l; a++) {var elem = files[a];var rel = elem.rel;if (typeof rel != 'string' || rel.length === 0 || rel === 'stylesheet') {links.push({'elem': elem,'href': elem.getAttribute('href').split('?')[0],'last': false});}}for ( a = 0, l = links.length; a < l; a++) {var link = links[a];link.elem.setAttribute('href', (link.href + '?x=' + Math.random()));}"))
def reload_page(self):
channel.send(webkit.Page.reload(), on_reload)
def on_post_save(self, v):
if channel and utils.get_setting('reload_on_save'):
channel.send(webkit.Network.clearBrowserCache())
if v.file_name().endswith('.css') or v.file_name().endswith('.less') or v.file_name().endswith('.sass') or v.file_name().endswith('.scss'):
sublime.set_timeout(lambda: self.reload_styles(), utils.get_setting('reload_timeout'))
elif v.file_name().endswith('.js'):
scriptId = find_script(v.file_name())
if scriptId and set_script_source:
scriptSource = v.substr(sublime.Region(0, v.size()))
# Editing script can potentially modify the callstack
logger.info('Live updating script source %s %s' % (scriptId, find_script_url(scriptId)))
channel.send(webkit.Debugger.setScriptSource(scriptId, scriptSource), self.update_stack)
else:
sublime.set_timeout(lambda: self.reload_page(), utils.get_setting('reload_timeout'))
else:
sublime.set_timeout(lambda: self.reload_page(), utils.get_setting('reload_timeout'))
views.wrap_view(v).on_post_save()
def on_modified(self, v):
views.wrap_view(v).on_modified()
#update_overlays()
def on_activated(self, v):
#todo can we move to on load?
views.wrap_view(v).on_activated()
def on_deactivated(self, v):
views.wrap_view(v).on_deactivated()
def on_query_context(self, v, key, operator, operand, match_all):
views.wrap_view(v).on_query_context(key, operator, operand, match_all)
def update_stack(self, command):
""" Called on setScriptSource """
update_stack(command.data)
####################################################################################
# GLOBAL HANDLERS
####################################################################################
def on_reload(command):
global file_to_scriptId
file_to_scriptId = []
####################################################################################
# Console
####################################################################################
def clear_all_views():
views.clear_view('console')
views.clear_view('stack')
views.clear_view('scope')
views.clear_view('mapping')
def close_all_our_windows():
global window
if not window:
window = sublime.active_window()
window.focus_group(0)
for v in window.views_in_group(0):
if v.name() == 'File mapping':
window.run_command("close")
break
window.focus_group(1)
for v in window.views_in_group(1):
window.run_command("close")
window.focus_group(2)
for v in window.views_in_group(2):
window.run_command("close")
window.set_layout(original_layout)
def update_stack(data):
if not channel: # race with shutdown
return
if (not 'callFrames' in data):
return
callFrames = data['callFrames']
if len(callFrames) == 0: # Chrome can return none eg on setScriptSource when not broken
return
if utils.get_setting('enable_pause_overlay'):
channel.send(webkit.Debugger.setOverlayMessage('Paused in Sublime Web Inspector'))
window.set_layout(utils.get_setting('stack_layout'))
console_show_stack(callFrames)
change_to_call_frame(callFrames[0])
def change_to_call_frame(callFrame):
scriptId = callFrame.location.scriptId
line_number = callFrame.location.lineNumber
column_number = callFrame.location.columnNumber
file_name = find_script(str(scriptId))
first_scope = callFrame.scopeChain[0]
position = get_authored_position_if_necessary(file_name, line_number, column_number)
if position:
file_name = position.file_name()
line_number = position.zero_based_line()
column_number = position.zero_based_column()
global current_call_frame
current_call_frame = callFrame.callFrameId