forked from facelessuser/EasyDiff
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheasy_diff_basic.py
312 lines (253 loc) · 10.2 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
"""
Easy Diff Basic Commands
Copyright (c) 2013 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
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):
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):
def get_selections(self):
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):
return self.view.encoding()
def has_selections(self):
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):
def run(self, edit, external=False, group=-1, index=-1):
if index != -1:
# Ensure we have the correct view
self.view = sublime.active_window().views_in_group(group)[index]
diff(self.get_right(), external=external)
def view_has_selections(self, group=-1, index=-1):
has_selections = False
if index != -1:
view = sublime.active_window().views_in_group(group)[index]
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):
return None
def check_enabled(self, group=-1, index=-1):
return True
def is_enabled(self, external=False, group=-1, index=-1):
return LEFT is not None and self.check_enabled(group, index)
class _EasyDiffCompareBothWindowCommand(sublime_plugin.WindowCommand):
no_view = False
def run(self, external=False, paths=[], group=-1, index=-1):
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):
if self.view.is_loading():
sublime.set_timeout(self.is_loaded, 100)
else:
self.diff()
def diff(self):
diff(self.get_right(), external=self.external)
def set_view(self, paths, group=-1, index=-1, open_file=True):
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 = self.window.views_in_group(group)[index]
else:
self.view = self.window.active_view()
def get_right(self):
return None
def check_enabled(self, paths=[], group=-1, index=-1):
return True
def is_enabled(self, external=False, paths=[], group=-1, index=-1):
return LEFT is not None and self.check_enabled(paths)
###############################
# Set View
###############################
class EasyDiffSetLeftCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], group=-1, index=-1):
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):
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 = self.window.views_in_group(group)[index]
else:
self.view = self.window.active_view()
def is_enabled(self, paths=[], group=-1, index=-1):
return get_target(paths, group, index) is not None if len(paths) or index != -1 else True
class EasyDiffCompareBothViewCommand(_EasyDiffCompareBothWindowCommand):
def get_right(self):
return {"win_id": self.view.window().id(), "view_id": self.view.id(), "clip": None}
def check_enabled(self, paths=[], group=-1, index=-1):
return True
def is_enabled(self, external=False, paths=[], group=-1, index=-1):
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):
def run(self, paths=[], group=-1, index=-1):
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):
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):
return bool(load_settings().get("use_clipboard", True))
class EasyDiffCompareBothClipboardCommand(_EasyDiffCompareBothWindowCommand):
no_view = True
def get_right(self):
return {"win_id": None, "view_id": None, "clip": EasyDiffView("**clipboard**", sublime.get_clipboard(), "UTF-8")}
def check_enabled(self, paths=[], group=-1, index=-1):
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):
return bool(load_settings().get("use_clipboard", True))
###############################
# Set Selection
###############################
class EasyDiffSetLeftSelectionCommand(sublime_plugin.TextCommand, _EasyDiffSelection):
def run(self, edit, group=-1, index=-1):
global LEFT
if index != -1:
# Ensure we have the correct view
self.view = sublime.active_window().views_in_group(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):
has_selections = False
if index != -1:
view = sublime.active_window().views_in_group(group)[index]
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):
return bool(load_settings().get("use_selections", True)) and self.view_has_selections(group, index)
def is_visible(self, group=-1, index=-1):
return bool(load_settings().get("use_selections", True))
class EasyDiffCompareBothSelectionCommand(_EasyDiffCompareBothTextCommand, _EasyDiffSelection):
def get_right(self):
return {"win_id": None, "view_id": None, "clip": EasyDiffView("**selection**", self.get_selections(), self.get_encoding())}
def check_enabled(self, group=-1, index=-1):
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):
return bool(load_settings().get("use_selections", True))
###############################
# View Close Listener
###############################
class EasyDiffListener(sublime_plugin.EventListener):
def on_close(self, view):
global LEFT
vid = view.id()
if LEFT is not None and vid == LEFT["view_id"]:
LEFT = None
update_menu()
###############################
# Loaders
###############################
def basic_reload():
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():
basic_reload()