diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..ce5880b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +*.cache +*.sublime-project +*.sublime-workspace \ No newline at end of file diff --git a/README.rst b/README.rst index 2a372002..b78e5958 100644 --- a/README.rst +++ b/README.rst @@ -35,16 +35,15 @@ Syntax Definition Development In AAAPackageDev, syntax definitions are written in JSON. Because Sublime Text uses ``.tmLanguage`` files, though, they need to be converted before use. The -conversion is done through the included build system ``Json to tmLanguage``. +conversion is done through the included build system ``JSON to Property List``. Creating a New Syntax Definition ******************************** #. Create new template (through **Tools | Packages | Package Development**) or the *Command Palette* -#. Select ``Json to tmLanguage`` build system from **Tools | Build System** +#. Select ``JSON to Property List`` build system from **Tools | Build System** or leave as ``Automatic`` #. Press ``F7`` -To reload changes to a syntax definition, you must restart Sublime Text. Other included resources for syntax definition development: @@ -68,7 +67,7 @@ Commands .. Completions .. ----------- -.. +.. .. * sublime text plugin dev (off by default) .. Will clutter your completions list in any kind of python dev. .. To turn on, change scope selector so ``source.python``. @@ -133,7 +132,7 @@ About Snippets in AAAPackageDev The ``AAAPackageDev/Snippets`` folder contains many snippets for all kinds of development mentioned above. These snippets follow memorable rules to make their -use easy. +use easy. The snippets used more often have short tab triggers like ``f`` (*field*), ``c`` (*completion*), ``k`` (*key binding*), etc. In cases where increasingly diff --git a/Support/AAAPackageDev.sublime-commands b/Support/AAAPackageDev.sublime-commands index 90eac671..b52abf20 100644 --- a/Support/AAAPackageDev.sublime-commands +++ b/Support/AAAPackageDev.sublime-commands @@ -5,7 +5,7 @@ { "caption": "z:AAAPackageDev: New Syntax Definition", "command": "new_syntax_def" }, { "caption": "z:AAAPackageDev: New Syntax Definition from Buffer", "command": "new_syntax_def_from_buffer" }, - { "caption": "z:AAAPackageDev: Generate .tmLanguage from Buffer", "command": "make_tmlanguage" }, + { "caption": "z:AAAPackageDev: JSON to Property List", "command": "json_to_plist" }, { "caption": "z:AAAPackageDev: New Settings File", "command": "new_settings" }, diff --git a/Support/JSON to Property List.sublime-build b/Support/JSON to Property List.sublime-build new file mode 100644 index 00000000..e1a55701 --- /dev/null +++ b/Support/JSON to Property List.sublime-build @@ -0,0 +1,4 @@ +{ + "target": "json_to_plist", + "selector": "source.json, source.json-tmlanguage" +} \ No newline at end of file diff --git a/Support/Json to tmLanguage.sublime-build b/Support/Json to tmLanguage.sublime-build deleted file mode 100644 index 6b92efa6..00000000 --- a/Support/Json to tmLanguage.sublime-build +++ /dev/null @@ -1,3 +0,0 @@ -{ - "target": "make_tmlanguage" -} \ No newline at end of file diff --git a/json2plist.py b/json2plist.py deleted file mode 100644 index 528edba6..00000000 --- a/json2plist.py +++ /dev/null @@ -1,14 +0,0 @@ -import json -import plistlib -import os - -def make_grammar(json_grammar): - path, fname = os.path.split(json_grammar) - grammar_name, ext = os.path.splitext(fname) - - with open(json_grammar) as grammar_in_json: - tmlanguage = json.load(grammar_in_json) - - target = os.path.join(path, grammar_name + '.tmLanguage') - if os.path.exists(target): os.remove(target) - plistlib.writePlist(tmlanguage, target) diff --git a/syntax_def_dev.py b/syntax_def_dev.py index 351081f0..000e91fe 100644 --- a/syntax_def_dev.py +++ b/syntax_def_dev.py @@ -2,11 +2,12 @@ import os import plistlib import uuid +import re import sublime_plugin from sublime_lib.path import root_at_packages -from sublime_lib.view import has_file_ext, in_one_edit +from sublime_lib.view import in_one_edit JSON_TMLANGUAGE_SYNTAX = 'Packages/AAAPackageDev/Support/Sublime JSON Syntax Definition.tmLanguage' @@ -57,28 +58,41 @@ def run(self, edit): # XXX: Why is this a WindowCommand? Wouldn't it work otherwise in build-systems? -class MakeTmlanguageCommand(sublime_plugin.WindowCommand): - """Generates a ``.tmLanguage`` file from a ``.JSON-tmLanguage`` syntax def. - Should be used from a ``.build-system only``. +class JsonToPlistCommand(sublime_plugin.WindowCommand): """ - # XXX: We whould prevent this from working except if called in a build system. + Parses ``.json`` files and writes them into corresponding ``.plist``. + Source file ``.JSON-XXX`` will generate a plist file named ``.XXX``. + Pretty useful with ``.JSON-tmLanguage`` but works with almost any other name. + """ + ext_regexp = re.compile(r'\.json(?:-([^\.]+))?$', flags=re.I) + def is_enabled(self): - return has_file_ext(self.window.active_view(), '.JSON-tmLanguage') + return self.get_file_ext(self.window.active_view().file_name()) is not None + + def get_file_ext(self, file_name): + ret = self.ext_regexp.search(file_name) + if ret is None: + return None + return '.' + (ret.group(1) or 'plist') def run(self, **kwargs): v = self.window.active_view() path = v.file_name() - if not (os.path.exists(path) and has_file_ext(v, 'JSON-tmLanguage')): - print "[AAAPackageDev] Not a valid JSON-tmLanguage file. (%s)" % path + ext = self.get_file_ext(path) + if not os.path.exists(path): + print "[AAAPackageDev] File does not exists. (%s)" % path + return + if ext is None: + print "[AAAPackageDev] Not a valid JSON file, please check extension. (%s)" % path return - assert os.path.exists(path), "Need a path to a .JSON-tmLanguage file." - self.make_tmlanguage_grammar(path) + self.json_to_plist(path, ext) - def make_tmlanguage_grammar(self, json_grammar): - path, fname = os.path.split(json_grammar) - grammar_name, ext = os.path.splitext(fname) - file_regex = r"Error:\s+'(.*?)'\s+.*?\s+line\s+(\d+)\s+column\s+(\d+)" + def json_to_plist(self, json_file, new_ext): + print os.path.split(json_file) + path, fname = os.path.split(json_file) + fbase, old_ext = os.path.splitext(fname) + file_regex = r"Error parsing JSON:\s+'(.*?)'\s+.*?\s+line\s+(\d+)\s+column\s+(\d+)" if not hasattr(self, 'output_view'): # Try not to call get_output_panel until the regexes are assigned @@ -94,13 +108,13 @@ def make_tmlanguage_grammar(self, json_grammar): with in_one_edit(self.output_view) as edit: try: - with open(json_grammar) as grammar_in_json: - tmlanguage = json.load(grammar_in_json) + with open(json_file) as json_content: + tmlanguage = json.load(json_content) except ValueError, e: - self.output_view.insert(edit, 0, "Error: '%s' %s" % (json_grammar, str(e))) + self.output_view.insert(edit, 0, "Error parsing JSON: '%s' %s" % (json_file, str(e))) else: - target = os.path.join(path, grammar_name + '.tmLanguage') - self.output_view.insert(edit, 0, "Writing tmLanguage... (%s)" % target) + target = os.path.join(path, fbase + new_ext) + self.output_view.insert(edit, 0, "Writing plist... (%s)" % target) plistlib.writePlist(tmlanguage, target) self.window.run_command("show_panel", {"panel": "output.aaa_package_dev"})