Home Download Buy Blog Forum Support

DetectSyntax

DetectSyntax

Postby phillip.koebbe on Thu Jan 19, 2012 10:38 pm

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 https://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.
Last edited by phillip.koebbe on Sun Jan 22, 2012 8:22 pm, edited 1 time in total.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: DetectSyntax

Postby Alex on Fri Jan 20, 2012 1:52 pm

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:
Code: Select all
      {
         "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.
Alex
 
Posts: 4
Joined: Fri Jan 20, 2012 1:48 pm

Re: DetectSyntax

Postby phillip.koebbe on Fri Jan 20, 2012 2:22 pm

Hi Alex,

Glad you find it useful!

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

Code: Select all
      {
         "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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: DetectSyntax

Postby Alex on Fri Jan 20, 2012 5:16 pm

On this configuration (function config on top, custom below) the test css file becomes opened to Syntax "Plain Text":
Code: Select all
{
   "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"
Code: Select all
{
   "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"
Alex
 
Posts: 4
Joined: Fri Jan 20, 2012 1:48 pm

Re: DetectSyntax

Postby phillip.koebbe on Fri Jan 20, 2012 8:30 pm

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!
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: DetectSyntax

Postby C0D312 on Fri Jan 20, 2012 8:36 pm

phillip.koebbe wrote:I will also look for a way to determine the plugin's directory at runtime. That would have also avoided your pain.


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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: DetectSyntax

Postby phillip.koebbe on Fri Jan 20, 2012 8:46 pm

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: DetectSyntax

Postby phillip.koebbe on Sat Jan 21, 2012 12:47 am

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: DetectSyntax

Postby aparajita on Wed Jan 25, 2012 7:27 am

Totally awesome! I am going to retire SetSyntax, this is much better.
aparajita
 
Posts: 177
Joined: Sun Jul 24, 2011 10:42 pm

Re: DetectSyntax

Postby phillip.koebbe on Wed Jan 25, 2012 10:29 am

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


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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests