forked from t-bltg/sublime-compare-side-by-side
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbs_compare.py
784 lines (618 loc) · 26.3 KB
/
sbs_compare.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
# coding: utf8
import os
import re
import difflib
import json
import sublime
import sublime_plugin
def plugin_loaded():
# creating these files before they're needed avoids some weird issues
# where sublime insists the file does't exist, when it definitely does
folder = os.path.join( sublime.packages_path(), 'User' )
if not os.path.exists( folder ):
os.makedirs( folder )
with open( os.path.join( folder, 'SBSCompareTheme.tmTheme' ), 'w', encoding='utf-8' ) as f:
f.write( '' )
with open( os.path.join( folder, 'SBSCompareScheme.sublime-color-scheme' ), 'w', encoding='utf-8' ) as f:
f.write( '' )
def sbs_settings():
return sublime.load_settings( 'SBSCompare.sublime-settings' )
class EraseViewCommand( sublime_plugin.TextCommand ):
def run( self, edit ):
self.view.erase( edit, sublime.Region( 0, self.view.size() ) )
class InsertViewCommand( sublime_plugin.TextCommand ):
def run( self, edit, string='' ):
self.view.insert( edit, self.view.size(), string )
class SbsLayoutPreserver( sublime_plugin.EventListener ):
def count_views( self, ignore=None ):
numCompare = 0
numNotCompare = 0
nonCompareWin = None
for w in sublime.windows():
for v in w.views():
if ignore and v.id() == ignore:
continue
if v.settings().get( 'is_sbs_compare' ):
numCompare += 1
else:
numNotCompare += 1
nonCompareWin = w
if len( w.views() ) == 0:
nonCompareWin = w
return { 'compare': numCompare, 'notCompare': numNotCompare, 'nonCompareWin': nonCompareWin }
def on_pre_close( self, view ):
# if one comparison view is closed, close the other
if view.settings().get( 'is_sbs_compare' ):
win = view.window()
sublime.set_timeout( lambda: win.run_command( 'close_window' ), 10 )
return
# if there are no non-comparison views open after this closes...
count = self.count_views( ignore=view.id() )
if count['compare'] > 0 and count['notCompare'] == 0:
last_file = view.file_name()
# wait until the view is closed, then check again
def after_close():
# if there's no non-comparison window still open, make a new one
# (there will be if the user only closes a tab!)
count = self.count_views()
if count['nonCompareWin'] is None:
sublime.active_window().run_command( 'new_window' )
win = sublime.active_window()
# attempt to restore sidebar and menu visibility on ST2
if int( sublime.version() ) < 3000:
if sbs_settings().get( 'hide_sidebar', False ):
win.run_command( 'toggle_side_bar' )
if sbs_settings().get( 'hide_menu', False ):
win.run_command( 'toggle_menu' )
# reopen last file
if last_file is not None:
win.open_file( last_file )
sublime.set_timeout( lambda: win.show_quick_panel( [ 'Please close all comparison windows first' ], None ), 10 )
sublime.set_timeout( after_close, 100 )
sbs_markedSelection = [ '', '' ]
sbs_files = []
class SbsMarkSelCommand( sublime_plugin.TextCommand ):
def run( self, edit ):
global sbs_markedSelection
window = sublime.active_window()
view = window.active_view()
sel = view.sel()
region = sel[0]
selectionText = view.substr( region )
sbs_markedSelection[0] = sbs_markedSelection[1]
sbs_markedSelection[1] = selectionText
class SbsCompareFilesCommand( sublime_plugin.ApplicationCommand ):
def run( self, A=None, B=None ):
global sbs_files
if A == None or B == None:
print( 'Compare Error: file(s) not specified' )
return
A = os.path.abspath( A )
B = os.path.abspath( B )
if not os.path.isfile( A ) or not os.path.isfile( B ):
print( 'Compare Error: file(s) not found' )
return
del sbs_files[:]
sbs_files.append( A )
sbs_files.append( B )
print( 'Comparing "%s" and "%s"' % ( A, B ) )
window = sublime.active_window()
window.run_command( 'sbs_compare' )
class SbsCompareCommand( sublime_plugin.TextCommand ):
def generate_colour_scheme( self, view, generate=True ):
# make sure we have hex AND we're >= ST3 (load_resource doesn't work in ST2)
colour_removed = sbs_settings().get( 'remove_colour', 'invalid.illegal' )
colour_added = sbs_settings().get( 'add_colour', 'string' )
colour_modified_deletion = sbs_settings().get( 'modified_colour_deletion', 'support.class' )
colour_modified_addition = sbs_settings().get( 'modified_colour_addition', 'support.class' )
colour_unmodified_deletion = sbs_settings().get( 'unmodified_colour_deletion', 'invalid.illegal' )
colour_unmodified_addition = sbs_settings().get( 'unmodified_colour_addition', 'string' )
colour_text = sbs_settings().get( 'text_colour', '' )
notHex = False
for col in [ colour_removed, colour_added, colour_modified_deletion, colour_modified_addition, colour_unmodified_deletion, colour_unmodified_addition ]:
if not '#' in col:
notHex = True
if int( sublime.version() ) < 3000 or notHex:
return { 'removed': colour_removed, 'added': colour_added, 'modified_deletion': colour_modified_deletion, 'modified_addition': colour_modified_addition, 'unmodified_deletion': colour_unmodified_deletion, 'unmodified_addition': colour_unmodified_addition }
# generate theme strings
colourStrings = {}
colourHexes = {}
for col in [ [ 'removed', colour_removed ], [ 'added', colour_added ], [ 'modified_deletion', colour_modified_deletion ], [ 'modified_addition', colour_modified_addition ], [ 'unmodified_deletion', colour_unmodified_deletion ], [ 'unmodified_addition', colour_unmodified_addition ] ]:
colourStrings[ col[0] ] = 'comparison.' + col[0]
colourHexes[ col[0] ] = col[1]
# generate modified theme
if generate:
# load current scheme
current_scheme = view.settings().get( 'color_scheme' ) # no 'u' >:(
try:
scheme = sublime.load_resource( current_scheme )
except:
# sometimes load_resource can fail (seemingly on OSX when upgrading from ST2->ST3)
# manually re-selecting the colour scheme once should fix this for good (see issue #31)
sublime.message_dialog( 'Could not load colour scheme.\nFalling back to a blank colour scheme.\n\nTo fix this, please manually re-select your colour scheme in\n\tPreferences > Color Scheme\n\nThis should not happen again once action has been taken.\nSorry for the inconvenience.' )
scheme = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>settings</key><array></array></dict></plist>'
# determine if scheme is using the new .sublime-color-scheme json format
scheme_json = False
try:
scheme = sublime.decode_value( scheme )
scheme_json = True
except:
scheme_json = False
# create format specific data
if scheme_json:
for name in colourStrings:
string = colourStrings[name]
chex = colourHexes[name]
scheme['rules'].append( { "name": name, "scope": string, "background": chex, "foreground": colour_text } )
data = json.dumps( scheme )
else:
xml = ''
xml_tmpl = '<dict><key>name</key><string>{}</string><key>scope</key><string>{}</string><key>settings</key><dict><key>background</key><string>{}</string><key>foreground</key><string>{}</string></dict></dict>'
dropzone = scheme.rfind( '</array>' )
# loop through colours and generate their xml
for name in colourStrings:
string = colourStrings[name]
chex = colourHexes[name]
xml += xml_tmpl.format( 'Comparison ' + name, string, chex, colour_text )
# combiiiiiiiiiiiiine
data = scheme[:dropzone] + xml + scheme[dropzone:]
# determine theme filename
# relative for settings, absolute for writing to file
# replacing slashes for relative path necessary on windows
# completely separate filenames are necessary to avoid json erroneously taking precedence
theme_name = 'SBSCompareTheme.tmTheme'
if scheme_json:
theme_name = 'SBSCompareScheme.sublime-color-scheme'
abs_theme_file = os.path.join( sublime.packages_path(), 'User', theme_name )
rel_theme_file = os.path.join( os.path.basename( sublime.packages_path() ), 'User', theme_name )
rel_theme_file = rel_theme_file.replace( '\\', '/' )
# save new theme
try:
with open( abs_theme_file, 'w', encoding='utf-8' ) as f:
f.write( data )
except:
sublime.message_dialog( 'Could not write theme file.\nPlease ensure that your Sublime config directory is writeable and restart Sublime.\n\nFull path:\n' + abs_theme_file )
# save filename for later use (if we rerun this without regenerating)
self.last_theme_file = rel_theme_file
# set view settings to use new theme
view.settings().set( 'color_scheme', self.last_theme_file )
return colourStrings
def get_view_contents( self, view ):
selection = sublime.Region( 0, view.size() )
content = view.substr( selection )
return content
def close_view( self, view ):
parent = view.window()
parent.focus_view( view )
parent.run_command( "close_file" )
def get_drawtype( self ):
# fill highlighting (DRAW_NO_OUTLINE) only exists on ST3+
drawType = sublime.DRAW_OUTLINED
if int( sublime.version() ) >= 3000:
if not sbs_settings().get( 'outlines_only', False ):
drawType = sublime.DRAW_NO_OUTLINE
return drawType
def highlight_lines( self, view, lines, sublines, col ):
# full line diffs
regionList = []
markers = []
for lineNum in lines:
lineStart = view.text_point( lineNum, 0 )
markers.append( lineStart )
for sub in (sub for sub in sublines if sub[0] == lineNum):
subStart = view.text_point( lineNum, sub[1] )
subEnd = view.text_point( lineNum, sub[2] )
region = sublime.Region( lineStart, subStart )
regionList.append( region )
lineStart = subEnd
lineEnd = view.text_point( lineNum+1, -1 )
region = sublime.Region( lineStart, lineEnd )
regionList.append( region )
colour = self.colours['removed']
if col == 'B':
colour = self.colours['added']
drawType = self.get_drawtype()
view.add_regions( 'diff_highlighted-' + col, regionList, colour, '', drawType )
view.settings().set( 'sbs_markers', markers )
def sub_highlight_lines( self, view, lines, col ):
# intra-line diffs
regionList = []
lineRegionList = []
for diff in lines:
lineNum = diff[0]
start = view.text_point( lineNum, diff[1] )
end = view.text_point( lineNum, diff[2] )
lineStart = view.text_point( lineNum, 0 )
lineEnd = view.text_point( lineNum+1, -1 )
region = sublime.Region( start, end )
regionList.append( region )
lineRegion = sublime.Region( lineStart, lineEnd )
lineRegionList.append(lineRegion)
colour = self.colours['modified_deletion']
colourUnmodified = self.colours['unmodified_deletion']
if col == 'B':
colour = self.colours['modified_addition']
colourUnmodified = self.colours['unmodified_addition']
drawType = self.get_drawtype()
view.add_regions( 'diff_intraline_unmodified-' + col, lineRegionList, colourUnmodified, '', drawType )
view.add_regions( 'diff_intraline-' + col, regionList, colour, '', drawType )
def compare_views( self, view1, view2 ):
view1_contents = self.get_view_contents( view1 )
view2_contents = self.get_view_contents( view2 )
linesA = view1_contents.splitlines( False )
linesB = view2_contents.splitlines( False )
if sbs_settings().has( 'ignore_pattern' ):
ignore_pattern = sbs_settings().get( 'ignore_pattern' )
pattern = re.compile(ignore_pattern, re.MULTILINE)
view1_contents = pattern.sub( '', view1_contents )
view2_contents = pattern.sub( '', view2_contents )
if sbs_settings().get( 'ignore_whitespace', False ):
view1_contents = re.sub( r'[ \t]', '', view1_contents )
view2_contents = re.sub( r'[ \t]', '', view2_contents )
if sbs_settings().get( 'ignore_case', False ):
view1_contents = view1_contents.lower()
view2_contents = view2_contents.lower()
diffLinesA = view1_contents.splitlines( False )
diffLinesB = view2_contents.splitlines( False )
bufferA = []
bufferB = []
highlightA = []
highlightB = []
subHighlightA = []
subHighlightB = []
diff = difflib.ndiff( diffLinesA, diffLinesB, charjunk = None )
hasDiffA = False
hasDiffB = False
intraLineA = ''
intraLineB = ''
hasIntraline = False
'''
An "intraline" difference is always a '-' line, possibly followed by
'?' line, and immediately followed by a '+' line; the next line after
that '+' might be another '?' line as well, or not. This is all
dependent on whether the new file line (view2's) added, removed, or
just changed characters relative to the original. If the new file line
has more characters but no other differences, then the diff sequence
would be '-', '+', '?'; if the new file has fewer characters but no
other differences, the sequence will be '-', '?', '+'; if the new file
has other character differences relative to the original, then the
sequence will be '-', '?', '+', '?'.
'''
lineNum = 0
lineA = 0
lineB = 0
for line in diff:
lineNum += 1
code = line[:2]
if code == '- ':
bufferA.append( linesA[lineA] )
bufferB.append( '' )
highlightA.append( lineNum - 1 )
intraLineA = linesA[lineA]
hasDiffA = True
hasDiffB = False
hasIntraline = False
lineA += 1
elif code == '+ ':
bufferA.append( '' )
bufferB.append( linesB[lineB] )
highlightB.append( lineNum - 1 )
intraLineB = linesB[lineB]
hasDiffB = True
lineB += 1
elif code == ' ':
bufferA.append( linesA[lineA] )
bufferB.append( linesB[lineB] )
hasDiffA = False
hasDiffB = False
hasIntraline = False
lineA += 1
lineB += 1
elif code == '? ':
lineNum -= 1
hasIntraline = True
else:
lineNum -= 1
if hasIntraline and hasDiffA and hasDiffB:
if sbs_settings().get( 'enable_intraline', True ):
# fixup line alignment
assert bufferA[-1] == ''
assert bufferB[-2] == ''
bufferB[-1] = bufferB.pop()
bufferA.pop()
highlightB[-1] = highlightA[-1]
lineNum -= 1
assert highlightB[-1] == lineNum - 1
s = difflib.SequenceMatcher( None, intraLineA, intraLineB )
for tag, i1, i2, j1, j2 in s.get_opcodes():
if tag != 'equal': # == replace
if sbs_settings().get( 'intraline_emptyspace', False ):
if tag == 'insert':
i2 += j2 - j1
if tag == 'delete':
j2 += i2 - i1
subHighlightA.append( [ lineNum - 1, i1, i2 ] )
subHighlightB.append( [ lineNum - 1, j1, j2 ] )
hasDiffA = False
hasDiffB = False
hasIntraline = False
window = sublime.active_window()
window.focus_view( view1 )
window.run_command( 'erase_view' )
window.run_command( 'insert_view', { 'string': '\n'.join( bufferA ) } )
window.focus_view( view2 )
window.run_command( 'erase_view' )
window.run_command( 'insert_view', { 'string': '\n'.join( bufferB ) } )
self.highlight_lines( view1, highlightA, subHighlightA, 'A' )
self.highlight_lines( view2, highlightB, subHighlightB, 'B' )
intraDiff = ''
if sbs_settings().get( 'enable_intraline', True ):
self.sub_highlight_lines( view1, subHighlightA, 'A' )
self.sub_highlight_lines( view2, subHighlightB, 'B' )
numIntra = len( subHighlightB )
intraDiff = str( numIntra ) + ' intra-line modifications\n'
if sbs_settings().get( 'line_count_popup', False ):
numDiffs = len( highlightA ) + len( highlightB )
sublime.message_dialog( intraDiff + str( len( highlightA ) ) + ' lines removed, ' + str( len( highlightB ) ) + ' lines added\n' + str( numDiffs ) + ' line differences total' )
def run( self, edit, with_active = False, group = -1, index = -1, compare_selections = False ):
global sbs_markedSelection, sbs_files
active_view = self.view
active_window = active_view.window()
active_id = active_view.id()
openTabs = []
for view in active_window.views():
if view.id() != active_id:
viewName = 'untitled'
if view.file_name():
viewName = view.file_name()
elif view.name():
viewName = view.name()
openTabs.append( [ viewName, view ] )
def create_comparison( view1_contents, view2_contents, syntax, name1_override = False, name2_override = False ):
view1_syntax = syntax
view2_syntax = syntax
# make new window
active_window.run_command( 'new_window' )
new_window = sublime.active_window()
new_window.set_layout( { "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]] } )
if int( sublime.version() ) >= 3000:
if sbs_settings().get( 'hide_sidebar', False ):
new_window.set_sidebar_visible(False)
if sbs_settings().get( 'hide_menu', False ):
new_window.set_menu_visible(False)
if sbs_settings().get( 'hide_minimap', False ):
new_window.set_minimap_visible(False)
if sbs_settings().get( 'hide_status_bar', False ):
new_window.set_status_bar_visible(False)
if sbs_settings().get( 'hide_tabs', False ):
new_window.set_tabs_visible(False)
else:
if sbs_settings().get( 'hide_sidebar', False ):
new_window.run_command( 'toggle_side_bar' )
if sbs_settings().get( 'hide_menu', False ):
new_window.run_command( 'toggle_menu' )
# view names
view_prefix = sbs_settings().get( 'display_prefix', '' )
view2_name = name2_override
view1_name = 'untitled'
if active_view.file_name():
view1_name = active_view.file_name()
elif active_view.name():
view1_name = active_view.name()
if name1_override != False:
view1_name = name1_override
name1base = os.path.basename( view1_name )
name2base = os.path.basename( view2_name )
if name1base == name2base:
dirname1 = os.path.dirname( view1_name )
dirname2 = os.path.dirname( view2_name )
path_prefix = os.path.commonprefix( [ dirname1, dirname2 ] )
if path_prefix != '':
path_prefix = path_prefix.replace( '\\', '/' )
path_prefix = path_prefix.split( '/' )[:-1] # leave last directory in path
path_prefix = '/'.join( path_prefix ) + '/'
plen = len( path_prefix )
dirname1 = dirname1[plen:]
dirname2 = dirname2[plen:]
separator = ' - '
if int( sublime.version() ) >= 3000:
separator = ' — '
view1_name = name1base + separator + dirname1
view2_name = name2base + separator + dirname2
if dirname1 == dirname2:
view1_name = name1base
view2_name = name2base
else:
view1_name = name1base
view2_name = name2base
view1_name += ' (active)'
view2_name += ' (other)'
# view 1
new_window.run_command( 'new_file' )
new_window.run_command( 'insert_view', { 'string': view1_contents } )
new_window.active_view().set_syntax_file( view1_syntax )
new_window.active_view().set_name( view_prefix + view1_name )
new_window.active_view().set_scratch( True )
view1 = new_window.active_view()
# view 2
new_window.run_command( 'new_file' )
new_window.run_command( 'insert_view', { 'string': view2_contents } )
new_window.active_view().set_syntax_file( view2_syntax )
new_window.active_view().set_name( view_prefix + view2_name )
# move view 2 to group 2
new_window.set_view_index( new_window.active_view(), 1, 0 )
new_window.active_view().set_scratch( True )
view2 = new_window.active_view()
# keep track of these views
view1.settings().set( "is_sbs_compare", True )
view2.settings().set( "is_sbs_compare", True )
# disable word wrap
view1.settings().set( 'word_wrap', 'false' )
view2.settings().set( 'word_wrap', 'false' )
# generate and set colour scheme
self.colours = self.generate_colour_scheme( view1 )
self.generate_colour_scheme( view2, generate=False )
# run diff
self.compare_views( view1, view2 )
# make readonly
new_window.focus_view( view1 )
if sbs_settings().get( 'read_only', False ):
new_window.active_view().set_read_only( True )
new_window.focus_view( view2 )
if sbs_settings().get( 'read_only', False ):
new_window.active_view().set_read_only( True )
# activate scroll syncer
ViewScrollSyncer( new_window, [ view1, view2 ] )
# move views to top left
view1.set_viewport_position( (0, 0), False )
view2.set_viewport_position( (0, 0), False )
# move cursors to top left
origin = view.text_point( 0, 0 )
view1.sel().clear()
view1.sel().add( sublime.Region( origin ) )
view1.show( origin )
view2.sel().clear()
view2.sel().add( sublime.Region( origin ) )
view2.show( origin )
# focus first view
new_window.focus_view( view1 )
def on_click( index ):
if index > -1:
# get original views' data
view1_contents = self.get_view_contents( active_view )
view2_contents = self.get_view_contents( openTabs[index][1] )
syntax = active_view.settings().get( 'syntax' )
create_comparison( view1_contents, view2_contents, syntax, False, openTabs[index][0] )
def compare_from_views( view1, view2 ):
if view1.is_loading() or view2.is_loading():
sublime.set_timeout( lambda: compare_from_views( view1, view2 ), 10 )
else:
view1_contents = self.get_view_contents( view1 )
view2_contents = self.get_view_contents( view2 )
syntax = view1.settings().get( 'syntax' )
self.close_view( view1 )
self.close_view( view2 )
create_comparison( view1_contents, view2_contents, syntax, file1, file2 )
if len( sbs_files ) > 0:
file1 = sbs_files[0]
file2 = sbs_files[1]
view1 = active_window.open_file( file1 )
view2 = active_window.open_file( file2 )
compare_from_views( view1, view2 )
del sbs_files[:]
elif compare_selections == True:
selA = sbs_markedSelection[0]
selB = sbs_markedSelection[1]
sel = active_view.sel()
selNum = 0
for selection in sel:
selNum += 1
if selNum == 2:
selA = active_view.substr( sel[0] )
selB = active_view.substr( sel[1] )
syntax = active_view.settings().get( 'syntax' )
create_comparison( selA, selB, syntax, 'selection A', 'selection B' )
elif len( openTabs ) == 1:
on_click( 0 )
else:
if with_active == True:
active_group, active_group_index = active_window.get_view_index( active_view )
if group == -1 and index == -1:
group = 0 if active_group == 1 else 1
other_active_view = active_window.active_view_in_group( group )
index = active_window.get_view_index( other_active_view )[1]
if index > active_group_index:
index -= 1
if group > active_group:
index += len( active_window.views_in_group( active_group ) ) - 1
elif group < active_group:
index += 1
on_click( index )
else:
menu_items = []
for tab in openTabs:
fileName = tab[0]
if os.path.basename( fileName ) == fileName:
menu_items.append( [ os.path.basename( fileName ), '' ] )
else:
menu_items.append( [ os.path.basename( fileName ), fileName ] )
sublime.set_timeout( self.view.window().show_quick_panel( menu_items, on_click ) )
class ViewScrollSyncer( object ):
def __init__( self, window, viewList ):
self.window = window
self.views = viewList
self.timeout_focused = 10
self.timeout_unfocused = 50
self.run()
def update_scroll( self, view1, view2, lastUpdated ):
if lastUpdated == 'A':
view2.set_viewport_position( view1.viewport_position(), False )
elif lastUpdated == 'B':
view1.set_viewport_position( view2.viewport_position(), False )
def run( self ):
if not self.window:
return
if self.window.id() != sublime.active_window().id():
sublime.set_timeout( self.run, self.timeout_unfocused )
return
view1 = self.views[0]
view2 = self.views[1]
if not view1 or not view2:
return
vecA = view1.viewport_position()
vecB = view2.viewport_position()
if vecA != vecB:
lastVecA0 = view1.settings().get( 'viewsync_last_vec0', 1 )
lastVecA1 = view1.settings().get( 'viewsync_last_vec1', 1 )
lastVecB0 = view2.settings().get( 'viewsync_last_vec0', 1 )
lastVecB1 = view2.settings().get( 'viewsync_last_vec1', 1 )
lastVecA = ( lastVecA0, lastVecA1 )
lastVecB = ( lastVecB0, lastVecB1 )
lastUpdated = ''
if lastVecA != vecA:
lastUpdated = 'A'
view1.settings().set( 'viewsync_last_vec0', vecA[0] )
view1.settings().set( 'viewsync_last_vec1', vecA[1] )
view2.settings().set( 'viewsync_last_vec0', vecA[0] )
view2.settings().set( 'viewsync_last_vec1', vecA[1] )
if lastVecB != vecB:
lastUpdated = 'B'
view1.settings().set( 'viewsync_last_vec0', vecB[0] )
view1.settings().set( 'viewsync_last_vec1', vecB[1] )
view2.settings().set( 'viewsync_last_vec0', vecB[0] )
view2.settings().set( 'viewsync_last_vec1', vecB[1] )
if ( lastUpdated != '' ):
self.update_scroll( view1, view2, lastUpdated )
sublime.set_timeout( self.run, self.timeout_focused )
def sbs_scroll_to( view, prev=False ):
if not view.settings().get( 'is_sbs_compare' ):
return
current_pos = view.sel()[0].begin()
for col in [ 'A', 'B' ]:
regions = view.settings().get( 'sbs_markers' )
if prev:
regions.reverse()
for highlight in regions:
found = False
if prev:
if highlight < current_pos:
found = True
else:
if highlight > current_pos:
found = True
if found:
view.sel().clear()
view.sel().add( sublime.Region( highlight ) )
view.show( highlight )
# sometimes necessary, better safe than sorry
sublime.set_timeout( lambda: view.show( highlight ), 10 )
return
msg = 'Reached the '
msg += 'beginning' if prev else 'end'
view.window().show_quick_panel( [ msg ], None )
class SbsPrevDiffCommand( sublime_plugin.TextCommand ):
def run( self, edit, string='' ):
sbs_scroll_to( self.view, prev=True )
class SbsNextDiffCommand( sublime_plugin.TextCommand ):
def run( self, edit, string='' ):
sbs_scroll_to( self.view )