Created
June 22, 2012 18:35
-
-
Save fnando/2974422 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
@fnando nem é ruby, tava zoando, mas esses "+" nas strings faria pythonistas chorarem de emoção. Use string interpolation :P
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@PotHix o que tem de Ruby nesse Python, que eu perdi! :P