-
Notifications
You must be signed in to change notification settings - Fork 6
/
diffy.py
67 lines (50 loc) · 2.03 KB
/
diffy.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
import sublime, sublime_plugin
import sys
if sys.version_info >= (3, 0):
from .diffy_lib import diffier
else:
from diffy_lib import diffier
class DiffyCommand(sublime_plugin.TextCommand):
def get_entire_content(self, view):
selection = sublime.Region(0, view.size())
content = view.substr(selection)
return content
def clear(self, view):
view.erase_regions('highlighted_lines')
"""
return the marked lines
"""
def draw_difference(self, view, diffs):
self.clear(view)
lines = [d.get_region(view) for d in diffs]
view.add_regions(
'highlighted_lines',
lines,
'keyword',
'dot',
sublime.DRAW_OUTLINED
)
return lines
def set_view_point(self, view, lines):
if len(lines) > 0:
view.show(lines[0])
def run(self, edit, **kwargs):
diffy = diffier.Diffy()
window = self.view.window()
action = kwargs.get('action', None)
view_1 = window.selected_sheets()[0].view() if len(window.selected_sheets()) >= 2 else window.active_view_in_group(0)
view_2 = window.selected_sheets()[1].view() if len(window.selected_sheets()) >= 2 else window.active_view_in_group(1)
if action == 'clear':
if view_1: self.clear(view_1)
if view_2: self.clear(view_2)
else:
#make sure there are 2 columns side by side
if view_1 and view_2:
text_1 = self.get_entire_content(view_1)
text_2 = self.get_entire_content(view_2)
if len(text_1) > 0 and len(text_2) > 0:
diff_1, diff_2 = diffy.calculate_diff(text_1.split('\n'), text_2.split('\n'))
highlighted_lines_1 = self.draw_difference(view_1, diff_1)
highlighted_lines_2 = self.draw_difference(view_2, diff_2)
self.set_view_point(view_1, highlighted_lines_1)
self.set_view_point(view_2, highlighted_lines_2)