forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latextools_sublime_version_listener.py
67 lines (52 loc) · 2.04 KB
/
latextools_sublime_version_listener.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
from __future__ import print_function
import sublime
import sublime_plugin
import operator as opi
import re
VERSION = sublime.version()
class LatextoolsSublimeVersionEventListener(sublime_plugin.EventListener):
def __init__(self, *args, **kwargs):
super(LatextoolsSublimeVersionEventListener, self).__init__(
*args, **kwargs
)
self.ops = {
sublime.OP_EQUAL: self._equal,
sublime.OP_NOT_EQUAL: self._not_equal,
sublime.OP_REGEX_MATCH: self._regex_match,
sublime.OP_NOT_REGEX_MATCH: self._not_regex_match,
sublime.OP_REGEX_CONTAINS: self._regex_contains,
sublime.OP_NOT_REGEX_CONTAINS: self._not_regex_contains
}
def on_query_context(self, view, key, operator, operand, match_all):
if not key == 'latextools.st_version':
return None
try:
return self.ops[operator](operand)
# caution
except KeyError:
return None
def _equal(self, operand):
# if the operand starts with <, >, <=, >=, (=, or ==)
# change the compare operator correspondingly and strip the operand
if operand[0:1] in "<>=":
i = 1 if operand[1:2] != "=" else 2
comp_str, operand = operand[:i], operand[i:]
compare = {
"<": opi.lt,
">": opi.gt,
"<=": opi.le,
">=": opi.ge
}.get(comp_str, opi.eq)
else:
compare = opi.eq
return compare(VERSION, operand.strip())
def _not_equal(self, operand):
return not self._equal(operand)
def _regex_match(self, operand):
return re.match(operand, VERSION, re.UNICODE) is not None
def _not_regex_match(self, operand):
return re.match(operand, VERSION, re.UNICODE) is None
def _regex_contains(self, operand):
return re.search(operand, VERSION, re.UNICODE) is not None
def _not_regex_contains(self, operand):
return re.search(operand, VERSION, re.UNICODE) is None