-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patheasy_diff.py
187 lines (143 loc) · 5.1 KB
/
easy_diff.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
"""
Easy Diff.
Copyright (c) 2013 - 2015 Isaac Muse <[email protected]>
License: MIT
"""
import sublime
import time
import difflib
from os.path import basename, join, splitext, exists
from os import stat as osstat
import tempfile
from EasyDiff.easy_diff_global import load_settings, get_encoding, notify
import subprocess
LEFT = 1
RIGHT = 2
class EasyDiffView(object):
"""Simulate the look of a view."""
def __init__(self, name, content, encoding):
"""Initialize."""
self.filename = name
self.content = content
self.time = time.ctime()
self.encode = encoding
def encoding(self):
"""Return enconding."""
return self.encode
def get_time(self):
"""Get the time."""
return self.time
def file_name(self):
"""Get the file name."""
return self.filename
def substr(self, region):
"""Get the desired region from the content buffer."""
return self.content[region.begin():region.end() + 1]
def size(self):
"""Get the size."""
return len(self.content)
class EasyDiffInput(object):
"""Class for diff input."""
def __init__(self, v1, v2, external=False):
"""Initialize."""
self.untitled = False
self.temp_folder = None
self.process_view(v1, LEFT, external)
self.process_view(v2, RIGHT, external)
def process_view(self, view, side, external):
"""Process the view."""
self.side = side
name = view.file_name()
if name is None or not exists(name):
self.set_view_buffer(view, external)
elif isinstance(view, EasyDiffView):
self.set_special(view, external)
else:
self.set_view(view)
self.set_buffer(view, external)
def set_buffer(self, view, external):
"""Set buffer."""
setattr(
self,
"b%d" % self.side,
view.substr(sublime.Region(0, view.size())).splitlines() if not external else []
)
def set_view(self, view):
"""Set the view."""
setattr(self, "f%d" % self.side, view.file_name())
setattr(self, "t%d" % self.side, time.ctime(osstat(view.file_name()).st_mtime))
def set_special(self, view, external):
"""Set special."""
setattr(self, "f%d" % self.side, view.file_name())
if external:
setattr(self, "f%d" % self.side, self.create_temp(view, view.file_name()))
setattr(self, "t%d" % self.side, view.get_time())
def set_view_buffer(self, view, external):
"""Set view buffer."""
setattr(
self,
"f%d" % self.side,
self.create_temp(
view, "Untitled2" if self.untitled else "Untitled"
) if external else "Untitled2" if self.untitled else "Untitled"
)
setattr(self, "t%d" % self.side, time.ctime())
self.untitled = True
def create_temp(self, v, name):
"""Create temp file or file in temp folder."""
file_name = None
if self.temp_folder is None:
self.temp_folder = tempfile.mkdtemp(prefix="easydiff")
file_name = self.create_file(v, name)
else:
file_name = self.create_file(v, name)
return file_name
def create_file(self, v, name):
"""Create the actual temp file."""
root, ext = splitext(name)
with open(join(self.temp_folder, "%s-%s%s" % (root, "LEFT" if self.side == LEFT else "RIGHT", ext)), "wb") as f:
encoding = get_encoding(v)
try:
bfr = v.substr(sublime.Region(0, v.size())).encode(encoding)
except Exception:
bfr = v.substr(sublime.Region(0, v.size())).encode("utf-8")
f.write(bfr)
return f.name
class EasyDiff(object):
"""Basic diff object."""
@classmethod
def extcompare(cls, inputs, ext_diff):
"""External app compare."""
subprocess.Popen(
[
ext_diff,
inputs.f1,
inputs.f2
]
)
@classmethod
def compare(cls, inputs):
"""Compare the views."""
diff = difflib.unified_diff(
inputs.b1, inputs.b2,
inputs.f1, inputs.f2,
inputs.t1, inputs.t2,
lineterm=''
)
result = u"\n".join(line for line in diff)
if result == "":
notify("No Difference")
return
use_buffer = bool(load_settings().get("use_buffer", False))
win = sublime.active_window()
if use_buffer:
v = win.new_file()
v.set_name("EasyDiff: %s -> %s (%s)" % (basename(inputs.f1), basename(inputs.f2), time.ctime()))
v.set_scratch(True)
v.assign_syntax('Packages/Diff/Diff.tmLanguage')
v.run_command('append', {'characters': result})
else:
v = win.create_output_panel('easy_diff')
v.assign_syntax('Packages/Diff/Diff.tmLanguage')
v.run_command('append', {'characters': result})
win.run_command("show_panel", {"panel": "output.easy_diff"})