I’m trying to run php -l (lint) on every file that I save, to check for syntax errors.
I would like to update the status bar with the result, if there is an error, but somehow I can’t manage to do that onPostSave.
First I had this which doesn’t work at all because I guess the sublime “File Saved blabla” message overwrites it.
[code]import sublime, sublimeplugin, subprocess
class phpLint(sublimeplugin.Plugin):
def onPostSave(self, view):
if view.fileName()-4:] == ‘.php’:
file = subprocess.Popen('php -l '+view.fileName(), stdout=subprocess.PIPE).stdout
if file.readline()[0:2] == ‘No’:
print ‘Syntax OK’;
else:
sublime.setStatus(‘phpLint’, file.readline());
[/code]
Then I tried this with the timeout as seen in the IdleWatcher example:
[code]import sublime, sublimeplugin, subprocess, functools
class phpLint(sublimeplugin.Plugin):
def onPostSave(self, view):
if view.fileName()-4:] == ‘.php’:
file = subprocess.Popen('php -l '+view.fileName(), stdout=subprocess.PIPE).stdout
if file.readline()[0:2] == ‘No’:
print ‘Syntax OK’;
else:
sublime.setTimeout(functools.partial(self.updateStatus, view, file.readline()), 500)
def updateStatus(self, view, text):
sublime.setStatus('phpLint', text);
[/code]
This fails with AttributeError: ‘module’ object has no attribute ‘setStatus’, which I’m not sure I understand.
Alternatively I could do it onPreSave and prevent the saving if the file has syntax error, but I’m not sure if that’s possible.
Also I have the problem that Popen() pops up a console window while php -l runs, which really sucks even though it just flashes… Any idea how to prevent that ?
Cheers.