4747
4848class AdvancedNewFileCommand (sublime_plugin .WindowCommand ):
4949 def run (self , is_python = False , initial_path = None , rename = False , delete = False , rename_file = None ):
50+ self .view = self .window .active_view ()
51+ self .settings = get_settings (self .view )
5052 if delete :
5153 self .delete_current_file ()
5254 return
@@ -57,11 +59,9 @@ def run(self, is_python=False, initial_path=None, rename=False, delete=False, re
5759 self .top_level_split_char = ":"
5860 self .is_python = is_python
5961 self .rename = rename
60- self .view = self .window .active_view ()
6162 self .rename_filename = rename_file
6263
6364 # Settings will be based on the view
64- self .settings = get_settings (self .view )
6565 self .aliases = self .get_aliases ()
6666 self .show_path = self .settings .get ("show_path" )
6767 self .default_folder_index = self .settings .get ("default_folder_index" )
@@ -498,13 +498,23 @@ def entered_filename(self, filename):
498498 else :
499499 attempt_open = True
500500 logger .debug ("Creating file at %s" , file_path )
501- if not os .path .exists (file_path ):
502- try :
503- self .create (file_path )
504- except OSError as e :
505- attempt_open = False
506- sublime .error_message ("Cannot create '" + file_path + "'. See console for details" )
507- logger .error ("Exception: %s '%s'" % (e .strerror , e .filename ))
501+ if self .rename :
502+ path = os .path .dirname (file_path )
503+ if not os .path .exists (path ):
504+ try :
505+ self .create_folder (path )
506+ except OSError as e :
507+ attempt_open = False
508+ sublime .error_message ("Cannot create '" + file_path + "'. See console for details" )
509+ logger .error ("Exception: %s '%s'" % (e .strerror , e .filename ))
510+ else :
511+ if not os .path .exists (file_path ):
512+ try :
513+ self .create (file_path )
514+ except OSError as e :
515+ attempt_open = False
516+ sublime .error_message ("Cannot create '" + file_path + "'. See console for details" )
517+ logger .error ("Exception: %s '%s'" % (e .strerror , e .filename ))
508518 if attempt_open :
509519 if self .rename :
510520 self .rename_file (file_path )
@@ -524,15 +534,15 @@ def open_file(self, file_path):
524534 return new_view
525535
526536 def rename_file (self , file_path ):
527- if os .path .isdir (file_path ):
537+ if os .path .isdir (file_path ) or re . search ( r"(/|\\)$" , file_path ) :
528538 # use original name if a directory path has been passed in.
529539 file_path = os .path .join (file_path , self .original_name )
530540
531- tracked_by_git = self .file_tracked_by_git (self .view .file_name ())
532541 window = self .window
533542 if self .rename_filename :
543+ tracked_by_git = self .file_tracked_by_git (self .rename_filename )
534544 if tracked_by_git :
535- self .git_mv (self .view . file_name () , file_path )
545+ self .git_mv (self .rename_filename , file_path )
536546 else :
537547 shutil .move (self .rename_filename , file_path )
538548 file_view = self .find_open_file (self .rename_filename )
@@ -541,15 +551,17 @@ def rename_file(self, file_path):
541551 window .run_command ("close" )
542552 self .open_file (file_path )
543553
544- elif self .view :
545- if self .view .file_name ():
554+ elif self .view is not None and self .view .file_name () is not None :
555+ filename = self .view .file_name ()
556+ tracked_by_git = self .file_tracked_by_git (filename )
557+ if filename :
546558 self .view .run_command ("save" )
547559 window .focus_view (self .view )
548560 window .run_command ("close" )
549561 if tracked_by_git :
550- self .git_mv (self . view . file_name () , file_path )
562+ self .git_mv (filename , file_path )
551563 else :
552- shutil .move (self . view . file_name () , file_path )
564+ shutil .move (filename , file_path )
553565 else :
554566 content = self .view .substr (sublime .Region (0 , self .view .size ()))
555567 self .view .set_scratch (True )
@@ -669,7 +681,8 @@ def file_tracked_by_git(self, filepath):
669681
670682 def git_mv (self , from_filepath , to_filepath ):
671683 path , filename = os .path .split (from_filepath )
672- result = subprocess .call (['git' , 'mv' , filename , to_filepath ], cwd = path )
684+ args = ["git" , "mv" , filename , to_filepath ]
685+ result = subprocess .call (args , cwd = path )
673686 if result != 0 :
674687 sublime .error_message ("Git move of %s to %s failed" % (from_filepath , to_filepath ))
675688
0 commit comments