Skip to content

Commit 74584d3

Browse files
committed
Improve file_diff_previous command with selection
1 parent 5b9051a commit 74584d3

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

file_diffs.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ class FileDiffCommand(sublime_plugin.TextCommand):
6161
def settings(self):
6262
return sublime.load_settings('FileDiffs.sublime-settings')
6363

64-
def diff_content(self):
64+
def diff_content(self, view):
6565
content = ''
6666

67-
for region in self.view.sel():
67+
for region in view.sel():
6868
if region.empty():
6969
continue
70-
content += self.view.substr(region)
70+
content += view.substr(region)
7171

7272
if not content:
73-
content = self.view.substr(sublime.Region(0, self.view.size()))
73+
content = view.substr(sublime.Region(0, view.size()))
7474
return content
7575

7676
def prep_content(self, ab, file_name, default_name):
@@ -183,6 +183,16 @@ def read_file(self, file_name):
183183
content = f.read()
184184
return content
185185

186+
def get_file_name(self, view, default_name):
187+
file_name = ''
188+
if view.file_name():
189+
file_name = view.file_name()
190+
elif view.name():
191+
file_name = view.name()
192+
else:
193+
file_name = default_name
194+
return file_name
195+
186196

187197
class FileDiffDummy1Command(sublime_plugin.TextCommand):
188198
def run(self, edit, content):
@@ -197,7 +207,7 @@ def run(self, edit, **kwargs):
197207
from_file += ' (Selection)'
198208
break
199209
clipboard = sublime.get_clipboard()
200-
self.run_diff(self.diff_content(), clipboard,
210+
self.run_diff(self.diff_content(self.view), clipboard,
201211
from_file=from_file,
202212
to_file='(clipboard)',
203213
**kwargs)
@@ -254,7 +264,7 @@ def is_visible(self):
254264

255265
class FileDiffSavedCommand(FileDiffCommand):
256266
def run(self, edit, **kwargs):
257-
self.run_diff(self.read_file(self.view.file_name()), self.diff_content(),
267+
self.run_diff(self.read_file(self.view.file_name()), self.diff_content(self.view),
258268
from_file=self.view.file_name(),
259269
to_file=self.view.file_name() + ' (Unsaved)',
260270
**kwargs)
@@ -285,7 +295,7 @@ def run(self, edit, **kwargs):
285295

286296
def on_done(index):
287297
if index > -1:
288-
self.run_diff(self.diff_content(), self.read_file(files[index]),
298+
self.run_diff(self.diff_content(self.view), self.read_file(files[index]),
289299
from_file=self.view.file_name(),
290300
to_file=files[index],
291301
**kwargs)
@@ -340,7 +350,7 @@ def run(self, edit, **kwargs):
340350

341351
def on_done(index):
342352
if index > -1:
343-
self.run_diff(self.diff_content(), contents[index],
353+
self.run_diff(self.diff_content(self.view), contents[index],
344354
from_file=self.view.file_name(),
345355
to_file=files[index],
346356
**kwargs)
@@ -363,26 +373,9 @@ def is_visible(self):
363373
class FileDiffPreviousCommand(FileDiffCommand):
364374
def run(self, edit, **kwargs):
365375
if previous_view:
366-
previous_view_content = previous_view.substr(sublime.Region(0, previous_view.size()))
367-
previous_view_name = ''
368-
if previous_view.file_name():
369-
previous_view_name = previous_view.file_name()
370-
elif previous_view.name():
371-
previous_view_name = previous_view.name()
372-
else:
373-
previous_view_name = 'untitled (Previous)'
374-
375-
view_name = ''
376-
if self.view.file_name():
377-
view_name = self.view.file_name()
378-
elif self.view.name():
379-
view_name = self.view.name()
380-
else:
381-
view_name = 'untitled (Current)'
382-
383-
self.run_diff(previous_view_content, self.diff_content(),
384-
from_file=previous_view_name,
385-
to_file=view_name,
376+
self.run_diff(self.diff_content(previous_view), self.diff_content(self.view),
377+
from_file=self.get_file_name(previous_view, 'untitled (Previous)'),
378+
to_file=self.get_file_name(self.view, 'untitled (Current)'),
386379
**kwargs)
387380

388381
def is_visible(self):

0 commit comments

Comments
 (0)