Skip to content

Commit 8df3a61

Browse files
committed
added 'Diff Selections' command
1 parent 4bdd32d commit 8df3a61

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FileDiffs Plugin for Sublime Text 2
22
===================================
33

4-
Show diffs between the current file, or selection(s) in the current file, and clipboard, another file, or unsaved changes.
4+
Show diffs between the current file (or selection(s) in the current file) with the clipboard, another file, or unsaved changes.
55

66

77
Installation
@@ -28,6 +28,8 @@ The rest of the commands are not bound by default:
2828

2929
`file_diff_clipboard`: Shows the diff of the current file or selection(s) and the clipboard (the clipboard is considered the "new" file unless `reverse` is True)
3030

31+
`file_diff_selections`: Shows the diff of the first and second selected regions. The file_diff_menu command checks for exactly two regions selected, otherwise it doesn't display this command.
32+
3133
`file_diff_saved`: Shows the diff of the current file or selection(s) and the saved file.
3234

3335
`file_diff_file`: Shows the diff of the current file or selection(s) and a file that is in the current project.

file_diffs.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
CLIPBOARD = u'Diff file with Clipboard'
13+
SELECTIONS = u'Diff Selections'
1314
SAVED = u'Diff file with Saved'
1415
FILE = u'Diff file with File in Project…'
1516
TAB = u'Diff file with Open Tab…'
@@ -21,17 +22,19 @@
2122
class FileDiffMenuCommand(sublime_plugin.TextCommand):
2223
def run(self, edit):
2324
menu_items = FILE_DIFFS
24-
regions = [region for region in self.view.sel()]
25-
for region in regions:
26-
if not region.empty():
27-
menu_items = [f.replace(u'Diff file', u'Diff selection') for f in FILE_DIFFS]
28-
break
25+
regions = self.view.sel()
26+
if len(regions) == 1 and not regions[0].empty():
27+
menu_items = [f.replace(u'Diff file', u'Diff selection') for f in FILE_DIFFS]
28+
elif len(regions) == 2:
29+
menu_items.insert(1, SELECTIONS)
2930

3031
def on_done(index):
3132
if index == -1:
3233
return
3334
elif FILE_DIFFS[index] == CLIPBOARD:
3435
self.view.run_command('file_diff_clipboard')
36+
elif FILE_DIFFS[index] == SELECTIONS:
37+
self.view.run_command('file_diff_selections')
3538
elif FILE_DIFFS[index] == SAVED:
3639
self.view.run_command('file_diff_saved')
3740
elif FILE_DIFFS[index] == FILE:
@@ -104,6 +107,15 @@ def run(self, edit, **kwargs):
104107
self.show_diff(diff)
105108

106109

110+
class FileDiffSelectionsCommand(FileDiffCommand):
111+
def run(self, edit, **kwargs):
112+
regions = self.view.sel()
113+
current = self.view.substr(regions[0])
114+
diff = self.view.substr(regions[1])
115+
diff = self.run_diff(current, diff)
116+
self.show_diff(diff)
117+
118+
107119
class FileDiffSavedCommand(FileDiffCommand):
108120
def run(self, edit, **kwargs):
109121
content = ''

0 commit comments

Comments
 (0)