Skip to content

Instantly share code, notes, and snippets.

@JeanMertz
Created April 18, 2011 08:39
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save JeanMertz/925008 to your computer and use it in GitHub Desktop.
Save JeanMertz/925008 to your computer and use it in GitHub Desktop.
Ruby on Rails syntax highlight switcher for Sublime Text 2
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 == "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', 'Packages/'+ path + '/' + syntax + '.tmLanguage')
print "Switched syntax to: " + syntax
@andreimoment
Copy link

Added a check for erb files here: https://gist.github.com/2142985

@survivorist
Copy link

Error loading syntax file "Packages/RSpec/RSpec.tmLanguage": Error parsing plist xml: Failed to open file In file "Packages/RSpec/RSpec.tmLanguage" Error on Ubuntu any ideas how to fix it. Dropped the syntax_highlighting.py file into "/home/$usename$/.config/sublime-text-2/Packages/User" but still getting the error.

@phillipkoebbe
Copy link

For everyone still coming across this gist and needing more control over applying the correct syntax to files in ST2, I encourage you to consider DetectSyntax [1], a plugin I created based on JeanMertz's work above.

[1] https://github.com/phillipkoebbe/DetectSyntax

@IrakliJani
Copy link

Added erb & ru https://gist.github.com/2940866

@survivorist use "Package Control" and install "RSpec"

@BrianScottK
Copy link

I've been seeing the same error on Sublime Text 2 for Windows. Tried adding the @irakli-janiashvili version noted above to my package directory, but still receiving same error.

@fahenao
Copy link

fahenao commented Sep 11, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment