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