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