forked from mawww/kakoune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkakoune.py
228 lines (159 loc) · 5.91 KB
/
kakoune.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import gdb.printing
class ArrayIterator:
def __init__(self, data, count):
self.data = data
self.count = count
self.index = 0
def __iter__(self):
return self
def next(self):
if self.index == self.count:
raise StopIteration
index = self.index
self.index = self.index + 1
return ('[%d]' % index, (self.data + index).dereference())
def __next__(self):
return self.next()
class ArrayView:
"""Print a ArrayView"""
def __init__(self, val):
self.val = val
def display_hint(self):
return 'array'
def children(self):
return ArrayIterator(self.val['m_pointer'], self.val['m_size'])
def to_string(self):
type = self.val.type.template_argument(0).unqualified().strip_typedefs()
return "ArrayView<%s>" % (type)
class LineAndColumn:
"""Print a LineAndColumn"""
def __init__(self, val):
self.val = val
def to_string(self):
value_type = self.val.type.unqualified()
return "%s(%s, %s)" % (value_type, self.val['line'],
self.val['column'])
class BufferCoordAndTarget:
"""Print a BufferCoordAndTarget"""
def __init__(self, val):
self.val = val
def to_string(self):
value_type = self.val.type.unqualified()
return "%s(%s, %s, %s)" % (value_type, self.val['line'],
self.val['column'], self.val['target'])
class BufferIterator:
""" Print a BufferIterator"""
def __init__(self, val):
self.val = val
def to_string(self):
line = self.val['m_coord']['line']
column = self.val['m_coord']['column']
if self.val['m_buffer']['m_ptr'] != 0:
buf = self.val['m_buffer']['m_ptr'].dereference()['m_name']
return "buffer<%s>@(%s, %s)" % (buf, line, column)
else:
return "buffer<none>@(%s, %s)" % (line, column)
class String:
""" Print a String"""
def __init__(self, val):
self.val = val
def to_string(self):
data = self.val["m_data"]
if (data["u"]["s"]["size"] & 1) != 1:
ptr = data["u"]["l"]["ptr"]
len = data["u"]["l"]["size"]
else:
ptr = data["u"]["s"]["string"]
len = data["u"]["s"]["size"] >> 1
return "\"%s\"" % (ptr.string("utf-8", "ignore", len))
class StringView:
""" Print a StringView"""
def __init__(self, val):
self.val = val
def to_string(self):
len = self.val['m_length']['m_value']
return "\"%s\"" % (self.val['m_data'].string("utf-8", "ignore", len))
class StringDataPtr:
""" Print a RefPtr<StringData>"""
def __init__(self, val):
self.val = val
def to_string(self):
ptr = self.val['m_ptr']
str_type = gdb.lookup_type("char").pointer()
len = ptr.dereference()['length']
refcount = ptr.dereference()['refcount']
content = (ptr + 1).cast(str_type).string("utf-8", "ignore", len)
return "\"%s\" (ref:%d)" % (content.replace("\n", "\\n"), refcount)
class RefPtr:
""" Print a RefPtr"""
def __init__(self, val):
self.val = val
def to_string(self):
ptr = self.val['m_ptr']
return "\"refptr %s\"" % (ptr)
class Option:
""" Print a Option"""
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["m_value"]
class CharCount:
"""Print a CharCount"""
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["m_value"]
class ColumnCount:
"""Print a ColumnCount"""
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["m_value"]
class ByteCount:
"""Print a ByteCount"""
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["m_value"]
class LineCount:
"""Print a LineCount"""
def __init__(self, val):
self.val = val
def to_string(self):
return self.val["m_value"]
class Color:
"""Print a Color"""
def __init__(self, val):
self.val = val
def to_string(self):
named_color = gdb.lookup_type("Kakoune::Color::NamedColor")
if self.val["color"] == named_color["Kakoune::Color::RGB"].enumval:
return "%s #%02x%02x%02x" % (self.val["color"], self.val["r"],
self.val["g"], self.val["b"])
else:
return self.val["color"]
class Regex:
"""Print a Regex"""
def __init__(self, val):
self.val = val
def to_string(self):
return "regex%s" % (self.val["m_str"])
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("kakoune")
pp.add_printer('ArrayView', '^Kakoune::(Const)?ArrayView<.*>$', ArrayView)
pp.add_printer('LineAndColumn', '^Kakoune::(Buffer|Display)Coord$', LineAndColumn)
pp.add_printer('BufferCoordAndTarget', '^Kakoune::BufferCoordAndTarget$', BufferCoordAndTarget)
pp.add_printer('BufferIterator', '^Kakoune::BufferIterator$', BufferIterator)
pp.add_printer('String', '^Kakoune::String$', String)
pp.add_printer('StringView', '^Kakoune::(StringView|SharedString)$', StringView)
pp.add_printer('StringDataPtr', '^Kakoune::StringDataPtr$', StringDataPtr)
pp.add_printer('StringDataPtr', '^Kakoune::RefPtr<Kakoune::StringData,.*>$', StringDataPtr)
pp.add_printer('RefPtr', '^Kakoune::RefPtr<.*>$', RefPtr)
pp.add_printer('Option', '^Kakoune::Option$', Option)
pp.add_printer('LineCount', '^Kakoune::LineCount$', LineCount)
pp.add_printer('CharCount', '^Kakoune::CharCount$', CharCount)
pp.add_printer('ColumnCount', '^Kakoune::ColumnCount$', ColumnCount)
pp.add_printer('ByteCount', '^Kakoune::ByteCount$', ByteCount)
pp.add_printer('Color', '^Kakoune::Color$', Color)
pp.add_printer('Regex', '^Kakoune::Regex$', Regex)
return pp