forked from rorydriscoll/LuaSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseLua.py
46 lines (40 loc) · 1.5 KB
/
ParseLua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sublime
import sublime_plugin
import re
from subprocess import Popen, PIPE
class ParseLuaCommand(sublime_plugin.EventListener):
TIMEOUT_MS = 200
def __init__(self):
self.pending = 0
def on_modified(self, view):
self.settings = sublime.load_settings("Lua Dev.sublime-settings")
if not self.settings.get("live_parser"):
return
filename = view.file_name()
if not filename or not filename.endswith('.lua'):
return
self.pending = self.pending + 1
sublime.set_timeout(lambda: self.parse(view), self.TIMEOUT_MS)
def parse(self, view):
# Don't bother parsing if there's another parse command pending
self.pending = self.pending - 1
if self.pending > 0:
return
# Grab the path to luac from the settings
luac_path = self.settings.get("luac_path")
# Run luac with the parse option
p = Popen(luac_path + ' -p -', stdin=PIPE, stderr=PIPE, shell=True)
text = view.substr(sublime.Region(0, view.size()))
errors = p.communicate(text.encode('utf-8'))[1]
result = p.wait()
# Clear out any old region markers
view.erase_regions('lua')
# Nothing to do if it parsed successfully
if result == 0:
return
# Add regions and place the error message in the status bar
errors = errors.decode("utf-8")
sublime.status_message(errors)
pattern = re.compile(r':([0-9]+):')
regions = [view.full_line(view.text_point(int(match) - 1, 0)) for match in pattern.findall(errors)]
view.add_regions('lua', regions, 'invalid', 'DOT', sublime.HIDDEN)