Ok, try this updated version. If it doesn’t work have a look in the console for error messages, etc…
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 self.syntaxMap:
message = 'Please type the name of the syntax type to switch the view to: '
sublime.active_window().show_input_panel(message, self.get_current_syntax(), 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"""
if syntax in self.syntaxMap:
self.view.set_syntax_file(self.syntaxMap[syntax.lower()])
else:
print '"%s" is not a known syntax highlighting' % (syntax)
print self.syntaxMap
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]
full_path = os.path.join(root, filename)
relative_path = full_path.replace(sublime.packages_path(), 'Packages')
relative_path = relative_path.replace('\\', '/') # fix windows paths
self.syntaxMap[name.lower()] = relative_path
print 'Syntax: %-20s -> "%s" (%s)' % (name, relative_path, full_path)
def get_current_syntax(self):
current_syntax = self.view.settings().get('syntax')
(path, filename) = os.path.split(current_syntax)
(shortname, extension) = os.path.splitext(filename)
return shortname
By the way, these key mappings work for me:
{ "keys": "alt+1"], "command": "set_file_type", "args":{ "syntax" : "Packages/Java/Java.tmLanguage" } },
{ "keys": "alt+2"], "command": "set_file_type", "args":{ "syntax" : "Packages/Python/Python.tmLanguage" } },
On Windows at least, it looks like the above doesn’t work if you use backslashes (), but it works fine if you use (unix style) forward slashes. Maybe your key-mappings weren’t working because you were using backslashes?
As a side note, “alt+1”…“alt+0” are mapped by default to quickly switch between views so if you want that functionality you may want to change your key mappings.