@@ -100,14 +100,14 @@ def run_diff(self, a, b, from_file, to_file, **options):
100100 open_in_sublime = self .settings ().get ('open_in_sublime' , not external_command )
101101
102102 if external_command :
103- self .diff_with_external (external_command , a , b , from_file , to_file )
103+ self .diff_with_external (external_command , a , b , from_file , to_file , ** options )
104104
105105 if open_in_sublime :
106106 # fix diffs
107107 diffs = map (lambda line : (line and line [- 1 ] == "\n " ) and line or line + "\n " , diffs )
108108 self .diff_in_sublime (diffs )
109109
110- def diff_with_external (self , external_command , a , b , from_file = None , to_file = None ):
110+ def diff_with_external (self , external_command , a , b , from_file = None , to_file = None , ** options ):
111111 try :
112112 try :
113113 from_file_exists = os .path .exists (from_file )
@@ -166,6 +166,17 @@ def trim_trailing_white_space(file_name):
166166 Popen (external_command )
167167 else :
168168 subprocess .Popen (external_command )
169+
170+ apply_tempfile_changes_after_diff_tool = self .settings ().get ('apply_tempfile_changes_after_diff_tool' , False )
171+ post_diff_tool = options .get ('post_diff_tool' )
172+ if apply_tempfile_changes_after_diff_tool and post_diff_tool is not None and (not from_file_exists or not to_file_exists ):
173+ if from_file_exists :
174+ from_file = None
175+ if to_file_exists :
176+ to_file = None
177+ # Use a dialog to block st and wait for the closing of the diff tool
178+ if sublime .ok_cancel_dialog ("Apply changes from tempfile after external diff tool execution?" ):
179+ post_diff_tool (from_file , to_file )
169180 except Exception as e :
170181 # some basic logging here, since we are cluttering the /tmp folder
171182 sublime .status_message (str (e ))
@@ -193,6 +204,27 @@ def get_file_name(self, view, default_name):
193204 file_name = default_name
194205 return file_name
195206
207+ def get_content_from_file (self , file_name ):
208+ with codecs .open (file_name , encoding = 'utf-8' , mode = 'r' ) as f :
209+ lines = f .readlines ()
210+ lines = [line .replace ("\r \n " , "\n " ).replace ("\r " , "\n " ) for line in lines ]
211+ content = '' .join (lines )
212+ return content
213+
214+ def update_view (self , view , edit , tmp_file ):
215+ if tmp_file :
216+ non_empty_regions = [region for region in view .sel () if not region .empty ()]
217+ nb_non_empty_regions = len (non_empty_regions )
218+ region = None
219+ if nb_non_empty_regions == 0 :
220+ region = sublime .Region (0 , view .size ())
221+ elif nb_non_empty_regions == 1 :
222+ region = non_empty_regions [0 ]
223+ else :
224+ sublime .status_message ('Cannot update multiselection' )
225+ return
226+ view .replace (edit , region , self .get_content_from_file (tmp_file ))
227+
196228
197229class FileDiffDummy1Command (sublime_plugin .TextCommand ):
198230 def run (self , edit , content ):
@@ -207,6 +239,11 @@ def run(self, edit, **kwargs):
207239 from_file += ' (Selection)'
208240 break
209241 clipboard = sublime .get_clipboard ()
242+ def on_post_diff_tool (from_file , to_file ):
243+ self .update_view (self .view , edit , from_file )
244+ sublime .set_clipboard (self .get_content_from_file (to_file ))
245+
246+ kwargs .update ({'post_diff_tool' : on_post_diff_tool })
210247 self .run_diff (self .diff_content (self .view ), clipboard ,
211248 from_file = from_file ,
212249 to_file = '(clipboard)' ,
@@ -264,6 +301,10 @@ def is_visible(self):
264301
265302class FileDiffSavedCommand (FileDiffCommand ):
266303 def run (self , edit , ** kwargs ):
304+ def on_post_diff_tool (from_file , to_file ):
305+ self .update_view (self .view , edit , to_file )
306+
307+ kwargs .update ({'post_diff_tool' : on_post_diff_tool })
267308 self .run_diff (self .read_file (self .view .file_name ()), self .diff_content (self .view ),
268309 from_file = self .view .file_name (),
269310 to_file = self .view .file_name () + ' (Unsaved)' ,
@@ -334,6 +375,7 @@ def run(self, edit, **kwargs):
334375 my_id = self .view .id ()
335376 files = []
336377 contents = []
378+ views = []
337379 untitled_count = 1
338380 for v in self .view .window ().views ():
339381 if v .id () != my_id :
@@ -347,9 +389,15 @@ def run(self, edit, **kwargs):
347389 untitled_count += 1
348390
349391 contents .append (this_content )
392+ views .append (v )
350393
351394 def on_done (index ):
352395 if index > - 1 :
396+ def on_post_diff_tool (from_file , to_file ):
397+ self .update_view (self .view , edit , from_file )
398+ self .update_view (views [index ], edit , to_file )
399+
400+ kwargs .update ({'post_diff_tool' : on_post_diff_tool })
353401 self .run_diff (self .diff_content (self .view ), contents [index ],
354402 from_file = self .view .file_name (),
355403 to_file = files [index ],
@@ -373,6 +421,11 @@ def is_visible(self):
373421class FileDiffPreviousCommand (FileDiffCommand ):
374422 def run (self , edit , ** kwargs ):
375423 if previous_view :
424+ def on_post_diff_tool (from_file , to_file ):
425+ self .update_view (previous_view , edit , from_file )
426+ self .update_view (current_view , edit , to_file )
427+
428+ kwargs .update ({'post_diff_tool' : on_post_diff_tool })
376429 self .run_diff (self .diff_content (previous_view ), self .diff_content (self .view ),
377430 from_file = self .get_file_name (previous_view , 'untitled (Previous)' ),
378431 to_file = self .get_file_name (self .view , 'untitled (Current)' ),
0 commit comments