From b0d3e2e860e6498332bc51125a0f1565c69126e2 Mon Sep 17 00:00:00 2001 From: Lime Date: Tue, 25 Nov 2014 14:22:24 +0800 Subject: [PATCH] Fix issue #23 --- Context.sublime-menu | 1 - Default (Linux).sublime-keymap | 4 +- Default (OSX).sublime-keymap | 4 +- Default (Windows).sublime-keymap | 4 +- FileHeader.py | 39 +++++++++----- FileHeader.sublime-settings | 11 +++- Main.sublime-menu | 1 - README.rst | 92 +++++++++++++++++++------------- Side Bar.sublime-menu | 2 +- template/body/ASP.tmpl | 4 +- template/body/C++.tmpl | 2 +- template/body/D.tmpl | 2 +- template/body/JavaScript.tmpl | 4 +- template/body/Pascal.tmpl | 2 +- template/header/PHP.tmpl | 1 - 15 files changed, 102 insertions(+), 71 deletions(-) diff --git a/Context.sublime-menu b/Context.sublime-menu index 937efed..5cb46bb 100644 --- a/Context.sublime-menu +++ b/Context.sublime-menu @@ -18,6 +18,5 @@ } } ] - } ] diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 8c2c953..47c2bbe 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -1,13 +1,13 @@ [ { - "keys": ["ctrl+alt+n"], + "keys": ["ctrl+alt+n"], "command": "file_header_new_file", "args":{ "paths": [] } }, { - "keys": ["ctrl+alt+a"], + "keys": ["ctrl+alt+a"], "command": "file_header_add_header", "args":{ "paths": [] diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index b3bcdc1..fa5efbd 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -1,13 +1,13 @@ [ { - "keys": ["super+alt+n"], + "keys": ["super+alt+n"], "command": "file_header_new_file", "args":{ "paths": [] } }, { - "keys": ["super+alt+a"], + "keys": ["super+alt+a"], "command": "file_header_add_header", "args":{ "paths": [] diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 8c2c953..47c2bbe 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -1,13 +1,13 @@ [ { - "keys": ["ctrl+alt+n"], + "keys": ["ctrl+alt+n"], "command": "file_header_new_file", "args":{ "paths": [] } }, { - "keys": ["ctrl+alt+a"], + "keys": ["ctrl+alt+a"], "command": "file_header_add_header", "args":{ "paths": [] diff --git a/FileHeader.py b/FileHeader.py index 36c7a5d..ad13c35 100644 --- a/FileHeader.py +++ b/FileHeader.py @@ -3,8 +3,7 @@ # @Author: lime # @Date: 2013-10-28 13:39:48 # @Last Modified by: Lime -# @Last Modified time: 2014-08-11 19:45:15 - +# @Last Modified time: 2014-11-25 14:20:32 import os import sys @@ -27,7 +26,6 @@ else: import subprocess as process - PLUGIN_NAME = 'FileHeader' INSTALLED_PLUGIN_NAME = '%s.sublime-package' % PLUGIN_NAME PACKAGES_PATH = sublime.packages_path() @@ -106,7 +104,6 @@ def get_template_part(syntax_type, part): tmpl_file = os.path.join(path, tmpl_name) custom_template_path = Settings().get('custom_template_%s_path' % part) - print(custom_template_path) if custom_template_path: _ = os.path.join(custom_template_path, tmpl_name) if os.path.exists(_) and os.path.isfile(_): @@ -123,8 +120,9 @@ def get_template_part(syntax_type, part): def get_template(syntax_type): - parts = ['header', 'body'] - return ''.join([get_template_part(syntax_type, part) for part in parts]) + return ''.join( + [get_template_part(syntax_type, part) + for part in ['header', 'body']]) def get_strftime(): @@ -160,7 +158,8 @@ def get_project_name(): '''Get project name''' project_data = sublime.active_window().project_data() - project = os.path.basename(project_data['folders'][0]['path']) if project_data else None + project = os.path.basename( + project_data['folders'][0]['path']) if project_data else None return project @@ -275,14 +274,23 @@ def get_syntax_type(name): syntax_type = Settings().get('syntax_when_not_match') file_suffix_mapping = Settings().get('file_suffix_mapping') + extension_equivalence = Settings().get('extension_equivalence') if name is not None: name = name.split('.') if len(name) <= 1: return syntax_type + for i in range(1, len(name)): + suffix = '%s' % '.'.join(name[i:]) + if suffix in extension_equivalence: + suffix = extension_equivalence[suffix] + break + else: + suffix = name[-1] + try: - syntax_type = file_suffix_mapping[name[-1]] + syntax_type = file_suffix_mapping[suffix] except KeyError: pass @@ -340,7 +348,8 @@ def new_file(self, path, syntax_type): new_file = Window().open_file(path) try: - block(new_file, new_file.set_syntax_file, get_syntax_file(syntax_type)) + block(new_file, + new_file.set_syntax_file, get_syntax_file(syntax_type)) except: pass @@ -563,7 +572,8 @@ class FileHeaderListener(sublime_plugin.EventListener): LAST_MODIFIED_BY_REGEX = re.compile('\{\{\s*last_modified_by\s*\}\}') LAST_MODIFIED_TIME_REGEX = re.compile('\{\{\s*last_modified_time\s*\}\}') FILE_NAME_REGEX = re.compile('\{\{\s*file_name\s*\}\}') - FILE_NAME_WITHOUT_EXTENSION_REGEX = re.compile('\{\{\s*file_name_without_extension\s*\}\}') + FILE_NAME_WITHOUT_EXTENSION_REGEX = re.compile( + '\{\{\s*file_name_without_extension\s*\}\}') FILE_PATH_REGEX = re.compile('\{\{\s*file_path\s*\}\}') new_view_id = [] @@ -601,11 +611,13 @@ def update_automatically(self, view, what): line_header = re.escape(line_header) if what == LAST_MODIFIED_BY or what == FILE_NAME or \ - what == FILE_NAME_WITHOUT_EXTENSION or what == FILE_PATH: + what == FILE_NAME_WITHOUT_EXTENSION or \ + what == FILE_PATH: line_pattern = '%s.*\n' % line_header elif what == LAST_MODIFIED_TIME: - line_pattern = '%s\s*%s.*\n' % (line_header, self.time_pattern()) + line_pattern = '%s\s*%s.*\n' % ( + line_header, self.time_pattern()) else: raise KeyError() @@ -619,7 +631,8 @@ def update_automatically(self, view, what): b = _.b - 1 file_name = get_file_name(view.file_name()) - file_name_without_extension = get_file_name_without_extension(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: diff --git a/FileHeader.sublime-settings b/FileHeader.sublime-settings index fb0c655..8914deb 100644 --- a/FileHeader.sublime-settings +++ b/FileHeader.sublime-settings @@ -78,7 +78,7 @@ that under **Default**. If FileHeader don't find the suffix, FileHeader will set language as **syntax_when_not_match** above. */ - "file_suffix_mapping":{ + "file_suffix_mapping": { "as": "ActionScript", "scpt": "AppleScript", "asp": "ASP", @@ -120,6 +120,14 @@ "txt": "Text", "xml": "XML" }, + /* + Set special file suffix equivalence. Take `blade.php` for example, + FileHeader will initial file with suffix `blade.php` with that of `html`. + + */ + "extension_equivalence": { + "blade.php": "html", + }, /* Variables @@ -205,7 +213,6 @@ Can't be set custom. */ - /* Email */ diff --git a/Main.sublime-menu b/Main.sublime-menu index 164ade9..84fc0da 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -13,7 +13,6 @@ "caption": "File Header", "children": [ - { "command": "open_file", "args": {"file": "${packages}/FileHeader/FileHeader.sublime-settings"}, diff --git a/README.rst b/README.rst index 964aa1b..5fc50de 100644 --- a/README.rst +++ b/README.rst @@ -58,7 +58,7 @@ Create a new file .. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/new-file.gif -- Shortcuts +- Shortcuts The default shortcuts is **super+alt+n** on OS X, **ctrl+alt+n** on Windows and Linux. @@ -80,13 +80,13 @@ Add header to an existed file - Context Menu Similar to **Sidebar Menu**. - + Add header to files in the specified directory ---------------------------------------------- .. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/add-header-dir.gif -A very important feature of FileHeader is that it can automatically update **last_modified_time** and **last_modified_by** (see options below). Just look this picture, take care of the **@Last modified time:** before save and after save: +A very important feature of FileHeader is that it can automatically update **last_modified_time** and **last_modified_by** (see options below). Just look this picture, take care of the **@Last modified time:** before save and after save: .. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/update.gif @@ -97,7 +97,7 @@ Settings There are two kinds of arguments: **options** and kinds of languages variables settings. **options** is the functional setting, **Default** is the default language variables settings. Language variables setting will cover that in **Default**. .. code-block:: c++ - + { /* options @@ -113,24 +113,30 @@ There are two kinds of arguments: **options** and kinds of languages variables s */ "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 tempalte structure is: + The template structure is: I am a file ----------- @@ -140,23 +146,23 @@ There are two kinds of arguments: **options** and kinds of languages variables s */ "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, /* @@ -168,16 +174,17 @@ There are two kinds of arguments: **options** and kinds of languages variables s "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. */ - "file_suffix_mapping":{ + "file_suffix_mapping": { "as": "ActionScript", "scpt": "AppleScript", "asp": "ASP", "aspx": "ASP", "bat": "Batch File", + "cmd": "Batch File", "c": "C", "cs": "C#", "cpp": "C++", @@ -213,12 +220,20 @@ There are two kinds of arguments: **options** and kinds of languages variables s "txt": "Text", "xml": "XML" }, + /* + Set special file suffix equivalence. Take `blade.php` for example, + FileHeader will initial file with suffix `blade.php` with that of `html`. + + */ + "extension_equivalence": { + "blade.php": "html", + }, /* - variables + Variables ========= */ - + /* Below is the variables you render templater. */ @@ -226,39 +241,39 @@ There are two kinds of arguments: **options** and kinds of languages variables s /* 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. @@ -306,7 +321,7 @@ There are two kinds of arguments: **options** and kinds of languages variables s // 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": {}, @@ -352,12 +367,13 @@ There are two kinds of arguments: **options** and kinds of languages variables s "YAML": {} } + Template ======== -FileHeader use Jinja2_ template, find out how to use it `here `_. +FileHeader use Jinja2_ template, find out how to use it `here `_. -The template is made up of **header** and **body**. You also can write you +The template is made up of **header** and **body**. You also can write you own templates. Take the Python template header **Python.tmpl** for example. .. code-block:: ++ @@ -371,9 +387,9 @@ own templates. Take the Python template header **Python.tmpl** for example. **{{ }}** is variable, you can set it in setting files. **create_time** will be set when you create a new file using FileHeader, **last_modified_time** and **last_modified_by** will be update every time you save your file. -You can define your functions and classes or other contents in your **body** +You can define your functions and classes or other contents in your **body** file. Also take Python template body for example. - + .. code-block:: python def main(): diff --git a/Side Bar.sublime-menu b/Side Bar.sublime-menu index 3965124..41a86f2 100644 --- a/Side Bar.sublime-menu +++ b/Side Bar.sublime-menu @@ -1,5 +1,5 @@ [ - { + { "caption": "File Header", "children": [ diff --git a/template/body/ASP.tmpl b/template/body/ASP.tmpl index 4108eda..7f12d28 100644 --- a/template/body/ASP.tmpl +++ b/template/body/ASP.tmpl @@ -1,5 +1,5 @@ - - <% Response.Write "" %> + + <% Response.Write "" %> \ No newline at end of file diff --git a/template/body/C++.tmpl b/template/body/C++.tmpl index a1feb8b..e936e99 100644 --- a/template/body/C++.tmpl +++ b/template/body/C++.tmpl @@ -3,6 +3,6 @@ using namespace std; int main(){ - + return 0; } diff --git a/template/body/D.tmpl b/template/body/D.tmpl index 6c26e75..5b33c8c 100644 --- a/template/body/D.tmpl +++ b/template/body/D.tmpl @@ -1,5 +1,5 @@ import std.stdio; void main(){ - + } diff --git a/template/body/JavaScript.tmpl b/template/body/JavaScript.tmpl index 4be5888..ad9a93a 100644 --- a/template/body/JavaScript.tmpl +++ b/template/body/JavaScript.tmpl @@ -1,3 +1 @@ -$(document).ready(function(){ - -}); \ No newline at end of file +'use strict'; diff --git a/template/body/Pascal.tmpl b/template/body/Pascal.tmpl index ccae377..ea91d13 100644 --- a/template/body/Pascal.tmpl +++ b/template/body/Pascal.tmpl @@ -1,4 +1,4 @@ program Main(output); begin - + end. diff --git a/template/header/PHP.tmpl b/template/header/PHP.tmpl index afa7b71..e52064a 100644 --- a/template/header/PHP.tmpl +++ b/template/header/PHP.tmpl @@ -1,4 +1,3 @@ -