From 2e01c84f0cd87f700da4eb77907afd9959c7387a Mon Sep 17 00:00:00 2001 From: Zachary Miller Date: Sun, 10 Feb 2019 15:09:06 -0500 Subject: [PATCH] Introduced new configuration spec. --- Default.sublime-commands | 12 +++-------- Main.sublime-menu | 16 +++++++++++++-- utils.py | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 utils.py diff --git a/Default.sublime-commands b/Default.sublime-commands index 1fe93eea..55201e0a 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -6,22 +6,16 @@ { "caption": "Color Helper: Show Palettes", "command": "color_helper", - "args": { - "mode": "palette" - } + "args": { "mode": "palette" } }, { "caption": "Color Helper: Show Color Info", "command": "color_helper", - "args": { - "mode": "info" - } + "args": { "mode": "info" } }, { "caption": "Color Helper: Show Color Picker", "command": "color_helper", - "args": { - "mode": "color_picker" - } + "args": { "mode": "color_picker" } } ] diff --git a/Main.sublime-menu b/Main.sublime-menu index 98369101..d7e832b4 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -50,11 +50,23 @@ [ { "caption": "Settings", - "command": "edit_settings", + "command": "z_edit_settings", "args": { "base_file": "${packages}/ColorHelper/color_helper.sublime-settings", - "default": "{\n$0\n}\n" + "user_file": "${packages}/User/ColorHelper/color_helper.sublime-settings", + "default": "{\n\t$0\n}\n" + } + }, + { "caption": "-" }, + { + "caption": "Commands", + "command": "z_edit_settings", + "args": + { + "file": "${packages}/ColorHelper/Default.sublime-commands", + "is_parent_setting": "1", + "default": "[\n\t$0\n]\n" } }, { "caption": "-" }, diff --git a/utils.py b/utils.py new file mode 100644 index 00000000..bfe7163a --- /dev/null +++ b/utils.py @@ -0,0 +1,44 @@ +import sublime +import sublime_plugin +import os + + +class ZEditSettings(sublime_plugin.WindowCommand): + def run(self, **kwargs): + is_parent_setting = False + + if 'is_parent_setting' in kwargs: + is_parent_setting = kwargs.get('is_parent_setting') + del kwargs['is_parent_setting'] + + if is_parent_setting: + expanded_target_directory = sublime.expand_variables(os.path.dirname(kwargs.get('file')), self.window.extract_variables()) + if not os.path.exists(expanded_target_directory): + os.makedirs(expanded_target_directory) + + if 'contents' in kwargs: + kwargs['contents'] = kwargs.get('contents') + + self.window.run_command('open_file', kwargs) + + else: + if 'user_file' in kwargs: + expanded_target_directory = sublime.expand_variables(os.path.dirname(kwargs.get('user_file')), self.window.extract_variables()) + if not os.path.exists(expanded_target_directory): + os.makedirs(expanded_target_directory) + + if os.path.isfile(sublime.expand_variables(kwargs.get('base_file'), self.window.extract_variables())): + self.window.run_command('edit_settings', kwargs) + else: + if 'user_file' in kwargs: + kwargs['file'] = kwargs.get('user_file') + del kwargs['user_file'] + else: + # source_path = kwargs.get('base_file') + # kwargs['file'] = source_path.replace(sublime.packages_path(), os.path.join(sublime.packages_path(), 'User')) + kwargs['file'] = os.path.join(sublime.packages_path(), 'User', os.path.basename(kwargs.get('base_file'))) + del kwargs['base_file'] + kwargs['contents'] = kwargs.get('default') + del kwargs['default'] + + self.window.run_command('open_file', kwargs)