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