forked from facelessuser/EasyDiff
/
easy_diff_basic.py
812 lines (665 loc) · 26.3 KB
/
easy_diff_basic.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
"""
Easy Diff Basic Commands.
Copyright (c) 2013 - 2015 Isaac Muse <isaacmuse@gmail.com>
License: MIT
"""
import sublime
import sublime_plugin
from os.path import basename
from EasyDiff.easy_diff_global import load_settings, log, get_external_diff, get_target, get_group_view
from EasyDiff.easy_diff_dynamic_menu import update_menu
from EasyDiff.easy_diff import EasyDiffView, EasyDiffInput, EasyDiff
LEFT = None
###############################
# Helper Functions
###############################
def diff(right, external=False):
"""
Initiate diff by getting left side and right side compare.
Call the appropriate diff method and call internal or external diff.
"""
lw = None
rw = None
lv = None
rv = None
for w in sublime.windows():
if w.id() == LEFT["win_id"]:
lw = w
if w.id() == right["win_id"]:
rw = w
if lw is not None and rw is not None:
break
if lw is not None:
for v in lw.views():
if v.id() == LEFT["view_id"]:
lv = v
break
else:
if LEFT["clip"]:
lv = LEFT["clip"]
if rw is not None:
for v in rw.views():
if v.id() == right["view_id"]:
rv = v
break
else:
if right["clip"]:
rv = right["clip"]
if lv is not None and rv is not None:
ext_diff = get_external_diff()
if external:
EasyDiff.extcompare(EasyDiffInput(lv, rv, external=True), ext_diff)
else:
EasyDiff.compare(EasyDiffInput(lv, rv))
else:
log("Can't compare")
###############################
# Helper Classes
###############################
class _EasyDiffSelection(object):
"""Handle selection diff."""
def get_selections(self):
"""Get the selections."""
bfr = ""
length = len(self.view.sel())
for s in self.view.sel():
if s.size() == 0:
continue
bfr += self.view.substr(s)
if length > 1:
bfr += "\n"
length -= 1
return bfr
def get_encoding(self):
"""Get view encoding."""
return self.view.encoding()
def has_selections(self):
"""Check if view has a valid selection."""
selections = False
if bool(load_settings().get("multi_select", False)):
for s in self.view.sel():
if s.size() > 0:
selections = True
break
else:
selections = len(self.view.sel()) == 1 and self.view.sel()[0].size() > 0
return selections
class _EasyDiffCompareBothTextCommand(sublime_plugin.TextCommand):
"""Compare text command."""
def run(self, edit, external=False, group=-1, index=-1):
"""Run command."""
if index != -1:
# Ensure we have the correct view
self.view = get_group_view(sublime.active_window(), group, index)
diff(self.get_right(), external=external)
def view_has_selections(self, group=-1, index=-1):
"""Check if view has selections."""
has_selections = False
if index != -1:
view = get_group_view(sublime.active_window(), group, index)
if view is not None:
if bool(load_settings().get("multi_select", False)):
for sel in view.sel():
if sel.size() > 0:
has_selections = True
break
else:
has_selections = len(view.sel()) == 1 and view.sel()[0].size() > 0
else:
has_selections = self.has_selections()
return has_selections
def get_right(self):
"""Get right."""
return None
def check_enabled(self, group=-1, index=-1):
"""Check if command is enabled logic."""
return True
def is_enabled(self, external=False, group=-1, index=-1):
"""Check if command is enabled."""
return LEFT is not None and self.check_enabled(group, index)
class _EasyDiffCompareBothWindowCommand(sublime_plugin.WindowCommand):
"""Compare window command."""
no_view = False
def run(self, external=False, paths=[], group=-1, index=-1):
"""run command."""
self.external = external
self.set_view(paths, group, index)
if not self.no_view and self.view is None:
return
if not self.no_view:
sublime.set_timeout(self.is_loaded, 100)
else:
self.diff()
def is_loaded(self):
"""Check if view is loaded."""
if self.view.is_loading():
sublime.set_timeout(self.is_loaded, 100)
else:
self.diff()
def diff(self):
"""Diff."""
diff(self.get_right(), external=self.external)
def set_view(self, paths, group=-1, index=-1, open_file=True):
"""Set view."""
if len(paths):
file_path = get_target(paths)
if file_path is None:
return
if open_file:
self.view = self.window.open_file(file_path)
elif index != -1:
self.view = get_group_view(self.window, group, index)
else:
self.view = self.window.active_view()
def get_right(self):
"""Get right."""
return None
def check_enabled(self, paths=[], group=-1, index=-1):
"""Check if command is enabled logic."""
return True
def is_enabled(self, external=False, paths=[], group=-1, index=-1):
"""Check if command is enabled."""
return LEFT is not None and self.check_enabled(paths)
###############################
# Set View
###############################
class EasyDiffSetLeftCommand(sublime_plugin.WindowCommand):
"""Set left side command."""
def run(self, paths=[], group=-1, index=-1):
"""Run command."""
global LEFT
self.set_view(paths, group, index)
if self.view is None:
return
LEFT = {"win_id": self.view.window().id(), "view_id": self.view.id(), "clip": None}
name = self.view.file_name()
if name is None:
name = "Untitled"
update_menu(basename(name))
def set_view(self, paths, group=-1, index=-1, open_file=True):
"""Set view."""
if len(paths):
file_path = get_target(paths)
if file_path is None:
return
if open_file:
self.view = self.window.open_file(file_path)
elif index != -1:
self.view = get_group_view(self.window, group, index)
else:
self.view = self.window.active_view()
def is_enabled(self, paths=[], group=-1, index=-1):
"""Check if command is enabled."""
return get_target(paths, group, index) is not None if len(paths) or index != -1 else True
class EasyDiffCompareBothViewCommand(_EasyDiffCompareBothWindowCommand):
"""Compare view command."""
def get_right(self):
"""Get right."""
return {"win_id": self.view.window().id(), "view_id": self.view.id(), "clip": None}
def check_enabled(self, paths=[], group=-1, index=-1):
"""Check if view is enabled logic."""
return True
def is_enabled(self, external=False, paths=[], group=-1, index=-1):
"""Check if command is enabled."""
return (
LEFT is not None and
(get_target(paths, group, index) is not None if len(paths) or index != -1 else True) and
self.check_enabled()
)
###############################
# Set Clipboard
###############################
class EasyDiffSetLeftClipboardCommand(sublime_plugin.WindowCommand):
"""Set left side clipboard command."""
def run(self, paths=[], group=-1, index=-1):
"""Run command."""
global LEFT
LEFT = {
"win_id": None, "view_id": None,
"clip": EasyDiffView("**clipboard**", sublime.get_clipboard(), "UTF-8")
}
update_menu("**clipboard**")
def is_enabled(self, paths=[], group=-1, index=-1):
"""Check if command is enabled."""
valid_path = get_target(paths, group, index) is not None if len(paths) or index != -1 else True
return bool(load_settings().get("use_clipboard", True)) and valid_path
def is_visible(self, paths=[], group=-1, index=-1):
"""Check if command is visible."""
return bool(load_settings().get("use_clipboard", True))
class EasyDiffCompareBothClipboardCommand(_EasyDiffCompareBothWindowCommand):
"""Compare clipboard."""
no_view = True
def get_right(self):
"""Get right."""
return {
"win_id": None, "view_id": None,
"clip": EasyDiffView("**clipboard**", sublime.get_clipboard(), "UTF-8")
}
def check_enabled(self, paths=[], group=-1, index=-1):
"""Check if command is enabled."""
valid_path = get_target(paths, group, index) is not None if len(paths) or index != -1 else True
return bool(load_settings().get("use_clipboard", True)) and valid_path
def is_visible(self, external=False, paths=[], group=-1, index=-1):
"""Check if command is visible."""
return bool(load_settings().get("use_clipboard", True))
###############################
# Set Selection
###############################
class EasyDiffSetLeftSelectionCommand(sublime_plugin.TextCommand, _EasyDiffSelection):
"""Set left side selection command."""
def run(self, edit, group=-1, index=-1):
"""Run command."""
global LEFT
if index != -1:
# Ensure we have the correct view
self.view = get_group_view(sublime.active_window(), group, index)
LEFT = {
"win_id": None, "view_id": None,
"clip": EasyDiffView("**selection**", self.get_selections(), self.get_encoding())
}
update_menu("**selection**")
def view_has_selections(self, group=-1, index=-1):
"""Check if view has selections."""
has_selections = False
if index != -1:
view = get_group_view(sublime.active_window(), group, index)
if view is not None:
if bool(load_settings().get("multi_select", False)):
for sel in view.sel():
if sel.size() > 0:
has_selections = True
break
else:
has_selections = len(view.sel()) == 1 and view.sel()[0].size() > 0
else:
has_selections = self.has_selections()
return has_selections
def is_enabled(self, group=-1, index=-1):
"""Check if command is enabled."""
return bool(load_settings().get("use_selections", True)) and self.view_has_selections(group, index)
def is_visible(self, group=-1, index=-1):
"""Check if command is visible."""
return bool(load_settings().get("use_selections", True))
class EasyDiffCompareBothSelectionCommand(_EasyDiffCompareBothTextCommand, _EasyDiffSelection):
"""Compare selection command."""
def get_right(self):
"""Get right."""
return {
"win_id": None, "view_id": None,
"clip": EasyDiffView("**selection**", self.get_selections(), self.get_encoding())
}
def check_enabled(self, group=-1, index=-1):
"""Check if command is enabled."""
return bool(load_settings().get("use_selections", True)) and self.view_has_selections(group, index)
def is_visible(self, external=False, group=-1, index=-1):
"""Check if command is visible."""
return bool(load_settings().get("use_selections", True))
###############################
# MRU Tab Command
###############################
class EasyDiffMruPanelCompareCommand(sublime_plugin.WindowCommand):
"""Most recently used panel compare command."""
def run(self, method="view", external=False):
"""Run command."""
if method == "view":
self.window.run_command(
"easy_diff_set_left",
{"group": EasyDiffListener.last[0], "index": EasyDiffListener.last[1]}
)
self.window.run_command(
"easy_diff_compare_both_view",
{"external": external}
)
elif method == "selection":
self.window.run_command(
"easy_diff_set_left_selection",
{"group": EasyDiffListener.last[0], "index": EasyDiffListener.last[1]}
)
self.window.run_command(
"easy_diff_compare_both_selection",
{"external": external}
)
elif method == "clipboard":
self.window.run_command(
"easy_diff_set_left",
{"group": EasyDiffListener.current[0], "index": EasyDiffListener.current[1]}
)
self.window.run_command(
"easy_diff_compare_both_clipboard",
{"external": external}
)
elif method == "clipboard_selection":
self.window.run_command(
"easy_diff_set_left_selection",
{"group": EasyDiffListener.current[0], "index": EasyDiffListener.current[1]}
)
self.window.run_command(
"easy_diff_compare_both_clipboard",
{"external": external}
)
@staticmethod
def enable_check(method="view", external=False):
"""Enable check."""
allow = bool(load_settings().get("last_activated_commands", True))
enabled = False
if method == "view":
enabled = (
allow and
bool(EasyDiffListener.current and EasyDiffListener.last)
)
elif method == "selection":
enabled = (
allow and
bool(load_settings().get("use_selections", True)) and
bool(EasyDiffListener.current and EasyDiffListener.last)
)
elif method == "clipboard":
enabled = (
allow and
bool(load_settings().get("use_clipboard", True)) and
bool(EasyDiffListener.last)
)
elif method == "clipboard_selection":
enabled = (
allow and
bool(load_settings().get("use_clipboard", True)) and
bool(load_settings().get("use_selections", True)) and
bool(EasyDiffListener.last)
)
if external:
enabled = (
enabled and
bool(load_settings().get("show_external", True)) and
get_external_diff() is not None
)
else:
enabled = (
enabled and
bool(load_settings().get("show_internal", True))
)
return enabled
def is_enabled(self, method="view", external=False):
"""Check if command is enabled."""
return self.enable_check(method, external)
###############################
# Quick Panel Diff
###############################
PANEL_ENTRIES = [
{
"caption": "Set Left Side",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_panel_set_left", {"external": external}
),
"condition": lambda self, external: bool(
load_settings().get("quick_panel_left_right_commands", True)
)
},
{
"caption": "Compare with %(file)s ...",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_panel_compare", {"external": external}
),
"condition": lambda self, external: LEFT is not None and bool(
load_settings().get("quick_panel_left_right_commands", True)
)
},
{
"caption": "Compare Last Active with Current Tab",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_mru_panel_compare", {"method": "view", "external": external}
),
"condition": lambda self, external: EasyDiffMruPanelCompareCommand.enable_check(
method="view", external=external
)
},
{
"caption": "Compare Last Active with Current Tab Selection(s)",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_mru_panel_compare", {"method": "selection", "external": external}
),
"condition": lambda self, external: EasyDiffMruPanelCompareCommand.enable_check(
method="selection", external=external
)
},
{
"caption": "Compare Current Tab with Clipboard",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_mru_panel_compare", {"method": "clipboard", "external": external}
),
"condition": lambda self, external: EasyDiffMruPanelCompareCommand.enable_check(
method="clipboard", external=external
)
},
{
"caption": "Compare Current Tab Selection(s) with Clipboard",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_mru_panel_compare", {"method": "clipboard_selection", "external": external}
),
"condition": lambda self, external: EasyDiffMruPanelCompareCommand.enable_check(
method="clipboard_selection", external=external
)
},
{
"caption": "SVN Diff",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_svn", {"external": external}
),
"condition": lambda self, external: not bool(load_settings().get("svn_disabled", False))
},
{
"caption": "SVN Diff with Previous Revision",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_svn", {"external": external, "last": True}
),
"condition": lambda self, external: not bool(load_settings().get("svn_disabled", False))
},
{
"caption": "SVN Revert",
"cmd": lambda self, external: self.view.run_command(
"easy_diff_svn", {"revert": True}
),
"condition": lambda self, external: not bool(load_settings().get("svn_disabled", False))
},
{
"caption": "GIT Diff",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_git", {"external": external}
),
"condition": lambda self, external: not bool(load_settings().get("git_disabled", False))
},
{
"caption": "GIT Diff with Previous Revision",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_git", {"external": external, "last": True}
),
"condition": lambda self, external: not bool(load_settings().get("git_disabled", False))
},
{
"caption": "GIT Revert",
"cmd": lambda self, external: self.view.run_command(
"easy_diff_git", {"revert": True}
),
"condition": lambda self, external: not bool(load_settings().get("git_disabled", False))
},
{
"caption": "Mercurial Diff",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_hg", {"external": external}
),
"condition": lambda self, external: not bool(load_settings().get("hg_disabled", False))
},
{
"caption": "Mercurial Diff with Previous Revision",
"cmd": lambda self, external: self.view.window().run_command(
"easy_diff_hg", {"external": external, "last": True}
),
"condition": lambda self, external: not bool(load_settings().get("hg_disabled", False))
},
{
"caption": "Mercurial Revert",
"cmd": lambda self, external: self.view.run_command(
"easy_diff_hg", {"revert": True}
),
"condition": lambda self, external: not bool(load_settings().get("hg_disabled", False))
},
]
class EasyDiffPanelCommand(sublime_plugin.TextCommand):
"""Diff panel command."""
def run(self, edit, external=False):
"""Run command."""
self.external = external
self.menu_options = []
self.menu_callback = []
if (
(not external and bool(load_settings().get("show_internal", True))) or
(external and bool(load_settings().get("show_external", True)))
):
left_name = self.get_left_name()
for entry in PANEL_ENTRIES:
if entry["condition"](self, external):
self.menu_options.append(entry["caption"] % {"file": left_name})
self.menu_callback.append(entry["cmd"])
if len(self.menu_options) > 1:
self.view.window().show_quick_panel(self.menu_options, self.check_selection)
elif len(self.menu_options) == 1:
self.check_selection(0)
def get_left_name(self):
"""Get left name."""
name = None
if LEFT is not None:
left = LEFT.get("clip")
name = None
if left is not None:
name = left.file_name()
else:
win_id = LEFT.get("win_id")
view_id = LEFT.get("view_id")
window = None
view = None
for w in sublime.windows():
if w.id() == win_id:
window = w
if window is not None:
for v in window.views():
if v.id() == view_id:
view = v
if view is not None:
name = view.file_name()
if name is None:
name = "Untitled"
else:
name = basename(name)
return name
def check_selection(self, value):
"""Check user's selection."""
if value != -1:
callback = self.menu_callback[value]
sublime.set_timeout(lambda: callback(self, self.external), 0)
def is_enabled(self, external=False):
"""Check if command is enabled."""
return (
(not external and bool(load_settings().get("show_internal", True))) or
(external and bool(load_settings().get("show_external", True)))
) and bool(load_settings().get("quick_panel_commands", True))
class EasyDiffPanelSetLeftCommand(sublime_plugin.TextCommand):
"""Panel set left command."""
def run(self, edit, external):
"""Run command."""
self.menu_options = ["View"]
if bool(load_settings().get("use_selections", True)):
self.menu_options.append("Selection(s)")
if bool(load_settings().get("use_clipboard", True)):
self.menu_options.append("Clipboard")
if len(self.menu_options) > 1:
self.view.window().show_quick_panel(self.menu_options, self.check_selection)
else:
self.check_selection(0)
def check_selection(self, value):
"""Check user's selection."""
if value != -1:
option = self.menu_options[value]
if option == "View":
self.view.window().run_command("easy_diff_set_left")
elif option == "Selection(s)":
self.view.window().run_command("easy_diff_set_left_selection")
elif option == "Clipboard":
self.view.window().run_command("easy_diff_set_left_clipboard")
def is_enabled(self, external):
"""Check if command is enabled."""
enabled = False
if not external and bool(load_settings().get("show_internal", True)):
enabled = True
elif external and bool(load_settings().get("show_external", True)):
enabled = True
return enabled
class EasyDiffPanelCompareCommand(sublime_plugin.TextCommand):
"""Manage panel compare commands."""
def run(self, edit, external):
"""Run command."""
self.external = external
self.menu_options = ["View"]
if bool(load_settings().get("use_selections", True)):
self.menu_options.append("Selection(s)")
if bool(load_settings().get("use_clipboard", True)):
self.menu_options.append("Clipboard")
if len(self.menu_options) > 1:
self.view.window().show_quick_panel(self.menu_options, self.check_selection)
else:
self.check_selection(0)
def check_selection(self, value):
"""Check user's selection."""
if value != -1:
option = self.menu_options[value]
if option == "View":
self.view.window().run_command("easy_diff_compare_both_view", {"external": self.external})
elif option == "Selection(s)":
self.view.window().run_command("easy_diff_compare_both_selection", {"external": self.external})
elif option == "Clipboard":
self.view.window().run_command("easy_diff_compare_both_clipboard", {"external": self.external})
def is_enabled(self, external):
"""Check if command is enabled."""
enabled = False
if not external and bool(load_settings().get("show_internal", True)):
enabled = True
elif external and bool(load_settings().get("show_external", True)):
enabled = True
return LEFT is not None and enabled
###############################
# View Close Listener
###############################
class EasyDiffListener(sublime_plugin.EventListener):
"""Listener for EasyDiff."""
current = None
last = None
def on_close(self, view):
"""Update menu on view close."""
global LEFT
vid = view.id()
if LEFT is not None and vid == LEFT["view_id"]:
LEFT = None
update_menu()
def on_activated(self, view):
"""Track last activated view."""
cls = EasyDiffListener
window = view.window()
if window is not None:
sheet = window.active_sheet()
group, index = window.get_sheet_index(sheet)
if group != -1:
if (group, index) != cls.current:
cls.last = cls.current
cls.current = (group, index)
###############################
# Loaders
###############################
def basic_reload():
"""Reload on settings change."""
global LEFT
LEFT = None
update_menu()
settings = load_settings()
settings.clear_on_change('reload_basic')
settings.add_on_change('reload_basic', basic_reload)
def plugin_loaded():
"""Plugin reload."""
basic_reload()