Skip to content

Commit

Permalink
#14 + enable_project_variables, support: ${project_base_name}, ${proj…
Browse files Browse the repository at this point in the history
…ect_path} and ${platform}
  • Loading branch information
kairyou committed Jul 19, 2016
1 parent 5a880c5 commit 4075770
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ To disable all default shortcuts, set value to `all`.

> default "date_format" : "%Y-%m-%d %H:%M:%S" .
- `*.tmpl` file support `${saved_filename}`, `${saved_filepath}` on save file

> Enable: `SublimeTmpl > settings - user` add `"enable_file_variables_on_save": true,`
- `*.tmpl` file support `${project_base_name}`, `${project_path}` and `${platform}`

> Enable: `SublimeTmpl > settings - user` add `"enable_project_variables": true,`
> NOTE: This will only work with SublimeText 3.0 or above.
It is recommended that you put your own custom settings in `settings - user`. Please see note below in "*Detailed Instructions for Sublime Newbies"

- custom variables: `attr`
Expand Down
1 change: 1 addition & 0 deletions SublimeTmpl.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"extension": "yaml"
},
"disable_keymap_actions": false, // "all"; "html,css"
"enable_project_variables": false, // ${project_base_name}, ${project_path} and ${platform}
"enable_file_variables_on_save": false, // ${saved_filename}, ${saved_filepath} on save file
"date_format" : "%Y-%m-%d %H:%M:%S",
"attr": {
Expand Down
21 changes: 15 additions & 6 deletions sublime-tmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

IS_GTE_ST3 = int(sublime.version()[0]) >= 3
DISABLE_KEYMAP = None
UNSAVED_IDS = {}

class SublimeTmplCommand(sublime_plugin.TextCommand):

Expand Down Expand Up @@ -106,6 +105,7 @@ def get_code(self, type):
return self.format_tag(code)

def format_tag(self, code):
win = self.view.window()
code = code.replace('\r', '') # replace \r\n -> \n
# format
settings = self.get_settings()
Expand All @@ -115,10 +115,17 @@ def format_tag(self, code):
code = code.decode('utf8') # for st2 && Chinese characters
code = code.replace('${date}', date)


attr = settings.get('attr', {})
for key in attr:
code = code.replace('${%s}' % key, attr.get(key, ''))

# print(hasattr(win, 'extract_variables'))
# print(win.extract_variables(), win.project_data())
if settings.get('enable_project_variables', False) and hasattr(win, 'extract_variables'):
variables = win.extract_variables()
for key in ['project_base_name', 'project_path', 'platform']:
code = code.replace('${%s}' % key, variables.get(key, ''))

# keep ${var..}
code = re.sub(r"(?<!\\)\${(?!\d)", '\${', code)
return code
Expand Down Expand Up @@ -156,6 +163,8 @@ def run(self, edit, old, new):
self.view.replace(edit, region, s)

class SublimeTmplEventListener(sublime_plugin.EventListener):
def __init__(self):
self.unsaved_ids = {}
def on_query_context(self, view, key, operator, operand, match_all):
settings = sublime.load_settings(PACKAGE_NAME + '.sublime-settings')
disable_keymap_actions = settings.get('disable_keymap_actions', '')
Expand All @@ -179,18 +188,18 @@ def on_activated(self, view):
return
settings = sublime.load_settings(PACKAGE_NAME + '.sublime-settings')
if settings.get('enable_file_variables_on_save', False):
UNSAVED_IDS[view.id()] = True
# print('on_activated', UNSAVED_IDS, view.id(), view.file_name())
self.unsaved_ids[view.id()] = True
# print('on_activated', self.unsaved_ids, view.id(), view.file_name())
def on_pre_save(self, view):
if not view.id() in UNSAVED_IDS:
if not view.id() in self.unsaved_ids:
return
settings = sublime.load_settings(PACKAGE_NAME + '.sublime-settings')
if settings.get('enable_file_variables_on_save', False):
filepath = view.file_name()
filename = os.path.basename(filepath)
view.run_command('sublime_tmpl_replace', {'old': '${saved_filepath}', 'new': filepath})
view.run_command('sublime_tmpl_replace', {'old': '${saved_filename}', 'new': filename})
del UNSAVED_IDS[view.id()]
del self.unsaved_ids[view.id()]

def plugin_loaded(): # for ST3 >= 3016
# global PACKAGES_PATH
Expand Down

0 comments on commit 4075770

Please sign in to comment.