-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_sublime.py
78 lines (62 loc) · 1.94 KB
/
mock_sublime.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import bisect
class Region(object):
__slots__ = ["a", "b"]
def __init__(self, a, b):
assert isinstance(a, int)
assert isinstance(b, int)
self.a = a
self.b = b
def __str__(self):
return "Region({}, {})".format(self.a, self.b)
class View(object):
def __init__(self, text: str, file_name: str = None):
self.text = text
self._line_index = [0]
self._file_name = file_name
idx = None
while idx != -1:
idx = self.text.find("\n", self._line_index[-1])
if idx != -1:
self._line_index.append(idx + 1)
def file_name(self):
return self._file_name
def id(self):
return None
def size(self):
return len(self.text)
def sp_iter_all_line_regions(self):
a = 0
while a < self.size():
b = self.text.find('\n', a)
if b == -1:
b = self.text.size()
yield Region(a, b)
a = b + 1
def substr(self, region):
return self.text[region.a:region.b]
def rowcol(self, point):
assert point >= 0
row_index = bisect.bisect_right(self._line_index, point) - 1
col_index = point - self._line_index[row_index]
return (row_index, col_index)
def lines(self, region):
# TODO: интересно, как это счастье работает когда region на границе линии.
a = self.text.rfind("\n", 0, region.a)
if a == -1:
a = 0
else:
a += 1
result = []
while a < region.b:
b = self.text.find('\n', a)
if b == -1:
try:
b = self.size()
except:
print(repr(self))
raise
result.append(Region(a, b - 1))
a = b + 1
return result