Skip to content

Instantly share code, notes, and snippets.

@Knio
Created August 11, 2010 01:39
Show Gist options
  • Save Knio/518345 to your computer and use it in GitHub Desktop.
Save Knio/518345 to your computer and use it in GitHub Desktop.
#
# A Sublime Text (http://www.sublimetext.com/) plugin that
# runs jsl (http://www.javascriptlint.com/) on files that end in .js
# Will higlight the offending lines, and flash error messages to the
# status bar.
import sublime, sublime_plugin
import subprocess
import re
import functools
import os
JSL_PATH = '/home/tom/imo.im/default/scripts/jslint/imojslint'
class JSLint(sublime_plugin.EventListener):
def __init__(self):
sublime_plugin.EventListener.__init__(self)
self.errors = []
self.i = 0
self.updating = False
self.linting = False
def update(self, cb=True):
if cb:
self.updating = False
if not self.errors:
return
l = len(self.errors)
j = self.i % l
sublime.status_message(('[%d/%d] ' % (j+1, l)) + self.errors[j])
self.i += 1
if not self.updating:
sublime.set_timeout(self.update, 5000)
self.updating = True
def on_load(self, view):
print view.file_name(), "just got loaded"
self.dolint(view)
def on_pre_save(self, view):
print view.file_name(), "is about to be saved"
def dolint(self, view, t=10):
if self.linting:
return
self.linting = True
print 'setting timeout to', t
sublime.set_timeout(functools.partial(self.lint, view), t)
def lint(self, view):
fn = view.file_name()
if not (fn and fn.endswith('.js')):
self.linting = False
print 'not linting', fn
return
self.linting = False
print 'running jslint on %s' % fn
print subprocess
p = subprocess.Popen(
['bash', JSL_PATH, fn],
# shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = p.communicate()
print 'exit', p.returncode
print 'out', out
print 'err', err
errs = out.split('\n')
regions = []
self.errors = []
for e in errs:
if not e: continue
print e.split('|##|')
row,col,fname,we,er,dline = e.split('|##|')
if er.startswith('unexpected end of line'):
continue
if er.startswith("Unexpected dangling '_'"):
continue
row = int(row)-1
col = int(col)-1
point = view.text_point(row, col)
line = view.line(point)
line = sublime.Region(line.a + col, line.b)
self.errors.append(
'%s(%d) %s ----- %s' % \
(os.path.basename(fn), row+1, er, dline)
)
regions.append(line)
view.add_regions('lint', regions, 'string')
self.update(False)
self.linting = False
def on_post_save(self, view):
self.dolint(view, 10)
print view.file_name(), "just got saved"
def on_new(self, view):
print "new file"
def on_modified(self, view):
print view.file_name(), "modified"
# self.dolint(view, 10000)
def on_activated(self, view):
print view.file_name(), "is now the active view"
self.dolint(view, 3000)
def on_close(self, view):
print view.file_name(), "is no more"
def on_clone(self, view):
print view.file_name(), "just got cloned"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment