##// END OF EJS Templates
FIX/API: use inspect.signature for python3...
Thomas A Caswell -
Show More
@@ -1,1267 +1,1271 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
843 ret += self._default_arguments_from_docstring(
842 ret += self._default_arguments_from_docstring(
844 getattr(call_obj, '__doc__', ''))
843 getattr(call_obj, '__doc__', ''))
845
846 try:
844 try:
847 args,_,_1,defaults = inspect.getargspec(call_obj)
845 if PY3:
848 if defaults:
846 _keepers = (inspect.Parameter.KEYWORD_ONLY,
849 ret+=args[-len(defaults):]
847 inspect.Parameter.POSITIONAL_OR_KEYWORD)
848 sig = inspect.signature(call_obj)
849 ret.extend(k for k, v in sig.parameters.items() if
850 v.kind in _keepers)
851 else:
852 args, _, _1, defaults = inspect.getargspec(call_obj)
853 if defaults:
854 ret += args[-len(defaults):]
850 except TypeError:
855 except TypeError:
851 pass
856 pass
852
853 return list(set(ret))
857 return list(set(ret))
854
858
855 def python_func_kw_matches(self,text):
859 def python_func_kw_matches(self,text):
856 """Match named parameters (kwargs) of the last open function"""
860 """Match named parameters (kwargs) of the last open function"""
857
861
858 if "." in text: # a parameter cannot be dotted
862 if "." in text: # a parameter cannot be dotted
859 return []
863 return []
860 try: regexp = self.__funcParamsRegex
864 try: regexp = self.__funcParamsRegex
861 except AttributeError:
865 except AttributeError:
862 regexp = self.__funcParamsRegex = re.compile(r'''
866 regexp = self.__funcParamsRegex = re.compile(r'''
863 '.*?(?<!\\)' | # single quoted strings or
867 '.*?(?<!\\)' | # single quoted strings or
864 ".*?(?<!\\)" | # double quoted strings or
868 ".*?(?<!\\)" | # double quoted strings or
865 \w+ | # identifier
869 \w+ | # identifier
866 \S # other characters
870 \S # other characters
867 ''', re.VERBOSE | re.DOTALL)
871 ''', re.VERBOSE | re.DOTALL)
868 # 1. find the nearest identifier that comes before an unclosed
872 # 1. find the nearest identifier that comes before an unclosed
869 # parenthesis before the cursor
873 # parenthesis before the cursor
870 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
874 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
871 tokens = regexp.findall(self.text_until_cursor)
875 tokens = regexp.findall(self.text_until_cursor)
872 tokens.reverse()
876 tokens.reverse()
873 iterTokens = iter(tokens); openPar = 0
877 iterTokens = iter(tokens); openPar = 0
874
878
875 for token in iterTokens:
879 for token in iterTokens:
876 if token == ')':
880 if token == ')':
877 openPar -= 1
881 openPar -= 1
878 elif token == '(':
882 elif token == '(':
879 openPar += 1
883 openPar += 1
880 if openPar > 0:
884 if openPar > 0:
881 # found the last unclosed parenthesis
885 # found the last unclosed parenthesis
882 break
886 break
883 else:
887 else:
884 return []
888 return []
885 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
889 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
886 ids = []
890 ids = []
887 isId = re.compile(r'\w+$').match
891 isId = re.compile(r'\w+$').match
888
892
889 while True:
893 while True:
890 try:
894 try:
891 ids.append(next(iterTokens))
895 ids.append(next(iterTokens))
892 if not isId(ids[-1]):
896 if not isId(ids[-1]):
893 ids.pop(); break
897 ids.pop(); break
894 if not next(iterTokens) == '.':
898 if not next(iterTokens) == '.':
895 break
899 break
896 except StopIteration:
900 except StopIteration:
897 break
901 break
898 # lookup the candidate callable matches either using global_matches
902 # lookup the candidate callable matches either using global_matches
899 # or attr_matches for dotted names
903 # or attr_matches for dotted names
900 if len(ids) == 1:
904 if len(ids) == 1:
901 callableMatches = self.global_matches(ids[0])
905 callableMatches = self.global_matches(ids[0])
902 else:
906 else:
903 callableMatches = self.attr_matches('.'.join(ids[::-1]))
907 callableMatches = self.attr_matches('.'.join(ids[::-1]))
904 argMatches = []
908 argMatches = []
905 for callableMatch in callableMatches:
909 for callableMatch in callableMatches:
906 try:
910 try:
907 namedArgs = self._default_arguments(eval(callableMatch,
911 namedArgs = self._default_arguments(eval(callableMatch,
908 self.namespace))
912 self.namespace))
909 except:
913 except:
910 continue
914 continue
911
915
912 for namedArg in namedArgs:
916 for namedArg in namedArgs:
913 if namedArg.startswith(text):
917 if namedArg.startswith(text):
914 argMatches.append("%s=" %namedArg)
918 argMatches.append("%s=" %namedArg)
915 return argMatches
919 return argMatches
916
920
917 def dict_key_matches(self, text):
921 def dict_key_matches(self, text):
918 "Match string keys in a dictionary, after e.g. 'foo[' "
922 "Match string keys in a dictionary, after e.g. 'foo[' "
919 def get_keys(obj):
923 def get_keys(obj):
920 # Only allow completion for known in-memory dict-like types
924 # Only allow completion for known in-memory dict-like types
921 if isinstance(obj, dict) or\
925 if isinstance(obj, dict) or\
922 _safe_isinstance(obj, 'pandas', 'DataFrame'):
926 _safe_isinstance(obj, 'pandas', 'DataFrame'):
923 try:
927 try:
924 return list(obj.keys())
928 return list(obj.keys())
925 except Exception:
929 except Exception:
926 return []
930 return []
927 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
931 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
928 _safe_isinstance(obj, 'numpy', 'void'):
932 _safe_isinstance(obj, 'numpy', 'void'):
929 return obj.dtype.names or []
933 return obj.dtype.names or []
930 return []
934 return []
931
935
932 try:
936 try:
933 regexps = self.__dict_key_regexps
937 regexps = self.__dict_key_regexps
934 except AttributeError:
938 except AttributeError:
935 dict_key_re_fmt = r'''(?x)
939 dict_key_re_fmt = r'''(?x)
936 ( # match dict-referring expression wrt greedy setting
940 ( # match dict-referring expression wrt greedy setting
937 %s
941 %s
938 )
942 )
939 \[ # open bracket
943 \[ # open bracket
940 \s* # and optional whitespace
944 \s* # and optional whitespace
941 ([uUbB]? # string prefix (r not handled)
945 ([uUbB]? # string prefix (r not handled)
942 (?: # unclosed string
946 (?: # unclosed string
943 '(?:[^']|(?<!\\)\\')*
947 '(?:[^']|(?<!\\)\\')*
944 |
948 |
945 "(?:[^"]|(?<!\\)\\")*
949 "(?:[^"]|(?<!\\)\\")*
946 )
950 )
947 )?
951 )?
948 $
952 $
949 '''
953 '''
950 regexps = self.__dict_key_regexps = {
954 regexps = self.__dict_key_regexps = {
951 False: re.compile(dict_key_re_fmt % '''
955 False: re.compile(dict_key_re_fmt % '''
952 # identifiers separated by .
956 # identifiers separated by .
953 (?!\d)\w+
957 (?!\d)\w+
954 (?:\.(?!\d)\w+)*
958 (?:\.(?!\d)\w+)*
955 '''),
959 '''),
956 True: re.compile(dict_key_re_fmt % '''
960 True: re.compile(dict_key_re_fmt % '''
957 .+
961 .+
958 ''')
962 ''')
959 }
963 }
960
964
961 match = regexps[self.greedy].search(self.text_until_cursor)
965 match = regexps[self.greedy].search(self.text_until_cursor)
962 if match is None:
966 if match is None:
963 return []
967 return []
964
968
965 expr, prefix = match.groups()
969 expr, prefix = match.groups()
966 try:
970 try:
967 obj = eval(expr, self.namespace)
971 obj = eval(expr, self.namespace)
968 except Exception:
972 except Exception:
969 try:
973 try:
970 obj = eval(expr, self.global_namespace)
974 obj = eval(expr, self.global_namespace)
971 except Exception:
975 except Exception:
972 return []
976 return []
973
977
974 keys = get_keys(obj)
978 keys = get_keys(obj)
975 if not keys:
979 if not keys:
976 return keys
980 return keys
977 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
981 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
978 if not matches:
982 if not matches:
979 return matches
983 return matches
980
984
981 # get the cursor position of
985 # get the cursor position of
982 # - the text being completed
986 # - the text being completed
983 # - the start of the key text
987 # - the start of the key text
984 # - the start of the completion
988 # - the start of the completion
985 text_start = len(self.text_until_cursor) - len(text)
989 text_start = len(self.text_until_cursor) - len(text)
986 if prefix:
990 if prefix:
987 key_start = match.start(2)
991 key_start = match.start(2)
988 completion_start = key_start + token_offset
992 completion_start = key_start + token_offset
989 else:
993 else:
990 key_start = completion_start = match.end()
994 key_start = completion_start = match.end()
991
995
992 # grab the leading prefix, to make sure all completions start with `text`
996 # grab the leading prefix, to make sure all completions start with `text`
993 if text_start > key_start:
997 if text_start > key_start:
994 leading = ''
998 leading = ''
995 else:
999 else:
996 leading = text[text_start:completion_start]
1000 leading = text[text_start:completion_start]
997
1001
998 # the index of the `[` character
1002 # the index of the `[` character
999 bracket_idx = match.end(1)
1003 bracket_idx = match.end(1)
1000
1004
1001 # append closing quote and bracket as appropriate
1005 # append closing quote and bracket as appropriate
1002 # this is *not* appropriate if the opening quote or bracket is outside
1006 # this is *not* appropriate if the opening quote or bracket is outside
1003 # the text given to this method
1007 # the text given to this method
1004 suf = ''
1008 suf = ''
1005 continuation = self.line_buffer[len(self.text_until_cursor):]
1009 continuation = self.line_buffer[len(self.text_until_cursor):]
1006 if key_start > text_start and closing_quote:
1010 if key_start > text_start and closing_quote:
1007 # quotes were opened inside text, maybe close them
1011 # quotes were opened inside text, maybe close them
1008 if continuation.startswith(closing_quote):
1012 if continuation.startswith(closing_quote):
1009 continuation = continuation[len(closing_quote):]
1013 continuation = continuation[len(closing_quote):]
1010 else:
1014 else:
1011 suf += closing_quote
1015 suf += closing_quote
1012 if bracket_idx > text_start:
1016 if bracket_idx > text_start:
1013 # brackets were opened inside text, maybe close them
1017 # brackets were opened inside text, maybe close them
1014 if not continuation.startswith(']'):
1018 if not continuation.startswith(']'):
1015 suf += ']'
1019 suf += ']'
1016
1020
1017 return [leading + k + suf for k in matches]
1021 return [leading + k + suf for k in matches]
1018
1022
1019 def unicode_name_matches(self, text):
1023 def unicode_name_matches(self, text):
1020 u"""Match Latex-like syntax for unicode characters base
1024 u"""Match Latex-like syntax for unicode characters base
1021 on the name of the character.
1025 on the name of the character.
1022
1026
1023 This does \\GREEK SMALL LETTER ETA -> Ξ·
1027 This does \\GREEK SMALL LETTER ETA -> Ξ·
1024
1028
1025 Works only on valid python 3 identifier, or on combining characters that
1029 Works only on valid python 3 identifier, or on combining characters that
1026 will combine to form a valid identifier.
1030 will combine to form a valid identifier.
1027
1031
1028 Used on Python 3 only.
1032 Used on Python 3 only.
1029 """
1033 """
1030 slashpos = text.rfind('\\')
1034 slashpos = text.rfind('\\')
1031 if slashpos > -1:
1035 if slashpos > -1:
1032 s = text[slashpos+1:]
1036 s = text[slashpos+1:]
1033 try :
1037 try :
1034 unic = unicodedata.lookup(s)
1038 unic = unicodedata.lookup(s)
1035 # allow combining chars
1039 # allow combining chars
1036 if ('a'+unic).isidentifier():
1040 if ('a'+unic).isidentifier():
1037 return '\\'+s,[unic]
1041 return '\\'+s,[unic]
1038 except KeyError as e:
1042 except KeyError as e:
1039 pass
1043 pass
1040 return u'', []
1044 return u'', []
1041
1045
1042
1046
1043
1047
1044
1048
1045 def latex_matches(self, text):
1049 def latex_matches(self, text):
1046 u"""Match Latex syntax for unicode characters.
1050 u"""Match Latex syntax for unicode characters.
1047
1051
1048 This does both \\alp -> \\alpha and \\alpha -> Ξ±
1052 This does both \\alp -> \\alpha and \\alpha -> Ξ±
1049
1053
1050 Used on Python 3 only.
1054 Used on Python 3 only.
1051 """
1055 """
1052 slashpos = text.rfind('\\')
1056 slashpos = text.rfind('\\')
1053 if slashpos > -1:
1057 if slashpos > -1:
1054 s = text[slashpos:]
1058 s = text[slashpos:]
1055 if s in latex_symbols:
1059 if s in latex_symbols:
1056 # Try to complete a full latex symbol to unicode
1060 # Try to complete a full latex symbol to unicode
1057 # \\alpha -> Ξ±
1061 # \\alpha -> Ξ±
1058 return s, [latex_symbols[s]]
1062 return s, [latex_symbols[s]]
1059 else:
1063 else:
1060 # If a user has partially typed a latex symbol, give them
1064 # If a user has partially typed a latex symbol, give them
1061 # a full list of options \al -> [\aleph, \alpha]
1065 # a full list of options \al -> [\aleph, \alpha]
1062 matches = [k for k in latex_symbols if k.startswith(s)]
1066 matches = [k for k in latex_symbols if k.startswith(s)]
1063 return s, matches
1067 return s, matches
1064 return u'', []
1068 return u'', []
1065
1069
1066 def dispatch_custom_completer(self, text):
1070 def dispatch_custom_completer(self, text):
1067 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
1071 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
1068 line = self.line_buffer
1072 line = self.line_buffer
1069 if not line.strip():
1073 if not line.strip():
1070 return None
1074 return None
1071
1075
1072 # Create a little structure to pass all the relevant information about
1076 # Create a little structure to pass all the relevant information about
1073 # the current completion to any custom completer.
1077 # the current completion to any custom completer.
1074 event = Bunch()
1078 event = Bunch()
1075 event.line = line
1079 event.line = line
1076 event.symbol = text
1080 event.symbol = text
1077 cmd = line.split(None,1)[0]
1081 cmd = line.split(None,1)[0]
1078 event.command = cmd
1082 event.command = cmd
1079 event.text_until_cursor = self.text_until_cursor
1083 event.text_until_cursor = self.text_until_cursor
1080
1084
1081 #print "\ncustom:{%s]\n" % event # dbg
1085 #print "\ncustom:{%s]\n" % event # dbg
1082
1086
1083 # for foo etc, try also to find completer for %foo
1087 # for foo etc, try also to find completer for %foo
1084 if not cmd.startswith(self.magic_escape):
1088 if not cmd.startswith(self.magic_escape):
1085 try_magic = self.custom_completers.s_matches(
1089 try_magic = self.custom_completers.s_matches(
1086 self.magic_escape + cmd)
1090 self.magic_escape + cmd)
1087 else:
1091 else:
1088 try_magic = []
1092 try_magic = []
1089
1093
1090 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1094 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1091 try_magic,
1095 try_magic,
1092 self.custom_completers.flat_matches(self.text_until_cursor)):
1096 self.custom_completers.flat_matches(self.text_until_cursor)):
1093 #print "try",c # dbg
1097 #print "try",c # dbg
1094 try:
1098 try:
1095 res = c(event)
1099 res = c(event)
1096 if res:
1100 if res:
1097 # first, try case sensitive match
1101 # first, try case sensitive match
1098 withcase = [r for r in res if r.startswith(text)]
1102 withcase = [r for r in res if r.startswith(text)]
1099 if withcase:
1103 if withcase:
1100 return withcase
1104 return withcase
1101 # if none, then case insensitive ones are ok too
1105 # if none, then case insensitive ones are ok too
1102 text_low = text.lower()
1106 text_low = text.lower()
1103 return [r for r in res if r.lower().startswith(text_low)]
1107 return [r for r in res if r.lower().startswith(text_low)]
1104 except TryNext:
1108 except TryNext:
1105 pass
1109 pass
1106
1110
1107 return None
1111 return None
1108
1112
1109 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1113 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1110 """Find completions for the given text and line context.
1114 """Find completions for the given text and line context.
1111
1115
1112 Note that both the text and the line_buffer are optional, but at least
1116 Note that both the text and the line_buffer are optional, but at least
1113 one of them must be given.
1117 one of them must be given.
1114
1118
1115 Parameters
1119 Parameters
1116 ----------
1120 ----------
1117 text : string, optional
1121 text : string, optional
1118 Text to perform the completion on. If not given, the line buffer
1122 Text to perform the completion on. If not given, the line buffer
1119 is split using the instance's CompletionSplitter object.
1123 is split using the instance's CompletionSplitter object.
1120
1124
1121 line_buffer : string, optional
1125 line_buffer : string, optional
1122 If not given, the completer attempts to obtain the current line
1126 If not given, the completer attempts to obtain the current line
1123 buffer via readline. This keyword allows clients which are
1127 buffer via readline. This keyword allows clients which are
1124 requesting for text completions in non-readline contexts to inform
1128 requesting for text completions in non-readline contexts to inform
1125 the completer of the entire text.
1129 the completer of the entire text.
1126
1130
1127 cursor_pos : int, optional
1131 cursor_pos : int, optional
1128 Index of the cursor in the full line buffer. Should be provided by
1132 Index of the cursor in the full line buffer. Should be provided by
1129 remote frontends where kernel has no access to frontend state.
1133 remote frontends where kernel has no access to frontend state.
1130
1134
1131 Returns
1135 Returns
1132 -------
1136 -------
1133 text : str
1137 text : str
1134 Text that was actually used in the completion.
1138 Text that was actually used in the completion.
1135
1139
1136 matches : list
1140 matches : list
1137 A list of completion matches.
1141 A list of completion matches.
1138 """
1142 """
1139 # io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1143 # io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1140
1144
1141 # if the cursor position isn't given, the only sane assumption we can
1145 # if the cursor position isn't given, the only sane assumption we can
1142 # make is that it's at the end of the line (the common case)
1146 # make is that it's at the end of the line (the common case)
1143 if cursor_pos is None:
1147 if cursor_pos is None:
1144 cursor_pos = len(line_buffer) if text is None else len(text)
1148 cursor_pos = len(line_buffer) if text is None else len(text)
1145
1149
1146 if PY3:
1150 if PY3:
1147
1151
1148 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1152 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1149 latex_text, latex_matches = self.latex_matches(base_text)
1153 latex_text, latex_matches = self.latex_matches(base_text)
1150 if latex_matches:
1154 if latex_matches:
1151 return latex_text, latex_matches
1155 return latex_text, latex_matches
1152 name_text = ''
1156 name_text = ''
1153 name_matches = []
1157 name_matches = []
1154 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1158 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1155 name_text, name_matches = meth(base_text)
1159 name_text, name_matches = meth(base_text)
1156 if name_text:
1160 if name_text:
1157 return name_text, name_matches
1161 return name_text, name_matches
1158
1162
1159 # if text is either None or an empty string, rely on the line buffer
1163 # if text is either None or an empty string, rely on the line buffer
1160 if not text:
1164 if not text:
1161 text = self.splitter.split_line(line_buffer, cursor_pos)
1165 text = self.splitter.split_line(line_buffer, cursor_pos)
1162
1166
1163 # If no line buffer is given, assume the input text is all there was
1167 # If no line buffer is given, assume the input text is all there was
1164 if line_buffer is None:
1168 if line_buffer is None:
1165 line_buffer = text
1169 line_buffer = text
1166
1170
1167 self.line_buffer = line_buffer
1171 self.line_buffer = line_buffer
1168 self.text_until_cursor = self.line_buffer[:cursor_pos]
1172 self.text_until_cursor = self.line_buffer[:cursor_pos]
1169 # io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1173 # io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1170
1174
1171 # Start with a clean slate of completions
1175 # Start with a clean slate of completions
1172 self.matches[:] = []
1176 self.matches[:] = []
1173 custom_res = self.dispatch_custom_completer(text)
1177 custom_res = self.dispatch_custom_completer(text)
1174 if custom_res is not None:
1178 if custom_res is not None:
1175 # did custom completers produce something?
1179 # did custom completers produce something?
1176 self.matches = custom_res
1180 self.matches = custom_res
1177 else:
1181 else:
1178 # Extend the list of completions with the results of each
1182 # Extend the list of completions with the results of each
1179 # matcher, so we return results to the user from all
1183 # matcher, so we return results to the user from all
1180 # namespaces.
1184 # namespaces.
1181 if self.merge_completions:
1185 if self.merge_completions:
1182 self.matches = []
1186 self.matches = []
1183 for matcher in self.matchers:
1187 for matcher in self.matchers:
1184 try:
1188 try:
1185 self.matches.extend(matcher(text))
1189 self.matches.extend(matcher(text))
1186 except:
1190 except:
1187 # Show the ugly traceback if the matcher causes an
1191 # Show the ugly traceback if the matcher causes an
1188 # exception, but do NOT crash the kernel!
1192 # exception, but do NOT crash the kernel!
1189 sys.excepthook(*sys.exc_info())
1193 sys.excepthook(*sys.exc_info())
1190 else:
1194 else:
1191 for matcher in self.matchers:
1195 for matcher in self.matchers:
1192 self.matches = matcher(text)
1196 self.matches = matcher(text)
1193 if self.matches:
1197 if self.matches:
1194 break
1198 break
1195 # FIXME: we should extend our api to return a dict with completions for
1199 # FIXME: we should extend our api to return a dict with completions for
1196 # different types of objects. The rlcomplete() method could then
1200 # different types of objects. The rlcomplete() method could then
1197 # simply collapse the dict into a list for readline, but we'd have
1201 # simply collapse the dict into a list for readline, but we'd have
1198 # richer completion semantics in other evironments.
1202 # richer completion semantics in other evironments.
1199
1203
1200 # use penalize_magics_key to put magics after variables with same name
1204 # use penalize_magics_key to put magics after variables with same name
1201 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1205 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1202
1206
1203 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1207 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1204 return text, self.matches
1208 return text, self.matches
1205
1209
1206 def rlcomplete(self, text, state):
1210 def rlcomplete(self, text, state):
1207 """Return the state-th possible completion for 'text'.
1211 """Return the state-th possible completion for 'text'.
1208
1212
1209 This is called successively with state == 0, 1, 2, ... until it
1213 This is called successively with state == 0, 1, 2, ... until it
1210 returns None. The completion should begin with 'text'.
1214 returns None. The completion should begin with 'text'.
1211
1215
1212 Parameters
1216 Parameters
1213 ----------
1217 ----------
1214 text : string
1218 text : string
1215 Text to perform the completion on.
1219 Text to perform the completion on.
1216
1220
1217 state : int
1221 state : int
1218 Counter used by readline.
1222 Counter used by readline.
1219 """
1223 """
1220 if state==0:
1224 if state==0:
1221
1225
1222 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1226 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1223 cursor_pos = self.readline.get_endidx()
1227 cursor_pos = self.readline.get_endidx()
1224
1228
1225 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1229 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1226 # (text, line_buffer, cursor_pos) ) # dbg
1230 # (text, line_buffer, cursor_pos) ) # dbg
1227
1231
1228 # if there is only a tab on a line with only whitespace, instead of
1232 # if there is only a tab on a line with only whitespace, instead of
1229 # the mostly useless 'do you want to see all million completions'
1233 # the mostly useless 'do you want to see all million completions'
1230 # message, just do the right thing and give the user his tab!
1234 # message, just do the right thing and give the user his tab!
1231 # Incidentally, this enables pasting of tabbed text from an editor
1235 # Incidentally, this enables pasting of tabbed text from an editor
1232 # (as long as autoindent is off).
1236 # (as long as autoindent is off).
1233
1237
1234 # It should be noted that at least pyreadline still shows file
1238 # It should be noted that at least pyreadline still shows file
1235 # completions - is there a way around it?
1239 # completions - is there a way around it?
1236
1240
1237 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1241 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1238 # we don't interfere with their own tab-completion mechanism.
1242 # we don't interfere with their own tab-completion mechanism.
1239 if not (self.dumb_terminal or line_buffer.strip()):
1243 if not (self.dumb_terminal or line_buffer.strip()):
1240 self.readline.insert_text('\t')
1244 self.readline.insert_text('\t')
1241 sys.stdout.flush()
1245 sys.stdout.flush()
1242 return None
1246 return None
1243
1247
1244 # Note: debugging exceptions that may occur in completion is very
1248 # Note: debugging exceptions that may occur in completion is very
1245 # tricky, because readline unconditionally silences them. So if
1249 # tricky, because readline unconditionally silences them. So if
1246 # during development you suspect a bug in the completion code, turn
1250 # during development you suspect a bug in the completion code, turn
1247 # this flag on temporarily by uncommenting the second form (don't
1251 # this flag on temporarily by uncommenting the second form (don't
1248 # flip the value in the first line, as the '# dbg' marker can be
1252 # flip the value in the first line, as the '# dbg' marker can be
1249 # automatically detected and is used elsewhere).
1253 # automatically detected and is used elsewhere).
1250 DEBUG = False
1254 DEBUG = False
1251 #DEBUG = True # dbg
1255 #DEBUG = True # dbg
1252 if DEBUG:
1256 if DEBUG:
1253 try:
1257 try:
1254 self.complete(text, line_buffer, cursor_pos)
1258 self.complete(text, line_buffer, cursor_pos)
1255 except:
1259 except:
1256 import traceback; traceback.print_exc()
1260 import traceback; traceback.print_exc()
1257 else:
1261 else:
1258 # The normal production version is here
1262 # The normal production version is here
1259
1263
1260 # This method computes the self.matches array
1264 # This method computes the self.matches array
1261 self.complete(text, line_buffer, cursor_pos)
1265 self.complete(text, line_buffer, cursor_pos)
1262
1266
1263 try:
1267 try:
1264 return self.matches[state]
1268 return self.matches[state]
1265 except IndexError:
1269 except IndexError:
1266 return None
1270 return None
1267
1271
General Comments 0
You need to be logged in to leave comments. Login now