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