Skip to content

Commit

Permalink
fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyanhui committed Aug 11, 2014
1 parent 524a67f commit 43a4c0d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
38 changes: 22 additions & 16 deletions FileHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# @Author: lime
# @Date: 2013-10-28 13:39:48
# @Last Modified by: Lime
# @Last Modified time: 2014-08-02 17:43:53
# @Last Modified time: 2014-08-11 19:45:15


import os
import sys
Expand Down Expand Up @@ -73,7 +74,7 @@ def plugin_loaded():
shutil.rmtree(PLUGIN_PATH)
except:
pass

if not os.path.exists(PLUGIN_PATH):
os.mkdir(PLUGIN_PATH)

Expand Down Expand Up @@ -130,10 +131,15 @@ def get_strftime():
'''Get `time_format` setting'''

_ = ['%Y-%m-%d %H:%M:%S', '%Y-%m-%d', '%H:%M:%S']
try:
format = _[Settings().get('time_format')]
except IndexError:
format = _[0]

format = Settings().get('custom_time_format')

if not format:
try:
format = _[Settings().get('time_format')]
except IndexError:
format = _[0]

return format


Expand Down Expand Up @@ -223,7 +229,7 @@ def get_st2_time():

format = get_strftime()
c_time, m_time = get_st3_time() if IS_ST3 else get_st2_time()

file_path = get_file_path(options.get('path', None))
file_name = get_file_name(options.get('path', None))
file_name_without_extension = get_file_name_without_extension(file_name)
Expand Down Expand Up @@ -266,7 +272,7 @@ def render_template(syntax_type, part=None, options={}):

def get_syntax_type(name):
'''Judge `syntax_type` according to to `name`'''

syntax_type = Settings().get('syntax_when_not_match')
file_suffix_mapping = Settings().get('file_suffix_mapping')

Expand Down Expand Up @@ -454,7 +460,7 @@ def can_add(self, path):
'''Whether can add header to path'''

def can_add_to_dir(path):
return enable_add_to_hidden_dir or (not enable_add_to_hidden_dir
return enable_add_to_hidden_dir or (not enable_add_to_hidden_dir
and not self.is_hidden(path))

if not os.path.exists(path):
Expand Down Expand Up @@ -571,11 +577,11 @@ def time_pattern(self):
_ = ['\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}',
'\d{4}-\d{2}-\d{2}', '\d{2}:\d{2}:\d{2}']
return _[choice]

def update_automatically(self, view, what):
syntax_type = get_syntax_type(view.file_name())

template = get_template_part(syntax_type, 'header')
template = get_template_part(syntax_type, 'header')
lines = template.split('\n')

line_pattern = None
Expand All @@ -591,7 +597,7 @@ def update_automatically(self, view, what):
if line[i] != ' ':
space_start = i + 1
line_header = line[:space_start]
break
break

line_header = re.escape(line_header)
if what == LAST_MODIFIED_BY or what == FILE_NAME or \
Expand All @@ -610,15 +616,15 @@ def update_automatically(self, view, what):
_ = view.find(line_pattern, 0)
if(_ != sublime.Region(-1, -1) and _ is not None):
a = _.a + space_start
b = _.b - 1
b = _.b - 1

file_name = get_file_name(view.file_name())
file_name_without_extension = get_file_name_without_extension(file_name)
file_path = get_file_path(view.file_name())

if what == LAST_MODIFIED_BY:
strings = get_args(syntax_type)['last_modified_by']
elif what == LAST_MODIFIED_TIME:
elif what == LAST_MODIFIED_TIME:
strings = datetime.now().strftime(get_strftime())
elif what == FILE_NAME:
strings = file_name
Expand All @@ -632,7 +638,7 @@ def update_automatically(self, view, what):

region = sublime.Region(int(a), int(b))
if view.substr(region) != strings:
view.run_command('file_header_replace',
view.run_command('file_header_replace',
{'a': a, 'b': b, 'strings': strings})

def insert_template(self, view, exists):
Expand Down Expand Up @@ -679,5 +685,5 @@ def on_activated(self, view):
c_time, _ = get_time(view.file_name())
if c_time is not None and settings.get('c_time', None) is None:
settings.set('c_time', pickle.dumps(c_time))

self.insert_template(view, True)
62 changes: 34 additions & 28 deletions FileHeader.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@
*/
"time_format": 0,
/*
The custom time format. It will format `create_time` and `last_modified_time`
instead of `time_format` if you set it. The time format refers to`
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior`.
*/
"custom_time_format": "",
/*
Whether add template to the empty file.
It's useful when you create new file through other command, for
It's useful when you create new file through other command, for
example, the default Sublime Text's **New File...** or other plugin.
*/
"enable_add_template_to_empty_file": true,
/*
Set your custom template header path here, it is a directory in which
you write your own header files. The file name should be a language,
"Python.tmpl" for example.
Set your custom template header path here, it is a directory in which
you write your own header files. The file name should be a language,
"Python.tmpl" for example.
*/
"custom_template_header_path": "",
/*
Set your custom template body path here, it is a directory in which
you write your own body files. The file name should be a language,
"Python.tmpl" for example.
Set your custom template body path here, it is a directory in which
you write your own body files. The file name should be a language,
"Python.tmpl" for example.
The template structure is:
Expand All @@ -40,23 +46,23 @@
*/
"custom_template_body_path": "",
/*
Whether show input panel when you add header. The default file which
Whether show input panel when you add header. The default file which
you add header to is the current file you edit.
*/
"show_input_panel_when_add_header": true,
/*
Whether open file when you add header to files in the specified
Whether open file when you add header to files in the specified
directory.
*/
"open_file_when_add_header_to_directory": true,
/*
Whether enable add header to hidden directory. If false, FileHeader
won't add header to files under it.
Whether enable add header to hidden directory. If false, FileHeader
won't add header to files under it.
*/
"enable_add_header_to_hidden_dir": false,
/*
Whether enable add header to hidden file. If false, FileHeader
won't add header to it.
Whether enable add header to hidden file. If false, FileHeader
won't add header to it.
*/
"enable_add_header_to_hidden_file": false,
/*
Expand All @@ -68,7 +74,7 @@
"syntax_when_not_match": "Text",
/*
FileHeader will judge programming language according to file suffix.
You can add more file suffix here. Note: language should be one of
You can add more file suffix here. Note: language should be one of
that under **Default**. If FileHeader don't find the suffix,
FileHeader will set language as **syntax_when_not_match** above.
*/
Expand All @@ -78,7 +84,7 @@
"asp": "ASP",
"aspx": "ASP",
"bat": "Batch File",
"cmd": "Batch File",
"cmd": "Batch File",
"c": "C",
"cs": "C#",
"cpp": "C++",
Expand Down Expand Up @@ -119,47 +125,47 @@
Variables
=========
*/

/*
Below is the variables you render templater.
*/
"Default": {
/*
Builtin Variables
=================
- create_time
The file created time. It will be automatically set when you create
a new file if you use it.
a new file if you use it.
Can't be set custom.
- author
The file creator.
The file creator.
FileHeader will set it automatically. If it's in
a git repository and the `user.name` has been set, `autor`
will set to `user.name`, otherwise it will be set to current
a git repository and the `user.name` has been set, `autor`
will set to `user.name`, otherwise it will be set to current
system user.
Can be set custom.
- last_modified_by
The file last modified by who? It is specially useful when
cooperation programming.
The file last modified by who? It is specially useful when
cooperation programming.
FileHeader will set it automatically. If it's in
a git repository and the `user.name` has been set, `autor`
will set to `user.name`, otherwise it will be set to current
system logined user.
a git repository and the `user.name` has been set, `autor`
will set to `user.name`, otherwise it will be set to current
system logined user.
Can be set custom.
- last_modified_time
The file last modified time.
FileHeader will set it automatically when you save the file.
Expand Down Expand Up @@ -208,7 +214,7 @@
// You can add more here......
},
/*
You can set different variables in different languages. It will cover
You can set different variables in different languages. It will cover
that in "Default".
*/
"ASP": {},
Expand Down

0 comments on commit 43a4c0d

Please sign in to comment.