Home Download Buy Blog Forum Support

Validate PHP Syntax

Validate PHP Syntax

Postby natebeaty on Thu Apr 07, 2011 2:24 am

I'm trying to replicate TextMates ctrl+shift+v Validate Syntax, specifically for PHP. It's handy to quickly check if there are any errors before saving a remotely-edited file, instead of finding the error in the web browser.

This is the command used in TM:

Code: Select all
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/textmate'
version = %x{#{ENV['TM_PHP'] || 'php'} -v}.split[0..2].join(' ')
puts "Running syntax check with " + version + "…"
result = `#{ENV['TM_PHP'] || 'php'} -d display_errors=on -l`
puts result.gsub('in -', '')
TextMate.go_to :line => $1 if result =~ /line (\d+)/


Any suggestions as to the best way to reproduce this in ST2? My first attempt was along the lines of:

Code: Select all
{ "keys": ["ctrl+shift+v"], "command": "exec", "args": { "cmd": ["php -d display_errors=on -l $TM_FILENAME"]} }


.. but that obviously didn't work. Sorry for the dumb question, I know it should be dead easy, but I'm new to learning ST2 and any help is appreciated.
natebeaty
 
Posts: 122
Joined: Wed Apr 06, 2011 8:28 pm
Location: Chicago, IL

Re: Validate PHP Syntax

Postby allaire on Mon Apr 11, 2011 8:15 pm

Any input on this? I miss this too :(
allaire
 
Posts: 15
Joined: Mon Apr 11, 2011 6:44 pm

Re: Validate PHP Syntax

Postby n00ge on Mon Apr 11, 2011 8:35 pm

I setup a simple build file for this. Here's what I have:
Code: Select all
{
   "cmd": ["c:\\xampp\\php\\php.exe", "-l", "$file"],
   "selector": "source.php"
}


Just change the c:\\xampp\\php\\php.exe to the path of your php executable

It would be nicer as a Plugin to tie in when saving but I haven't gotten far enough in Python to figure that out.
http://twitter.com/n00ge
Sublime Text 3 dev builds | OS X 10.8.2 & Ubuntu 13.04
n00ge
 
Posts: 140
Joined: Mon Oct 05, 2009 1:23 am

Re: Validate PHP Syntax

Postby n00ge on Mon Apr 11, 2011 8:36 pm

Oh, and to use a build file, save that as PHP.sublime-build in your PHP package (Preferences > Browse Packages). Then hit F7 or Tools > Build to run it.
http://twitter.com/n00ge
Sublime Text 3 dev builds | OS X 10.8.2 & Ubuntu 13.04
n00ge
 
Posts: 140
Joined: Mon Oct 05, 2009 1:23 am

Re: Validate PHP Syntax

Postby Anomareh on Mon Apr 11, 2011 9:42 pm

n00ge wrote:It would be nicer as a Plugin to tie in when saving but I haven't gotten far enough in Python to figure that out.

Can't you just turn on build on save for PHP files? I knew you could in Sublime 1.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Validate PHP Syntax

Postby n00ge on Mon Apr 11, 2011 11:30 pm

I haven't seen any options for that but it might be handy. Probably needs to be a per-language setting, right?
http://twitter.com/n00ge
Sublime Text 3 dev builds | OS X 10.8.2 & Ubuntu 13.04
n00ge
 
Posts: 140
Joined: Mon Oct 05, 2009 1:23 am

Re: Validate PHP Syntax

Postby allaire on Tue Apr 12, 2011 12:00 am

Hey thanks Nooge, it worked flawlessly! Would be cool that it show the error in a tooltip, but oh well! and I didn't find how to build on save, any tips to do that for Sublime text 2?
allaire
 
Posts: 15
Joined: Mon Apr 11, 2011 6:44 pm

Re: Validate PHP Syntax

Postby jbrooksuk on Tue Apr 12, 2011 8:26 am

What happened to lineNumberRegex? Or for ST2, line_number/line_number_regex

Wham. Even better.

Code: Select all
{
   "cmd": ["c:\\xampp\\php\\php.exe", "-l", "$file"],
   "selector": "source.php",
   "file_regex": "^Parse error: .* in (.*?) on line ([0-9]*)"
}

Press F4 to jump to the line :)
jbrooksuk
 
Posts: 781
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Validate PHP Syntax

Postby allaire on Tue Apr 12, 2011 1:19 pm

Hey that's nice! Just missing build on save and I'm an happy guy!

Also, is there any way to make the console that pops a fixed height each time I open a window or I start Sublime? I have to resize it everytime :)
allaire
 
Posts: 15
Joined: Mon Apr 11, 2011 6:44 pm

Re: Validate PHP Syntax

Postby jbrooksuk on Tue Apr 12, 2011 2:53 pm

allaire wrote:Also, is there any way to make the console that pops a fixed height each time I open a window or I start Sublime? I have to resize it everytime :)

Unless Jon has added an option for it, I don't think there is.
jbrooksuk
 
Posts: 781
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Next

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 19 guests