1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 from libreoffice
.util
import printing
13 class SwPositionPrinter(object):
14 '''Prints SwPosition.'''
16 def __init__(self
, typename
, value
):
17 self
.typename
= typename
21 node
= self
.value
['nNode']['pNd'].dereference();
22 block
= node
['pBlock'].dereference();
23 nodeindex
= block
['nStart'] + node
['nOffset']
24 offset
= self
.value
['nContent']['m_nIndex']
25 return "%s (node %d, offset %d)" % (self
.typename
, nodeindex
, offset
)
27 class SwNodeIndexPrinter(object):
28 '''Prints SwNodeIndex.'''
30 def __init__(self
, typename
, value
):
31 self
.typename
= typename
35 node
= self
.value
['pNd'].dereference();
36 block
= node
['pBlock'].dereference();
37 nodeindex
= block
['nStart'] + node
['nOffset']
38 return "%s (node %d)" % (self
.typename
, nodeindex
)
40 class SwIndexPrinter(object):
43 def __init__(self
, typename
, value
):
44 self
.typename
= typename
48 offset
= self
.value
['m_nIndex']
49 return "%s (offset %d)" % (self
.typename
, offset
)
51 class SwPaMPrinter(object):
54 def __init__(self
, typename
, value
):
55 self
.typename
= typename
59 return "%s" % (self
.typename
)
62 next_
= self
.value
['pNext']
63 prev
= self
.value
['pPrev']
64 point
= self
.value
['m_pPoint'].dereference()
65 mark
= self
.value
['m_pMark'].dereference()
66 children
= [ ( 'point', point
), ( 'mark', mark
) ]
67 if next_
!= self
.value
.address
:
68 children
.append(("next", next_
))
69 if prev
!= self
.value
.address
:
70 children
.append(("prev", prev
))
71 return children
.__iter
__()
73 class SwUnoCrsrPrinter(SwPaMPrinter
):
74 '''Prints SwUnoCrsr.'''
76 class SwRectPrinter(object):
79 def __init__(self
, typename
, value
):
80 self
.typename
= typename
84 return "%s" % (self
.typename
)
87 point
= self
.value
['m_Point']
88 size
= self
.value
['m_Size']
89 children
= [ ( 'point', point
), ( 'size', size
) ]
90 return children
.__iter
__()
92 class SwUnoMarkPrinter(object):
93 '''Prints sw::mark::UnoMark.'''
95 def __init__(self
, typename
, value
):
96 self
.typename
= typename
100 return "%s" % (self
.typename
)
103 unoMark
= self
.value
.cast(self
.value
.dynamic_type
)
104 pos1
= unoMark
['m_pPos1']
105 pos2
= unoMark
['m_pPos2']
106 children
= [ ( 'pos1', pos1
), ( 'pos2', pos2
) ]
107 return children
.__iter
__()
109 class SwXTextRangeImplPrinter(object):
110 '''Prints SwXTextRange::Impl.'''
112 def __init__(self
, typename
, value
):
113 self
.typename
= typename
117 return "%s" % (self
.typename
)
120 mark
= self
.value
['m_pMark'].dereference()
121 children
= [('mark', mark
)]
122 return children
.__iter
__()
124 class SwXTextCursorImplPrinter(object):
125 '''Prints SwXTextCursor::Impl.'''
127 def __init__(self
, typename
, value
):
128 self
.typename
= typename
132 return "%s" % (self
.typename
)
135 registeredIn
= self
.value
['pRegisteredIn'].dereference()
136 children
= [('registeredIn', registeredIn
)]
137 return children
.__iter
__()
139 class SwUnoImplPtrPrinter(object):
140 """Prints sw::UnoImplPtr"""
142 def __init__(self
, typename
, value
):
143 self
.typename
= typename
147 if self
.value
['m_p']:
148 return "%s %s" % (self
.typename
, self
.value
['m_p'].dereference())
150 return "empty %s" % (self
.typename
,)
152 class SwXTextRangePrinter(object):
153 '''Prints SwXTextRange.'''
155 def __init__(self
, typename
, value
):
156 self
.typename
= typename
160 return "%s %s" % (self
.typename
, self
.value
['m_pImpl'])
162 class SwXTextCursorPrinter(object):
163 '''Prints SwXTextCursor.'''
165 def __init__(self
, typename
, value
):
166 self
.typename
= typename
170 return "%s %s" % (self
.typename
, self
.value
['m_pImpl'])
172 class BigPtrArrayPrinter(object):
173 '''Prints BigPtrArray.'''
175 def __init__(self
, typename
, value
):
176 self
.typename
= typename
180 length
= self
.value
['nSize']
182 return "%s of length %d" % (self
.typename
, length
)
184 return "empty %s" % self
.typename
187 return self
._iterator
(self
.value
)
189 def display_hint(self
):
193 class _iterator(six
.Iterator
):
195 def __init__(self
, array
):
196 self
.blocks
= array
['ppInf']
197 self
.count
= array
['nSize']
199 self
.block_count
= array
['nBlock']
203 self
.max_indent
= " "
204 self
._next
_block
(False)
205 self
._check
_invariant
()
210 def _node_value(self
, node
):
211 cur_indent
= self
.indent
212 if str(node
.dynamic_type
.target()) == "SwTextNode":
213 # accessing this is completely non-obvious...
214 # also, node.dynamic_cast(node.dynamic_type) is null?
215 value
= " TextNode " + \
216 six
.text_type(node
.cast(node
.dynamic_type
).dereference()['m_Text'])
217 elif str(node
.dynamic_type
.target()) == "SwOLENode":
219 elif str(node
.dynamic_type
.target()) == "SwGrfNode":
221 elif str(node
.dynamic_type
.target()) == "SwSectionNode":
222 value
= " SectionNode "
224 elif str(node
.dynamic_type
.target()) == "SwTableNode":
225 value
= " TableNode "
227 elif str(node
.dynamic_type
.target()) == "SwStartNode":
228 value
= " StartNode "
230 elif str(node
.dynamic_type
.target()) == "SwEndNode":
232 self
.indent
= self
.indent
[:-1]
233 cur_indent
= self
.indent
234 elif str(node
.dynamic_type
.target()) == "SwDummySectionNode":
235 value
= "DummySctNode "
236 else: # must be currently being deleted, so has some abstract type
237 value
= "~DeletedNode "
238 # return "\n[%s%4d%s] %s %s" % (cur_indent, self.pos, \
239 # self.max_indent[len(cur_indent):], node, value)
240 return "\n[%4d] %s%s%s %s" % (self
.pos
, cur_indent
, \
241 node
, self
.max_indent
[len(cur_indent
):], value
)
244 if self
.pos
== self
.count
:
245 raise StopIteration()
248 node
= self
.block
['pData'][self
.pos
- self
.block
['nStart']]
249 value
= self
._node
_value
(node
)
250 if self
.pos
== self
.block
['nEnd']:
254 self
._check
_invariant
()
257 def _next_block(self
, advance
= True):
261 if self
.block_pos
== self
.block_count
:
264 pblock
= self
.blocks
[self
.block_pos
]
266 block
= pblock
.dereference()
267 start
= block
['nStart']
269 assert end
- start
+ 1 == block
['nElem']
271 assert start
== self
.block
['nEnd'] + 1
272 assert end
<= self
.count
277 def _check_invariant(self
):
278 assert self
.pos
<= self
.count
279 assert self
.block_pos
<= self
.block_count
280 if self
.pos
== 0 and self
.pos
< self
.count
:
281 assert self
.block
!= None
285 def build_pretty_printers():
288 printer
= printing
.Printer("libreoffice/sw")
289 printer
.add('BigPtrArray', BigPtrArrayPrinter
)
290 printer
.add('SwPosition', SwPositionPrinter
)
291 printer
.add('SwNodeIndex', SwNodeIndexPrinter
)
292 printer
.add('SwIndex', SwIndexPrinter
)
293 printer
.add('SwPaM', SwPaMPrinter
)
294 printer
.add('SwUnoCrsr', SwUnoCrsrPrinter
)
295 printer
.add('SwRect', SwRectPrinter
)
296 printer
.add('sw::mark::UnoMark', SwUnoMarkPrinter
)
297 printer
.add('SwXTextRange::Impl', SwXTextRangeImplPrinter
)
298 printer
.add('sw::UnoImplPtr', SwUnoImplPtrPrinter
)
299 printer
.add('SwXTextRange', SwXTextRangePrinter
)
300 printer
.add('SwXTextCursor::Impl', SwXTextCursorImplPrinter
)
301 printer
.add('SwXTextCursor', SwXTextCursorPrinter
)
303 def register_pretty_printers(obj
):
304 printing
.register_pretty_printer(printer
, obj
)
306 build_pretty_printers()
308 # vim:set shiftwidth=4 softtabstop=4 expandtab: