1 """AutoComplete.py - An IDLE extension for automatically completing names.
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.
10 from configHandler
import idleConf
12 import AutoCompleteWindow
13 from HyperParser
import HyperParser
17 # This string includes all chars that may be in a file name (without a path
19 FILENAME_CHARS
= string
.ascii_letters
+ string
.digits
+ os
.curdir
+ "._~#$:-"
20 # This string includes all chars that may be in an identifier
21 ID_CHARS
= string
.ascii_letters
+ string
.digits
+ "_"
23 # These constants represent the two different types of completions
24 COMPLETE_ATTRIBUTES
, COMPLETE_FILES
= range(1, 2+1)
27 if os
.altsep
: # e.g. '/' on Windows...
34 ("Show Completions", "<<force-open-completions>>"),
38 popupwait
= idleConf
.GetOption("extensions", "AutoComplete",
39 "popupwait", type="int", default
=0)
41 def __init__(self
, editwin
=None):
42 self
.editwin
= editwin
43 if editwin
is None: # subprocess and test
45 self
.text
= editwin
.text
46 self
.autocompletewindow
= None
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
51 self
._delayed
_completion
_id
= None
52 self
._delayed
_completion
_index
= None
54 def _make_autocomplete_window(self
):
55 return AutoCompleteWindow
.AutoCompleteWindow(self
.text
)
57 def _remove_autocomplete_window(self
, event
=None):
58 if self
.autocompletewindow
:
59 self
.autocompletewindow
.hide_window()
60 self
.autocompletewindow
= None
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.
66 self
.open_completions(True, False, True)
68 def try_open_completions_event(self
, event
):
69 """Happens when it would be nice to open a completion list, but not
70 really necessary, for example after an dot, so function
73 lastchar
= self
.text
.get("insert-1c")
75 self
._open
_completions
_later
(False, False, False,
77 elif lastchar
in SEPS
:
78 self
._open
_completions
_later
(False, False, False,
81 def autocomplete_event(self
, event
):
82 """Happens when the user wants to complete his word, and if necessary,
83 open a completion list after that (if there is more than one
86 if hasattr(event
, "mc_state") and event
.mc_state
:
87 # A modifier was pressed along with the tab, continue as usual.
89 if self
.autocompletewindow
and self
.autocompletewindow
.is_active():
90 self
.autocompletewindow
.complete()
93 opened
= self
.open_completions(False, True, True)
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
,
105 def _delayed_open_completions(self
, *args
):
106 self
._delayed
_completion
_id
= None
107 if self
.text
.index("insert") != self
._delayed
_completion
_index
:
109 self
.open_completions(*args
)
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.
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
123 hp
= HyperParser(self
.editwin
, "insert")
124 curline
= self
.text
.get("insert linestart", "insert")
126 if hp
.is_in_string() and (not mode
or mode
==COMPLETE_FILES
):
127 self
._remove
_autocomplete
_window
()
128 mode
= COMPLETE_FILES
129 while i
and curline
[i
-1] in FILENAME_CHARS
:
131 comp_start
= curline
[i
:j
]
133 while i
and curline
[i
-1] in FILENAME_CHARS
+ SEPS
:
135 comp_what
= curline
[i
:j
]
136 elif hp
.is_in_code() and (not mode
or mode
==COMPLETE_ATTRIBUTES
):
137 self
._remove
_autocomplete
_window
()
138 mode
= COMPLETE_ATTRIBUTES
139 while i
and curline
[i
-1] in ID_CHARS
:
141 comp_start
= curline
[i
:j
]
142 if i
and curline
[i
-1] == '.':
143 hp
.set_index("insert-%dc" % (len(curline
)-(i
-1)))
144 comp_what
= hp
.get_expression()
145 if not comp_what
or \
146 (not evalfuncs
and comp_what
.find('(') != -1):
153 if complete
and not comp_what
and not comp_start
:
155 comp_lists
= self
.fetch_completions(comp_what
, mode
)
156 if not comp_lists
[0]:
158 self
.autocompletewindow
= self
._make
_autocomplete
_window
()
159 self
.autocompletewindow
.show_window(comp_lists
,
160 "insert-%dc" % len(comp_start
),
166 def fetch_completions(self
, what
, mode
):
167 """Return a pair of lists of completions for something. The first list
168 is a sublist of the second. Both are sorted.
170 If there is a Python subprocess, get the comp. list there. Otherwise,
171 either fetch_completions() is running in the subprocess itself or it
172 was called in an IDLE EditorWindow before any script had been run.
174 The subprocess environment is that of the most recently run script. If
175 two unrelated modules are being edited some calltips in the current
176 module may be inoperative if the module was not the last to run.
179 rpcclt
= self
.editwin
.flist
.pyshell
.interp
.rpcclt
183 return rpcclt
.remotecall("exec", "get_the_completion_list",
186 if mode
== COMPLETE_ATTRIBUTES
:
188 namespace
= __main__
.__dict
__.copy()
189 namespace
.update(__main__
.__builtins
__.__dict
__)
190 bigl
= eval("dir()", namespace
)
192 if "__all__" in bigl
:
193 smalll
= eval("__all__", namespace
)
196 smalll
= filter(lambda s
: s
[:1] != '_', bigl
)
199 entity
= self
.get_entity(what
)
202 if "__all__" in bigl
:
203 smalll
= entity
.__all
__
206 smalll
= filter(lambda s
: s
[:1] != '_', bigl
)
210 elif mode
== COMPLETE_FILES
:
214 expandedpath
= os
.path
.expanduser(what
)
215 bigl
= os
.listdir(expandedpath
)
217 smalll
= filter(lambda s
: s
[:1] != '.', bigl
)
225 def get_entity(self
, name
):
226 """Lookup name in a namespace spanning sys.modules and __main.dict__"""
227 namespace
= sys
.modules
.copy()
228 namespace
.update(__main__
.__dict
__)
229 return eval(name
, namespace
)