/usr/lib/python3/dist-packages/IPython/qt/console/completion_html.py is in ipython3-qtconsole 2.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | """A navigable completer for the qtconsole"""
# coding : utf-8
#-----------------------------------------------------------------------------
# Copyright (c) 2012, IPython Development Team.$
#
# Distributed under the terms of the Modified BSD License.$
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
# System library imports
import IPython.utils.text as text
from IPython.external.qt import QtCore, QtGui
#--------------------------------------------------------------------------
# Return an HTML table with selected item in a special class
#--------------------------------------------------------------------------
def html_tableify(item_matrix, select=None, header=None , footer=None) :
""" returnr a string for an html table"""
if not item_matrix :
return ''
html_cols = []
tds = lambda text : u'<td>'+text+u' </td>'
trs = lambda text : u'<tr>'+text+u'</tr>'
tds_items = [list(map(tds, row)) for row in item_matrix]
if select :
row, col = select
tds_items[row][col] = u'<td class="inverted">'\
+item_matrix[row][col]\
+u' </td>'
#select the right item
html_cols = map(trs, (u''.join(row) for row in tds_items))
head = ''
foot = ''
if header :
head = (u'<tr>'\
+''.join((u'<td>'+header+u'</td>')*len(item_matrix[0]))\
+'</tr>')
if footer :
foot = (u'<tr>'\
+''.join((u'<td>'+footer+u'</td>')*len(item_matrix[0]))\
+'</tr>')
html = (u'<table class="completion" style="white-space:pre">'+head+(u''.join(html_cols))+foot+u'</table>')
return html
class SlidingInterval(object):
"""a bound interval that follows a cursor
internally used to scoll the completion view when the cursor
try to go beyond the edges, and show '...' when rows are hidden
"""
_min = 0
_max = 1
_current = 0
def __init__(self, maximum=1, width=6, minimum=0, sticky_lenght=1):
"""Create a new bounded interval
any value return by this will be bound between maximum and
minimum. usual width will be 'width', and sticky_length
set when the return interval should expand to max and min
"""
self._min = minimum
self._max = maximum
self._start = 0
self._width = width
self._stop = self._start+self._width+1
self._sticky_lenght = sticky_lenght
@property
def current(self):
"""current cursor position"""
return self._current
@current.setter
def current(self, value):
"""set current cursor position"""
current = min(max(self._min, value), self._max)
self._current = current
if current > self._stop :
self._stop = current
self._start = current-self._width
elif current < self._start :
self._start = current
self._stop = current + self._width
if abs(self._start - self._min) <= self._sticky_lenght :
self._start = self._min
if abs(self._stop - self._max) <= self._sticky_lenght :
self._stop = self._max
@property
def start(self):
"""begiiing of interval to show"""
return self._start
@property
def stop(self):
"""end of interval to show"""
return self._stop
@property
def width(self):
return self._stop - self._start
@property
def nth(self):
return self.current - self.start
class CompletionHtml(QtGui.QWidget):
""" A widget for tab completion, navigable by arrow keys """
#--------------------------------------------------------------------------
# 'QObject' interface
#--------------------------------------------------------------------------
_items = ()
_index = (0, 0)
_consecutive_tab = 0
_size = (1, 1)
_old_cursor = None
_start_position = 0
_slice_start = 0
_slice_len = 4
def __init__(self, console_widget):
""" Create a completion widget that is attached to the specified Qt
text edit widget.
"""
assert isinstance(console_widget._control, (QtGui.QTextEdit, QtGui.QPlainTextEdit))
super(CompletionHtml, self).__init__()
self._text_edit = console_widget._control
self._console_widget = console_widget
self._text_edit.installEventFilter(self)
self._sliding_interval = None
self._justified_items = None
# Ensure that the text edit keeps focus when widget is displayed.
self.setFocusProxy(self._text_edit)
def eventFilter(self, obj, event):
""" Reimplemented to handle keyboard input and to auto-hide when the
text edit loses focus.
"""
if obj == self._text_edit:
etype = event.type()
if etype == QtCore.QEvent.KeyPress:
key = event.key()
if self._consecutive_tab == 0 and key in (QtCore.Qt.Key_Tab,):
return False
elif self._consecutive_tab == 1 and key in (QtCore.Qt.Key_Tab,):
# ok , called twice, we grab focus, and show the cursor
self._consecutive_tab = self._consecutive_tab+1
self._update_list()
return True
elif self._consecutive_tab == 2:
if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
self._complete_current()
return True
if key in (QtCore.Qt.Key_Tab,):
self.select_right()
self._update_list()
return True
elif key in ( QtCore.Qt.Key_Down,):
self.select_down()
self._update_list()
return True
elif key in (QtCore.Qt.Key_Right,):
self.select_right()
self._update_list()
return True
elif key in ( QtCore.Qt.Key_Up,):
self.select_up()
self._update_list()
return True
elif key in ( QtCore.Qt.Key_Left,):
self.select_left()
self._update_list()
return True
elif key in ( QtCore.Qt.Key_Escape,):
self.cancel_completion()
return True
else :
self.cancel_completion()
else:
self.cancel_completion()
elif etype == QtCore.QEvent.FocusOut:
self.cancel_completion()
return super(CompletionHtml, self).eventFilter(obj, event)
#--------------------------------------------------------------------------
# 'CompletionHtml' interface
#--------------------------------------------------------------------------
def cancel_completion(self):
"""Cancel the completion
should be called when the completer have to be dismissed
This reset internal variable, clearing the temporary buffer
of the console where the completion are shown.
"""
self._consecutive_tab = 0
self._slice_start = 0
self._console_widget._clear_temporary_buffer()
self._index = (0, 0)
if(self._sliding_interval):
self._sliding_interval = None
#
# ... 2 4 4 4 4 4 4 4 4 4 4 4 4
# 2 2 4 4 4 4 4 4 4 4 4 4 4 4
#
#2 2 x x x x x x x x x x x 5 5
#6 6 x x x x x x x x x x x 5 5
#6 6 x x x x x x x x x x ? 5 5
#6 6 x x x x x x x x x x ? 1 1
#
#3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 ...
#3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 ...
def _select_index(self, row, col):
"""Change the selection index, and make sure it stays in the right range
A little more complicated than just dooing modulo the number of row columns
to be sure to cycle through all element.
horizontaly, the element are maped like this :
to r <-- a b c d e f --> to g
to f <-- g h i j k l --> to m
to l <-- m n o p q r --> to a
and vertically
a d g j m p
b e h k n q
c f i l o r
"""
nr, nc = self._size
nr = nr-1
nc = nc-1
# case 1
if (row > nr and col >= nc) or (row >= nr and col > nc):
self._select_index(0, 0)
# case 2
elif (row <= 0 and col < 0) or (row < 0 and col <= 0):
self._select_index(nr, nc)
# case 3
elif row > nr :
self._select_index(0, col+1)
# case 4
elif row < 0 :
self._select_index(nr, col-1)
# case 5
elif col > nc :
self._select_index(row+1, 0)
# case 6
elif col < 0 :
self._select_index(row-1, nc)
elif 0 <= row and row <= nr and 0 <= col and col <= nc :
self._index = (row, col)
else :
raise NotImplementedError("you'r trying to go where no completion\
have gone before : %d:%d (%d:%d)"%(row, col, nr, nc) )
@property
def _slice_end(self):
end = self._slice_start+self._slice_len
if end > len(self._items) :
return None
return end
def select_up(self):
"""move cursor up"""
r, c = self._index
self._select_index(r-1, c)
def select_down(self):
"""move cursor down"""
r, c = self._index
self._select_index(r+1, c)
def select_left(self):
"""move cursor left"""
r, c = self._index
self._select_index(r, c-1)
def select_right(self):
"""move cursor right"""
r, c = self._index
self._select_index(r, c+1)
def show_items(self, cursor, items):
""" Shows the completion widget with 'items' at the position specified
by 'cursor'.
"""
if not items :
return
self._start_position = cursor.position()
self._consecutive_tab = 1
items_m, ci = text.compute_item_matrix(items, empty=' ')
self._sliding_interval = SlidingInterval(len(items_m)-1)
self._items = items_m
self._size = (ci['rows_numbers'], ci['columns_numbers'])
self._old_cursor = cursor
self._index = (0, 0)
sjoin = lambda x : [ y.ljust(w, ' ') for y, w in zip(x, ci['columns_width'])]
self._justified_items = list(map(sjoin, items_m))
self._update_list(hilight=False)
def _update_list(self, hilight=True):
""" update the list of completion and hilight the currently selected completion """
self._sliding_interval.current = self._index[0]
head = None
foot = None
if self._sliding_interval.start > 0 :
head = '...'
if self._sliding_interval.stop < self._sliding_interval._max:
foot = '...'
items_m = self._justified_items[\
self._sliding_interval.start:\
self._sliding_interval.stop+1\
]
self._console_widget._clear_temporary_buffer()
if(hilight):
sel = (self._sliding_interval.nth, self._index[1])
else :
sel = None
strng = html_tableify(items_m, select=sel, header=head, footer=foot)
self._console_widget._fill_temporary_buffer(self._old_cursor, strng, html=True)
#--------------------------------------------------------------------------
# Protected interface
#--------------------------------------------------------------------------
def _complete_current(self):
""" Perform the completion with the currently selected item.
"""
i = self._index
item = self._items[i[0]][i[1]]
item = item.strip()
if item :
self._current_text_cursor().insertText(item)
self.cancel_completion()
def _current_text_cursor(self):
""" Returns a cursor with text between the start position and the
current position selected.
"""
cursor = self._text_edit.textCursor()
if cursor.position() >= self._start_position:
cursor.setPosition(self._start_position,
QtGui.QTextCursor.KeepAnchor)
return cursor
|