-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiffable.py
89 lines (68 loc) · 3.23 KB
/
diffable.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
from sublime import View, Region, load_settings, version
from sublime_plugin import WindowCommand
import os
import subprocess
class Diffable(WindowCommand):
def __get_view__(self, number: int) -> View:
if int(version()) >= 4000: # ST4
## TODO: Validate that there's just 2 groups or tabs somehow.
return self.window.selected_sheets()[number].view() if len(self.window.selected_sheets()) == 2 else self.window.active_view_in_group(number)
else: # ST3
return self.window.active_view_in_group(number)
def is_enabled(self):
return True if self.__get_view__(0) and self.__get_view__(1) else False
def get_entire_content(self, view):
selection = Region(0, view.size())
content = view.substr(selection)
return content
def toggle_inline_diff(self, view):
sel = view.sel()
old_sel = list(sel)
sel.clear()
view.run_command("select_all")
view.run_command("toggle_inline_diff")
sel.clear()
sel.add_all(old_sel)
def run(self, **kwargs):
self.settings = load_settings("Diffable.sublime-settings")
action = kwargs.get('action', None)
view_1 = self.__get_view__(0)
view_2 = self.__get_view__(1)
if action == 'clear':
if view_1: view_1.reset_reference_document()
if view_2: view_2.reset_reference_document()
if view_1 and view_2:
text_left = self.get_entire_content(view_1)
text_right = self.get_entire_content(view_2)
if action == 'inline':
if self.settings.get("two_panes_mode"):
view_2.set_reference_document(text_left)
view_1.set_reference_document(text_right)
if self.settings.get("auto_show_diff_hunks"):
self.toggle_inline_diff(view_2)
self.toggle_inline_diff(view_1)
else:
if self.settings.get("left_to_right"):
view_2.set_reference_document(text_left)
if self.settings.get("auto_show_diff_hunks"):
self.toggle_inline_diff(view_2)
else:
view_1.set_reference_document(text_right)
if self.settings.get("auto_show_diff_hunks"):
self.toggle_inline_diff(view_1)
elif action == 'kaleidoscope':
self.write_in_pipe(text_left, text_right)
def write_in_pipe(self, left_text, right_test):
lhs_read_fd, lhs_write_fd = os.pipe()
rhs_read_fd, rhs_write_fd = os.pipe()
lhs_bytes = bytes(left_text, encoding='utf-8')
rhs_bytes = bytes(right_test, encoding='utf-8')
lhs_path = "/dev/fd/" + str(lhs_read_fd)
rhs_path = "/dev/fd/" + str(rhs_read_fd)
ksdiff_path = self.settings.get("kaleidoscope_path")
ksdiff = subprocess.Popen([ksdiff_path, "-l", "Sublime Text", lhs_path, rhs_path], pass_fds=[lhs_read_fd, rhs_read_fd], close_fds=True)
os.write(lhs_write_fd, lhs_bytes)
os.close(lhs_write_fd)
os.write(rhs_write_fd, rhs_bytes)
os.close(rhs_write_fd)
_, _ = ksdiff.communicate()