Skip to content

Commit 69125f7

Browse files
committed
refactor of run_diff to support internal (in sublime), external, or both, and using @jiriurban's tempfile support.
1 parent edd2fee commit 69125f7

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

file_diffs.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,53 @@ def run_diff(self, a, b, from_file=None, to_file=None):
9292

9393
diffs = list(difflib.unified_diff(from_content, to_content, from_file, to_file))
9494

95-
FileDiffCommand.diff_with_external(self, a, b, from_file, to_file)
96-
return diffs
95+
open_in_sublime = SETTINGS.get('open_in_sublime', True)
96+
external_command = SETTINGS.get('cmd')
97+
if not diffs:
98+
sublime.status_message('No Difference')
99+
else:
100+
if external_command:
101+
self.diff_with_external(a, b, from_file, to_file)
102+
103+
if open_in_sublime:
104+
self.diff_in_sublime(diffs)
97105

98106
def diff_with_external(self, a, b, from_file=None, to_file=None):
99-
if os.path.exists(from_file):
100-
if not os.path.exists(to_file):
107+
try:
108+
if not os.path.exists(from_file):
101109
tmp_file = tempfile.NamedTemporaryFile(delete=False)
102-
to_file = tmp_file.name
110+
from_file = tmp_file.name
103111
tmp_file.close()
104112

105-
tmp_file = codecs.open(to_file, encoding='utf-8', mode='w+')
106-
tmp_file.write(b)
113+
with codecs.open(from_file, encoding='utf-8', mode='w+') as tmp_file:
114+
tmp_file.write(a)
115+
116+
if not os.path.exists(to_file):
117+
tmp_file = tempfile.NamedTemporaryFile(delete=False)
118+
to_file = tmp_file.name
107119
tmp_file.close()
108120

109-
command = SETTINGS.get('cmd')
110-
if command is not None:
111-
command = [c.replace(u'$file1', from_file) for c in command]
112-
command = [c.replace(u'$file2', to_file) for c in command]
113-
self.view.window().run_command("exec", {"cmd": command})
114-
115-
def show_diff(self, diffs):
116-
if diffs:
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)
125-
else:
126-
sublime.status_message('No Difference')
121+
with codecs.open(to_file, encoding='utf-8', mode='w+') as tmp_file:
122+
tmp_file.write(b)
123+
124+
if os.path.exists(from_file):
125+
command = SETTINGS.get('cmd')
126+
if command is not None:
127+
command = [c.replace(u'$file1', from_file) for c in command]
128+
command = [c.replace(u'$file2', to_file) for c in command]
129+
self.view.window().run_command("exec", {"cmd": command})
130+
except Exception as e:
131+
# some basic logging here, since we are cluttering the /tmp folder
132+
print repr(e)
133+
sublime.status_message(str(e))
134+
135+
def diff_in_sublime(self, diffs):
136+
scratch = self.view.window().new_file()
137+
scratch.set_scratch(True)
138+
scratch.set_syntax_file('Packages/Diff/Diff.tmLanguage')
139+
scratch_edit = scratch.begin_edit('file_diffs')
140+
scratch.insert(scratch_edit, 0, ''.join(diffs))
141+
scratch.end_edit(scratch_edit)
127142

128143

129144
class FileDiffClipboardCommand(FileDiffCommand):

0 commit comments

Comments
 (0)