-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API method to get list of available syntax definitions. #2186
Comments
I ran into another example application today. I wrote a command that creates a new view with text from a template: import sublime
import sublime_plugin
import yaml
def parse(template):
i = template.find('\n---\n')
ret = yaml.load(template[:i])
ret['text'] = template[i+5:]
return ret
class NewWithTemplateCommand(sublime_plugin.WindowCommand):
def run(self):
templates = [
parse(sublime.load_resource(path))
for path in sublime.find_resources('*.sublime-template')
]
def callback(i):
if i == -1: return
text = templates[i]['text']
view = self.window.new_file()
view.run_command('insert_snippet', {'contents': text})
self.window.show_quick_panel(
[
[ template['name'], template['description'] ]
for template in templates
],
callback
) Example template: name: Test!
description: This is a test template.
---
Hello, ${0:World}! An obvious omission in this system is a way to specify a syntax for the new view. I could allow templates to specify a syntax definition file (e.g. |
Based on internal discussions, we wouldn't be able to provide |
ST 4050+ provides
|
Overview
Sublime maintains an internal list of available syntax definitions. This list, I presume, is used to implement several important features, including:
Set Syntax
entries in the command palette.include: scope:...
in syntax definitionsUnfortunately, there is no API method to access this list, making it difficult to implement some syntax-related features in plugins. Replicating the same information in a plugin would require reading and parsing a lot of files, including YAML 1.2 files that
pyyaml
doesn't understand. In addition, finding a syntax definition by file extension, first line, or scope would require determining the exact load order of syntax definitions, and this is surprisingly hard to get right.Exposing this list via the API would be extremely useful.
Suggested API
Return a list of objects in loading order with the following properties:
path
: the resource path (e.g."Packages/JavaScript/JavaScript.sublime-syntax"
).name
: the name as it would be displayed in the Syntax menu.hidden
: whether the syntax is hidden (havinghidden: true
, being ahidden-tmLanguage
).scope
: the top-level key that is used forinclude: scope:...
.file_extensions
: the top-level key, or an empty list.first_line_match
: the top-level key, orNone
.tmLanguage
files that are "shadowed" bysublime-syntax
files should be absent.Hidden syntaxes should be present. Depending on the application, users can filter them out.
(It might be convenient to have API methods to find a syntax definition by scope, by file extension, or by first line, but these could be trivially implemented by users or by libraries in terms of
sublime.get_syntax_definitions
, whereas the reverse is not true.)Uses
Using this API, package developers could:
Prior work
sublime_lib
tries to provide this list. However:pyyaml
can't handlesublime-syntax
files, andruamel.yaml
is too slow, so we use some sketchy manual parsing code.The text was updated successfully, but these errors were encountered: