Skip to content

Commit

Permalink
Coffee file linting
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrton committed Aug 24, 2013
1 parent 3c0b9ee commit 3b506d9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
58 changes: 51 additions & 7 deletions CoffeeScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import sublime_plugin
import time
import functools
import sys
import locale


def settings_get(name, default=None):
# load up the plugin settings
plugin_settings = sublime.load_settings('CoffeeScript.sublime-settings')
Expand All @@ -38,7 +38,7 @@ def run(cmd, args=[], source="", cwd=None, env=None):
if sys.version_info[0] == 2:
for i in range(len(args)):
args[i] = args[i].encode(locale.getdefaultlocale()[1])
proc = Popen(args, env=env, cwd=cwd, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell = True)
proc = Popen(args, env=env, cwd=cwd, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
try:
stat = proc.communicate(input=source)
except:
Expand Down Expand Up @@ -137,7 +137,7 @@ def run(self, *args, **kwargs):
compile_dir = os.path.join(norm_path, compile_dir)
compile_dir = os.path.join(compile_dir, appendix)

if compile_dir and (isinstance(compile_dir, str) or isinstance(compile_dir, unicode)):
if compile_dir and (isinstance(compile_dir, str)):
# Check for absolute path or relative path for compile_dir
if not os.path.isabs(compile_dir):
compile_dir = os.path.join(source_dir, compile_dir)
Expand Down Expand Up @@ -280,7 +280,7 @@ def run(self, edit):
views[myvid]["input_obj"] = self.view

print("Now watching", watched_filename(myvid))
createOut(myvid,edit)
createOut(myvid, edit)

else:
views = ToggleWatch.views
Expand Down Expand Up @@ -311,7 +311,7 @@ def get_output_filename(input_view_id):
return output_filename


def createOut(input_view_id,edit):
def createOut(input_view_id, edit):
#create output panel and save
this_view = ToggleWatch.views[input_view_id]
outputs = ToggleWatch.outputs
Expand Down Expand Up @@ -386,7 +386,7 @@ def handleTimeout(self, vid):
modified = this_view['modified']
if modified is True:
# been 1000ms since the last modification
refreshOut(vid,this_view["edit"])
refreshOut(vid, this_view["edit"])

def on_modified(self, view):
vid = view.id()
Expand Down Expand Up @@ -418,7 +418,7 @@ def on_post_save(self, view):
save_view = ToggleWatch.views[save_id]
# check if modified
if save_view['modified'] is True:
refreshOut(save_id,save_view['edit'])
refreshOut(save_id, save_view['edit'])
compile_on_save = settings_get('compileOnSave', True)
if compile_on_save is True and isCoffee() is True:

Expand Down Expand Up @@ -452,6 +452,50 @@ def on_close(self, view):
return


class LintCommand(TextCommand):

def is_enabled(self):
return isCoffee(self.view)

def run(self, edit):
filepath = self.view.file_name()
res = run("coffeelint", args=[filepath, "--csv"])
error_list = []
for line in res["out"].split('\n'):
if not len(line.split(","))-1:
continue
file, line, type, message = line.split(",")
error_list.append({"message": message, "line": int(line)-1})
self.popup_error_list(error_list)

def popup_error_list(self, error_list):

panel_items = []

for error in error_list:
line_text = self.view.substr(self.view.full_line(self.view.text_point(error['line'], 0)))
item = [error['message'], '{0}: {1}'.format(error['line'] + 1, line_text.strip())]
panel_items.append(item)

def on_done(selected_item):
if selected_item == -1:
return

selected = self.view.sel()
selected.clear()

error = error_list[selected_item]
region_begin = self.view.text_point(error['line'], 0)

selected.add(sublime.Region(region_begin, region_begin))
# We have to force a move to update the cursor position
self.view.run_command('move', {'by': 'characters', 'forward': True})
self.view.run_command('move', {'by': 'characters', 'forward': False})
self.view.show_at_center(region_begin)

self.view.window().show_quick_panel(panel_items, on_done)


class RunScriptCommand(TextCommand):
PANEL_NAME = 'coffee_compile_output'
PANEL_IS_OPEN = False
Expand Down
4 changes: 4 additions & 0 deletions CoffeeScript.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
"caption": "Coffee: Run Script / Selection"
, "command": "run_script"
}
, {
"caption": "Coffee: Lint"
, "command": "lint"
}
]

0 comments on commit 3b506d9

Please sign in to comment.