From d109259c0bc58ef3d19c2df58592a36907b39ef0 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 15:21:11 +0200 Subject: [PATCH 1/7] gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore 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 From 39b8a2d454b963216706e4337b3109f670edbf6f Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 22:51:25 +0200 Subject: [PATCH 2/7] Remove unused json2plist.py --- json2plist.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 json2plist.py 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) From 5bb8a5a96e5673448dd6ed8ce981f72149b1e9ee Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 22:55:37 +0200 Subject: [PATCH 3/7] Rewrite `make_tmlanguage` command to convert every json file into a plist Use the part after `.JSON-` as new extension to be compatible with `.JSON-tmLanguage` (which is not a bad standard at all) Use simple `.plist` for `.json` files --- syntax_def_dev.py | 48 ++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/syntax_def_dev.py b/syntax_def_dev.py index 351081f0..326fef43 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' @@ -58,26 +59,39 @@ 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``. """ - # 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) + 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:\s+'(.*?)'\s+.*?\s+line\s+(\d+)\s+column\s+(\d+)" if not hasattr(self, 'output_view'): @@ -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: '%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"}) From 0c25d7c9d0fe18ba10c057dd00fd84cac53e466e Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 23:02:05 +0200 Subject: [PATCH 4/7] Rename `make_tmlanguage` command to `json_to_plist` and specify a selector value so Sublime's auto-detection of build system works for these --- Support/JSON to Property List.sublime-build | 4 ++++ Support/Json to tmLanguage.sublime-build | 3 --- syntax_def_dev.py | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 Support/JSON to Property List.sublime-build delete mode 100644 Support/Json to tmLanguage.sublime-build 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/syntax_def_dev.py b/syntax_def_dev.py index 326fef43..e1c2ae0a 100644 --- a/syntax_def_dev.py +++ b/syntax_def_dev.py @@ -58,7 +58,7 @@ def run(self, edit): # XXX: Why is this a WindowCommand? Wouldn't it work otherwise in build-systems? -class MakeTmlanguageCommand(sublime_plugin.WindowCommand): +class JsonToPlistCommand(sublime_plugin.WindowCommand): """ Parses ``.json`` files and writes them into corresponding ``.plist``. Source file ``.JSON-XXX`` will generate a plist file named ``.XXX``. From 1d1f3ea11713499fea44d8599d3419b63db27c84 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 23:05:53 +0200 Subject: [PATCH 5/7] Print some more information when parsing JSON failed --- syntax_def_dev.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax_def_dev.py b/syntax_def_dev.py index e1c2ae0a..000e91fe 100644 --- a/syntax_def_dev.py +++ b/syntax_def_dev.py @@ -92,7 +92,7 @@ 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:\s+'(.*?)'\s+.*?\s+line\s+(\d+)\s+column\s+(\d+)" + 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 @@ -111,7 +111,7 @@ def json_to_plist(self, json_file, new_ext): 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_file, str(e))) + self.output_view.insert(edit, 0, "Error parsing JSON: '%s' %s" % (json_file, str(e))) else: target = os.path.join(path, fbase + new_ext) self.output_view.insert(edit, 0, "Writing plist... (%s)" % target) From 0f480bf3a3c1e1f41bf7fc42b730f8ed5d1336f1 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 14 May 2012 23:13:38 +0200 Subject: [PATCH 6/7] Update commands file to `json_to_plist` --- Support/AAAPackageDev.sublime-commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" }, From 056a7847bf7761713ea3a527e2b24f73eaa4a1ac Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Tue, 15 May 2012 16:41:57 +0200 Subject: [PATCH 7/7] Update Readme for `json_to_plist` (btw, it is not *always* necessary to reload a syntax definition file; rather rarely) --- README.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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