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 |
| 245 | # Position the completion list window |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 246 | text = self.widget |
| 247 | text.see(self.startindex) |
| 248 | x, y, cx, cy = text.bbox(self.startindex) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 249 | acw = self.autocompletewindow |
Tal Einat | 71662dc | 2019-08-14 20:06:06 +0300 | [diff] [blame] | 250 | acw.update() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 251 | acw_width, acw_height = acw.winfo_width(), acw.winfo_height() |
| 252 | text_width, text_height = text.winfo_width(), text.winfo_height() |
| 253 | new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width)) |
| 254 | new_y = text.winfo_rooty() + y |
| 255 | if (text_height - (y + cy) >= acw_height # enough height below |
| 256 | or y < acw_height): # not enough height above |
| 257 | # place acw below current line |
| 258 | new_y += cy |
| 259 | else: |
| 260 | # place acw above current line |
| 261 | new_y -= acw_height |
| 262 | acw.wm_geometry("+%d+%d" % (new_x, new_y)) |
JohnnyNajera | bbc4162 | 2019-12-10 02:30:01 +0200 | [diff] [blame] | 263 | acw.update_idletasks() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 264 | |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 265 | if platform.system().startswith('Windows'): |
Terry Jan Reedy | 33c7420 | 2018-06-20 22:49:55 -0400 | [diff] [blame] | 266 | # See issue 15786. When on Windows platform, Tk will misbehave |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 267 | # to call winconfig_event multiple times, we need to prevent this, |
| 268 | # otherwise mouse button double click will not be able to used. |
| 269 | acw.unbind(WINCONFIG_SEQUENCE, self.winconfigid) |
| 270 | self.winconfigid = None |
| 271 | |
Tal Einat | 71662dc | 2019-08-14 20:06:06 +0300 | [diff] [blame] | 272 | self.is_configuring = False |
| 273 | |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 274 | def _hide_event_check(self): |
| 275 | if not self.autocompletewindow: |
| 276 | return |
| 277 | |
| 278 | try: |
| 279 | if not self.autocompletewindow.focus_get(): |
| 280 | self.hide_window() |
| 281 | except KeyError: |
| 282 | # See issue 734176, when user click on menu, acw.focus_get() |
| 283 | # will get KeyError. |
Terry Jan Reedy | c665dfd | 2016-07-24 23:01:28 -0400 | [diff] [blame] | 284 | self.hide_window() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 285 | |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 286 | def hide_event(self, event): |
| 287 | # Hide autocomplete list if it exists and does not have focus or |
| 288 | # mouse click on widget / text area. |
| 289 | if self.is_active(): |
| 290 | if event.type == EventType.FocusOut: |
Terry Jan Reedy | 33c7420 | 2018-06-20 22:49:55 -0400 | [diff] [blame] | 291 | # On Windows platform, it will need to delay the check for |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 292 | # acw.focus_get() when click on acw, otherwise it will return |
| 293 | # None and close the window |
| 294 | self.widget.after(1, self._hide_event_check) |
| 295 | elif event.type == EventType.ButtonPress: |
| 296 | # ButtonPress event only bind to self.widget |
| 297 | self.hide_window() |
| 298 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 299 | def listselect_event(self, event): |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 300 | if self.is_active(): |
| 301 | self.userwantswindow = True |
| 302 | cursel = int(self.listbox.curselection()[0]) |
| 303 | self._change_start(self.completions[cursel]) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 304 | |
| 305 | def doubleclick_event(self, event): |
| 306 | # Put the selected completion in the text, and close the list |
| 307 | cursel = int(self.listbox.curselection()[0]) |
| 308 | self._change_start(self.completions[cursel]) |
| 309 | self.hide_window() |
| 310 | |
| 311 | def keypress_event(self, event): |
| 312 | if not self.is_active(): |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 313 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 314 | keysym = event.keysym |
| 315 | if hasattr(event, "mc_state"): |
| 316 | state = event.mc_state |
| 317 | else: |
| 318 | state = 0 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 319 | if keysym != "Tab": |
| 320 | self.lastkey_was_tab = False |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 321 | if (len(keysym) == 1 or keysym in ("underscore", "BackSpace") |
Terry Jan Reedy | 1213123 | 2019-08-04 19:48:52 -0400 | [diff] [blame] | 322 | or (self.mode == FILES and keysym in |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 323 | ("period", "minus"))) \ |
| 324 | and not (state & ~MC_SHIFT): |
| 325 | # Normal editing of text |
| 326 | if len(keysym) == 1: |
| 327 | self._change_start(self.start + keysym) |
| 328 | elif keysym == "underscore": |
| 329 | self._change_start(self.start + '_') |
| 330 | elif keysym == "period": |
| 331 | self._change_start(self.start + '.') |
| 332 | elif keysym == "minus": |
| 333 | self._change_start(self.start + '-') |
| 334 | else: |
| 335 | # keysym == "BackSpace" |
| 336 | if len(self.start) == 0: |
| 337 | self.hide_window() |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 338 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 339 | self._change_start(self.start[:-1]) |
| 340 | self.lasttypedstart = self.start |
| 341 | self.listbox.select_clear(0, int(self.listbox.curselection()[0])) |
| 342 | self.listbox.select_set(self._binary_search(self.start)) |
| 343 | self._selection_changed() |
| 344 | return "break" |
| 345 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 346 | elif keysym == "Return": |
terryjreedy | 32fd874 | 2017-06-14 15:43:15 -0400 | [diff] [blame] | 347 | self.complete() |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 348 | self.hide_window() |
terryjreedy | 32fd874 | 2017-06-14 15:43:15 -0400 | [diff] [blame] | 349 | return 'break' |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 350 | |
Terry Jan Reedy | 1213123 | 2019-08-04 19:48:52 -0400 | [diff] [blame] | 351 | elif (self.mode == ATTRS and keysym in |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 352 | ("period", "space", "parenleft", "parenright", "bracketleft", |
| 353 | "bracketright")) or \ |
Terry Jan Reedy | 1213123 | 2019-08-04 19:48:52 -0400 | [diff] [blame] | 354 | (self.mode == FILES and keysym in |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 355 | ("slash", "backslash", "quotedbl", "apostrophe")) \ |
| 356 | and not (state & ~MC_SHIFT): |
| 357 | # If start is a prefix of the selection, but is not '' when |
| 358 | # completing file names, put the whole |
| 359 | # selected completion. Anyway, close the list. |
| 360 | cursel = int(self.listbox.curselection()[0]) |
| 361 | if self.completions[cursel][:len(self.start)] == self.start \ |
Terry Jan Reedy | 1213123 | 2019-08-04 19:48:52 -0400 | [diff] [blame] | 362 | and (self.mode == ATTRS or self.start): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 363 | self._change_start(self.completions[cursel]) |
| 364 | self.hide_window() |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 365 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 366 | |
| 367 | elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \ |
| 368 | not state: |
| 369 | # Move the selection in the listbox |
| 370 | self.userwantswindow = True |
| 371 | cursel = int(self.listbox.curselection()[0]) |
| 372 | if keysym == "Home": |
| 373 | newsel = 0 |
| 374 | elif keysym == "End": |
| 375 | newsel = len(self.completions)-1 |
| 376 | elif keysym in ("Prior", "Next"): |
| 377 | jump = self.listbox.nearest(self.listbox.winfo_height()) - \ |
| 378 | self.listbox.nearest(0) |
| 379 | if keysym == "Prior": |
| 380 | newsel = max(0, cursel-jump) |
| 381 | else: |
| 382 | assert keysym == "Next" |
| 383 | newsel = min(len(self.completions)-1, cursel+jump) |
| 384 | elif keysym == "Up": |
| 385 | newsel = max(0, cursel-1) |
| 386 | else: |
| 387 | assert keysym == "Down" |
| 388 | newsel = min(len(self.completions)-1, cursel+1) |
| 389 | self.listbox.select_clear(cursel) |
| 390 | self.listbox.select_set(newsel) |
| 391 | self._selection_changed() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 392 | self._change_start(self.completions[newsel]) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 393 | return "break" |
| 394 | |
| 395 | elif (keysym == "Tab" and not state): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 396 | if self.lastkey_was_tab: |
| 397 | # two tabs in a row; insert current selection and close acw |
| 398 | cursel = int(self.listbox.curselection()[0]) |
| 399 | self._change_start(self.completions[cursel]) |
| 400 | self.hide_window() |
| 401 | return "break" |
| 402 | else: |
| 403 | # first tab; let AutoComplete handle the completion |
| 404 | self.userwantswindow = True |
| 405 | self.lastkey_was_tab = True |
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 | |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 408 | elif any(s in keysym for s in ("Shift", "Control", "Alt", |
| 409 | "Meta", "Command", "Option")): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 410 | # A modifier key, so ignore |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 411 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 412 | |
Martin v. Löwis | 97aa21b | 2012-06-03 12:26:09 +0200 | [diff] [blame] | 413 | elif event.char and event.char >= ' ': |
Martin v. Löwis | 862d13a | 2012-06-03 11:55:32 +0200 | [diff] [blame] | 414 | # Regular character with a non-length-1 keycode |
| 415 | self._change_start(self.start + event.char) |
| 416 | self.lasttypedstart = self.start |
| 417 | self.listbox.select_clear(0, int(self.listbox.curselection()[0])) |
| 418 | self.listbox.select_set(self._binary_search(self.start)) |
| 419 | self._selection_changed() |
| 420 | return "break" |
| 421 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 422 | else: |
| 423 | # Unknown event, close the window and let it through. |
| 424 | self.hide_window() |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 425 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 426 | |
| 427 | def keyrelease_event(self, event): |
| 428 | if not self.is_active(): |
| 429 | return |
| 430 | if self.widget.index("insert") != \ |
| 431 | self.widget.index("%s+%dc" % (self.startindex, len(self.start))): |
| 432 | # If we didn't catch an event which moved the insert, close window |
| 433 | self.hide_window() |
| 434 | |
| 435 | def is_active(self): |
| 436 | return self.autocompletewindow is not None |
| 437 | |
| 438 | def complete(self): |
| 439 | self._change_start(self._complete_string(self.start)) |
| 440 | # The selection doesn't change. |
| 441 | |
| 442 | def hide_window(self): |
| 443 | if not self.is_active(): |
| 444 | return |
| 445 | |
| 446 | # unbind events |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 447 | self.autocompletewindow.event_delete(HIDE_VIRTUAL_EVENT_NAME, |
| 448 | HIDE_FOCUS_OUT_SEQUENCE) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 449 | for seq in HIDE_SEQUENCES: |
| 450 | self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq) |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 451 | |
| 452 | self.autocompletewindow.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideaid) |
| 453 | self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hidewid) |
| 454 | self.hideaid = None |
| 455 | self.hidewid = None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 456 | for seq in KEYPRESS_SEQUENCES: |
| 457 | self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq) |
| 458 | self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid) |
| 459 | self.keypressid = None |
| 460 | self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME, |
| 461 | KEYRELEASE_SEQUENCE) |
| 462 | self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid) |
| 463 | self.keyreleaseid = None |
| 464 | self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid) |
| 465 | self.listupdateid = None |
mlouielu | 778b484 | 2017-06-14 23:13:19 +0800 | [diff] [blame] | 466 | if self.winconfigid: |
| 467 | self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid) |
| 468 | self.winconfigid = None |
| 469 | |
| 470 | # Re-focusOn frame.text (See issue #15786) |
| 471 | self.widget.focus_set() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 472 | |
| 473 | # destroy widgets |
| 474 | self.scrollbar.destroy() |
| 475 | self.scrollbar = None |
| 476 | self.listbox.destroy() |
| 477 | self.listbox = None |
| 478 | self.autocompletewindow.destroy() |
| 479 | self.autocompletewindow = None |
Terry Jan Reedy | ee5ef30 | 2018-06-15 18:20:55 -0400 | [diff] [blame] | 480 | |
| 481 | |
| 482 | if __name__ == '__main__': |
| 483 | from unittest import main |
| 484 | main('idlelib.idle_test.test_autocomplete_w', verbosity=2, exit=False) |
| 485 | |
| 486 | # TODO: autocomplete/w htest here |