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