Skip to content

Instantly share code, notes, and snippets.

@cherring
Created July 10, 2012 06:41
Show Gist options
  • Select an option

  • Save cherring/3081616 to your computer and use it in GitHub Desktop.

Select an option

Save cherring/3081616 to your computer and use it in GitHub Desktop.
~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/syntax_highlighting.py
import sublime, sublime_plugin
import os
class DetectFileTypeCommand(sublime_plugin.EventListener):
""" Detects current file type if the file's extension isn't conclusive """
""" Modified for Ruby on Rails and Sublime Text 2 """
""" Original pastie here: http://pastie.org/private/kz8gtts0cjcvkec0d4quqa """
def on_load(self, view):
filename = view.file_name()
if not filename: # buffer has never been saved
return
name = os.path.basename(filename.lower())
if name[-8:] == "_spec.rb":
set_syntax(view, "Rspec", "User/rspec")
elif name[-8:] == "_steps.rb":
set_syntax(view, "Cucumber Steps", "Cucumber")
elif name == "factories.rb":
set_syntax(view, "Rspec", "User/rspec")
elif name == "gemfile":
set_syntax(view, "Ruby on Rails", "Rails")
elif name[-2:] == "rb":
set_syntax(view, "Ruby on Rails", "Rails")
def set_syntax(view, syntax, path=None):
if path is None:
path = syntax
view.settings().set('syntax', 'Packages5G/'+ path + '/' + syntax + '.tmLanguage')
print "Switched syntax to: " + syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment