-
-
Save JeanMertz/925008 to your computer and use it in GitHub Desktop.
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 |
It worked for me! Thanks
Thanks!
Please, prompt what exact directory means. I don't have "Packages/User directory". I have "lib" and "Pristine Packages" and there is now subfolders. Maybe I should create some, or it means my User folder, but I can't find any.
Thanks! I found it, but in Ubuntu it is slightly different - "/home/$usename$/.config/sublime-text-2/Packages/"
I found it by "application support", so it's a same idea:)
Nice! Thank you...
I added one more condition to identify the "erb" files. https://gist.github.com/1476493.
In case anyone is interested, I've been working on an expanded version of this concept (started with this gist actually). I wanted to try to distinguish between Ruby files in a Rails project and plain old Ruby files. I added other things as I came across them until it became obvious that it's a generic syntax switcher. I also added some event hooks to help in identifying the syntax on newly created files.
@StealthyGecko
What platform are you on? What is the full path of your Packages/User directory? Are you positive you saved the file with a .py extension? Show the console before you load a file and see if any messages are displayed. Alternatively, you could try the expanded switcher I created (based on this one): https://gist.github.com/1497794
I'm on Snow Leopard and the full path is "/Users/stealthygecko/Library/Application Support/Sublime Text 2/Packages/User" are you using the rspec bundle that was created for Sublime of the TM bundle? as I cannot get the Sublime bundle to work but the TMBundle (once selected manually) works fine.
I will give your expanded switcher a try and see if that works :)
thanks
@StealthyGecko
Path is correct. Not sure why the script wouldn't work.
I'm using the RSpec syntax definition from TextMate. All I've basically done so far is just copy the language files over. I haven't experimented much with snippets or anything else.
Try the other one. I use it all the time. There are still some tweaks I need to make to the code, but so far it has been working very well for me. Feel free to customize it for your specific needs.
I believe this where the scripts are breaking as yours is doing the same, where did you get the language files from as I am still getting the following error Error loading syntax file "Packages/RSpec/RSpec.tmLanguage": Error parsing plist xml: Failed to open file In file "Packages/RSpec/RSpec.tmLanguage" this would suggest to me that the language file does not actually reside in the TMbundle itself but its possible compiled afterwards.
I copied the syntax file from:
~/Library/Application Support/TextMate/Managed/Bundles/RSpec.tmbundle/Syntaxes/RSpec.tmLanguage
In Finder, right click on the RSpec.tmbundle and choose Show Package Contents (or whatever it is on Snow Leopard), then you can get down to the syntax file.
actually looking at your script it looks like all that needs to be done is to make sure that if the tmbundle exists it uses that otherwise use the tmLanguage as you are already doing. I am not overly familiar with Python but will take a look and see if I can fix this and then fork your gist.
Oops. Take out the "Managed". It should be
~/Library/Application Support/TextMate/Bundles/RSpec.tmbundle/Syntaxes/RSpec.tmLanguage
Sorry for the confusion.
If you are going to use the script I extended, we should move the conversation there.
Sorry I only just saw your reply, I have done this and its working fine now :) it was entirely my fault that it was not working in the first place, I had not drilled down that far into the bundle to the language file :) thanks for your help :)
@StealthyGecko
I'm very pleased you got it working!
It works great! I don't even need to restart sublime text!
Dropped in to say thanks! Works beautifully for me!
Where does it go?
This is exactly what we were planning on adding to the RSpec package in the Sublime Text org on GitHub. Mind if I borrow this and plop it right into that package? Seems to make sense to consolidate our efforts!
Added a check for erb files here: https://gist.github.com/2142985
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.
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.
Added erb & ru https://gist.github.com/2940866
@survivorist use "Package Control" and install "RSpec"
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.
Thanks!
Thanks