##// END OF EJS Templates
Tweak wording of warning message
Thomas Kluyver -
Show More
@@ -1,1236 +1,1236 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Word completion for IPython.
2 """Word completion for IPython.
3
3
4 This module started as fork of the rlcompleter module in the Python standard
4 This module started as fork of the rlcompleter module in the Python standard
5 library. The original enhancements made to rlcompleter have been sent
5 library. The original enhancements made to rlcompleter have been sent
6 upstream and were accepted as of Python 2.3,
6 upstream and were accepted as of Python 2.3,
7
7
8 """
8 """
9
9
10 # Copyright (c) IPython Development Team.
10 # Copyright (c) IPython Development Team.
11 # Distributed under the terms of the Modified BSD License.
11 # Distributed under the terms of the Modified BSD License.
12 #
12 #
13 # Some of this code originated from rlcompleter in the Python standard library
13 # Some of this code originated from rlcompleter in the Python standard library
14 # Copyright (C) 2001 Python Software Foundation, www.python.org
14 # Copyright (C) 2001 Python Software Foundation, www.python.org
15
15
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 import __main__
18 import __main__
19 import glob
19 import glob
20 import inspect
20 import inspect
21 import itertools
21 import itertools
22 import keyword
22 import keyword
23 import os
23 import os
24 import re
24 import re
25 import sys
25 import sys
26 import unicodedata
26 import unicodedata
27 import string
27 import string
28 import warnings
28 import warnings
29
29
30 from traitlets.config.configurable import Configurable
30 from traitlets.config.configurable import Configurable
31 from IPython.core.error import TryNext
31 from IPython.core.error import TryNext
32 from IPython.core.inputsplitter import ESC_MAGIC
32 from IPython.core.inputsplitter import ESC_MAGIC
33 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
33 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
34 from IPython.utils import generics
34 from IPython.utils import generics
35 from IPython.utils.decorators import undoc
35 from IPython.utils.decorators import undoc
36 from IPython.utils.dir2 import dir2, get_real_method
36 from IPython.utils.dir2 import dir2, get_real_method
37 from IPython.utils.process import arg_split
37 from IPython.utils.process import arg_split
38 from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2
38 from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2
39 from traitlets import Bool, Enum, observe
39 from traitlets import Bool, Enum, observe
40
40
41 from functools import wraps
41 from functools import wraps
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Globals
44 # Globals
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 # Public API
47 # Public API
48 __all__ = ['Completer','IPCompleter']
48 __all__ = ['Completer','IPCompleter']
49
49
50 if sys.platform == 'win32':
50 if sys.platform == 'win32':
51 PROTECTABLES = ' '
51 PROTECTABLES = ' '
52 else:
52 else:
53 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
53 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
54
54
55
55
56 #-----------------------------------------------------------------------------
56 #-----------------------------------------------------------------------------
57 # Work around BUG decorators.
57 # Work around BUG decorators.
58 #-----------------------------------------------------------------------------
58 #-----------------------------------------------------------------------------
59
59
60 def _strip_single_trailing_space(complete):
60 def _strip_single_trailing_space(complete):
61 """
61 """
62 This is a workaround for a weird IPython/Prompt_toolkit behavior,
62 This is a workaround for a weird IPython/Prompt_toolkit behavior,
63 that can be removed once we rely on a slightly more recent prompt_toolkit
63 that can be removed once we rely on a slightly more recent prompt_toolkit
64 version (likely > 1.0.3). So this can likely be removed in IPython 6.0
64 version (likely > 1.0.3). So this can likely be removed in IPython 6.0
65
65
66 cf https://github.com/ipython/ipython/issues/9658
66 cf https://github.com/ipython/ipython/issues/9658
67 and https://github.com/jonathanslenders/python-prompt-toolkit/pull/328
67 and https://github.com/jonathanslenders/python-prompt-toolkit/pull/328
68
68
69 The bug is due to the fact that in PTK the completer will reinvoke itself
69 The bug is due to the fact that in PTK the completer will reinvoke itself
70 after trying to completer to the longuest common prefix of all the
70 after trying to completer to the longuest common prefix of all the
71 completions, unless only one completion is available.
71 completions, unless only one completion is available.
72
72
73 This logic is faulty if the completion ends with space, which can happen in
73 This logic is faulty if the completion ends with space, which can happen in
74 case like::
74 case like::
75
75
76 from foo import im<ta>
76 from foo import im<ta>
77
77
78 which only matching completion is `import `. Note the leading space at the
78 which only matching completion is `import `. Note the leading space at the
79 end. So leaving a space at the end is a reasonable request, but for now
79 end. So leaving a space at the end is a reasonable request, but for now
80 we'll strip it.
80 we'll strip it.
81 """
81 """
82
82
83 @wraps(complete)
83 @wraps(complete)
84 def comp(*args, **kwargs):
84 def comp(*args, **kwargs):
85 text, matches = complete(*args, **kwargs)
85 text, matches = complete(*args, **kwargs)
86 if len(matches) == 1:
86 if len(matches) == 1:
87 return text, [matches[0].rstrip()]
87 return text, [matches[0].rstrip()]
88 return text, matches
88 return text, matches
89
89
90 return comp
90 return comp
91
91
92
92
93
93
94 #-----------------------------------------------------------------------------
94 #-----------------------------------------------------------------------------
95 # Main functions and classes
95 # Main functions and classes
96 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
97
97
98 def has_open_quotes(s):
98 def has_open_quotes(s):
99 """Return whether a string has open quotes.
99 """Return whether a string has open quotes.
100
100
101 This simply counts whether the number of quote characters of either type in
101 This simply counts whether the number of quote characters of either type in
102 the string is odd.
102 the string is odd.
103
103
104 Returns
104 Returns
105 -------
105 -------
106 If there is an open quote, the quote character is returned. Else, return
106 If there is an open quote, the quote character is returned. Else, return
107 False.
107 False.
108 """
108 """
109 # We check " first, then ', so complex cases with nested quotes will get
109 # We check " first, then ', so complex cases with nested quotes will get
110 # the " to take precedence.
110 # the " to take precedence.
111 if s.count('"') % 2:
111 if s.count('"') % 2:
112 return '"'
112 return '"'
113 elif s.count("'") % 2:
113 elif s.count("'") % 2:
114 return "'"
114 return "'"
115 else:
115 else:
116 return False
116 return False
117
117
118
118
119 def protect_filename(s):
119 def protect_filename(s):
120 """Escape a string to protect certain characters."""
120 """Escape a string to protect certain characters."""
121 if set(s) & set(PROTECTABLES):
121 if set(s) & set(PROTECTABLES):
122 if sys.platform == "win32":
122 if sys.platform == "win32":
123 return '"' + s + '"'
123 return '"' + s + '"'
124 else:
124 else:
125 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
125 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
126 else:
126 else:
127 return s
127 return s
128
128
129
129
130 def expand_user(path):
130 def expand_user(path):
131 """Expand '~'-style usernames in strings.
131 """Expand '~'-style usernames in strings.
132
132
133 This is similar to :func:`os.path.expanduser`, but it computes and returns
133 This is similar to :func:`os.path.expanduser`, but it computes and returns
134 extra information that will be useful if the input was being used in
134 extra information that will be useful if the input was being used in
135 computing completions, and you wish to return the completions with the
135 computing completions, and you wish to return the completions with the
136 original '~' instead of its expanded value.
136 original '~' instead of its expanded value.
137
137
138 Parameters
138 Parameters
139 ----------
139 ----------
140 path : str
140 path : str
141 String to be expanded. If no ~ is present, the output is the same as the
141 String to be expanded. If no ~ is present, the output is the same as the
142 input.
142 input.
143
143
144 Returns
144 Returns
145 -------
145 -------
146 newpath : str
146 newpath : str
147 Result of ~ expansion in the input path.
147 Result of ~ expansion in the input path.
148 tilde_expand : bool
148 tilde_expand : bool
149 Whether any expansion was performed or not.
149 Whether any expansion was performed or not.
150 tilde_val : str
150 tilde_val : str
151 The value that ~ was replaced with.
151 The value that ~ was replaced with.
152 """
152 """
153 # Default values
153 # Default values
154 tilde_expand = False
154 tilde_expand = False
155 tilde_val = ''
155 tilde_val = ''
156 newpath = path
156 newpath = path
157
157
158 if path.startswith('~'):
158 if path.startswith('~'):
159 tilde_expand = True
159 tilde_expand = True
160 rest = len(path)-1
160 rest = len(path)-1
161 newpath = os.path.expanduser(path)
161 newpath = os.path.expanduser(path)
162 if rest:
162 if rest:
163 tilde_val = newpath[:-rest]
163 tilde_val = newpath[:-rest]
164 else:
164 else:
165 tilde_val = newpath
165 tilde_val = newpath
166
166
167 return newpath, tilde_expand, tilde_val
167 return newpath, tilde_expand, tilde_val
168
168
169
169
170 def compress_user(path, tilde_expand, tilde_val):
170 def compress_user(path, tilde_expand, tilde_val):
171 """Does the opposite of expand_user, with its outputs.
171 """Does the opposite of expand_user, with its outputs.
172 """
172 """
173 if tilde_expand:
173 if tilde_expand:
174 return path.replace(tilde_val, '~')
174 return path.replace(tilde_val, '~')
175 else:
175 else:
176 return path
176 return path
177
177
178
178
179 def completions_sorting_key(word):
179 def completions_sorting_key(word):
180 """key for sorting completions
180 """key for sorting completions
181
181
182 This does several things:
182 This does several things:
183
183
184 - Lowercase all completions, so they are sorted alphabetically with
184 - Lowercase all completions, so they are sorted alphabetically with
185 upper and lower case words mingled
185 upper and lower case words mingled
186 - Demote any completions starting with underscores to the end
186 - Demote any completions starting with underscores to the end
187 - Insert any %magic and %%cellmagic completions in the alphabetical order
187 - Insert any %magic and %%cellmagic completions in the alphabetical order
188 by their name
188 by their name
189 """
189 """
190 # Case insensitive sort
190 # Case insensitive sort
191 word = word.lower()
191 word = word.lower()
192
192
193 prio1, prio2 = 0, 0
193 prio1, prio2 = 0, 0
194
194
195 if word.startswith('__'):
195 if word.startswith('__'):
196 prio1 = 2
196 prio1 = 2
197 elif word.startswith('_'):
197 elif word.startswith('_'):
198 prio1 = 1
198 prio1 = 1
199
199
200 if word.endswith('='):
200 if word.endswith('='):
201 prio1 = -1
201 prio1 = -1
202
202
203 if word.startswith('%%'):
203 if word.startswith('%%'):
204 # If there's another % in there, this is something else, so leave it alone
204 # If there's another % in there, this is something else, so leave it alone
205 if not "%" in word[2:]:
205 if not "%" in word[2:]:
206 word = word[2:]
206 word = word[2:]
207 prio2 = 2
207 prio2 = 2
208 elif word.startswith('%'):
208 elif word.startswith('%'):
209 if not "%" in word[1:]:
209 if not "%" in word[1:]:
210 word = word[1:]
210 word = word[1:]
211 prio2 = 1
211 prio2 = 1
212
212
213 return prio1, word, prio2
213 return prio1, word, prio2
214
214
215
215
216 @undoc
216 @undoc
217 class Bunch(object): pass
217 class Bunch(object): pass
218
218
219
219
220 if sys.platform == 'win32':
220 if sys.platform == 'win32':
221 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
221 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
222 else:
222 else:
223 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
223 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
224
224
225 GREEDY_DELIMS = ' =\r\n'
225 GREEDY_DELIMS = ' =\r\n'
226
226
227
227
228 class CompletionSplitter(object):
228 class CompletionSplitter(object):
229 """An object to split an input line in a manner similar to readline.
229 """An object to split an input line in a manner similar to readline.
230
230
231 By having our own implementation, we can expose readline-like completion in
231 By having our own implementation, we can expose readline-like completion in
232 a uniform manner to all frontends. This object only needs to be given the
232 a uniform manner to all frontends. This object only needs to be given the
233 line of text to be split and the cursor position on said line, and it
233 line of text to be split and the cursor position on said line, and it
234 returns the 'word' to be completed on at the cursor after splitting the
234 returns the 'word' to be completed on at the cursor after splitting the
235 entire line.
235 entire line.
236
236
237 What characters are used as splitting delimiters can be controlled by
237 What characters are used as splitting delimiters can be controlled by
238 setting the `delims` attribute (this is a property that internally
238 setting the `delims` attribute (this is a property that internally
239 automatically builds the necessary regular expression)"""
239 automatically builds the necessary regular expression)"""
240
240
241 # Private interface
241 # Private interface
242
242
243 # A string of delimiter characters. The default value makes sense for
243 # A string of delimiter characters. The default value makes sense for
244 # IPython's most typical usage patterns.
244 # IPython's most typical usage patterns.
245 _delims = DELIMS
245 _delims = DELIMS
246
246
247 # The expression (a normal string) to be compiled into a regular expression
247 # The expression (a normal string) to be compiled into a regular expression
248 # for actual splitting. We store it as an attribute mostly for ease of
248 # for actual splitting. We store it as an attribute mostly for ease of
249 # debugging, since this type of code can be so tricky to debug.
249 # debugging, since this type of code can be so tricky to debug.
250 _delim_expr = None
250 _delim_expr = None
251
251
252 # The regular expression that does the actual splitting
252 # The regular expression that does the actual splitting
253 _delim_re = None
253 _delim_re = None
254
254
255 def __init__(self, delims=None):
255 def __init__(self, delims=None):
256 delims = CompletionSplitter._delims if delims is None else delims
256 delims = CompletionSplitter._delims if delims is None else delims
257 self.delims = delims
257 self.delims = delims
258
258
259 @property
259 @property
260 def delims(self):
260 def delims(self):
261 """Return the string of delimiter characters."""
261 """Return the string of delimiter characters."""
262 return self._delims
262 return self._delims
263
263
264 @delims.setter
264 @delims.setter
265 def delims(self, delims):
265 def delims(self, delims):
266 """Set the delimiters for line splitting."""
266 """Set the delimiters for line splitting."""
267 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
267 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
268 self._delim_re = re.compile(expr)
268 self._delim_re = re.compile(expr)
269 self._delims = delims
269 self._delims = delims
270 self._delim_expr = expr
270 self._delim_expr = expr
271
271
272 def split_line(self, line, cursor_pos=None):
272 def split_line(self, line, cursor_pos=None):
273 """Split a line of text with a cursor at the given position.
273 """Split a line of text with a cursor at the given position.
274 """
274 """
275 l = line if cursor_pos is None else line[:cursor_pos]
275 l = line if cursor_pos is None else line[:cursor_pos]
276 return self._delim_re.split(l)[-1]
276 return self._delim_re.split(l)[-1]
277
277
278
278
279 class Completer(Configurable):
279 class Completer(Configurable):
280
280
281 greedy = Bool(False,
281 greedy = Bool(False,
282 help="""Activate greedy completion
282 help="""Activate greedy completion
283 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
283 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
284
284
285 This will enable completion on elements of lists, results of function calls, etc.,
285 This will enable completion on elements of lists, results of function calls, etc.,
286 but can be unsafe because the code is actually evaluated on TAB.
286 but can be unsafe because the code is actually evaluated on TAB.
287 """
287 """
288 ).tag(config=True)
288 ).tag(config=True)
289
289
290
290
291 def __init__(self, namespace=None, global_namespace=None, **kwargs):
291 def __init__(self, namespace=None, global_namespace=None, **kwargs):
292 """Create a new completer for the command line.
292 """Create a new completer for the command line.
293
293
294 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
294 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
295
295
296 If unspecified, the default namespace where completions are performed
296 If unspecified, the default namespace where completions are performed
297 is __main__ (technically, __main__.__dict__). Namespaces should be
297 is __main__ (technically, __main__.__dict__). Namespaces should be
298 given as dictionaries.
298 given as dictionaries.
299
299
300 An optional second namespace can be given. This allows the completer
300 An optional second namespace can be given. This allows the completer
301 to handle cases where both the local and global scopes need to be
301 to handle cases where both the local and global scopes need to be
302 distinguished.
302 distinguished.
303
303
304 Completer instances should be used as the completion mechanism of
304 Completer instances should be used as the completion mechanism of
305 readline via the set_completer() call:
305 readline via the set_completer() call:
306
306
307 readline.set_completer(Completer(my_namespace).complete)
307 readline.set_completer(Completer(my_namespace).complete)
308 """
308 """
309
309
310 # Don't bind to namespace quite yet, but flag whether the user wants a
310 # Don't bind to namespace quite yet, but flag whether the user wants a
311 # specific namespace or to use __main__.__dict__. This will allow us
311 # specific namespace or to use __main__.__dict__. This will allow us
312 # to bind to __main__.__dict__ at completion time, not now.
312 # to bind to __main__.__dict__ at completion time, not now.
313 if namespace is None:
313 if namespace is None:
314 self.use_main_ns = 1
314 self.use_main_ns = 1
315 else:
315 else:
316 self.use_main_ns = 0
316 self.use_main_ns = 0
317 self.namespace = namespace
317 self.namespace = namespace
318
318
319 # The global namespace, if given, can be bound directly
319 # The global namespace, if given, can be bound directly
320 if global_namespace is None:
320 if global_namespace is None:
321 self.global_namespace = {}
321 self.global_namespace = {}
322 else:
322 else:
323 self.global_namespace = global_namespace
323 self.global_namespace = global_namespace
324
324
325 super(Completer, self).__init__(**kwargs)
325 super(Completer, self).__init__(**kwargs)
326
326
327 def complete(self, text, state):
327 def complete(self, text, state):
328 """Return the next possible completion for 'text'.
328 """Return the next possible completion for 'text'.
329
329
330 This is called successively with state == 0, 1, 2, ... until it
330 This is called successively with state == 0, 1, 2, ... until it
331 returns None. The completion should begin with 'text'.
331 returns None. The completion should begin with 'text'.
332
332
333 """
333 """
334 if self.use_main_ns:
334 if self.use_main_ns:
335 self.namespace = __main__.__dict__
335 self.namespace = __main__.__dict__
336
336
337 if state == 0:
337 if state == 0:
338 if "." in text:
338 if "." in text:
339 self.matches = self.attr_matches(text)
339 self.matches = self.attr_matches(text)
340 else:
340 else:
341 self.matches = self.global_matches(text)
341 self.matches = self.global_matches(text)
342 try:
342 try:
343 return self.matches[state]
343 return self.matches[state]
344 except IndexError:
344 except IndexError:
345 return None
345 return None
346
346
347 def global_matches(self, text):
347 def global_matches(self, text):
348 """Compute matches when text is a simple name.
348 """Compute matches when text is a simple name.
349
349
350 Return a list of all keywords, built-in functions and names currently
350 Return a list of all keywords, built-in functions and names currently
351 defined in self.namespace or self.global_namespace that match.
351 defined in self.namespace or self.global_namespace that match.
352
352
353 """
353 """
354 matches = []
354 matches = []
355 match_append = matches.append
355 match_append = matches.append
356 n = len(text)
356 n = len(text)
357 for lst in [keyword.kwlist,
357 for lst in [keyword.kwlist,
358 builtin_mod.__dict__.keys(),
358 builtin_mod.__dict__.keys(),
359 self.namespace.keys(),
359 self.namespace.keys(),
360 self.global_namespace.keys()]:
360 self.global_namespace.keys()]:
361 for word in lst:
361 for word in lst:
362 if word[:n] == text and word != "__builtins__":
362 if word[:n] == text and word != "__builtins__":
363 match_append(word)
363 match_append(word)
364 return [cast_unicode_py2(m) for m in matches]
364 return [cast_unicode_py2(m) for m in matches]
365
365
366 def attr_matches(self, text):
366 def attr_matches(self, text):
367 """Compute matches when text contains a dot.
367 """Compute matches when text contains a dot.
368
368
369 Assuming the text is of the form NAME.NAME....[NAME], and is
369 Assuming the text is of the form NAME.NAME....[NAME], and is
370 evaluatable in self.namespace or self.global_namespace, it will be
370 evaluatable in self.namespace or self.global_namespace, it will be
371 evaluated and its attributes (as revealed by dir()) are used as
371 evaluated and its attributes (as revealed by dir()) are used as
372 possible completions. (For class instances, class members are are
372 possible completions. (For class instances, class members are are
373 also considered.)
373 also considered.)
374
374
375 WARNING: this can still invoke arbitrary C code, if an object
375 WARNING: this can still invoke arbitrary C code, if an object
376 with a __getattr__ hook is evaluated.
376 with a __getattr__ hook is evaluated.
377
377
378 """
378 """
379
379
380 # Another option, seems to work great. Catches things like ''.<tab>
380 # Another option, seems to work great. Catches things like ''.<tab>
381 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
381 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
382
382
383 if m:
383 if m:
384 expr, attr = m.group(1, 3)
384 expr, attr = m.group(1, 3)
385 elif self.greedy:
385 elif self.greedy:
386 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
386 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
387 if not m2:
387 if not m2:
388 return []
388 return []
389 expr, attr = m2.group(1,2)
389 expr, attr = m2.group(1,2)
390 else:
390 else:
391 return []
391 return []
392
392
393 try:
393 try:
394 obj = eval(expr, self.namespace)
394 obj = eval(expr, self.namespace)
395 except:
395 except:
396 try:
396 try:
397 obj = eval(expr, self.global_namespace)
397 obj = eval(expr, self.global_namespace)
398 except:
398 except:
399 return []
399 return []
400
400
401 if self.limit_to__all__ and hasattr(obj, '__all__'):
401 if self.limit_to__all__ and hasattr(obj, '__all__'):
402 words = get__all__entries(obj)
402 words = get__all__entries(obj)
403 else:
403 else:
404 words = dir2(obj)
404 words = dir2(obj)
405
405
406 try:
406 try:
407 words = generics.complete_object(obj, words)
407 words = generics.complete_object(obj, words)
408 except TryNext:
408 except TryNext:
409 pass
409 pass
410 except Exception:
410 except Exception:
411 # Silence errors from completion function
411 # Silence errors from completion function
412 #raise # dbg
412 #raise # dbg
413 pass
413 pass
414 # Build match list to return
414 # Build match list to return
415 n = len(attr)
415 n = len(attr)
416 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
416 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
417
417
418
418
419 def get__all__entries(obj):
419 def get__all__entries(obj):
420 """returns the strings in the __all__ attribute"""
420 """returns the strings in the __all__ attribute"""
421 try:
421 try:
422 words = getattr(obj, '__all__')
422 words = getattr(obj, '__all__')
423 except:
423 except:
424 return []
424 return []
425
425
426 return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)]
426 return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)]
427
427
428
428
429 def match_dict_keys(keys, prefix, delims):
429 def match_dict_keys(keys, prefix, delims):
430 """Used by dict_key_matches, matching the prefix to a list of keys"""
430 """Used by dict_key_matches, matching the prefix to a list of keys"""
431 if not prefix:
431 if not prefix:
432 return None, 0, [repr(k) for k in keys
432 return None, 0, [repr(k) for k in keys
433 if isinstance(k, (string_types, bytes))]
433 if isinstance(k, (string_types, bytes))]
434 quote_match = re.search('["\']', prefix)
434 quote_match = re.search('["\']', prefix)
435 quote = quote_match.group()
435 quote = quote_match.group()
436 try:
436 try:
437 prefix_str = eval(prefix + quote, {})
437 prefix_str = eval(prefix + quote, {})
438 except Exception:
438 except Exception:
439 return None, 0, []
439 return None, 0, []
440
440
441 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
441 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
442 token_match = re.search(pattern, prefix, re.UNICODE)
442 token_match = re.search(pattern, prefix, re.UNICODE)
443 token_start = token_match.start()
443 token_start = token_match.start()
444 token_prefix = token_match.group()
444 token_prefix = token_match.group()
445
445
446 # TODO: support bytes in Py3k
446 # TODO: support bytes in Py3k
447 matched = []
447 matched = []
448 for key in keys:
448 for key in keys:
449 try:
449 try:
450 if not key.startswith(prefix_str):
450 if not key.startswith(prefix_str):
451 continue
451 continue
452 except (AttributeError, TypeError, UnicodeError):
452 except (AttributeError, TypeError, UnicodeError):
453 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
453 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
454 continue
454 continue
455
455
456 # reformat remainder of key to begin with prefix
456 # reformat remainder of key to begin with prefix
457 rem = key[len(prefix_str):]
457 rem = key[len(prefix_str):]
458 # force repr wrapped in '
458 # force repr wrapped in '
459 rem_repr = repr(rem + '"')
459 rem_repr = repr(rem + '"')
460 if rem_repr.startswith('u') and prefix[0] not in 'uU':
460 if rem_repr.startswith('u') and prefix[0] not in 'uU':
461 # Found key is unicode, but prefix is Py2 string.
461 # Found key is unicode, but prefix is Py2 string.
462 # Therefore attempt to interpret key as string.
462 # Therefore attempt to interpret key as string.
463 try:
463 try:
464 rem_repr = repr(rem.encode('ascii') + '"')
464 rem_repr = repr(rem.encode('ascii') + '"')
465 except UnicodeEncodeError:
465 except UnicodeEncodeError:
466 continue
466 continue
467
467
468 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
468 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
469 if quote == '"':
469 if quote == '"':
470 # The entered prefix is quoted with ",
470 # The entered prefix is quoted with ",
471 # but the match is quoted with '.
471 # but the match is quoted with '.
472 # A contained " hence needs escaping for comparison:
472 # A contained " hence needs escaping for comparison:
473 rem_repr = rem_repr.replace('"', '\\"')
473 rem_repr = rem_repr.replace('"', '\\"')
474
474
475 # then reinsert prefix from start of token
475 # then reinsert prefix from start of token
476 matched.append('%s%s' % (token_prefix, rem_repr))
476 matched.append('%s%s' % (token_prefix, rem_repr))
477 return quote, token_start, matched
477 return quote, token_start, matched
478
478
479
479
480 def _safe_isinstance(obj, module, class_name):
480 def _safe_isinstance(obj, module, class_name):
481 """Checks if obj is an instance of module.class_name if loaded
481 """Checks if obj is an instance of module.class_name if loaded
482 """
482 """
483 return (module in sys.modules and
483 return (module in sys.modules and
484 isinstance(obj, getattr(__import__(module), class_name)))
484 isinstance(obj, getattr(__import__(module), class_name)))
485
485
486
486
487 def back_unicode_name_matches(text):
487 def back_unicode_name_matches(text):
488 u"""Match unicode characters back to unicode name
488 u"""Match unicode characters back to unicode name
489
489
490 This does β˜ƒ -> \\snowman
490 This does β˜ƒ -> \\snowman
491
491
492 Note that snowman is not a valid python3 combining character but will be expanded.
492 Note that snowman is not a valid python3 combining character but will be expanded.
493 Though it will not recombine back to the snowman character by the completion machinery.
493 Though it will not recombine back to the snowman character by the completion machinery.
494
494
495 This will not either back-complete standard sequences like \\n, \\b ...
495 This will not either back-complete standard sequences like \\n, \\b ...
496
496
497 Used on Python 3 only.
497 Used on Python 3 only.
498 """
498 """
499 if len(text)<2:
499 if len(text)<2:
500 return u'', ()
500 return u'', ()
501 maybe_slash = text[-2]
501 maybe_slash = text[-2]
502 if maybe_slash != '\\':
502 if maybe_slash != '\\':
503 return u'', ()
503 return u'', ()
504
504
505 char = text[-1]
505 char = text[-1]
506 # no expand on quote for completion in strings.
506 # no expand on quote for completion in strings.
507 # nor backcomplete standard ascii keys
507 # nor backcomplete standard ascii keys
508 if char in string.ascii_letters or char in ['"',"'"]:
508 if char in string.ascii_letters or char in ['"',"'"]:
509 return u'', ()
509 return u'', ()
510 try :
510 try :
511 unic = unicodedata.name(char)
511 unic = unicodedata.name(char)
512 return '\\'+char,['\\'+unic]
512 return '\\'+char,['\\'+unic]
513 except KeyError:
513 except KeyError:
514 pass
514 pass
515 return u'', ()
515 return u'', ()
516
516
517 def back_latex_name_matches(text):
517 def back_latex_name_matches(text):
518 u"""Match latex characters back to unicode name
518 u"""Match latex characters back to unicode name
519
519
520 This does ->\\sqrt
520 This does ->\\sqrt
521
521
522 Used on Python 3 only.
522 Used on Python 3 only.
523 """
523 """
524 if len(text)<2:
524 if len(text)<2:
525 return u'', ()
525 return u'', ()
526 maybe_slash = text[-2]
526 maybe_slash = text[-2]
527 if maybe_slash != '\\':
527 if maybe_slash != '\\':
528 return u'', ()
528 return u'', ()
529
529
530
530
531 char = text[-1]
531 char = text[-1]
532 # no expand on quote for completion in strings.
532 # no expand on quote for completion in strings.
533 # nor backcomplete standard ascii keys
533 # nor backcomplete standard ascii keys
534 if char in string.ascii_letters or char in ['"',"'"]:
534 if char in string.ascii_letters or char in ['"',"'"]:
535 return u'', ()
535 return u'', ()
536 try :
536 try :
537 latex = reverse_latex_symbol[char]
537 latex = reverse_latex_symbol[char]
538 # '\\' replace the \ as well
538 # '\\' replace the \ as well
539 return '\\'+char,[latex]
539 return '\\'+char,[latex]
540 except KeyError:
540 except KeyError:
541 pass
541 pass
542 return u'', ()
542 return u'', ()
543
543
544
544
545 class IPCompleter(Completer):
545 class IPCompleter(Completer):
546 """Extension of the completer class with IPython-specific features"""
546 """Extension of the completer class with IPython-specific features"""
547
547
548 @observe('greedy')
548 @observe('greedy')
549 def _greedy_changed(self, change):
549 def _greedy_changed(self, change):
550 """update the splitter and readline delims when greedy is changed"""
550 """update the splitter and readline delims when greedy is changed"""
551 if change['new']:
551 if change['new']:
552 self.splitter.delims = GREEDY_DELIMS
552 self.splitter.delims = GREEDY_DELIMS
553 else:
553 else:
554 self.splitter.delims = DELIMS
554 self.splitter.delims = DELIMS
555
555
556 merge_completions = Bool(True,
556 merge_completions = Bool(True,
557 help="""Whether to merge completion results into a single list
557 help="""Whether to merge completion results into a single list
558
558
559 If False, only the completion results from the first non-empty
559 If False, only the completion results from the first non-empty
560 completer will be returned.
560 completer will be returned.
561 """
561 """
562 ).tag(config=True)
562 ).tag(config=True)
563 omit__names = Enum((0,1,2), default_value=2,
563 omit__names = Enum((0,1,2), default_value=2,
564 help="""Instruct the completer to omit private method names
564 help="""Instruct the completer to omit private method names
565
565
566 Specifically, when completing on ``object.<tab>``.
566 Specifically, when completing on ``object.<tab>``.
567
567
568 When 2 [default]: all names that start with '_' will be excluded.
568 When 2 [default]: all names that start with '_' will be excluded.
569
569
570 When 1: all 'magic' names (``__foo__``) will be excluded.
570 When 1: all 'magic' names (``__foo__``) will be excluded.
571
571
572 When 0: nothing will be excluded.
572 When 0: nothing will be excluded.
573 """
573 """
574 ).tag(config=True)
574 ).tag(config=True)
575 limit_to__all__ = Bool(False,
575 limit_to__all__ = Bool(False,
576 help="""
576 help="""
577 DEPRECATED as of version 5.0.
577 DEPRECATED as of version 5.0.
578
578
579 Instruct the completer to use __all__ for the completion
579 Instruct the completer to use __all__ for the completion
580
580
581 Specifically, when completing on ``object.<tab>``.
581 Specifically, when completing on ``object.<tab>``.
582
582
583 When True: only those names in obj.__all__ will be included.
583 When True: only those names in obj.__all__ will be included.
584
584
585 When False [default]: the __all__ attribute is ignored
585 When False [default]: the __all__ attribute is ignored
586 """,
586 """,
587 ).tag(config=True)
587 ).tag(config=True)
588
588
589 def __init__(self, shell=None, namespace=None, global_namespace=None,
589 def __init__(self, shell=None, namespace=None, global_namespace=None,
590 use_readline=False, config=None, **kwargs):
590 use_readline=False, config=None, **kwargs):
591 """IPCompleter() -> completer
591 """IPCompleter() -> completer
592
592
593 Return a completer object suitable for use by the readline library
593 Return a completer object suitable for use by the readline library
594 via readline.set_completer().
594 via readline.set_completer().
595
595
596 Inputs:
596 Inputs:
597
597
598 - shell: a pointer to the ipython shell itself. This is needed
598 - shell: a pointer to the ipython shell itself. This is needed
599 because this completer knows about magic functions, and those can
599 because this completer knows about magic functions, and those can
600 only be accessed via the ipython instance.
600 only be accessed via the ipython instance.
601
601
602 - namespace: an optional dict where completions are performed.
602 - namespace: an optional dict where completions are performed.
603
603
604 - global_namespace: secondary optional dict for completions, to
604 - global_namespace: secondary optional dict for completions, to
605 handle cases (such as IPython embedded inside functions) where
605 handle cases (such as IPython embedded inside functions) where
606 both Python scopes are visible.
606 both Python scopes are visible.
607
607
608 use_readline : bool, optional
608 use_readline : bool, optional
609 DEPRECATED, ignored.
609 DEPRECATED, ignored.
610 """
610 """
611
611
612 self.magic_escape = ESC_MAGIC
612 self.magic_escape = ESC_MAGIC
613 self.splitter = CompletionSplitter()
613 self.splitter = CompletionSplitter()
614
614
615 if use_readline:
615 if use_readline:
616 warnings.warn('The use_readline parameter is deprecated since IPython 6.0.',
616 warnings.warn('The use_readline parameter is deprecated and ignored since IPython 6.0.',
617 DeprecationWarning, stacklevel=2)
617 DeprecationWarning, stacklevel=2)
618
618
619 # _greedy_changed() depends on splitter and readline being defined:
619 # _greedy_changed() depends on splitter and readline being defined:
620 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
620 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
621 config=config, **kwargs)
621 config=config, **kwargs)
622
622
623 # List where completion matches will be stored
623 # List where completion matches will be stored
624 self.matches = []
624 self.matches = []
625 self.shell = shell
625 self.shell = shell
626 # Regexp to split filenames with spaces in them
626 # Regexp to split filenames with spaces in them
627 self.space_name_re = re.compile(r'([^\\] )')
627 self.space_name_re = re.compile(r'([^\\] )')
628 # Hold a local ref. to glob.glob for speed
628 # Hold a local ref. to glob.glob for speed
629 self.glob = glob.glob
629 self.glob = glob.glob
630
630
631 # Determine if we are running on 'dumb' terminals, like (X)Emacs
631 # Determine if we are running on 'dumb' terminals, like (X)Emacs
632 # buffers, to avoid completion problems.
632 # buffers, to avoid completion problems.
633 term = os.environ.get('TERM','xterm')
633 term = os.environ.get('TERM','xterm')
634 self.dumb_terminal = term in ['dumb','emacs']
634 self.dumb_terminal = term in ['dumb','emacs']
635
635
636 # Special handling of backslashes needed in win32 platforms
636 # Special handling of backslashes needed in win32 platforms
637 if sys.platform == "win32":
637 if sys.platform == "win32":
638 self.clean_glob = self._clean_glob_win32
638 self.clean_glob = self._clean_glob_win32
639 else:
639 else:
640 self.clean_glob = self._clean_glob
640 self.clean_glob = self._clean_glob
641
641
642 #regexp to parse docstring for function signature
642 #regexp to parse docstring for function signature
643 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
643 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
644 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
644 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
645 #use this if positional argument name is also needed
645 #use this if positional argument name is also needed
646 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
646 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
647
647
648 # All active matcher routines for completion
648 # All active matcher routines for completion
649 self.matchers = [
649 self.matchers = [
650 self.python_matches,
650 self.python_matches,
651 self.file_matches,
651 self.file_matches,
652 self.magic_matches,
652 self.magic_matches,
653 self.python_func_kw_matches,
653 self.python_func_kw_matches,
654 self.dict_key_matches,
654 self.dict_key_matches,
655 ]
655 ]
656
656
657 # This is set externally by InteractiveShell
657 # This is set externally by InteractiveShell
658 self.custom_completers = None
658 self.custom_completers = None
659
659
660 def all_completions(self, text):
660 def all_completions(self, text):
661 """
661 """
662 Wrapper around the complete method for the benefit of emacs.
662 Wrapper around the complete method for the benefit of emacs.
663 """
663 """
664 return self.complete(text)[1]
664 return self.complete(text)[1]
665
665
666 def _clean_glob(self, text):
666 def _clean_glob(self, text):
667 return self.glob("%s*" % text)
667 return self.glob("%s*" % text)
668
668
669 def _clean_glob_win32(self,text):
669 def _clean_glob_win32(self,text):
670 return [f.replace("\\","/")
670 return [f.replace("\\","/")
671 for f in self.glob("%s*" % text)]
671 for f in self.glob("%s*" % text)]
672
672
673 def file_matches(self, text):
673 def file_matches(self, text):
674 """Match filenames, expanding ~USER type strings.
674 """Match filenames, expanding ~USER type strings.
675
675
676 Most of the seemingly convoluted logic in this completer is an
676 Most of the seemingly convoluted logic in this completer is an
677 attempt to handle filenames with spaces in them. And yet it's not
677 attempt to handle filenames with spaces in them. And yet it's not
678 quite perfect, because Python's readline doesn't expose all of the
678 quite perfect, because Python's readline doesn't expose all of the
679 GNU readline details needed for this to be done correctly.
679 GNU readline details needed for this to be done correctly.
680
680
681 For a filename with a space in it, the printed completions will be
681 For a filename with a space in it, the printed completions will be
682 only the parts after what's already been typed (instead of the
682 only the parts after what's already been typed (instead of the
683 full completions, as is normally done). I don't think with the
683 full completions, as is normally done). I don't think with the
684 current (as of Python 2.3) Python readline it's possible to do
684 current (as of Python 2.3) Python readline it's possible to do
685 better."""
685 better."""
686
686
687 # chars that require escaping with backslash - i.e. chars
687 # chars that require escaping with backslash - i.e. chars
688 # that readline treats incorrectly as delimiters, but we
688 # that readline treats incorrectly as delimiters, but we
689 # don't want to treat as delimiters in filename matching
689 # don't want to treat as delimiters in filename matching
690 # when escaped with backslash
690 # when escaped with backslash
691 if text.startswith('!'):
691 if text.startswith('!'):
692 text = text[1:]
692 text = text[1:]
693 text_prefix = u'!'
693 text_prefix = u'!'
694 else:
694 else:
695 text_prefix = u''
695 text_prefix = u''
696
696
697 text_until_cursor = self.text_until_cursor
697 text_until_cursor = self.text_until_cursor
698 # track strings with open quotes
698 # track strings with open quotes
699 open_quotes = has_open_quotes(text_until_cursor)
699 open_quotes = has_open_quotes(text_until_cursor)
700
700
701 if '(' in text_until_cursor or '[' in text_until_cursor:
701 if '(' in text_until_cursor or '[' in text_until_cursor:
702 lsplit = text
702 lsplit = text
703 else:
703 else:
704 try:
704 try:
705 # arg_split ~ shlex.split, but with unicode bugs fixed by us
705 # arg_split ~ shlex.split, but with unicode bugs fixed by us
706 lsplit = arg_split(text_until_cursor)[-1]
706 lsplit = arg_split(text_until_cursor)[-1]
707 except ValueError:
707 except ValueError:
708 # typically an unmatched ", or backslash without escaped char.
708 # typically an unmatched ", or backslash without escaped char.
709 if open_quotes:
709 if open_quotes:
710 lsplit = text_until_cursor.split(open_quotes)[-1]
710 lsplit = text_until_cursor.split(open_quotes)[-1]
711 else:
711 else:
712 return []
712 return []
713 except IndexError:
713 except IndexError:
714 # tab pressed on empty line
714 # tab pressed on empty line
715 lsplit = ""
715 lsplit = ""
716
716
717 if not open_quotes and lsplit != protect_filename(lsplit):
717 if not open_quotes and lsplit != protect_filename(lsplit):
718 # if protectables are found, do matching on the whole escaped name
718 # if protectables are found, do matching on the whole escaped name
719 has_protectables = True
719 has_protectables = True
720 text0,text = text,lsplit
720 text0,text = text,lsplit
721 else:
721 else:
722 has_protectables = False
722 has_protectables = False
723 text = os.path.expanduser(text)
723 text = os.path.expanduser(text)
724
724
725 if text == "":
725 if text == "":
726 return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")]
726 return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")]
727
727
728 # Compute the matches from the filesystem
728 # Compute the matches from the filesystem
729 if sys.platform == 'win32':
729 if sys.platform == 'win32':
730 m0 = self.clean_glob(text)
730 m0 = self.clean_glob(text)
731 else:
731 else:
732 m0 = self.clean_glob(text.replace('\\', ''))
732 m0 = self.clean_glob(text.replace('\\', ''))
733
733
734 if has_protectables:
734 if has_protectables:
735 # If we had protectables, we need to revert our changes to the
735 # If we had protectables, we need to revert our changes to the
736 # beginning of filename so that we don't double-write the part
736 # beginning of filename so that we don't double-write the part
737 # of the filename we have so far
737 # of the filename we have so far
738 len_lsplit = len(lsplit)
738 len_lsplit = len(lsplit)
739 matches = [text_prefix + text0 +
739 matches = [text_prefix + text0 +
740 protect_filename(f[len_lsplit:]) for f in m0]
740 protect_filename(f[len_lsplit:]) for f in m0]
741 else:
741 else:
742 if open_quotes:
742 if open_quotes:
743 # if we have a string with an open quote, we don't need to
743 # if we have a string with an open quote, we don't need to
744 # protect the names at all (and we _shouldn't_, as it
744 # protect the names at all (and we _shouldn't_, as it
745 # would cause bugs when the filesystem call is made).
745 # would cause bugs when the filesystem call is made).
746 matches = m0
746 matches = m0
747 else:
747 else:
748 matches = [text_prefix +
748 matches = [text_prefix +
749 protect_filename(f) for f in m0]
749 protect_filename(f) for f in m0]
750
750
751 # Mark directories in input list by appending '/' to their names.
751 # Mark directories in input list by appending '/' to their names.
752 return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches]
752 return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches]
753
753
754 def magic_matches(self, text):
754 def magic_matches(self, text):
755 """Match magics"""
755 """Match magics"""
756 # Get all shell magics now rather than statically, so magics loaded at
756 # Get all shell magics now rather than statically, so magics loaded at
757 # runtime show up too.
757 # runtime show up too.
758 lsm = self.shell.magics_manager.lsmagic()
758 lsm = self.shell.magics_manager.lsmagic()
759 line_magics = lsm['line']
759 line_magics = lsm['line']
760 cell_magics = lsm['cell']
760 cell_magics = lsm['cell']
761 pre = self.magic_escape
761 pre = self.magic_escape
762 pre2 = pre+pre
762 pre2 = pre+pre
763
763
764 # Completion logic:
764 # Completion logic:
765 # - user gives %%: only do cell magics
765 # - user gives %%: only do cell magics
766 # - user gives %: do both line and cell magics
766 # - user gives %: do both line and cell magics
767 # - no prefix: do both
767 # - no prefix: do both
768 # In other words, line magics are skipped if the user gives %% explicitly
768 # In other words, line magics are skipped if the user gives %% explicitly
769 bare_text = text.lstrip(pre)
769 bare_text = text.lstrip(pre)
770 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
770 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
771 if not text.startswith(pre2):
771 if not text.startswith(pre2):
772 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
772 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
773 return [cast_unicode_py2(c) for c in comp]
773 return [cast_unicode_py2(c) for c in comp]
774
774
775
775
776 def python_matches(self, text):
776 def python_matches(self, text):
777 """Match attributes or global python names"""
777 """Match attributes or global python names"""
778 if "." in text:
778 if "." in text:
779 try:
779 try:
780 matches = self.attr_matches(text)
780 matches = self.attr_matches(text)
781 if text.endswith('.') and self.omit__names:
781 if text.endswith('.') and self.omit__names:
782 if self.omit__names == 1:
782 if self.omit__names == 1:
783 # true if txt is _not_ a __ name, false otherwise:
783 # true if txt is _not_ a __ name, false otherwise:
784 no__name = (lambda txt:
784 no__name = (lambda txt:
785 re.match(r'.*\.__.*?__',txt) is None)
785 re.match(r'.*\.__.*?__',txt) is None)
786 else:
786 else:
787 # true if txt is _not_ a _ name, false otherwise:
787 # true if txt is _not_ a _ name, false otherwise:
788 no__name = (lambda txt:
788 no__name = (lambda txt:
789 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
789 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
790 matches = filter(no__name, matches)
790 matches = filter(no__name, matches)
791 except NameError:
791 except NameError:
792 # catches <undefined attributes>.<tab>
792 # catches <undefined attributes>.<tab>
793 matches = []
793 matches = []
794 else:
794 else:
795 matches = self.global_matches(text)
795 matches = self.global_matches(text)
796 return matches
796 return matches
797
797
798 def _default_arguments_from_docstring(self, doc):
798 def _default_arguments_from_docstring(self, doc):
799 """Parse the first line of docstring for call signature.
799 """Parse the first line of docstring for call signature.
800
800
801 Docstring should be of the form 'min(iterable[, key=func])\n'.
801 Docstring should be of the form 'min(iterable[, key=func])\n'.
802 It can also parse cython docstring of the form
802 It can also parse cython docstring of the form
803 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
803 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
804 """
804 """
805 if doc is None:
805 if doc is None:
806 return []
806 return []
807
807
808 #care only the firstline
808 #care only the firstline
809 line = doc.lstrip().splitlines()[0]
809 line = doc.lstrip().splitlines()[0]
810
810
811 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
811 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
812 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
812 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
813 sig = self.docstring_sig_re.search(line)
813 sig = self.docstring_sig_re.search(line)
814 if sig is None:
814 if sig is None:
815 return []
815 return []
816 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
816 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
817 sig = sig.groups()[0].split(',')
817 sig = sig.groups()[0].split(',')
818 ret = []
818 ret = []
819 for s in sig:
819 for s in sig:
820 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
820 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
821 ret += self.docstring_kwd_re.findall(s)
821 ret += self.docstring_kwd_re.findall(s)
822 return ret
822 return ret
823
823
824 def _default_arguments(self, obj):
824 def _default_arguments(self, obj):
825 """Return the list of default arguments of obj if it is callable,
825 """Return the list of default arguments of obj if it is callable,
826 or empty list otherwise."""
826 or empty list otherwise."""
827 call_obj = obj
827 call_obj = obj
828 ret = []
828 ret = []
829 if inspect.isbuiltin(obj):
829 if inspect.isbuiltin(obj):
830 pass
830 pass
831 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
831 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
832 if inspect.isclass(obj):
832 if inspect.isclass(obj):
833 #for cython embededsignature=True the constructor docstring
833 #for cython embededsignature=True the constructor docstring
834 #belongs to the object itself not __init__
834 #belongs to the object itself not __init__
835 ret += self._default_arguments_from_docstring(
835 ret += self._default_arguments_from_docstring(
836 getattr(obj, '__doc__', ''))
836 getattr(obj, '__doc__', ''))
837 # for classes, check for __init__,__new__
837 # for classes, check for __init__,__new__
838 call_obj = (getattr(obj, '__init__', None) or
838 call_obj = (getattr(obj, '__init__', None) or
839 getattr(obj, '__new__', None))
839 getattr(obj, '__new__', None))
840 # for all others, check if they are __call__able
840 # for all others, check if they are __call__able
841 elif hasattr(obj, '__call__'):
841 elif hasattr(obj, '__call__'):
842 call_obj = obj.__call__
842 call_obj = obj.__call__
843 ret += self._default_arguments_from_docstring(
843 ret += self._default_arguments_from_docstring(
844 getattr(call_obj, '__doc__', ''))
844 getattr(call_obj, '__doc__', ''))
845
845
846 if PY3:
846 if PY3:
847 _keeps = (inspect.Parameter.KEYWORD_ONLY,
847 _keeps = (inspect.Parameter.KEYWORD_ONLY,
848 inspect.Parameter.POSITIONAL_OR_KEYWORD)
848 inspect.Parameter.POSITIONAL_OR_KEYWORD)
849 signature = inspect.signature
849 signature = inspect.signature
850 else:
850 else:
851 import IPython.utils.signatures
851 import IPython.utils.signatures
852 _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY,
852 _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY,
853 IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD)
853 IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD)
854 signature = IPython.utils.signatures.signature
854 signature = IPython.utils.signatures.signature
855
855
856 try:
856 try:
857 sig = signature(call_obj)
857 sig = signature(call_obj)
858 ret.extend(k for k, v in sig.parameters.items() if
858 ret.extend(k for k, v in sig.parameters.items() if
859 v.kind in _keeps)
859 v.kind in _keeps)
860 except ValueError:
860 except ValueError:
861 pass
861 pass
862
862
863 return list(set(ret))
863 return list(set(ret))
864
864
865 def python_func_kw_matches(self,text):
865 def python_func_kw_matches(self,text):
866 """Match named parameters (kwargs) of the last open function"""
866 """Match named parameters (kwargs) of the last open function"""
867
867
868 if "." in text: # a parameter cannot be dotted
868 if "." in text: # a parameter cannot be dotted
869 return []
869 return []
870 try: regexp = self.__funcParamsRegex
870 try: regexp = self.__funcParamsRegex
871 except AttributeError:
871 except AttributeError:
872 regexp = self.__funcParamsRegex = re.compile(r'''
872 regexp = self.__funcParamsRegex = re.compile(r'''
873 '.*?(?<!\\)' | # single quoted strings or
873 '.*?(?<!\\)' | # single quoted strings or
874 ".*?(?<!\\)" | # double quoted strings or
874 ".*?(?<!\\)" | # double quoted strings or
875 \w+ | # identifier
875 \w+ | # identifier
876 \S # other characters
876 \S # other characters
877 ''', re.VERBOSE | re.DOTALL)
877 ''', re.VERBOSE | re.DOTALL)
878 # 1. find the nearest identifier that comes before an unclosed
878 # 1. find the nearest identifier that comes before an unclosed
879 # parenthesis before the cursor
879 # parenthesis before the cursor
880 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
880 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
881 tokens = regexp.findall(self.text_until_cursor)
881 tokens = regexp.findall(self.text_until_cursor)
882 iterTokens = reversed(tokens); openPar = 0
882 iterTokens = reversed(tokens); openPar = 0
883
883
884 for token in iterTokens:
884 for token in iterTokens:
885 if token == ')':
885 if token == ')':
886 openPar -= 1
886 openPar -= 1
887 elif token == '(':
887 elif token == '(':
888 openPar += 1
888 openPar += 1
889 if openPar > 0:
889 if openPar > 0:
890 # found the last unclosed parenthesis
890 # found the last unclosed parenthesis
891 break
891 break
892 else:
892 else:
893 return []
893 return []
894 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
894 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
895 ids = []
895 ids = []
896 isId = re.compile(r'\w+$').match
896 isId = re.compile(r'\w+$').match
897
897
898 while True:
898 while True:
899 try:
899 try:
900 ids.append(next(iterTokens))
900 ids.append(next(iterTokens))
901 if not isId(ids[-1]):
901 if not isId(ids[-1]):
902 ids.pop(); break
902 ids.pop(); break
903 if not next(iterTokens) == '.':
903 if not next(iterTokens) == '.':
904 break
904 break
905 except StopIteration:
905 except StopIteration:
906 break
906 break
907
907
908 # Find all named arguments already assigned to, as to avoid suggesting
908 # Find all named arguments already assigned to, as to avoid suggesting
909 # them again
909 # them again
910 usedNamedArgs = set()
910 usedNamedArgs = set()
911 par_level = -1
911 par_level = -1
912 for token, next_token in zip(tokens, tokens[1:]):
912 for token, next_token in zip(tokens, tokens[1:]):
913 if token == '(':
913 if token == '(':
914 par_level += 1
914 par_level += 1
915 elif token == ')':
915 elif token == ')':
916 par_level -= 1
916 par_level -= 1
917
917
918 if par_level != 0:
918 if par_level != 0:
919 continue
919 continue
920
920
921 if next_token != '=':
921 if next_token != '=':
922 continue
922 continue
923
923
924 usedNamedArgs.add(token)
924 usedNamedArgs.add(token)
925
925
926 # lookup the candidate callable matches either using global_matches
926 # lookup the candidate callable matches either using global_matches
927 # or attr_matches for dotted names
927 # or attr_matches for dotted names
928 if len(ids) == 1:
928 if len(ids) == 1:
929 callableMatches = self.global_matches(ids[0])
929 callableMatches = self.global_matches(ids[0])
930 else:
930 else:
931 callableMatches = self.attr_matches('.'.join(ids[::-1]))
931 callableMatches = self.attr_matches('.'.join(ids[::-1]))
932 argMatches = []
932 argMatches = []
933 for callableMatch in callableMatches:
933 for callableMatch in callableMatches:
934 try:
934 try:
935 namedArgs = self._default_arguments(eval(callableMatch,
935 namedArgs = self._default_arguments(eval(callableMatch,
936 self.namespace))
936 self.namespace))
937 except:
937 except:
938 continue
938 continue
939
939
940 # Remove used named arguments from the list, no need to show twice
940 # Remove used named arguments from the list, no need to show twice
941 for namedArg in set(namedArgs) - usedNamedArgs:
941 for namedArg in set(namedArgs) - usedNamedArgs:
942 if namedArg.startswith(text):
942 if namedArg.startswith(text):
943 argMatches.append(u"%s=" %namedArg)
943 argMatches.append(u"%s=" %namedArg)
944 return argMatches
944 return argMatches
945
945
946 def dict_key_matches(self, text):
946 def dict_key_matches(self, text):
947 "Match string keys in a dictionary, after e.g. 'foo[' "
947 "Match string keys in a dictionary, after e.g. 'foo[' "
948 def get_keys(obj):
948 def get_keys(obj):
949 # Objects can define their own completions by defining an
949 # Objects can define their own completions by defining an
950 # _ipy_key_completions_() method.
950 # _ipy_key_completions_() method.
951 method = get_real_method(obj, '_ipython_key_completions_')
951 method = get_real_method(obj, '_ipython_key_completions_')
952 if method is not None:
952 if method is not None:
953 return method()
953 return method()
954
954
955 # Special case some common in-memory dict-like types
955 # Special case some common in-memory dict-like types
956 if isinstance(obj, dict) or\
956 if isinstance(obj, dict) or\
957 _safe_isinstance(obj, 'pandas', 'DataFrame'):
957 _safe_isinstance(obj, 'pandas', 'DataFrame'):
958 try:
958 try:
959 return list(obj.keys())
959 return list(obj.keys())
960 except Exception:
960 except Exception:
961 return []
961 return []
962 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
962 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
963 _safe_isinstance(obj, 'numpy', 'void'):
963 _safe_isinstance(obj, 'numpy', 'void'):
964 return obj.dtype.names or []
964 return obj.dtype.names or []
965 return []
965 return []
966
966
967 try:
967 try:
968 regexps = self.__dict_key_regexps
968 regexps = self.__dict_key_regexps
969 except AttributeError:
969 except AttributeError:
970 dict_key_re_fmt = r'''(?x)
970 dict_key_re_fmt = r'''(?x)
971 ( # match dict-referring expression wrt greedy setting
971 ( # match dict-referring expression wrt greedy setting
972 %s
972 %s
973 )
973 )
974 \[ # open bracket
974 \[ # open bracket
975 \s* # and optional whitespace
975 \s* # and optional whitespace
976 ([uUbB]? # string prefix (r not handled)
976 ([uUbB]? # string prefix (r not handled)
977 (?: # unclosed string
977 (?: # unclosed string
978 '(?:[^']|(?<!\\)\\')*
978 '(?:[^']|(?<!\\)\\')*
979 |
979 |
980 "(?:[^"]|(?<!\\)\\")*
980 "(?:[^"]|(?<!\\)\\")*
981 )
981 )
982 )?
982 )?
983 $
983 $
984 '''
984 '''
985 regexps = self.__dict_key_regexps = {
985 regexps = self.__dict_key_regexps = {
986 False: re.compile(dict_key_re_fmt % '''
986 False: re.compile(dict_key_re_fmt % '''
987 # identifiers separated by .
987 # identifiers separated by .
988 (?!\d)\w+
988 (?!\d)\w+
989 (?:\.(?!\d)\w+)*
989 (?:\.(?!\d)\w+)*
990 '''),
990 '''),
991 True: re.compile(dict_key_re_fmt % '''
991 True: re.compile(dict_key_re_fmt % '''
992 .+
992 .+
993 ''')
993 ''')
994 }
994 }
995
995
996 match = regexps[self.greedy].search(self.text_until_cursor)
996 match = regexps[self.greedy].search(self.text_until_cursor)
997 if match is None:
997 if match is None:
998 return []
998 return []
999
999
1000 expr, prefix = match.groups()
1000 expr, prefix = match.groups()
1001 try:
1001 try:
1002 obj = eval(expr, self.namespace)
1002 obj = eval(expr, self.namespace)
1003 except Exception:
1003 except Exception:
1004 try:
1004 try:
1005 obj = eval(expr, self.global_namespace)
1005 obj = eval(expr, self.global_namespace)
1006 except Exception:
1006 except Exception:
1007 return []
1007 return []
1008
1008
1009 keys = get_keys(obj)
1009 keys = get_keys(obj)
1010 if not keys:
1010 if not keys:
1011 return keys
1011 return keys
1012 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1012 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1013 if not matches:
1013 if not matches:
1014 return matches
1014 return matches
1015
1015
1016 # get the cursor position of
1016 # get the cursor position of
1017 # - the text being completed
1017 # - the text being completed
1018 # - the start of the key text
1018 # - the start of the key text
1019 # - the start of the completion
1019 # - the start of the completion
1020 text_start = len(self.text_until_cursor) - len(text)
1020 text_start = len(self.text_until_cursor) - len(text)
1021 if prefix:
1021 if prefix:
1022 key_start = match.start(2)
1022 key_start = match.start(2)
1023 completion_start = key_start + token_offset
1023 completion_start = key_start + token_offset
1024 else:
1024 else:
1025 key_start = completion_start = match.end()
1025 key_start = completion_start = match.end()
1026
1026
1027 # grab the leading prefix, to make sure all completions start with `text`
1027 # grab the leading prefix, to make sure all completions start with `text`
1028 if text_start > key_start:
1028 if text_start > key_start:
1029 leading = ''
1029 leading = ''
1030 else:
1030 else:
1031 leading = text[text_start:completion_start]
1031 leading = text[text_start:completion_start]
1032
1032
1033 # the index of the `[` character
1033 # the index of the `[` character
1034 bracket_idx = match.end(1)
1034 bracket_idx = match.end(1)
1035
1035
1036 # append closing quote and bracket as appropriate
1036 # append closing quote and bracket as appropriate
1037 # this is *not* appropriate if the opening quote or bracket is outside
1037 # this is *not* appropriate if the opening quote or bracket is outside
1038 # the text given to this method
1038 # the text given to this method
1039 suf = ''
1039 suf = ''
1040 continuation = self.line_buffer[len(self.text_until_cursor):]
1040 continuation = self.line_buffer[len(self.text_until_cursor):]
1041 if key_start > text_start and closing_quote:
1041 if key_start > text_start and closing_quote:
1042 # quotes were opened inside text, maybe close them
1042 # quotes were opened inside text, maybe close them
1043 if continuation.startswith(closing_quote):
1043 if continuation.startswith(closing_quote):
1044 continuation = continuation[len(closing_quote):]
1044 continuation = continuation[len(closing_quote):]
1045 else:
1045 else:
1046 suf += closing_quote
1046 suf += closing_quote
1047 if bracket_idx > text_start:
1047 if bracket_idx > text_start:
1048 # brackets were opened inside text, maybe close them
1048 # brackets were opened inside text, maybe close them
1049 if not continuation.startswith(']'):
1049 if not continuation.startswith(']'):
1050 suf += ']'
1050 suf += ']'
1051
1051
1052 return [leading + k + suf for k in matches]
1052 return [leading + k + suf for k in matches]
1053
1053
1054 def unicode_name_matches(self, text):
1054 def unicode_name_matches(self, text):
1055 u"""Match Latex-like syntax for unicode characters base
1055 u"""Match Latex-like syntax for unicode characters base
1056 on the name of the character.
1056 on the name of the character.
1057
1057
1058 This does \\GREEK SMALL LETTER ETA -> Ξ·
1058 This does \\GREEK SMALL LETTER ETA -> Ξ·
1059
1059
1060 Works only on valid python 3 identifier, or on combining characters that
1060 Works only on valid python 3 identifier, or on combining characters that
1061 will combine to form a valid identifier.
1061 will combine to form a valid identifier.
1062
1062
1063 Used on Python 3 only.
1063 Used on Python 3 only.
1064 """
1064 """
1065 slashpos = text.rfind('\\')
1065 slashpos = text.rfind('\\')
1066 if slashpos > -1:
1066 if slashpos > -1:
1067 s = text[slashpos+1:]
1067 s = text[slashpos+1:]
1068 try :
1068 try :
1069 unic = unicodedata.lookup(s)
1069 unic = unicodedata.lookup(s)
1070 # allow combining chars
1070 # allow combining chars
1071 if ('a'+unic).isidentifier():
1071 if ('a'+unic).isidentifier():
1072 return '\\'+s,[unic]
1072 return '\\'+s,[unic]
1073 except KeyError:
1073 except KeyError:
1074 pass
1074 pass
1075 return u'', []
1075 return u'', []
1076
1076
1077
1077
1078
1078
1079
1079
1080 def latex_matches(self, text):
1080 def latex_matches(self, text):
1081 u"""Match Latex syntax for unicode characters.
1081 u"""Match Latex syntax for unicode characters.
1082
1082
1083 This does both \\alp -> \\alpha and \\alpha -> Ξ±
1083 This does both \\alp -> \\alpha and \\alpha -> Ξ±
1084
1084
1085 Used on Python 3 only.
1085 Used on Python 3 only.
1086 """
1086 """
1087 slashpos = text.rfind('\\')
1087 slashpos = text.rfind('\\')
1088 if slashpos > -1:
1088 if slashpos > -1:
1089 s = text[slashpos:]
1089 s = text[slashpos:]
1090 if s in latex_symbols:
1090 if s in latex_symbols:
1091 # Try to complete a full latex symbol to unicode
1091 # Try to complete a full latex symbol to unicode
1092 # \\alpha -> Ξ±
1092 # \\alpha -> Ξ±
1093 return s, [latex_symbols[s]]
1093 return s, [latex_symbols[s]]
1094 else:
1094 else:
1095 # If a user has partially typed a latex symbol, give them
1095 # If a user has partially typed a latex symbol, give them
1096 # a full list of options \al -> [\aleph, \alpha]
1096 # a full list of options \al -> [\aleph, \alpha]
1097 matches = [k for k in latex_symbols if k.startswith(s)]
1097 matches = [k for k in latex_symbols if k.startswith(s)]
1098 return s, matches
1098 return s, matches
1099 return u'', []
1099 return u'', []
1100
1100
1101 def dispatch_custom_completer(self, text):
1101 def dispatch_custom_completer(self, text):
1102 if not self.custom_completers:
1102 if not self.custom_completers:
1103 return
1103 return
1104
1104
1105 line = self.line_buffer
1105 line = self.line_buffer
1106 if not line.strip():
1106 if not line.strip():
1107 return None
1107 return None
1108
1108
1109 # Create a little structure to pass all the relevant information about
1109 # Create a little structure to pass all the relevant information about
1110 # the current completion to any custom completer.
1110 # the current completion to any custom completer.
1111 event = Bunch()
1111 event = Bunch()
1112 event.line = line
1112 event.line = line
1113 event.symbol = text
1113 event.symbol = text
1114 cmd = line.split(None,1)[0]
1114 cmd = line.split(None,1)[0]
1115 event.command = cmd
1115 event.command = cmd
1116 event.text_until_cursor = self.text_until_cursor
1116 event.text_until_cursor = self.text_until_cursor
1117
1117
1118 # for foo etc, try also to find completer for %foo
1118 # for foo etc, try also to find completer for %foo
1119 if not cmd.startswith(self.magic_escape):
1119 if not cmd.startswith(self.magic_escape):
1120 try_magic = self.custom_completers.s_matches(
1120 try_magic = self.custom_completers.s_matches(
1121 self.magic_escape + cmd)
1121 self.magic_escape + cmd)
1122 else:
1122 else:
1123 try_magic = []
1123 try_magic = []
1124
1124
1125 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1125 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1126 try_magic,
1126 try_magic,
1127 self.custom_completers.flat_matches(self.text_until_cursor)):
1127 self.custom_completers.flat_matches(self.text_until_cursor)):
1128 try:
1128 try:
1129 res = c(event)
1129 res = c(event)
1130 if res:
1130 if res:
1131 # first, try case sensitive match
1131 # first, try case sensitive match
1132 withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)]
1132 withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)]
1133 if withcase:
1133 if withcase:
1134 return withcase
1134 return withcase
1135 # if none, then case insensitive ones are ok too
1135 # if none, then case insensitive ones are ok too
1136 text_low = text.lower()
1136 text_low = text.lower()
1137 return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)]
1137 return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)]
1138 except TryNext:
1138 except TryNext:
1139 pass
1139 pass
1140
1140
1141 return None
1141 return None
1142
1142
1143 @_strip_single_trailing_space
1143 @_strip_single_trailing_space
1144 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1144 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1145 """Find completions for the given text and line context.
1145 """Find completions for the given text and line context.
1146
1146
1147 Note that both the text and the line_buffer are optional, but at least
1147 Note that both the text and the line_buffer are optional, but at least
1148 one of them must be given.
1148 one of them must be given.
1149
1149
1150 Parameters
1150 Parameters
1151 ----------
1151 ----------
1152 text : string, optional
1152 text : string, optional
1153 Text to perform the completion on. If not given, the line buffer
1153 Text to perform the completion on. If not given, the line buffer
1154 is split using the instance's CompletionSplitter object.
1154 is split using the instance's CompletionSplitter object.
1155
1155
1156 line_buffer : string, optional
1156 line_buffer : string, optional
1157 If not given, the completer attempts to obtain the current line
1157 If not given, the completer attempts to obtain the current line
1158 buffer via readline. This keyword allows clients which are
1158 buffer via readline. This keyword allows clients which are
1159 requesting for text completions in non-readline contexts to inform
1159 requesting for text completions in non-readline contexts to inform
1160 the completer of the entire text.
1160 the completer of the entire text.
1161
1161
1162 cursor_pos : int, optional
1162 cursor_pos : int, optional
1163 Index of the cursor in the full line buffer. Should be provided by
1163 Index of the cursor in the full line buffer. Should be provided by
1164 remote frontends where kernel has no access to frontend state.
1164 remote frontends where kernel has no access to frontend state.
1165
1165
1166 Returns
1166 Returns
1167 -------
1167 -------
1168 text : str
1168 text : str
1169 Text that was actually used in the completion.
1169 Text that was actually used in the completion.
1170
1170
1171 matches : list
1171 matches : list
1172 A list of completion matches.
1172 A list of completion matches.
1173 """
1173 """
1174 # if the cursor position isn't given, the only sane assumption we can
1174 # if the cursor position isn't given, the only sane assumption we can
1175 # make is that it's at the end of the line (the common case)
1175 # make is that it's at the end of the line (the common case)
1176 if cursor_pos is None:
1176 if cursor_pos is None:
1177 cursor_pos = len(line_buffer) if text is None else len(text)
1177 cursor_pos = len(line_buffer) if text is None else len(text)
1178
1178
1179 if self.use_main_ns:
1179 if self.use_main_ns:
1180 self.namespace = __main__.__dict__
1180 self.namespace = __main__.__dict__
1181
1181
1182 if PY3:
1182 if PY3:
1183
1183
1184 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1184 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1185 latex_text, latex_matches = self.latex_matches(base_text)
1185 latex_text, latex_matches = self.latex_matches(base_text)
1186 if latex_matches:
1186 if latex_matches:
1187 return latex_text, latex_matches
1187 return latex_text, latex_matches
1188 name_text = ''
1188 name_text = ''
1189 name_matches = []
1189 name_matches = []
1190 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1190 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1191 name_text, name_matches = meth(base_text)
1191 name_text, name_matches = meth(base_text)
1192 if name_text:
1192 if name_text:
1193 return name_text, name_matches
1193 return name_text, name_matches
1194
1194
1195 # if text is either None or an empty string, rely on the line buffer
1195 # if text is either None or an empty string, rely on the line buffer
1196 if not text:
1196 if not text:
1197 text = self.splitter.split_line(line_buffer, cursor_pos)
1197 text = self.splitter.split_line(line_buffer, cursor_pos)
1198
1198
1199 # If no line buffer is given, assume the input text is all there was
1199 # If no line buffer is given, assume the input text is all there was
1200 if line_buffer is None:
1200 if line_buffer is None:
1201 line_buffer = text
1201 line_buffer = text
1202
1202
1203 self.line_buffer = line_buffer
1203 self.line_buffer = line_buffer
1204 self.text_until_cursor = self.line_buffer[:cursor_pos]
1204 self.text_until_cursor = self.line_buffer[:cursor_pos]
1205
1205
1206 # Start with a clean slate of completions
1206 # Start with a clean slate of completions
1207 self.matches[:] = []
1207 self.matches[:] = []
1208 custom_res = self.dispatch_custom_completer(text)
1208 custom_res = self.dispatch_custom_completer(text)
1209 if custom_res is not None:
1209 if custom_res is not None:
1210 # did custom completers produce something?
1210 # did custom completers produce something?
1211 self.matches = custom_res
1211 self.matches = custom_res
1212 else:
1212 else:
1213 # Extend the list of completions with the results of each
1213 # Extend the list of completions with the results of each
1214 # matcher, so we return results to the user from all
1214 # matcher, so we return results to the user from all
1215 # namespaces.
1215 # namespaces.
1216 if self.merge_completions:
1216 if self.merge_completions:
1217 self.matches = []
1217 self.matches = []
1218 for matcher in self.matchers:
1218 for matcher in self.matchers:
1219 try:
1219 try:
1220 self.matches.extend(matcher(text))
1220 self.matches.extend(matcher(text))
1221 except:
1221 except:
1222 # Show the ugly traceback if the matcher causes an
1222 # Show the ugly traceback if the matcher causes an
1223 # exception, but do NOT crash the kernel!
1223 # exception, but do NOT crash the kernel!
1224 sys.excepthook(*sys.exc_info())
1224 sys.excepthook(*sys.exc_info())
1225 else:
1225 else:
1226 for matcher in self.matchers:
1226 for matcher in self.matchers:
1227 self.matches = matcher(text)
1227 self.matches = matcher(text)
1228 if self.matches:
1228 if self.matches:
1229 break
1229 break
1230 # FIXME: we should extend our api to return a dict with completions for
1230 # FIXME: we should extend our api to return a dict with completions for
1231 # different types of objects. The rlcomplete() method could then
1231 # different types of objects. The rlcomplete() method could then
1232 # simply collapse the dict into a list for readline, but we'd have
1232 # simply collapse the dict into a list for readline, but we'd have
1233 # richer completion semantics in other evironments.
1233 # richer completion semantics in other evironments.
1234 self.matches = sorted(set(self.matches), key=completions_sorting_key)
1234 self.matches = sorted(set(self.matches), key=completions_sorting_key)
1235
1235
1236 return text, self.matches
1236 return text, self.matches
General Comments 0
You need to be logged in to leave comments. Login now