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