|
5 | 5 | import sublime |
6 | 6 | import sublime_plugin |
7 | 7 | import difflib |
| 8 | +import tempfile |
8 | 9 |
|
9 | 10 | from fnmatch import fnmatch |
10 | 11 | import codecs |
|
17 | 18 | FILE = u'Diff file with File in Project…' |
18 | 19 | TAB = u'Diff file with Open Tab…' |
19 | 20 |
|
20 | | - |
21 | 21 | FILE_DIFFS = [CLIPBOARD, SAVED, FILE, TAB] |
22 | 22 |
|
23 | | - |
24 | 23 | class FileDiffMenuCommand(sublime_plugin.TextCommand): |
25 | 24 | def run(self, edit): |
26 | 25 | menu_items = FILE_DIFFS[:] |
@@ -68,7 +67,7 @@ def diff_content(self): |
68 | 67 |
|
69 | 68 | def run_diff(self, a, b, from_file=None, to_file=None): |
70 | 69 | from_content = a |
71 | | - to_content = b |
| 70 | + to_content = b |
72 | 71 |
|
73 | 72 | if os.path.exists(a): |
74 | 73 | if from_file is None: |
@@ -96,21 +95,33 @@ def run_diff(self, a, b, from_file=None, to_file=None): |
96 | 95 | return diffs |
97 | 96 |
|
98 | 97 | 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 | + |
100 | 108 | command = SETTINGS.get('cmd') |
101 | 109 | if command is not None: |
102 | 110 | command = [c.replace(u'$file1', from_file) for c in command] |
103 | 111 | command = [c.replace(u'$file2', to_file) for c in command] |
104 | 112 | self.view.window().run_command("exec", {"cmd": command}) |
105 | 113 |
|
| 114 | + |
106 | 115 | def show_diff(self, diffs): |
107 | 116 | 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) |
114 | 125 | else: |
115 | 126 | sublime.status_message('No Difference') |
116 | 127 |
|
|
0 commit comments