wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 1 | """Complete either attribute names or file names. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 2 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 3 | Either on demand or after a user-selected delay after a key character, |
| 4 | pop up a list of candidates. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 5 | """ |
| 6 | import os |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 7 | import string |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 8 | import sys |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 9 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 10 | # These constants represent the two different types of completions. |
| 11 | # They must be defined here so autocomple_w can import them. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 12 | COMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1) |
| 13 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 14 | from idlelib import autocomplete_w |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 15 | from idlelib.config import idleConf |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 16 | from idlelib.hyperparser import HyperParser |
Kurt B. Kaiser | e1b4a16 | 2007-08-10 02:45:06 +0000 | [diff] [blame] | 17 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 18 | # This string includes all chars that may be in an identifier. |
| 19 | # TODO Update this here and elsewhere. |
| 20 | ID_CHARS = string.ascii_letters + string.digits + "_" |
| 21 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 22 | SEPS = os.sep |
| 23 | if os.altsep: # e.g. '/' on Windows... |
| 24 | SEPS += os.altsep |
| 25 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 26 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 27 | class AutoComplete: |
| 28 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 29 | def __init__(self, editwin=None): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 30 | self.editwin = editwin |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 31 | if editwin is not None: # not in subprocess or test |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 32 | self.text = editwin.text |
| 33 | self.autocompletewindow = None |
| 34 | # id of delayed call, and the index of the text insert when |
| 35 | # the delayed call was issued. If _delayed_completion_id is |
| 36 | # None, there is no delayed call. |
| 37 | self._delayed_completion_id = None |
| 38 | self._delayed_completion_index = None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 39 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 40 | @classmethod |
| 41 | def reload(cls): |
| 42 | cls.popupwait = idleConf.GetOption( |
| 43 | "extensions", "AutoComplete", "popupwait", type="int", default=0) |
| 44 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 45 | def _make_autocomplete_window(self): |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 46 | return autocomplete_w.AutoCompleteWindow(self.text) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 47 | |
| 48 | def _remove_autocomplete_window(self, event=None): |
| 49 | if self.autocompletewindow: |
| 50 | self.autocompletewindow.hide_window() |
| 51 | self.autocompletewindow = None |
| 52 | |
| 53 | def force_open_completions_event(self, event): |
| 54 | """Happens when the user really wants to open a completion list, even |
| 55 | if a function call is needed. |
| 56 | """ |
| 57 | self.open_completions(True, False, True) |
Serhiy Storchaka | 213ce12 | 2017-06-27 07:02:32 +0300 | [diff] [blame] | 58 | return "break" |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 59 | |
| 60 | def try_open_completions_event(self, event): |
| 61 | """Happens when it would be nice to open a completion list, but not |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 62 | really necessary, for example after a dot, so function |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 63 | calls won't be made. |
| 64 | """ |
| 65 | lastchar = self.text.get("insert-1c") |
| 66 | if lastchar == ".": |
| 67 | self._open_completions_later(False, False, False, |
| 68 | COMPLETE_ATTRIBUTES) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 69 | elif lastchar in SEPS: |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 70 | self._open_completions_later(False, False, False, |
| 71 | COMPLETE_FILES) |
| 72 | |
| 73 | def autocomplete_event(self, event): |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 74 | """Happens when the user wants to complete his word, and if necessary, |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 75 | open a completion list after that (if there is more than one |
| 76 | completion) |
| 77 | """ |
Terry Jan Reedy | c665dfd | 2016-07-24 23:01:28 -0400 | [diff] [blame] | 78 | if hasattr(event, "mc_state") and event.mc_state or\ |
| 79 | not self.text.get("insert linestart", "insert").strip(): |
| 80 | # A modifier was pressed along with the tab or |
| 81 | # there is only previous whitespace on this line, so tab. |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 82 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 83 | if self.autocompletewindow and self.autocompletewindow.is_active(): |
| 84 | self.autocompletewindow.complete() |
| 85 | return "break" |
| 86 | else: |
| 87 | opened = self.open_completions(False, True, True) |
Terry Jan Reedy | c665dfd | 2016-07-24 23:01:28 -0400 | [diff] [blame] | 88 | return "break" if opened else None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 89 | |
| 90 | def _open_completions_later(self, *args): |
| 91 | self._delayed_completion_index = self.text.index("insert") |
| 92 | if self._delayed_completion_id is not None: |
| 93 | self.text.after_cancel(self._delayed_completion_id) |
| 94 | self._delayed_completion_id = \ |
| 95 | self.text.after(self.popupwait, self._delayed_open_completions, |
| 96 | *args) |
| 97 | |
| 98 | def _delayed_open_completions(self, *args): |
| 99 | self._delayed_completion_id = None |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 100 | if self.text.index("insert") == self._delayed_completion_index: |
| 101 | self.open_completions(*args) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 102 | |
| 103 | def open_completions(self, evalfuncs, complete, userWantsWin, mode=None): |
| 104 | """Find the completions and create the AutoCompleteWindow. |
| 105 | Return True if successful (no syntax error or so found). |
| 106 | if complete is True, then if there's nothing to complete and no |
| 107 | start of completion, won't open completions and return False. |
| 108 | If mode is given, will open a completion list only in this mode. |
| 109 | """ |
| 110 | # Cancel another delayed call, if it exists. |
| 111 | if self._delayed_completion_id is not None: |
| 112 | self.text.after_cancel(self._delayed_completion_id) |
| 113 | self._delayed_completion_id = None |
| 114 | |
| 115 | hp = HyperParser(self.editwin, "insert") |
| 116 | curline = self.text.get("insert linestart", "insert") |
| 117 | i = j = len(curline) |
| 118 | if hp.is_in_string() and (not mode or mode==COMPLETE_FILES): |
Martin v. Löwis | 862d13a | 2012-06-03 11:55:32 +0200 | [diff] [blame] | 119 | # Find the beginning of the string |
| 120 | # fetch_completions will look at the file system to determine whether the |
| 121 | # string value constitutes an actual file name |
| 122 | # XXX could consider raw strings here and unescape the string value if it's |
| 123 | # not raw. |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 124 | self._remove_autocomplete_window() |
| 125 | mode = COMPLETE_FILES |
Martin v. Löwis | 862d13a | 2012-06-03 11:55:32 +0200 | [diff] [blame] | 126 | # Find last separator or string start |
| 127 | while i and curline[i-1] not in "'\"" + SEPS: |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 128 | i -= 1 |
| 129 | comp_start = curline[i:j] |
| 130 | j = i |
Martin v. Löwis | 862d13a | 2012-06-03 11:55:32 +0200 | [diff] [blame] | 131 | # Find string start |
| 132 | while i and curline[i-1] not in "'\"": |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 133 | i -= 1 |
| 134 | comp_what = curline[i:j] |
| 135 | elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES): |
| 136 | self._remove_autocomplete_window() |
| 137 | mode = COMPLETE_ATTRIBUTES |
Martin v. Löwis | 993fe3f | 2012-06-14 15:37:21 +0200 | [diff] [blame] | 138 | while i and (curline[i-1] in ID_CHARS or ord(curline[i-1]) > 127): |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 139 | i -= 1 |
| 140 | comp_start = curline[i:j] |
| 141 | if i and curline[i-1] == '.': |
| 142 | hp.set_index("insert-%dc" % (len(curline)-(i-1))) |
| 143 | comp_what = hp.get_expression() |
| 144 | if not comp_what or \ |
| 145 | (not evalfuncs and comp_what.find('(') != -1): |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 146 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 147 | else: |
| 148 | comp_what = "" |
| 149 | else: |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 150 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 151 | |
| 152 | if complete and not comp_what and not comp_start: |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 153 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 154 | comp_lists = self.fetch_completions(comp_what, mode) |
| 155 | if not comp_lists[0]: |
Terry Jan Reedy | c74fb9c | 2016-07-24 20:35:43 -0400 | [diff] [blame] | 156 | return None |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 157 | self.autocompletewindow = self._make_autocomplete_window() |
Serhiy Storchaka | dd4754e | 2013-09-11 22:46:27 +0300 | [diff] [blame] | 158 | return not self.autocompletewindow.show_window( |
| 159 | comp_lists, "insert-%dc" % len(comp_start), |
| 160 | complete, mode, userWantsWin) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 161 | |
| 162 | def fetch_completions(self, what, mode): |
| 163 | """Return a pair of lists of completions for something. The first list |
| 164 | is a sublist of the second. Both are sorted. |
| 165 | |
| 166 | If there is a Python subprocess, get the comp. list there. Otherwise, |
| 167 | either fetch_completions() is running in the subprocess itself or it |
| 168 | was called in an IDLE EditorWindow before any script had been run. |
| 169 | |
| 170 | The subprocess environment is that of the most recently run script. If |
| 171 | two unrelated modules are being edited some calltips in the current |
| 172 | module may be inoperative if the module was not the last to run. |
| 173 | """ |
| 174 | try: |
| 175 | rpcclt = self.editwin.flist.pyshell.interp.rpcclt |
| 176 | except: |
| 177 | rpcclt = None |
| 178 | if rpcclt: |
| 179 | return rpcclt.remotecall("exec", "get_the_completion_list", |
| 180 | (what, mode), {}) |
| 181 | else: |
| 182 | if mode == COMPLETE_ATTRIBUTES: |
| 183 | if what == "": |
Terry Jan Reedy | 2b75155 | 2019-03-23 03:50:15 -0400 | [diff] [blame] | 184 | namespace = {**__builtins__.__dict__, **globals()} |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 185 | bigl = eval("dir()", namespace) |
| 186 | bigl.sort() |
| 187 | if "__all__" in bigl: |
Terry Jan Reedy | a77aa69 | 2012-02-05 14:31:16 -0500 | [diff] [blame] | 188 | smalll = sorted(eval("__all__", namespace)) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 189 | else: |
Kurt B. Kaiser | f2335a9 | 2007-08-10 02:41:21 +0000 | [diff] [blame] | 190 | smalll = [s for s in bigl if s[:1] != '_'] |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 191 | else: |
| 192 | try: |
| 193 | entity = self.get_entity(what) |
| 194 | bigl = dir(entity) |
| 195 | bigl.sort() |
| 196 | if "__all__" in bigl: |
Terry Jan Reedy | a77aa69 | 2012-02-05 14:31:16 -0500 | [diff] [blame] | 197 | smalll = sorted(entity.__all__) |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 198 | else: |
Kurt B. Kaiser | f2335a9 | 2007-08-10 02:41:21 +0000 | [diff] [blame] | 199 | smalll = [s for s in bigl if s[:1] != '_'] |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 200 | except: |
| 201 | return [], [] |
| 202 | |
| 203 | elif mode == COMPLETE_FILES: |
| 204 | if what == "": |
| 205 | what = "." |
| 206 | try: |
| 207 | expandedpath = os.path.expanduser(what) |
| 208 | bigl = os.listdir(expandedpath) |
| 209 | bigl.sort() |
Kurt B. Kaiser | f2335a9 | 2007-08-10 02:41:21 +0000 | [diff] [blame] | 210 | smalll = [s for s in bigl if s[:1] != '.'] |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 211 | except OSError: |
| 212 | return [], [] |
| 213 | |
| 214 | if not smalll: |
| 215 | smalll = bigl |
| 216 | return smalll, bigl |
| 217 | |
| 218 | def get_entity(self, name): |
Terry Jan Reedy | 2b75155 | 2019-03-23 03:50:15 -0400 | [diff] [blame] | 219 | "Lookup name in a namespace spanning sys.modules and globals()." |
| 220 | return eval(name, {**sys.modules, **globals()}) |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 221 | |
| 222 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 223 | AutoComplete.reload() |
| 224 | |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 225 | if __name__ == '__main__': |
| 226 | from unittest import main |
| 227 | main('idlelib.idle_test.test_autocomplete', verbosity=2) |