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']['m_pNode'].dereference();
22 block
= node
['m_pBlock'].dereference();
23 nodeindex
= block
['nStart'] + node
['m_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
['m_pNode'].dereference();
36 block
= node
['m_pBlock'].dereference();
37 nodeindex
= block
['nStart'] + node
['m_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
['m_pNext']
63 prev
= self
.value
['m_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 # apparently the purpose of this is to suppress printing all the extra members
74 # that SwCursor and SwUnoCursor add
75 class SwUnoCursorPrinter(SwPaMPrinter
):
76 '''Prints SwUnoCursor.'''
78 class SwRectPrinter(object):
81 def __init__(self
, typename
, value
):
82 self
.typename
= typename
86 return "%s" % (self
.typename
)
89 point
= self
.value
['m_Point']
90 size
= self
.value
['m_Size']
91 children
= [ ( 'point', point
), ( 'size', size
) ]
92 return children
.__iter
__()
94 class MarkBasePrinter(object):
95 '''Prints sw::mark::MarkBase.'''
97 def __init__(self
, typename
, value
):
98 self
.typename
= typename
102 return "%s" % (self
.typename
)
105 m
= self
.value
.cast(self
.value
.dynamic_type
)
106 return [ ( v
, m
[ v
] )
107 for v
in ( 'm_aName', 'm_pPos1', 'm_pPos2' ) ].__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 cursor
= self
.value
['m_pUnoCursor']["m_pCursor"]["_M_ptr"]
136 registeredIn
= cursor
.dereference()
137 children
= [('m_pUnoCursor', registeredIn
)]
138 return children
.__iter
__()
140 class SwUnoImplPtrPrinter(object):
141 """Prints sw::UnoImplPtr"""
143 def __init__(self
, typename
, value
):
144 self
.typename
= typename
148 if self
.value
['m_p']:
149 return "%s %s" % (self
.typename
, self
.value
['m_p'].dereference())
151 return "empty %s" % (self
.typename
,)
153 class SwXTextRangePrinter(object):
154 '''Prints SwXTextRange.'''
156 def __init__(self
, typename
, value
):
157 self
.typename
= typename
161 return "%s %s" % (self
.typename
, self
.value
['m_pImpl'])
163 class SwXTextCursorPrinter(object):
164 '''Prints SwXTextCursor.'''
166 def __init__(self
, typename
, value
):
167 self
.typename
= typename
171 return "%s %s" % (self
.typename
, self
.value
['m_pImpl'])
173 class BigPtrArrayPrinter(object):
174 '''Prints BigPtrArray.'''
176 def __init__(self
, typename
, value
):
177 self
.typename
= typename
181 length
= self
.value
['m_nSize']
183 return "%s of length %d" % (self
.typename
, length
)
185 return "empty %s" % self
.typename
188 return self
._iterator
(self
.value
)
190 def display_hint(self
):
194 class _iterator(six
.Iterator
):
196 def __init__(self
, array
):
197 self
.blocks
= array
['m_ppInf']['_M_t']['_M_t']['_M_head_impl']
198 self
.count
= array
['m_nSize']
200 self
.block_count
= array
['m_nBlock']
204 self
.max_indent
= " "
205 self
._next
_block
(False)
206 self
._check
_invariant
()
211 def _node_value(self
, node
):
212 cur_indent
= self
.indent
213 if str(node
.dynamic_type
.target()) == "SwTextNode":
214 # accessing this is completely non-obvious...
215 # also, node.dynamic_cast(node.dynamic_type) is null?
216 value
= " TextNode " + \
217 six
.text_type(node
.cast(node
.dynamic_type
).dereference()['m_Text'])
218 elif str(node
.dynamic_type
.target()) == "SwOLENode":
220 elif str(node
.dynamic_type
.target()) == "SwGrfNode":
222 elif str(node
.dynamic_type
.target()) == "SwSectionNode":
223 value
= " SectionNode "
225 elif str(node
.dynamic_type
.target()) == "SwTableNode":
226 value
= " TableNode "
228 elif str(node
.dynamic_type
.target()) == "SwStartNode":
229 value
= " StartNode "
231 elif str(node
.dynamic_type
.target()) == "SwEndNode":
233 self
.indent
= self
.indent
[:-1]
234 cur_indent
= self
.indent
235 elif str(node
.dynamic_type
.target()) == "SwDummySectionNode":
236 value
= "DummySctNode "
237 else: # must be currently being deleted, so has some abstract type
238 value
= "~DeletedNode "
239 # return "\n[%s%4d%s] %s %s" % (cur_indent, self.pos, \
240 # self.max_indent[len(cur_indent):], node, value)
241 return "\n[%4d] %s%s%s %s" % (self
.pos
, cur_indent
, \
242 node
, self
.max_indent
[len(cur_indent
):], value
)
245 if self
.pos
== self
.count
:
246 raise StopIteration()
249 node
= self
.block
['mvData']['_M_elems'][self
.pos
- self
.block
['nStart']]
250 value
= self
._node
_value
(node
)
251 if self
.pos
== self
.block
['nEnd']:
255 self
._check
_invariant
()
258 def _next_block(self
, advance
= True):
262 if self
.block_pos
== self
.block_count
:
265 pblock
= self
.blocks
[self
.block_pos
]
267 block
= pblock
.dereference()
268 start
= block
['nStart']
270 assert end
- start
+ 1 == block
['nElem']
272 assert start
== self
.block
['nEnd'] + 1
273 assert end
<= self
.count
278 def _check_invariant(self
):
279 assert self
.pos
<= self
.count
280 assert self
.block_pos
<= self
.block_count
281 if self
.pos
== 0 and self
.pos
< self
.count
:
282 assert self
.block
!= None
286 def build_pretty_printers():
289 printer
= printing
.Printer("libreoffice/sw")
290 printer
.add('BigPtrArray', BigPtrArrayPrinter
)
291 printer
.add('SwPosition', SwPositionPrinter
)
292 printer
.add('SwNodeIndex', SwNodeIndexPrinter
)
293 printer
.add('SwIndex', SwIndexPrinter
)
294 printer
.add('SwPaM', SwPaMPrinter
)
295 printer
.add('SwUnoCursor', SwUnoCursorPrinter
)
296 printer
.add('SwRect', SwRectPrinter
)
297 printer
.add('sw::mark::Bookmark', MarkBasePrinter
)
298 printer
.add('sw::mark::MarkBase', MarkBasePrinter
)
299 printer
.add('sw::mark::UnoMark', MarkBasePrinter
)
300 printer
.add('sw::mark::IMark', MarkBasePrinter
)
301 printer
.add('SwXTextRange::Impl', SwXTextRangeImplPrinter
)
302 printer
.add('sw::UnoImplPtr', SwUnoImplPtrPrinter
)
303 printer
.add('SwXTextRange', SwXTextRangePrinter
)
304 printer
.add('SwXTextCursor::Impl', SwXTextCursorImplPrinter
)
305 printer
.add('SwXTextCursor', SwXTextCursorPrinter
)
307 def register_pretty_printers(obj
):
308 printing
.register_pretty_printer(printer
, obj
)
310 build_pretty_printers()
312 # vim:set shiftwidth=4 softtabstop=4 expandtab: