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