I like the “switch syntax” idea, but I’m not sure that having so many similar plugins is ideal. How about this instead (see below)? It also allows for the user to choose a syntax if one isn’t defined in the shortcut. Ideally (in the future), we could have access to the quick panel API and present a filterable list of syntaxes to choose from (if one isn’t defined in the shortcut).
import sublime, sublime_plugin, os, fnmatch
class SetFileSyntaxCommand(sublime_plugin.TextCommand):
def run(self, edit, syntax=''):
"""Set the syntax for this view as specified, or, if not specified, ask the user for the syntax type to change to"""
self.populate_map()
if not syntax in syntaxMap:
message = 'Please type the name of the syntax type to switch the view to: '
sublime.active_window().show_input_panel(message, 'java', lambda s: self.set_syntax(s), None, None)
else:
self.set_syntax(syntax)
def set_syntax(self, syntax):
"""Set the syntax for this view if it matches a key in the map"""
syntax = syntax.lower()
if syntax in syntaxMap:
self.view.settings().set('syntax', syntaxMap[syntax])
def populate_map(self):
"""Get a list of valid syntax highlighting options"""
if not hasattr(self, 'syntaxMap'):
self.syntaxMap = {}
for root, dirnames, filenames in os.walk(sublime.packages_path()):
for filename in fnmatch.filter(filenames, '*.tmLanguage'):
name = os.path.splitext(filename)[0].lower()
full_path = os.path.join(root, filename)
relative_path = full_path.replace(sublime.packages_path(), 'Packages')
self.syntaxMap[name] = relative_path
You could then setup your shortcuts as follows:
{ "keys": "alt+1"], "command": "set_file_syntax", "args": {"syntax": "javascript"}},
{ "keys": "alt+2"], "command": "set_file_syntax", "args": {"syntax": "html"}},
{ "keys": "alt+3"], "command": "set_file_syntax", "args": {"syntax": "php"}},
{ "keys": "alt+4"], "command": "set_file_syntax", "args": {"syntax": "css"}}