ยปCore Development>Code coverage>Lib/idlelib/AutoComplete.py

Python code coverage for Lib/idlelib/AutoComplete.py

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