This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.py
96 lines (79 loc) · 3.18 KB
/
plugin.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os.path
import sublime
import sublime_plugin
from . import checker
PACKAGE_NAME = os.path.splitext(os.path.basename(os.path.dirname(__file__)))[0]
class Validator(sublime_plugin.EventListener):
KNOWN_SYNTAXES = {'Packages/Python/Python.tmLanguage',
'Packages/Python/Python.sublime-syntax'}
def __init__(self, *args, **kwargs):
self.view_cache = {}
self.view_line = {}
super().__init__(*args, **kwargs)
def on_load_async(self, view):
if view.is_scratch():
return
self.revalidate(view)
def on_post_save_async(self, view):
if view.is_scratch():
return
self.revalidate(view)
def on_selection_modified_async(self, view):
if view.is_scratch():
return
self.update_statusbar(view)
def on_pre_close(self, view):
view_id = view.id()
if view_id in self.view_line:
del self.view_line[view_id]
if view_id in self.view_cache:
del self.view_cache[view_id]
def update_statusbar(self, view, force=False):
line = self.get_selected_line(view)
view_id = view.id()
if force or line != self.view_line.get(view.id(), None):
self.view_line[view_id] = line
error = self.view_cache.get(view_id, {}).get(line, None)
if error:
view.set_status('python-checker-problem', error)
else:
view.erase_status('python-checker-problem')
def get_selected_line(self, view):
selection = view.sel()
try:
return view.rowcol(selection[0].end())[0]
except Exception:
return None
def revalidate(self, view):
if view.settings().get("syntax") not in self.KNOWN_SYNTAXES:
return
problems = self.get_problems(view)
self.highlight_problems(view, problems)
self.update_statusbar(view, force=True)
def get_problems(self, view):
filename = view.file_name()
if not filename:
filename = '<unsaved>'
source = view.substr(sublime.Region(0, view.size()))
settings = view.settings()
python_executable = settings.get('python_interpreter_path', None)
if python_executable:
return checker.get_external(source, filename,
executable=python_executable)
return checker.get_problems(source, filename)
def highlight_problems(self, view, problems):
view.erase_regions('python-checker-problem')
view_id = view.id()
view_cache = {}
regions = []
for problem in problems:
view_cache[problem.lineno - 1] = problem.message
region_start = view.text_point(problem.lineno - 1, problem.offset)
region = view.word(region_start)
regions.append(region)
self.view_cache[view_id] = view_cache
style = (sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE |
sublime.DRAW_SQUIGGLY_UNDERLINE)
view.add_regions('python-checker-problem', regions, 'invalid',
'Packages/%s/images/marker.png' % (PACKAGE_NAME,),
style)