Skip to content

Commit 0a5b545

Browse files
committed
Added option to open only in external editor
Added feature to open all diffs in external editor
1 parent acdb099 commit 0a5b545

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

FileDiffs.sublime-settings

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
// just uncomment one of the examples
44
// or write your own command
55

6-
// opendiff (FileMerge)
7-
//"cmd": ["opendiff", "$file1", "$file2"]
6+
// opendiff (FileMerge)
7+
// "cmd": ["opendiff", "$file1", "$file2"]
88

99
// ksdiff (Kaleidoscope)
10-
//"cmd": ["ksdiff", "$file1", "$file2"]
10+
// "cmd": ["ksdiff", "$file1", "$file2"]
1111

12+
// "open_in_sublime": false
1213
}

file_diffs.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sublime
66
import sublime_plugin
77
import difflib
8+
import tempfile
89

910
from fnmatch import fnmatch
1011
import codecs
@@ -17,10 +18,8 @@
1718
FILE = u'Diff file with File in Project…'
1819
TAB = u'Diff file with Open Tab…'
1920

20-
2121
FILE_DIFFS = [CLIPBOARD, SAVED, FILE, TAB]
2222

23-
2423
class FileDiffMenuCommand(sublime_plugin.TextCommand):
2524
def run(self, edit):
2625
menu_items = FILE_DIFFS[:]
@@ -68,7 +67,7 @@ def diff_content(self):
6867

6968
def run_diff(self, a, b, from_file=None, to_file=None):
7069
from_content = a
71-
to_content = b
70+
to_content = b
7271

7372
if os.path.exists(a):
7473
if from_file is None:
@@ -96,21 +95,33 @@ def run_diff(self, a, b, from_file=None, to_file=None):
9695
return diffs
9796

9897
def diff_with_external(self, a, b, from_file=None, to_file=None):
99-
if os.path.exists(from_file) and os.path.exists(to_file):
98+
if os.path.exists(from_file):
99+
if not os.path.exists(to_file):
100+
tmpFile = tempfile.NamedTemporaryFile(delete=False)
101+
to_file = tmpFile.name
102+
tmpFile.close()
103+
104+
tmpFile = codecs.open(to_file, encoding='utf-8', mode='w+')
105+
tmpFile.write(b)
106+
tmpFile.close()
107+
100108
command = SETTINGS.get('cmd')
101109
if command is not None:
102110
command = [c.replace(u'$file1', from_file) for c in command]
103111
command = [c.replace(u'$file2', to_file) for c in command]
104112
self.view.window().run_command("exec", {"cmd": command})
105113

114+
106115
def show_diff(self, diffs):
107116
if diffs:
108-
scratch = self.view.window().new_file()
109-
scratch.set_scratch(True)
110-
scratch.set_syntax_file('Packages/Diff/Diff.tmLanguage')
111-
scratch_edit = scratch.begin_edit('file_diffs')
112-
scratch.insert(scratch_edit, 0, ''.join(diffs))
113-
scratch.end_edit(scratch_edit)
117+
command = SETTINGS.get('open_in_sublime')
118+
if command is None or command is True:
119+
scratch = self.view.window().new_file()
120+
scratch.set_scratch(True)
121+
scratch.set_syntax_file('Packages/Diff/Diff.tmLanguage')
122+
scratch_edit = scratch.begin_edit('file_diffs')
123+
scratch.insert(scratch_edit, 0, ''.join(diffs))
124+
scratch.end_edit(scratch_edit)
114125
else:
115126
sublime.status_message('No Difference')
116127

0 commit comments

Comments
 (0)