22# @Author: Lime
33# @Date: 2013-10-28 13:39:48
44# @Last Modified by: Lime
5- # @Last Modified time: 2016-02-23 15:10:05
5+ # @Last Modified time: 2016-02-29 12:32:35
66
77import os
88import sys
2020import sublime
2121import sublime_plugin
2222
23-
2423PLUGIN_NAME = 'FileHeader'
2524INSTALLED_PLUGIN_NAME = '%s.sublime-package' % PLUGIN_NAME
2625PACKAGES_PATH = sublime .packages_path ()
@@ -148,8 +147,9 @@ def get_user():
148147 '''Get user'''
149148
150149 user = getpass .getuser ()
150+ output , error = getOutputError (
151+ 'cd {} && git status' .format (get_dir_path ()))
151152
152- output , error = getOutputError ('git status' )
153153 if not error :
154154 output , error = getOutputError ('git config --get user.name' )
155155 if not error and output :
@@ -167,6 +167,17 @@ def get_project_name():
167167 return project
168168
169169
170+ def get_dir_path ():
171+ '''Get current file dir path'''
172+
173+ view , path = Window ().active_view (), None
174+ if view :
175+ file_name = view .file_name ()
176+ if file_name is not None :
177+ path = os .path .dirname (file_name )
178+ return path
179+
180+
170181def get_file_path (path ):
171182 '''Get absolute path of the file'''
172183
@@ -370,11 +381,7 @@ def new_view(self, syntax_type, name):
370381 def get_path (self , paths ):
371382 path = None
372383 if not paths :
373- current_view = Window ().active_view ()
374- if current_view :
375- file_name = current_view .file_name ()
376- if file_name is not None :
377- path = os .path .dirname (file_name )
384+ path = get_dir_path ()
378385 else :
379386 path = paths [0 ]
380387 if not os .path .isdir (path ):
@@ -583,49 +590,23 @@ class FileHeaderListener(sublime_plugin.EventListener):
583590
584591 new_view_id = []
585592
586- def time_pattern (self ):
587- choice = Settings ().get ('time_format' )
588- _ = [0 , 1 , 2 ]
589- if choice not in _ :
590- choice = 0
591-
592- _ = ['\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}' ,
593- '\d{4}-\d{2}-\d{2}' , '\d{2}:\d{2}:\d{2}' ]
594- return _ [choice ]
595-
596593 def update_automatically (self , view , what ):
597594 syntax_type = get_syntax_type (view .file_name ())
598595
599- template = get_template_part (syntax_type , 'header' )
600- lines = template .split ('\n ' )
601-
602596 line_pattern = None
597+ lines = get_template_part (syntax_type , 'header' ).split ('\n ' )
603598 regex = getattr (FileHeaderListener , '%s_REGEX' % what )
599+
604600 for line in lines :
605601 search = regex .search (line )
606-
607602 if search is not None :
608- var = search .group ()
609- index = line .find (var )
610-
611- line_header = ''
603+ index , line_header = line .find (search .group ()), ''
612604 for i in range (index - 1 , 0 , - 1 ):
613605 if line [i ] != ' ' :
614606 space_start = i + 1
615607 line_header = line [:space_start ]
616608 break
617-
618- line_header = re .escape (line_header )
619- if what in set ([
620- LAST_MODIFIED_BY , FILE_NAME ,
621- FILE_NAME_WITHOUT_EXTENSION , FILE_PATH ]):
622- line_pattern = '%s.*\n ' % line_header
623-
624- elif what == LAST_MODIFIED_TIME :
625- line_pattern = '%s\s*%s.*\n ' % (
626- line_header , self .time_pattern ())
627- else :
628- raise KeyError ()
609+ line_pattern = '{}.*\n ' .format (line_header )
629610 break
630611
631612 if line_pattern is not None :
@@ -650,8 +631,7 @@ def update_automatically(self, view, what):
650631 elif what == FILE_PATH :
651632 strings = file_path
652633
653- spaces = (index - space_start ) * ' '
654- strings = spaces + strings
634+ strings = ' ' * (index - space_start ) + strings
655635
656636 region = sublime .Region (int (a ), int (b ))
657637 if view .substr (region ) != strings :
@@ -669,9 +649,12 @@ def insert_template(self, view, exists):
669649 and view .size () <= 0 )
670650
671651 if exists :
672- condition = (condition and os .path .exists (path )
673- and os .path .isfile (path )
674- and os .path .getsize (path ) <= 0 )
652+ condition = (
653+ condition
654+ and os .path .exists (path )
655+ and os .path .isfile (path )
656+ and os .path .getsize (path ) <= 0
657+ )
675658
676659 if condition :
677660 block (view , view .run_command , 'add_file_header' , {'path' : path })
0 commit comments