jpayne@68
|
1 """
|
jpayne@68
|
2 An auto-completion window for IDLE, used by the autocomplete extension
|
jpayne@68
|
3 """
|
jpayne@68
|
4 import platform
|
jpayne@68
|
5
|
jpayne@68
|
6 from tkinter import *
|
jpayne@68
|
7 from tkinter.ttk import Frame, Scrollbar
|
jpayne@68
|
8
|
jpayne@68
|
9 from idlelib.autocomplete import FILES, ATTRS
|
jpayne@68
|
10 from idlelib.multicall import MC_SHIFT
|
jpayne@68
|
11
|
jpayne@68
|
12 HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>"
|
jpayne@68
|
13 HIDE_FOCUS_OUT_SEQUENCE = "<FocusOut>"
|
jpayne@68
|
14 HIDE_SEQUENCES = (HIDE_FOCUS_OUT_SEQUENCE, "<ButtonPress>")
|
jpayne@68
|
15 KEYPRESS_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keypress>>"
|
jpayne@68
|
16 # We need to bind event beyond <Key> so that the function will be called
|
jpayne@68
|
17 # before the default specific IDLE function
|
jpayne@68
|
18 KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>", "<Key-Tab>",
|
jpayne@68
|
19 "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>",
|
jpayne@68
|
20 "<Key-Prior>", "<Key-Next>", "<Key-Escape>")
|
jpayne@68
|
21 KEYRELEASE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keyrelease>>"
|
jpayne@68
|
22 KEYRELEASE_SEQUENCE = "<KeyRelease>"
|
jpayne@68
|
23 LISTUPDATE_SEQUENCE = "<B1-ButtonRelease>"
|
jpayne@68
|
24 WINCONFIG_SEQUENCE = "<Configure>"
|
jpayne@68
|
25 DOUBLECLICK_SEQUENCE = "<B1-Double-ButtonRelease>"
|
jpayne@68
|
26
|
jpayne@68
|
27 class AutoCompleteWindow:
|
jpayne@68
|
28
|
jpayne@68
|
29 def __init__(self, widget):
|
jpayne@68
|
30 # The widget (Text) on which we place the AutoCompleteWindow
|
jpayne@68
|
31 self.widget = widget
|
jpayne@68
|
32 # The widgets we create
|
jpayne@68
|
33 self.autocompletewindow = self.listbox = self.scrollbar = None
|
jpayne@68
|
34 # The default foreground and background of a selection. Saved because
|
jpayne@68
|
35 # they are changed to the regular colors of list items when the
|
jpayne@68
|
36 # completion start is not a prefix of the selected completion
|
jpayne@68
|
37 self.origselforeground = self.origselbackground = None
|
jpayne@68
|
38 # The list of completions
|
jpayne@68
|
39 self.completions = None
|
jpayne@68
|
40 # A list with more completions, or None
|
jpayne@68
|
41 self.morecompletions = None
|
jpayne@68
|
42 # The completion mode, either autocomplete.ATTRS or .FILES.
|
jpayne@68
|
43 self.mode = None
|
jpayne@68
|
44 # The current completion start, on the text box (a string)
|
jpayne@68
|
45 self.start = None
|
jpayne@68
|
46 # The index of the start of the completion
|
jpayne@68
|
47 self.startindex = None
|
jpayne@68
|
48 # The last typed start, used so that when the selection changes,
|
jpayne@68
|
49 # the new start will be as close as possible to the last typed one.
|
jpayne@68
|
50 self.lasttypedstart = None
|
jpayne@68
|
51 # Do we have an indication that the user wants the completion window
|
jpayne@68
|
52 # (for example, he clicked the list)
|
jpayne@68
|
53 self.userwantswindow = None
|
jpayne@68
|
54 # event ids
|
jpayne@68
|
55 self.hideid = self.keypressid = self.listupdateid = \
|
jpayne@68
|
56 self.winconfigid = self.keyreleaseid = self.doubleclickid = None
|
jpayne@68
|
57 # Flag set if last keypress was a tab
|
jpayne@68
|
58 self.lastkey_was_tab = False
|
jpayne@68
|
59 # Flag set to avoid recursive <Configure> callback invocations.
|
jpayne@68
|
60 self.is_configuring = False
|
jpayne@68
|
61
|
jpayne@68
|
62 def _change_start(self, newstart):
|
jpayne@68
|
63 min_len = min(len(self.start), len(newstart))
|
jpayne@68
|
64 i = 0
|
jpayne@68
|
65 while i < min_len and self.start[i] == newstart[i]:
|
jpayne@68
|
66 i += 1
|
jpayne@68
|
67 if i < len(self.start):
|
jpayne@68
|
68 self.widget.delete("%s+%dc" % (self.startindex, i),
|
jpayne@68
|
69 "%s+%dc" % (self.startindex, len(self.start)))
|
jpayne@68
|
70 if i < len(newstart):
|
jpayne@68
|
71 self.widget.insert("%s+%dc" % (self.startindex, i),
|
jpayne@68
|
72 newstart[i:])
|
jpayne@68
|
73 self.start = newstart
|
jpayne@68
|
74
|
jpayne@68
|
75 def _binary_search(self, s):
|
jpayne@68
|
76 """Find the first index in self.completions where completions[i] is
|
jpayne@68
|
77 greater or equal to s, or the last index if there is no such.
|
jpayne@68
|
78 """
|
jpayne@68
|
79 i = 0; j = len(self.completions)
|
jpayne@68
|
80 while j > i:
|
jpayne@68
|
81 m = (i + j) // 2
|
jpayne@68
|
82 if self.completions[m] >= s:
|
jpayne@68
|
83 j = m
|
jpayne@68
|
84 else:
|
jpayne@68
|
85 i = m + 1
|
jpayne@68
|
86 return min(i, len(self.completions)-1)
|
jpayne@68
|
87
|
jpayne@68
|
88 def _complete_string(self, s):
|
jpayne@68
|
89 """Assuming that s is the prefix of a string in self.completions,
|
jpayne@68
|
90 return the longest string which is a prefix of all the strings which
|
jpayne@68
|
91 s is a prefix of them. If s is not a prefix of a string, return s.
|
jpayne@68
|
92 """
|
jpayne@68
|
93 first = self._binary_search(s)
|
jpayne@68
|
94 if self.completions[first][:len(s)] != s:
|
jpayne@68
|
95 # There is not even one completion which s is a prefix of.
|
jpayne@68
|
96 return s
|
jpayne@68
|
97 # Find the end of the range of completions where s is a prefix of.
|
jpayne@68
|
98 i = first + 1
|
jpayne@68
|
99 j = len(self.completions)
|
jpayne@68
|
100 while j > i:
|
jpayne@68
|
101 m = (i + j) // 2
|
jpayne@68
|
102 if self.completions[m][:len(s)] != s:
|
jpayne@68
|
103 j = m
|
jpayne@68
|
104 else:
|
jpayne@68
|
105 i = m + 1
|
jpayne@68
|
106 last = i-1
|
jpayne@68
|
107
|
jpayne@68
|
108 if first == last: # only one possible completion
|
jpayne@68
|
109 return self.completions[first]
|
jpayne@68
|
110
|
jpayne@68
|
111 # We should return the maximum prefix of first and last
|
jpayne@68
|
112 first_comp = self.completions[first]
|
jpayne@68
|
113 last_comp = self.completions[last]
|
jpayne@68
|
114 min_len = min(len(first_comp), len(last_comp))
|
jpayne@68
|
115 i = len(s)
|
jpayne@68
|
116 while i < min_len and first_comp[i] == last_comp[i]:
|
jpayne@68
|
117 i += 1
|
jpayne@68
|
118 return first_comp[:i]
|
jpayne@68
|
119
|
jpayne@68
|
120 def _selection_changed(self):
|
jpayne@68
|
121 """Call when the selection of the Listbox has changed.
|
jpayne@68
|
122
|
jpayne@68
|
123 Updates the Listbox display and calls _change_start.
|
jpayne@68
|
124 """
|
jpayne@68
|
125 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
126
|
jpayne@68
|
127 self.listbox.see(cursel)
|
jpayne@68
|
128
|
jpayne@68
|
129 lts = self.lasttypedstart
|
jpayne@68
|
130 selstart = self.completions[cursel]
|
jpayne@68
|
131 if self._binary_search(lts) == cursel:
|
jpayne@68
|
132 newstart = lts
|
jpayne@68
|
133 else:
|
jpayne@68
|
134 min_len = min(len(lts), len(selstart))
|
jpayne@68
|
135 i = 0
|
jpayne@68
|
136 while i < min_len and lts[i] == selstart[i]:
|
jpayne@68
|
137 i += 1
|
jpayne@68
|
138 newstart = selstart[:i]
|
jpayne@68
|
139 self._change_start(newstart)
|
jpayne@68
|
140
|
jpayne@68
|
141 if self.completions[cursel][:len(self.start)] == self.start:
|
jpayne@68
|
142 # start is a prefix of the selected completion
|
jpayne@68
|
143 self.listbox.configure(selectbackground=self.origselbackground,
|
jpayne@68
|
144 selectforeground=self.origselforeground)
|
jpayne@68
|
145 else:
|
jpayne@68
|
146 self.listbox.configure(selectbackground=self.listbox.cget("bg"),
|
jpayne@68
|
147 selectforeground=self.listbox.cget("fg"))
|
jpayne@68
|
148 # If there are more completions, show them, and call me again.
|
jpayne@68
|
149 if self.morecompletions:
|
jpayne@68
|
150 self.completions = self.morecompletions
|
jpayne@68
|
151 self.morecompletions = None
|
jpayne@68
|
152 self.listbox.delete(0, END)
|
jpayne@68
|
153 for item in self.completions:
|
jpayne@68
|
154 self.listbox.insert(END, item)
|
jpayne@68
|
155 self.listbox.select_set(self._binary_search(self.start))
|
jpayne@68
|
156 self._selection_changed()
|
jpayne@68
|
157
|
jpayne@68
|
158 def show_window(self, comp_lists, index, complete, mode, userWantsWin):
|
jpayne@68
|
159 """Show the autocomplete list, bind events.
|
jpayne@68
|
160
|
jpayne@68
|
161 If complete is True, complete the text, and if there is exactly
|
jpayne@68
|
162 one matching completion, don't open a list.
|
jpayne@68
|
163 """
|
jpayne@68
|
164 # Handle the start we already have
|
jpayne@68
|
165 self.completions, self.morecompletions = comp_lists
|
jpayne@68
|
166 self.mode = mode
|
jpayne@68
|
167 self.startindex = self.widget.index(index)
|
jpayne@68
|
168 self.start = self.widget.get(self.startindex, "insert")
|
jpayne@68
|
169 if complete:
|
jpayne@68
|
170 completed = self._complete_string(self.start)
|
jpayne@68
|
171 start = self.start
|
jpayne@68
|
172 self._change_start(completed)
|
jpayne@68
|
173 i = self._binary_search(completed)
|
jpayne@68
|
174 if self.completions[i] == completed and \
|
jpayne@68
|
175 (i == len(self.completions)-1 or
|
jpayne@68
|
176 self.completions[i+1][:len(completed)] != completed):
|
jpayne@68
|
177 # There is exactly one matching completion
|
jpayne@68
|
178 return completed == start
|
jpayne@68
|
179 self.userwantswindow = userWantsWin
|
jpayne@68
|
180 self.lasttypedstart = self.start
|
jpayne@68
|
181
|
jpayne@68
|
182 # Put widgets in place
|
jpayne@68
|
183 self.autocompletewindow = acw = Toplevel(self.widget)
|
jpayne@68
|
184 # Put it in a position so that it is not seen.
|
jpayne@68
|
185 acw.wm_geometry("+10000+10000")
|
jpayne@68
|
186 # Make it float
|
jpayne@68
|
187 acw.wm_overrideredirect(1)
|
jpayne@68
|
188 try:
|
jpayne@68
|
189 # This command is only needed and available on Tk >= 8.4.0 for OSX
|
jpayne@68
|
190 # Without it, call tips intrude on the typing process by grabbing
|
jpayne@68
|
191 # the focus.
|
jpayne@68
|
192 acw.tk.call("::tk::unsupported::MacWindowStyle", "style", acw._w,
|
jpayne@68
|
193 "help", "noActivates")
|
jpayne@68
|
194 except TclError:
|
jpayne@68
|
195 pass
|
jpayne@68
|
196 self.scrollbar = scrollbar = Scrollbar(acw, orient=VERTICAL)
|
jpayne@68
|
197 self.listbox = listbox = Listbox(acw, yscrollcommand=scrollbar.set,
|
jpayne@68
|
198 exportselection=False)
|
jpayne@68
|
199 for item in self.completions:
|
jpayne@68
|
200 listbox.insert(END, item)
|
jpayne@68
|
201 self.origselforeground = listbox.cget("selectforeground")
|
jpayne@68
|
202 self.origselbackground = listbox.cget("selectbackground")
|
jpayne@68
|
203 scrollbar.config(command=listbox.yview)
|
jpayne@68
|
204 scrollbar.pack(side=RIGHT, fill=Y)
|
jpayne@68
|
205 listbox.pack(side=LEFT, fill=BOTH, expand=True)
|
jpayne@68
|
206 acw.lift() # work around bug in Tk 8.5.18+ (issue #24570)
|
jpayne@68
|
207
|
jpayne@68
|
208 # Initialize the listbox selection
|
jpayne@68
|
209 self.listbox.select_set(self._binary_search(self.start))
|
jpayne@68
|
210 self._selection_changed()
|
jpayne@68
|
211
|
jpayne@68
|
212 # bind events
|
jpayne@68
|
213 self.hideaid = acw.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
|
jpayne@68
|
214 self.hidewid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
|
jpayne@68
|
215 acw.event_add(HIDE_VIRTUAL_EVENT_NAME, HIDE_FOCUS_OUT_SEQUENCE)
|
jpayne@68
|
216 for seq in HIDE_SEQUENCES:
|
jpayne@68
|
217 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
|
jpayne@68
|
218
|
jpayne@68
|
219 self.keypressid = self.widget.bind(KEYPRESS_VIRTUAL_EVENT_NAME,
|
jpayne@68
|
220 self.keypress_event)
|
jpayne@68
|
221 for seq in KEYPRESS_SEQUENCES:
|
jpayne@68
|
222 self.widget.event_add(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
|
jpayne@68
|
223 self.keyreleaseid = self.widget.bind(KEYRELEASE_VIRTUAL_EVENT_NAME,
|
jpayne@68
|
224 self.keyrelease_event)
|
jpayne@68
|
225 self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE)
|
jpayne@68
|
226 self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE,
|
jpayne@68
|
227 self.listselect_event)
|
jpayne@68
|
228 self.is_configuring = False
|
jpayne@68
|
229 self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
|
jpayne@68
|
230 self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
|
jpayne@68
|
231 self.doubleclick_event)
|
jpayne@68
|
232 return None
|
jpayne@68
|
233
|
jpayne@68
|
234 def winconfig_event(self, event):
|
jpayne@68
|
235 if self.is_configuring:
|
jpayne@68
|
236 # Avoid running on recursive <Configure> callback invocations.
|
jpayne@68
|
237 return
|
jpayne@68
|
238
|
jpayne@68
|
239 self.is_configuring = True
|
jpayne@68
|
240 if not self.is_active():
|
jpayne@68
|
241 return
|
jpayne@68
|
242 # Position the completion list window
|
jpayne@68
|
243 text = self.widget
|
jpayne@68
|
244 text.see(self.startindex)
|
jpayne@68
|
245 x, y, cx, cy = text.bbox(self.startindex)
|
jpayne@68
|
246 acw = self.autocompletewindow
|
jpayne@68
|
247 acw.update()
|
jpayne@68
|
248 acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
|
jpayne@68
|
249 text_width, text_height = text.winfo_width(), text.winfo_height()
|
jpayne@68
|
250 new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
|
jpayne@68
|
251 new_y = text.winfo_rooty() + y
|
jpayne@68
|
252 if (text_height - (y + cy) >= acw_height # enough height below
|
jpayne@68
|
253 or y < acw_height): # not enough height above
|
jpayne@68
|
254 # place acw below current line
|
jpayne@68
|
255 new_y += cy
|
jpayne@68
|
256 else:
|
jpayne@68
|
257 # place acw above current line
|
jpayne@68
|
258 new_y -= acw_height
|
jpayne@68
|
259 acw.wm_geometry("+%d+%d" % (new_x, new_y))
|
jpayne@68
|
260 acw.update_idletasks()
|
jpayne@68
|
261
|
jpayne@68
|
262 if platform.system().startswith('Windows'):
|
jpayne@68
|
263 # See issue 15786. When on Windows platform, Tk will misbehave
|
jpayne@68
|
264 # to call winconfig_event multiple times, we need to prevent this,
|
jpayne@68
|
265 # otherwise mouse button double click will not be able to used.
|
jpayne@68
|
266 acw.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
|
jpayne@68
|
267 self.winconfigid = None
|
jpayne@68
|
268
|
jpayne@68
|
269 self.is_configuring = False
|
jpayne@68
|
270
|
jpayne@68
|
271 def _hide_event_check(self):
|
jpayne@68
|
272 if not self.autocompletewindow:
|
jpayne@68
|
273 return
|
jpayne@68
|
274
|
jpayne@68
|
275 try:
|
jpayne@68
|
276 if not self.autocompletewindow.focus_get():
|
jpayne@68
|
277 self.hide_window()
|
jpayne@68
|
278 except KeyError:
|
jpayne@68
|
279 # See issue 734176, when user click on menu, acw.focus_get()
|
jpayne@68
|
280 # will get KeyError.
|
jpayne@68
|
281 self.hide_window()
|
jpayne@68
|
282
|
jpayne@68
|
283 def hide_event(self, event):
|
jpayne@68
|
284 # Hide autocomplete list if it exists and does not have focus or
|
jpayne@68
|
285 # mouse click on widget / text area.
|
jpayne@68
|
286 if self.is_active():
|
jpayne@68
|
287 if event.type == EventType.FocusOut:
|
jpayne@68
|
288 # On Windows platform, it will need to delay the check for
|
jpayne@68
|
289 # acw.focus_get() when click on acw, otherwise it will return
|
jpayne@68
|
290 # None and close the window
|
jpayne@68
|
291 self.widget.after(1, self._hide_event_check)
|
jpayne@68
|
292 elif event.type == EventType.ButtonPress:
|
jpayne@68
|
293 # ButtonPress event only bind to self.widget
|
jpayne@68
|
294 self.hide_window()
|
jpayne@68
|
295
|
jpayne@68
|
296 def listselect_event(self, event):
|
jpayne@68
|
297 if self.is_active():
|
jpayne@68
|
298 self.userwantswindow = True
|
jpayne@68
|
299 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
300 self._change_start(self.completions[cursel])
|
jpayne@68
|
301
|
jpayne@68
|
302 def doubleclick_event(self, event):
|
jpayne@68
|
303 # Put the selected completion in the text, and close the list
|
jpayne@68
|
304 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
305 self._change_start(self.completions[cursel])
|
jpayne@68
|
306 self.hide_window()
|
jpayne@68
|
307
|
jpayne@68
|
308 def keypress_event(self, event):
|
jpayne@68
|
309 if not self.is_active():
|
jpayne@68
|
310 return None
|
jpayne@68
|
311 keysym = event.keysym
|
jpayne@68
|
312 if hasattr(event, "mc_state"):
|
jpayne@68
|
313 state = event.mc_state
|
jpayne@68
|
314 else:
|
jpayne@68
|
315 state = 0
|
jpayne@68
|
316 if keysym != "Tab":
|
jpayne@68
|
317 self.lastkey_was_tab = False
|
jpayne@68
|
318 if (len(keysym) == 1 or keysym in ("underscore", "BackSpace")
|
jpayne@68
|
319 or (self.mode == FILES and keysym in
|
jpayne@68
|
320 ("period", "minus"))) \
|
jpayne@68
|
321 and not (state & ~MC_SHIFT):
|
jpayne@68
|
322 # Normal editing of text
|
jpayne@68
|
323 if len(keysym) == 1:
|
jpayne@68
|
324 self._change_start(self.start + keysym)
|
jpayne@68
|
325 elif keysym == "underscore":
|
jpayne@68
|
326 self._change_start(self.start + '_')
|
jpayne@68
|
327 elif keysym == "period":
|
jpayne@68
|
328 self._change_start(self.start + '.')
|
jpayne@68
|
329 elif keysym == "minus":
|
jpayne@68
|
330 self._change_start(self.start + '-')
|
jpayne@68
|
331 else:
|
jpayne@68
|
332 # keysym == "BackSpace"
|
jpayne@68
|
333 if len(self.start) == 0:
|
jpayne@68
|
334 self.hide_window()
|
jpayne@68
|
335 return None
|
jpayne@68
|
336 self._change_start(self.start[:-1])
|
jpayne@68
|
337 self.lasttypedstart = self.start
|
jpayne@68
|
338 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
|
jpayne@68
|
339 self.listbox.select_set(self._binary_search(self.start))
|
jpayne@68
|
340 self._selection_changed()
|
jpayne@68
|
341 return "break"
|
jpayne@68
|
342
|
jpayne@68
|
343 elif keysym == "Return":
|
jpayne@68
|
344 self.complete()
|
jpayne@68
|
345 self.hide_window()
|
jpayne@68
|
346 return 'break'
|
jpayne@68
|
347
|
jpayne@68
|
348 elif (self.mode == ATTRS and keysym in
|
jpayne@68
|
349 ("period", "space", "parenleft", "parenright", "bracketleft",
|
jpayne@68
|
350 "bracketright")) or \
|
jpayne@68
|
351 (self.mode == FILES and keysym in
|
jpayne@68
|
352 ("slash", "backslash", "quotedbl", "apostrophe")) \
|
jpayne@68
|
353 and not (state & ~MC_SHIFT):
|
jpayne@68
|
354 # If start is a prefix of the selection, but is not '' when
|
jpayne@68
|
355 # completing file names, put the whole
|
jpayne@68
|
356 # selected completion. Anyway, close the list.
|
jpayne@68
|
357 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
358 if self.completions[cursel][:len(self.start)] == self.start \
|
jpayne@68
|
359 and (self.mode == ATTRS or self.start):
|
jpayne@68
|
360 self._change_start(self.completions[cursel])
|
jpayne@68
|
361 self.hide_window()
|
jpayne@68
|
362 return None
|
jpayne@68
|
363
|
jpayne@68
|
364 elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \
|
jpayne@68
|
365 not state:
|
jpayne@68
|
366 # Move the selection in the listbox
|
jpayne@68
|
367 self.userwantswindow = True
|
jpayne@68
|
368 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
369 if keysym == "Home":
|
jpayne@68
|
370 newsel = 0
|
jpayne@68
|
371 elif keysym == "End":
|
jpayne@68
|
372 newsel = len(self.completions)-1
|
jpayne@68
|
373 elif keysym in ("Prior", "Next"):
|
jpayne@68
|
374 jump = self.listbox.nearest(self.listbox.winfo_height()) - \
|
jpayne@68
|
375 self.listbox.nearest(0)
|
jpayne@68
|
376 if keysym == "Prior":
|
jpayne@68
|
377 newsel = max(0, cursel-jump)
|
jpayne@68
|
378 else:
|
jpayne@68
|
379 assert keysym == "Next"
|
jpayne@68
|
380 newsel = min(len(self.completions)-1, cursel+jump)
|
jpayne@68
|
381 elif keysym == "Up":
|
jpayne@68
|
382 newsel = max(0, cursel-1)
|
jpayne@68
|
383 else:
|
jpayne@68
|
384 assert keysym == "Down"
|
jpayne@68
|
385 newsel = min(len(self.completions)-1, cursel+1)
|
jpayne@68
|
386 self.listbox.select_clear(cursel)
|
jpayne@68
|
387 self.listbox.select_set(newsel)
|
jpayne@68
|
388 self._selection_changed()
|
jpayne@68
|
389 self._change_start(self.completions[newsel])
|
jpayne@68
|
390 return "break"
|
jpayne@68
|
391
|
jpayne@68
|
392 elif (keysym == "Tab" and not state):
|
jpayne@68
|
393 if self.lastkey_was_tab:
|
jpayne@68
|
394 # two tabs in a row; insert current selection and close acw
|
jpayne@68
|
395 cursel = int(self.listbox.curselection()[0])
|
jpayne@68
|
396 self._change_start(self.completions[cursel])
|
jpayne@68
|
397 self.hide_window()
|
jpayne@68
|
398 return "break"
|
jpayne@68
|
399 else:
|
jpayne@68
|
400 # first tab; let AutoComplete handle the completion
|
jpayne@68
|
401 self.userwantswindow = True
|
jpayne@68
|
402 self.lastkey_was_tab = True
|
jpayne@68
|
403 return None
|
jpayne@68
|
404
|
jpayne@68
|
405 elif any(s in keysym for s in ("Shift", "Control", "Alt",
|
jpayne@68
|
406 "Meta", "Command", "Option")):
|
jpayne@68
|
407 # A modifier key, so ignore
|
jpayne@68
|
408 return None
|
jpayne@68
|
409
|
jpayne@68
|
410 elif event.char and event.char >= ' ':
|
jpayne@68
|
411 # Regular character with a non-length-1 keycode
|
jpayne@68
|
412 self._change_start(self.start + event.char)
|
jpayne@68
|
413 self.lasttypedstart = self.start
|
jpayne@68
|
414 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
|
jpayne@68
|
415 self.listbox.select_set(self._binary_search(self.start))
|
jpayne@68
|
416 self._selection_changed()
|
jpayne@68
|
417 return "break"
|
jpayne@68
|
418
|
jpayne@68
|
419 else:
|
jpayne@68
|
420 # Unknown event, close the window and let it through.
|
jpayne@68
|
421 self.hide_window()
|
jpayne@68
|
422 return None
|
jpayne@68
|
423
|
jpayne@68
|
424 def keyrelease_event(self, event):
|
jpayne@68
|
425 if not self.is_active():
|
jpayne@68
|
426 return
|
jpayne@68
|
427 if self.widget.index("insert") != \
|
jpayne@68
|
428 self.widget.index("%s+%dc" % (self.startindex, len(self.start))):
|
jpayne@68
|
429 # If we didn't catch an event which moved the insert, close window
|
jpayne@68
|
430 self.hide_window()
|
jpayne@68
|
431
|
jpayne@68
|
432 def is_active(self):
|
jpayne@68
|
433 return self.autocompletewindow is not None
|
jpayne@68
|
434
|
jpayne@68
|
435 def complete(self):
|
jpayne@68
|
436 self._change_start(self._complete_string(self.start))
|
jpayne@68
|
437 # The selection doesn't change.
|
jpayne@68
|
438
|
jpayne@68
|
439 def hide_window(self):
|
jpayne@68
|
440 if not self.is_active():
|
jpayne@68
|
441 return
|
jpayne@68
|
442
|
jpayne@68
|
443 # unbind events
|
jpayne@68
|
444 self.autocompletewindow.event_delete(HIDE_VIRTUAL_EVENT_NAME,
|
jpayne@68
|
445 HIDE_FOCUS_OUT_SEQUENCE)
|
jpayne@68
|
446 for seq in HIDE_SEQUENCES:
|
jpayne@68
|
447 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
|
jpayne@68
|
448
|
jpayne@68
|
449 self.autocompletewindow.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideaid)
|
jpayne@68
|
450 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hidewid)
|
jpayne@68
|
451 self.hideaid = None
|
jpayne@68
|
452 self.hidewid = None
|
jpayne@68
|
453 for seq in KEYPRESS_SEQUENCES:
|
jpayne@68
|
454 self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
|
jpayne@68
|
455 self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid)
|
jpayne@68
|
456 self.keypressid = None
|
jpayne@68
|
457 self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME,
|
jpayne@68
|
458 KEYRELEASE_SEQUENCE)
|
jpayne@68
|
459 self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid)
|
jpayne@68
|
460 self.keyreleaseid = None
|
jpayne@68
|
461 self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid)
|
jpayne@68
|
462 self.listupdateid = None
|
jpayne@68
|
463 if self.winconfigid:
|
jpayne@68
|
464 self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
|
jpayne@68
|
465 self.winconfigid = None
|
jpayne@68
|
466
|
jpayne@68
|
467 # Re-focusOn frame.text (See issue #15786)
|
jpayne@68
|
468 self.widget.focus_set()
|
jpayne@68
|
469
|
jpayne@68
|
470 # destroy widgets
|
jpayne@68
|
471 self.scrollbar.destroy()
|
jpayne@68
|
472 self.scrollbar = None
|
jpayne@68
|
473 self.listbox.destroy()
|
jpayne@68
|
474 self.listbox = None
|
jpayne@68
|
475 self.autocompletewindow.destroy()
|
jpayne@68
|
476 self.autocompletewindow = None
|
jpayne@68
|
477
|
jpayne@68
|
478
|
jpayne@68
|
479 if __name__ == '__main__':
|
jpayne@68
|
480 from unittest import main
|
jpayne@68
|
481 main('idlelib.idle_test.test_autocomplete_w', verbosity=2, exit=False)
|
jpayne@68
|
482
|
jpayne@68
|
483 # TODO: autocomplete/w htest here
|