Skip to content

Instantly share code, notes, and snippets.

@uncreative
Forked from fnando/SetSyntaxHighlight.py
Created August 2, 2012 20:06
Show Gist options
  • Save uncreative/3240213 to your computer and use it in GitHub Desktop.
Save uncreative/3240213 to your computer and use it in GitHub Desktop.
Sublime Text 2 plugin for setting syntax highlighting automatically. Just put it on your Packages/User directory.
import sublime, sublime_plugin
import os
import re
from functools import partial
class SetSyntaxHighlightCommand(sublime_plugin.EventListener):
""" Sets the file grammar to whatever you want.
Author: Nando Vieira <http://nandovieira.com.br>
"""
def on_load(self, view):
path = view.file_name()
if not path:
return
path = path.lower()
name = os.path.basename(path)
syntax = partial(self.set_syntax, view)
syntax(name == "gemfile", "ruby")
syntax(name == "vagrantfile", "ruby")
syntax(name == "rakefile", "ruby")
syntax(name == "capfile", "ruby")
syntax(name == ".caprc", "ruby")
syntax(name == ".irbrc", "ruby")
syntax(name == ".pryrc", "ruby")
syntax(re.match(r'.*?/spec/.*?\.rb', path), "rspec")
syntax(re.match(r'.*?/(app|config|db)/.*?\.rb', path), "rails")
def set_syntax(self, view, apply, syntax):
syntaxes = {
"rspec": "RSpec (snippets and syntax)/Syntaxes/RSpec.tmLanguage",
"ruby": "Ruby/Ruby.tmLanguage",
"rails": "Rails/Ruby on Rails.tmLanguage"
}
if not apply:
return
if not syntax in syntaxes:
return
language_file = "Packages/" + syntaxes[syntax]
view.settings().set("syntax", language_file)
print "Set syntax to '" + syntax + "' using " + language_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment