Sublime Forum

DetectSyntax

#1

I just released DetectSyntax, another syntax switcher. The difference with this one is two-fold:

  • rules are entirely regexps applied to the file_name
  • if you need something more complicated than regexing the file_name, you can define your own function

My hope is that it is flexible enough for just about anyone to get benefit from it. If not, let me know and I’ll see if I can make it more so.

It can be found at github.com/phillipkoebbe/DetectSyntax and I have made a pull request to make it available in Package Control.

EDIT: as of 2012-01-22, it’s available via Package Control.

0 Likes

#2

Awesome addon! Directly configured it for the twig templates in my project where every file ends with .twig like stylesheet.css.twig, default.html.twig or javascript.js.twig.

Used these lines:

{ "name": "CSS", "rules": {"file_name": ".*\\.css\\.twig$"}] }, { "name": "PHP", "rules": {"file_name": ".*\\.(x?p?html|cml)\\.twig$"}] }, { "name": "JavaScript", "rules": {"file_name": ".*\\.js\\.twig$"}] },

But I had to insert this JSON above the function example as the extension seems to stop at that rule and does not process any further.

0 Likes

#3

Hi Alex,

Glad you find it useful!

Do you mean that the rules you posted have to be put before

		{
			"name": "Rails/Ruby on Rails",
			"rules": 
				{"function": {"name": "is_rails_file", "source": "is_rails_file.py"}}
			]
		},

Or they don’t work? Will you please post the entirety of DetectSyntax.sublime-settings (your customized one), minus comments? I’d like to figure out why that happens. From looking at the rules, there should be no reason why the is_rails_file function would hit. It looks only for .rb/.rake files in a Rails directory structure.

0 Likes

#4

On this configuration (function config on top, custom below) the test css file becomes opened to Syntax “Plain Text”:

{ "syntaxes": { "name": "Rails/Ruby on Rails", "rules": {"function": {"name": "is_rails_file", "source": "is_rails_file.py"}} ] }, { "name": "CSS", "rules": {"file_name": ".*\\.css\\.twig$"}] } ] }

If I put the CSS config on top, it works. The testfile “stylesheet.css.twig” becomes the syntax “CSS”

{ "syntaxes": { "name": "CSS", "rules": {"file_name": ".*\\.css\\.twig$"}] }, { "name": "Rails/Ruby on Rails", "rules": {"function": {"name": "is_rails_file", "source": "is_rails_file.py"}} ] } ] }

I work on OSX 10.7.2
Config file is ./Application Support/Sublime Text 2/User/DetectSyntax.sublime-settings
Package files are in ./Application Support/Sublime Text 2/phillipkoebbe-DetectSyntax/…
With the files inside “DetectSyntax.py” “DetectSyntax.pyc” “DetectSyntax.sublime-settings” “is_rails_file.py” “is_rails_file.pyc” “readme.md”

0 Likes

#5

Hi Alex,

Rename ~/Application Support/Sublime Text 2/phillipkoebbe-DetectSyntax to ~/Application Support/Sublime Text 2/DetectSyntax and it should work. The function rule looks for the file in User/ and then DetectSyntax/ and because it didn’t find it, it raised an error. If you try it again with the twig rule after the function rule, and view your console, you should see it. When I wrote the function processing part, I originally had exception handling in there to catch stuff like that and just return false, but then I decided to remove that in favor of letting the exceptions bubble up so those writing functions would get feedback when things didn’t work right. I think I’ll add an option in the settings that controls whether exceptions are handled gracefully or bubble up. I’ll default to handling errors properly so the average user won’t experience this problem.

I will also look for a way to determine the plugin’s directory at runtime. That would have also avoided your pain.

Thanks for helping work out the bugs!

0 Likes

#6

The Package Control Channel has an option to name your package, that way everyone has the same folder name for your package and eliminating these problems.

0 Likes

#7

I saw that when I was adding the package to the repository but I thought it was primarily for when your repository has one thing but you want the package to show up as something else. The GitHub repository name is the same as what I’d like the package to be. Either way, it isn’t available in Package Control yet. I’ve submitted a pull request, but have not received a response.

I think, in this case, the package was downloaded from GitHub. When I download it by clicking the ZIP icon, it comes down as phillipkoebbe-DetectSyntax.zip. Unzipping in the Packages directory would place it in the wrong directory. It would just add a nice bit of user-friendliness if I could determine the directory at runtime.

0 Likes

#8

I added an option (reraise_exceptions) to the settings file which controls whether the function rule will fail silently and just return False or reraise any exception encountered. Default is false, don’t reraise exceptions. If you work on a function rule, though, you should turn reraise_exceptions on while developing so you’ll know why it fails (if it does). Once satisfied that your function works correctly, you can then turn reraise_exceptions off again.

0 Likes

#9

Totally awesome! I am going to retire SetSyntax, this is much better.

0 Likes

#10

Considering I got the idea of using a settings file from SetSyntax, that is really saying something!

Thank you for the kind words. I’m humbled.

0 Likes

#11

I have an issue with this plugin. Not working for me on (I am on Windows 7 x64)
When I run Sublime I receive this error:

Error loading syntac file “Packages\PHP\PHP.tmLanguage”:
Error parsing plist xml: Failed to open file in file “Packages\PHP\PHP.tmLanguage”

Any ideas what wrong?

0 Likes

#12

[quote=“sbloom”]I have an issue with this plugin. Not working for me on (I am on Windows 7 x64)
When I run Sublime I receive this error:

Error loading syntac file “Packages\PHP\PHP.tmLanguage”:
Error parsing plist xml: Failed to open file in file “Packages\PHP\PHP.tmLanguage”

Any ideas what wrong?[/quote]

Hi sbloom,

Sorry it’s not working correctly for you. I don’t have access to windows 7, but I think I know what’s going on. In the default settings file where the syntax rules are defined, I use a forward slash to separate path from name, and I’m thinking that Windows doesn’t like that. So I made a small change to normalize the string before doing some more work on it, and I’m hopeful it will work for you. I pushed the change to GitHub, so the next time Package Control updates, you should get the fix. If it still doesn’t work, please let me know. If nothing else, I’ll install ST2 on XP and see what happens.

0 Likes

#13

[quote=“phillip.koebbe”]
Hi sbloom,

Sorry it’s not working correctly for you. I don’t have access to windows 7, but I think I know what’s going on. In the default settings file where the syntax rules are defined, I use a forward slash to separate path from name, and I’m thinking that Windows doesn’t like that. So I made a small change to normalize the string before doing some more work on it, and I’m hopeful it will work for you. I pushed the change to GitHub, so the next time Package Control updates, you should get the fix. If it still doesn’t work, please let me know. If nothing else, I’ll install ST2 on XP and see what happens.[/quote]

Hi,
Thanks for trying to fix this. I think your plugin is awesome. Only if I had a chance to try it heh :smile:
Unfortunately latest update still not working to me, I get same error.

Update: the odd thing about it, that I can’t get rid of that error now, even after disabling/deleting DetectSyntax. Really weird.

0 Likes

#14

[quote=“sbloom”]
Hi,
Thanks for trying to fix this. I think you plugin is awesome. Only if I had a chance to try it heh :smile:
Unfortunately latest update still not working to me, I get same error.[/quote]

Well, that’s certainly disheartening :frowning:

It will be tonight before I’ll have a chance to take another look at it, but I’ll get it fixed as soon as I can. Thanks for the encouragement!

0 Likes

#15

I was able to test on Windows XP in a virtual machine. I changed the use of os.path.sep to a forward slash when building the name of the syntax file that is given to ST2. It worked as expected. I pushed the changes out so if you update the plugin (or when Package Control updates it), it should work now.

As always, please let me know if it doesn’t. I’m sorry for the inconvenience.

0 Likes

#16

Great plugin! Thanks Phillip!

I was having the same problems as sbloom (I’m also on Win7), but your latest update fixed all of the problems and I’m now able to automatically load extensionless Apache config files with the correct syntax. Thanks again!

0 Likes

#17

Thanks for a fix!

0 Likes

#18

I have installed the plugin, and copied over the DetectSyntax.sublime-settings into my User folder. I changed default_syntaxes to syntaxes.

But all my rails files are still being ?detected? as plain Ruby files.

Did I miss a step? Do the default rules not detect rails? Do I have to add a rule?

0 Likes

#19

Installing the plugin should have been all you needed to do. Well, maybe restarting ST2. You only need to have a copy of DetectSyntax.sublime-settings in your User folder if you want to add your own rules. By default, DetectSyntax should identify *.rb files in a Rails directory structure and set the syntax to Ruby on Rails.

What platform are you on? What is the path to the Rails project? Is there a Rails.root/config/routes.rb (that’s what DetectSyntax looks for to determine if it’s a Rails project). Do you have any non-ascii characters in your path?

0 Likes

#20

I am on W7.

Path to rails project should be all ascii, it doesn’t even have spaces.

I do have a config/routes.rb folder. What does it look relative to? I started ST2 from start menu, then choose open recent projects. My project file lives in the root of the rails project directory.

C:\Users\myname\Code\SyncCode\MyProj

I removed the settings file from my User directory, and restarted ST2. But opening a model_obj.rb file still shows Ruby syntax, as opposed to Ruby on Rails.

ps - how the heck do you get notified of replies in this forum?

0 Likes