From fe73ef01a36974271dddf36f7e1267c972f08d6d Mon Sep 17 00:00:00 2001 From: Colin Thomas-Arnold Date: Wed, 29 Feb 2012 12:18:04 -0700 Subject: [PATCH] added filenames back, instead of 'from_file' and 'to_file' --- file_diffs.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/file_diffs.py b/file_diffs.py index 9c1d6b8..85b5178 100644 --- a/file_diffs.py +++ b/file_diffs.py @@ -65,28 +65,29 @@ def diff_content(self): content = self.view.substr(sublime.Region(0, self.view.size())) return content - def run_diff(self, a, b): + def run_diff(self, a, b, from_file=None, to_file=None): from_content = a - from_file = None - to_content = b - to_file = None if os.path.exists(a): - from_file = a + if from_file is None: + from_file = a with codecs.open(from_file, mode='U', encoding='utf-8') as f: from_content = f.readlines() else: from_content = a.splitlines(True) - from_file = 'from_file' + if from_file is None: + from_file = 'from_file' if os.path.exists(b): - to_file = b + if to_file is None: + to_file = b with codecs.open(to_file, mode='U', encoding='utf-8') as f: to_content = f.readlines() else: to_content = b.splitlines(True) - to_file = 'to_file' + if to_file is None: + to_file = 'to_file' diff = difflib.unified_diff(from_content, to_content, from_file, to_file) @@ -111,7 +112,9 @@ def show_diff(self, diff): class FileDiffClipboardCommand(FileDiffCommand): def run(self, edit, **kwargs): current = sublime.get_clipboard() - diff = self.run_diff(self.diff_content(), current) + diff = self.run_diff(self.diff_content(), current, + from_file=self.view.file_name(), + to_file='(clipboard)') self.show_diff(diff) @@ -154,7 +157,9 @@ def run(self, edit, **kwargs): if indent: diff = u"\n".join(line[len(indent):] for line in diff.splitlines()) - self.show_diff(self.run_diff(current, diff)) + self.show_diff(self.run_diff(current, diff), + from_file='first selection', + to_file='second selection') class FileDiffSavedCommand(FileDiffCommand): @@ -168,7 +173,9 @@ def run(self, edit, **kwargs): if not content: content = self.view.substr(sublime.Region(0, self.view.size())) - diff = self.run_diff(self.view.file_name(), content) + diff = self.run_diff(self.view.file_name(), content, + from_file=self.view.file_name(), + to_file=self.view.file_name() + u' (Unsaved)') self.show_diff(diff) @@ -194,7 +201,8 @@ def run(self, edit, **kwargs): def on_done(index): if index > -1: - diff = self.run_diff(self.diff_content(), files[index]) + diff = self.run_diff(self.diff_content(), files[index], + from_file=self.view.file_name()) self.show_diff(diff) self.view.window().show_quick_panel(file_picker, on_done) @@ -244,7 +252,9 @@ def run(self, edit, **kwargs): def on_done(index): if index > -1: - diff = self.run_diff(self.diff_content(), contents[index]) + diff = self.run_diff(self.diff_content(), contents[index], + from_file=self.view.file_name(), + to_file=files[index]) self.show_diff(diff) if len(files) == 1: