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