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 |
Don't care about Windows, don't use Ubuntu. :)
You put it in your Packages/User directory (create it if doesn't exist).
…On 22/06/2012, at 16:17, Washington Botelho wrote:
Little tips:
- Use OS separator `os.sep` instead the slash string;
- Use interpolation `print "Set syntax to '%s' using %s" % (syntax, language_file)` instead concat.
It works on Ubuntu? Where to put it?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2974422
Nào sei o que é mais bizarro, ser o Sublime, vc estar fazendo código Python, ou esse Python Rubyado! :P
Use Vim!
@PotHix o que tem de Ruby nesse Python, que eu perdi! :P
@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
Little tips:
os.sep
instead the slash string;print "Set syntax to '%s' using %s" % (syntax, language_file)
instead concat.It works on Ubuntu? Where to put it?