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