Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Forked from JeanMertz/syntax_highlighting.py
Created August 31, 2011 14:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olivierlacan/1183724 to your computer and use it in GitHub Desktop.
Save olivierlacan/1183724 to your computer and use it in GitHub Desktop.
Ruby on Rails syntax highlight switcher for Sublime Text 2
# ======================================================================================= #
# NOTE: This is relatively outdated, you should use the DetectSyntax package
# from Package Control instead: http://wbond.net/sublime_packages/community#sort-installs
# ======================================================================================= #
# Instructions: put this inside of the following folder on a Mac:
# /Users/<yourusername>/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/
# ======================================================================================= #
import sublime, sublime_plugin
import os
class DetectFileTypeCommand(sublime_plugin.EventListener):
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[-2:] == "rb":
set_syntax(view, "Ruby on Rails", "Rails")
elif name[-7:] == "gemfile":
set_syntax(view, "Ruby", "Ruby")
elif name[-4:] == "haml":
set_syntax(view, "Ruby Haml", "Ruby Haml")
elif name[-4:] == "sass":
set_syntax(view, "SASS", "SASS/Syntaxes")
elif name[-9:] == "guardfile":
set_syntax(view, "Ruby", "Ruby")
elif name[-2:] == "ru":
set_syntax(view, "Ruby", "Ruby")
elif name[-9:] == "coffeekup":
set_syntax(view, "CoffeeKup", "CoffeeKup")
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
@olivierlacan
Copy link
Author

Added Guardfile support for the wonderful Guard by @thibaudgg

@bcnice20
Copy link

I've found that if you aren't specifically working on a ruby file this script likes to set the syntax of say a python file to Ruby i modified the script a lil bit to prevent this thanks for the script!!!!

Btw it seems it specifically has to do with the Gemfile line heres the modified script.
https://gist.github.com/1212295

@olivierlacan
Copy link
Author

Good fix, the RSpec stuff was breaking anyway, took it out. Added your line for gemfile with an additional one for guardfile

@olivierlacan
Copy link
Author

I've updated this script to work with https://github.com/n00ge/sublime-text-haml-sass

For some reason Sass files were incorrectly detected as Ruby Haml.

@jlebrech
Copy link

where does this go?

@olivierlacan
Copy link
Author

Please note that you should probably be using the DetectSyntax package from Package Control instead of this outdated hack.

@noAlvaro
Copy link

noAlvaro commented Dec 4, 2012

Hi,

My .sass files were being detected as Ruby Haml syntax. I found your script and tried by using DetectSyntax on ST2.
Unfortunately its still failing on detection. I tried to add a Sass rule in the user configurations for the plugin but it doesn't seem to work also.

(...)
{
    "name": "Sass",
    "rules": [
        {"file_name": ".*\\.sass$"}
    ]
}
(...)

Do you have anything to share?

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