##// END OF EJS Templates
Fix bug in tab completion of filenames when quotes are present....
Fernando Perez -
Show More
@@ -1,849 +1,880 b''
1 1 """Word completion for IPython.
2 2
3 3 This module is a fork of the rlcompleter module in the Python standard
4 4 library. The original enhancements made to rlcompleter have been sent
5 5 upstream and were accepted as of Python 2.3, but we need a lot more
6 6 functionality specific to IPython, so this module will continue to live as an
7 7 IPython-specific utility.
8 8
9 9 Original rlcompleter documentation:
10 10
11 11 This requires the latest extension to the readline module (the
12 12 completes keywords, built-ins and globals in __main__; when completing
13 13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 14 completes its attributes.
15 15
16 16 It's very cool to do "import string" type "string.", hit the
17 17 completion key (twice), and see the list of names defined by the
18 18 string module!
19 19
20 20 Tip: to use the tab key as the completion key, call
21 21
22 22 readline.parse_and_bind("tab: complete")
23 23
24 24 Notes:
25 25
26 26 - Exceptions raised by the completer function are *ignored* (and
27 27 generally cause the completion to fail). This is a feature -- since
28 28 readline sets the tty device in raw (or cbreak) mode, printing a
29 29 traceback wouldn't work well without some complicated hoopla to save,
30 30 reset and restore the tty state.
31 31
32 32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 33 application defined code to be executed if an object with a
34 34 __getattr__ hook is found. Since it is the responsibility of the
35 35 application (or the user) to enable this feature, I consider this an
36 36 acceptable risk. More complicated expressions (e.g. function calls or
37 37 indexing operations) are *not* evaluated.
38 38
39 39 - GNU readline is also used by the built-in functions input() and
40 40 raw_input(), and thus these also benefit/suffer from the completer
41 41 features. Clearly an interactive application can benefit by
42 42 specifying its own completer function and using raw_input() for all
43 43 its input.
44 44
45 45 - When the original stdin is not a tty device, GNU readline is never
46 46 used, and this module (and the readline module) are silently inactive.
47 47 """
48 48
49 49 #*****************************************************************************
50 50 #
51 51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 52 # module which is part of the standard Python distribution, I assume that the
53 53 # proper procedure is to maintain its copyright as belonging to the Python
54 54 # Software Foundation (in addition to my own, for all new code).
55 55 #
56 56 # Copyright (C) 2008-2010 IPython Development Team
57 57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 59 #
60 60 # Distributed under the terms of the BSD License. The full license is in
61 61 # the file COPYING, distributed as part of this software.
62 62 #
63 63 #*****************************************************************************
64 64 from __future__ import print_function
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Imports
68 68 #-----------------------------------------------------------------------------
69 69
70 70 import __builtin__
71 71 import __main__
72 72 import glob
73 73 import inspect
74 74 import itertools
75 75 import keyword
76 76 import os
77 77 import re
78 78 import shlex
79 79 import sys
80 80
81 81 from IPython.core.error import TryNext
82 82 from IPython.core.prefilter import ESC_MAGIC
83 83 from IPython.utils import generics
84 84 from IPython.utils import io
85 85 from IPython.utils.dir2 import dir2
86 86 from IPython.utils.process import arg_split
87 87
88 88 #-----------------------------------------------------------------------------
89 89 # Globals
90 90 #-----------------------------------------------------------------------------
91 91
92 92 # Public API
93 93 __all__ = ['Completer','IPCompleter']
94 94
95 95 if sys.platform == 'win32':
96 96 PROTECTABLES = ' '
97 97 else:
98 98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
99 99
100 100 #-----------------------------------------------------------------------------
101 101 # Main functions and classes
102 102 #-----------------------------------------------------------------------------
103 103
104 def has_open_quotes(s):
105 """Return whether a string has open quotes.
106
107 This simply counts whether the number of quote characters of either type in
108 the string is odd.
109
110 Returns
111 -------
112 If there is an open quote, the quote character is returned. Else, return
113 False.
114 """
115 # We check " first, then ', so complex cases with nested quotes will get
116 # the " to take precedence.
117 if s.count('"') % 2:
118 return '"'
119 elif s.count("'") % 2:
120 return "'"
121 else:
122 return False
123
124
104 125 def protect_filename(s):
105 126 """Escape a string to protect certain characters."""
106 127
107 128 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
108 129 for ch in s])
109 130
110 131
111 132 def mark_dirs(matches):
112 133 """Mark directories in input list by appending '/' to their names."""
113 134 out = []
114 135 isdir = os.path.isdir
115 136 for x in matches:
116 137 if isdir(x):
117 138 out.append(x+'/')
118 139 else:
119 140 out.append(x)
120 141 return out
121 142
122 143
123 144 def expand_user(path):
124 145 """Expand '~'-style usernames in strings.
125 146
126 147 This is similar to :func:`os.path.expanduser`, but it computes and returns
127 148 extra information that will be useful if the input was being used in
128 149 computing completions, and you wish to return the completions with the
129 150 original '~' instead of its expanded value.
130 151
131 152 Parameters
132 153 ----------
133 154 path : str
134 155 String to be expanded. If no ~ is present, the output is the same as the
135 156 input.
136 157
137 158 Returns
138 159 -------
139 160 newpath : str
140 161 Result of ~ expansion in the input path.
141 162 tilde_expand : bool
142 163 Whether any expansion was performed or not.
143 164 tilde_val : str
144 165 The value that ~ was replaced with.
145 166 """
146 167 # Default values
147 168 tilde_expand = False
148 169 tilde_val = ''
149 170 newpath = path
150 171
151 172 if path.startswith('~'):
152 173 tilde_expand = True
153 174 rest = path[1:]
154 175 newpath = os.path.expanduser(path)
155 176 tilde_val = newpath.replace(rest, '')
156 177
157 178 return newpath, tilde_expand, tilde_val
158 179
159 180
160 181 def compress_user(path, tilde_expand, tilde_val):
161 182 """Does the opposite of expand_user, with its outputs.
162 183 """
163 184 if tilde_expand:
164 185 return path.replace(tilde_val, '~')
165 186 else:
166 187 return path
167 188
168 189
169 190 def single_dir_expand(matches):
170 191 "Recursively expand match lists containing a single dir."
171 192
172 193 if len(matches) == 1 and os.path.isdir(matches[0]):
173 194 # Takes care of links to directories also. Use '/'
174 195 # explicitly, even under Windows, so that name completions
175 196 # don't end up escaped.
176 197 d = matches[0]
177 198 if d[-1] in ['/','\\']:
178 199 d = d[:-1]
179 200
180 201 subdirs = os.listdir(d)
181 202 if subdirs:
182 203 matches = [ (d + '/' + p) for p in subdirs]
183 204 return single_dir_expand(matches)
184 205 else:
185 206 return matches
186 207 else:
187 208 return matches
188 209
189 210
190 211 class Bunch(object): pass
191 212
192 213
193 214 class CompletionSplitter(object):
194 215 """An object to split an input line in a manner similar to readline.
195 216
196 217 By having our own implementation, we can expose readline-like completion in
197 218 a uniform manner to all frontends. This object only needs to be given the
198 219 line of text to be split and the cursor position on said line, and it
199 220 returns the 'word' to be completed on at the cursor after splitting the
200 221 entire line.
201 222
202 223 What characters are used as splitting delimiters can be controlled by
203 224 setting the `delims` attribute (this is a property that internally
204 225 automatically builds the necessary """
205 226
206 227 # Private interface
207 228
208 229 # A string of delimiter characters. The default value makes sense for
209 230 # IPython's most typical usage patterns.
210 231 _delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
211 232
212 233 # The expression (a normal string) to be compiled into a regular expression
213 234 # for actual splitting. We store it as an attribute mostly for ease of
214 235 # debugging, since this type of code can be so tricky to debug.
215 236 _delim_expr = None
216 237
217 238 # The regular expression that does the actual splitting
218 239 _delim_re = None
219 240
220 241 def __init__(self, delims=None):
221 242 delims = CompletionSplitter._delims if delims is None else delims
222 243 self.set_delims(delims)
223 244
224 245 def set_delims(self, delims):
225 246 """Set the delimiters for line splitting."""
226 247 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
227 248 self._delim_re = re.compile(expr)
228 249 self._delims = delims
229 250 self._delim_expr = expr
230 251
231 252 def get_delims(self):
232 253 """Return the string of delimiter characters."""
233 254 return self._delims
234 255
235 256 def split_line(self, line, cursor_pos=None):
236 257 """Split a line of text with a cursor at the given position.
237 258 """
238 259 l = line if cursor_pos is None else line[:cursor_pos]
239 260 return self._delim_re.split(l)[-1]
240 261
241 262
242 263 class Completer(object):
243 264 def __init__(self, namespace=None, global_namespace=None):
244 265 """Create a new completer for the command line.
245 266
246 267 Completer([namespace,global_namespace]) -> completer instance.
247 268
248 269 If unspecified, the default namespace where completions are performed
249 270 is __main__ (technically, __main__.__dict__). Namespaces should be
250 271 given as dictionaries.
251 272
252 273 An optional second namespace can be given. This allows the completer
253 274 to handle cases where both the local and global scopes need to be
254 275 distinguished.
255 276
256 277 Completer instances should be used as the completion mechanism of
257 278 readline via the set_completer() call:
258 279
259 280 readline.set_completer(Completer(my_namespace).complete)
260 281 """
261 282
262 283 # Don't bind to namespace quite yet, but flag whether the user wants a
263 284 # specific namespace or to use __main__.__dict__. This will allow us
264 285 # to bind to __main__.__dict__ at completion time, not now.
265 286 if namespace is None:
266 287 self.use_main_ns = 1
267 288 else:
268 289 self.use_main_ns = 0
269 290 self.namespace = namespace
270 291
271 292 # The global namespace, if given, can be bound directly
272 293 if global_namespace is None:
273 294 self.global_namespace = {}
274 295 else:
275 296 self.global_namespace = global_namespace
276 297
277 298 def complete(self, text, state):
278 299 """Return the next possible completion for 'text'.
279 300
280 301 This is called successively with state == 0, 1, 2, ... until it
281 302 returns None. The completion should begin with 'text'.
282 303
283 304 """
284 305 if self.use_main_ns:
285 306 self.namespace = __main__.__dict__
286 307
287 308 if state == 0:
288 309 if "." in text:
289 310 self.matches = self.attr_matches(text)
290 311 else:
291 312 self.matches = self.global_matches(text)
292 313 try:
293 314 return self.matches[state]
294 315 except IndexError:
295 316 return None
296 317
297 318 def global_matches(self, text):
298 319 """Compute matches when text is a simple name.
299 320
300 321 Return a list of all keywords, built-in functions and names currently
301 322 defined in self.namespace or self.global_namespace that match.
302 323
303 324 """
304 325 #print 'Completer->global_matches, txt=%r' % text # dbg
305 326 matches = []
306 327 match_append = matches.append
307 328 n = len(text)
308 329 for lst in [keyword.kwlist,
309 330 __builtin__.__dict__.keys(),
310 331 self.namespace.keys(),
311 332 self.global_namespace.keys()]:
312 333 for word in lst:
313 334 if word[:n] == text and word != "__builtins__":
314 335 match_append(word)
315 336 return matches
316 337
317 338 def attr_matches(self, text):
318 339 """Compute matches when text contains a dot.
319 340
320 341 Assuming the text is of the form NAME.NAME....[NAME], and is
321 342 evaluatable in self.namespace or self.global_namespace, it will be
322 343 evaluated and its attributes (as revealed by dir()) are used as
323 344 possible completions. (For class instances, class members are are
324 345 also considered.)
325 346
326 347 WARNING: this can still invoke arbitrary C code, if an object
327 348 with a __getattr__ hook is evaluated.
328 349
329 350 """
330 351
331 352 #print 'Completer->attr_matches, txt=%r' % text # dbg
332 353 # Another option, seems to work great. Catches things like ''.<tab>
333 354 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
334 355
335 356 if not m:
336 357 return []
337 358
338 359 expr, attr = m.group(1, 3)
339 360 try:
340 361 obj = eval(expr, self.namespace)
341 362 except:
342 363 try:
343 364 obj = eval(expr, self.global_namespace)
344 365 except:
345 366 return []
346 367
347 368 words = dir2(obj)
348 369
349 370 try:
350 371 words = generics.complete_object(obj, words)
351 372 except TryNext:
352 373 pass
353 374 # Build match list to return
354 375 n = len(attr)
355 376 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
356 377 return res
357 378
358 379
359 380 class IPCompleter(Completer):
360 381 """Extension of the completer class with IPython-specific features"""
361 382
362 383 def __init__(self, shell, namespace=None, global_namespace=None,
363 384 omit__names=0, alias_table=None, use_readline=True):
364 385 """IPCompleter() -> completer
365 386
366 387 Return a completer object suitable for use by the readline library
367 388 via readline.set_completer().
368 389
369 390 Inputs:
370 391
371 392 - shell: a pointer to the ipython shell itself. This is needed
372 393 because this completer knows about magic functions, and those can
373 394 only be accessed via the ipython instance.
374 395
375 396 - namespace: an optional dict where completions are performed.
376 397
377 398 - global_namespace: secondary optional dict for completions, to
378 399 handle cases (such as IPython embedded inside functions) where
379 400 both Python scopes are visible.
380 401
381 402 - The optional omit__names parameter sets the completer to omit the
382 403 'magic' names (__magicname__) for python objects unless the text
383 404 to be completed explicitly starts with one or more underscores.
384 405
385 406 - If alias_table is supplied, it should be a dictionary of aliases
386 407 to complete.
387 408
388 409 use_readline : bool, optional
389 410 If true, use the readline library. This completer can still function
390 411 without readline, though in that case callers must provide some extra
391 412 information on each call about the current line."""
392 413
393 414 Completer.__init__(self, namespace, global_namespace)
394 415
395 416 self.magic_escape = ESC_MAGIC
396 417 self.splitter = CompletionSplitter()
397 418
398 419 # Readline configuration, only used by the rlcompleter method.
399 420 if use_readline:
400 421 # We store the right version of readline so that later code
401 422 import IPython.utils.rlineimpl as readline
402 423 self.readline = readline
403 424 else:
404 425 self.readline = None
405 426
406 427 # List where completion matches will be stored
407 428 self.matches = []
408 429 self.omit__names = omit__names
409 430 self.merge_completions = shell.readline_merge_completions
410 431 self.shell = shell.shell
411 432 if alias_table is None:
412 433 alias_table = {}
413 434 self.alias_table = alias_table
414 435 # Regexp to split filenames with spaces in them
415 436 self.space_name_re = re.compile(r'([^\\] )')
416 437 # Hold a local ref. to glob.glob for speed
417 438 self.glob = glob.glob
418 439
419 440 # Determine if we are running on 'dumb' terminals, like (X)Emacs
420 441 # buffers, to avoid completion problems.
421 442 term = os.environ.get('TERM','xterm')
422 443 self.dumb_terminal = term in ['dumb','emacs']
423 444
424 445 # Special handling of backslashes needed in win32 platforms
425 446 if sys.platform == "win32":
426 447 self.clean_glob = self._clean_glob_win32
427 448 else:
428 449 self.clean_glob = self._clean_glob
429 450
430 451 # All active matcher routines for completion
431 452 self.matchers = [self.python_matches,
432 453 self.file_matches,
433 454 self.magic_matches,
434 455 self.alias_matches,
435 456 self.python_func_kw_matches,
436 457 ]
437 458
438 459 # Code contributed by Alex Schmolck, for ipython/emacs integration
439 460 def all_completions(self, text):
440 461 """Return all possible completions for the benefit of emacs."""
441 462
442 463 completions = []
443 464 comp_append = completions.append
444 465 try:
445 466 for i in xrange(sys.maxint):
446 467 res = self.complete(text, i, text)
447 468 if not res:
448 469 break
449 470 comp_append(res)
450 471 #XXX workaround for ``notDefined.<tab>``
451 472 except NameError:
452 473 pass
453 474 return completions
454 475 # /end Alex Schmolck code.
455 476
456 477 def _clean_glob(self,text):
457 478 return self.glob("%s*" % text)
458 479
459 480 def _clean_glob_win32(self,text):
460 481 return [f.replace("\\","/")
461 482 for f in self.glob("%s*" % text)]
462 483
463 484 def file_matches(self, text):
464 485 """Match filenames, expanding ~USER type strings.
465 486
466 487 Most of the seemingly convoluted logic in this completer is an
467 488 attempt to handle filenames with spaces in them. And yet it's not
468 489 quite perfect, because Python's readline doesn't expose all of the
469 490 GNU readline details needed for this to be done correctly.
470 491
471 492 For a filename with a space in it, the printed completions will be
472 493 only the parts after what's already been typed (instead of the
473 494 full completions, as is normally done). I don't think with the
474 495 current (as of Python 2.3) Python readline it's possible to do
475 496 better."""
476 497
477 498 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
478 499
479 500 # chars that require escaping with backslash - i.e. chars
480 501 # that readline treats incorrectly as delimiters, but we
481 502 # don't want to treat as delimiters in filename matching
482 503 # when escaped with backslash
483 504 if text.startswith('!'):
484 505 text = text[1:]
485 506 text_prefix = '!'
486 507 else:
487 508 text_prefix = ''
488
509
489 510 text_until_cursor = self.text_until_cursor
490 open_quotes = 0 # track strings with open quotes
491 try:
492 # arg_split ~ shlex.split, but with unicode bugs fixed by us
493 lsplit = arg_split(text_until_cursor)[-1]
494 except ValueError:
495 # typically an unmatched ", or backslash without escaped char.
496 if text_until_cursor.count('"')==1:
497 open_quotes = 1
498 lsplit = text_until_cursor.split('"')[-1]
499 elif text_until_cursor.count("'")==1:
500 open_quotes = 1
501 lsplit = text_until_cursor.split("'")[-1]
502 else:
503 return []
504 except IndexError:
505 # tab pressed on empty line
506 lsplit = ""
511 # track strings with open quotes
512 open_quotes = has_open_quotes(text_until_cursor)
513
514 if '(' in text_until_cursor or '[' in text_until_cursor:
515 lsplit = text
516 else:
517 try:
518 # arg_split ~ shlex.split, but with unicode bugs fixed by us
519 lsplit = arg_split(text_until_cursor)[-1]
520 except ValueError:
521 # typically an unmatched ", or backslash without escaped char.
522 if open_quotes:
523 lsplit = text_until_cursor.split(open_quotes)[-1]
524 else:
525 return []
526 except IndexError:
527 # tab pressed on empty line
528 lsplit = ""
507 529
508 530 if not open_quotes and lsplit != protect_filename(lsplit):
509 # if protectables are found, do matching on the whole escaped
510 # name
511 has_protectables = 1
531 # if protectables are found, do matching on the whole escaped name
532 has_protectables = True
512 533 text0,text = text,lsplit
513 534 else:
514 has_protectables = 0
535 has_protectables = False
515 536 text = os.path.expanduser(text)
516 537
517 538 if text == "":
518 539 return [text_prefix + protect_filename(f) for f in self.glob("*")]
519 540
541 # Compute the matches from the filesystem
520 542 m0 = self.clean_glob(text.replace('\\',''))
543
521 544 if has_protectables:
522 545 # If we had protectables, we need to revert our changes to the
523 546 # beginning of filename so that we don't double-write the part
524 547 # of the filename we have so far
525 548 len_lsplit = len(lsplit)
526 549 matches = [text_prefix + text0 +
527 550 protect_filename(f[len_lsplit:]) for f in m0]
528 551 else:
529 552 if open_quotes:
530 553 # if we have a string with an open quote, we don't need to
531 554 # protect the names at all (and we _shouldn't_, as it
532 555 # would cause bugs when the filesystem call is made).
533 556 matches = m0
534 557 else:
535 558 matches = [text_prefix +
536 559 protect_filename(f) for f in m0]
537 560
538 561 #io.rprint('mm', matches) # dbg
539 562 return mark_dirs(matches)
540 563
541 564 def magic_matches(self, text):
542 565 """Match magics"""
543 566 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
544 567 # Get all shell magics now rather than statically, so magics loaded at
545 568 # runtime show up too
546 569 magics = self.shell.lsmagic()
547 570 pre = self.magic_escape
548 571 baretext = text.lstrip(pre)
549 572 return [ pre+m for m in magics if m.startswith(baretext)]
550 573
551 574 def alias_matches(self, text):
552 575 """Match internal system aliases"""
553 576 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
554 577
555 578 # if we are not in the first 'item', alias matching
556 579 # doesn't make sense - unless we are starting with 'sudo' command.
557 580 main_text = self.text_until_cursor.lstrip()
558 581 if ' ' in main_text and not main_text.startswith('sudo'):
559 582 return []
560 583 text = os.path.expanduser(text)
561 584 aliases = self.alias_table.keys()
562 585 if text == '':
563 586 return aliases
564 587 else:
565 588 return [a for a in aliases if a.startswith(text)]
566 589
567 590 def python_matches(self,text):
568 591 """Match attributes or global python names"""
569 592
570 593 #print 'Completer->python_matches, txt=%r' % text # dbg
571 594 if "." in text:
572 595 try:
573 596 matches = self.attr_matches(text)
574 597 if text.endswith('.') and self.omit__names:
575 598 if self.omit__names == 1:
576 599 # true if txt is _not_ a __ name, false otherwise:
577 600 no__name = (lambda txt:
578 601 re.match(r'.*\.__.*?__',txt) is None)
579 602 else:
580 603 # true if txt is _not_ a _ name, false otherwise:
581 604 no__name = (lambda txt:
582 605 re.match(r'.*\._.*?',txt) is None)
583 606 matches = filter(no__name, matches)
584 607 except NameError:
585 608 # catches <undefined attributes>.<tab>
586 609 matches = []
587 610 else:
588 611 matches = self.global_matches(text)
589 612
590 613 return matches
591 614
592 615 def _default_arguments(self, obj):
593 616 """Return the list of default arguments of obj if it is callable,
594 617 or empty list otherwise."""
595 618
596 619 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
597 620 # for classes, check for __init__,__new__
598 621 if inspect.isclass(obj):
599 622 obj = (getattr(obj,'__init__',None) or
600 623 getattr(obj,'__new__',None))
601 624 # for all others, check if they are __call__able
602 625 elif hasattr(obj, '__call__'):
603 626 obj = obj.__call__
604 627 # XXX: is there a way to handle the builtins ?
605 628 try:
606 629 args,_,_1,defaults = inspect.getargspec(obj)
607 630 if defaults:
608 631 return args[-len(defaults):]
609 632 except TypeError: pass
610 633 return []
611 634
612 635 def python_func_kw_matches(self,text):
613 636 """Match named parameters (kwargs) of the last open function"""
614 637
615 638 if "." in text: # a parameter cannot be dotted
616 639 return []
617 640 try: regexp = self.__funcParamsRegex
618 641 except AttributeError:
619 642 regexp = self.__funcParamsRegex = re.compile(r'''
620 643 '.*?' | # single quoted strings or
621 644 ".*?" | # double quoted strings or
622 645 \w+ | # identifier
623 646 \S # other characters
624 647 ''', re.VERBOSE | re.DOTALL)
625 648 # 1. find the nearest identifier that comes before an unclosed
626 649 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
627 650 tokens = regexp.findall(self.line_buffer)
628 651 tokens.reverse()
629 652 iterTokens = iter(tokens); openPar = 0
630 653 for token in iterTokens:
631 654 if token == ')':
632 655 openPar -= 1
633 656 elif token == '(':
634 657 openPar += 1
635 658 if openPar > 0:
636 659 # found the last unclosed parenthesis
637 660 break
638 661 else:
639 662 return []
640 663 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
641 664 ids = []
642 665 isId = re.compile(r'\w+$').match
643 666 while True:
644 667 try:
645 668 ids.append(iterTokens.next())
646 669 if not isId(ids[-1]):
647 670 ids.pop(); break
648 671 if not iterTokens.next() == '.':
649 672 break
650 673 except StopIteration:
651 674 break
652 675 # lookup the candidate callable matches either using global_matches
653 676 # or attr_matches for dotted names
654 677 if len(ids) == 1:
655 678 callableMatches = self.global_matches(ids[0])
656 679 else:
657 680 callableMatches = self.attr_matches('.'.join(ids[::-1]))
658 681 argMatches = []
659 682 for callableMatch in callableMatches:
660 683 try:
661 684 namedArgs = self._default_arguments(eval(callableMatch,
662 685 self.namespace))
663 686 except:
664 687 continue
665 688 for namedArg in namedArgs:
666 689 if namedArg.startswith(text):
667 690 argMatches.append("%s=" %namedArg)
668 691 return argMatches
669 692
670 693 def dispatch_custom_completer(self, text):
671 694 #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
672 695 line = self.line_buffer
673 696 if not line.strip():
674 697 return None
675 698
676 699 # Create a little structure to pass all the relevant information about
677 700 # the current completion to any custom completer.
678 701 event = Bunch()
679 702 event.line = line
680 703 event.symbol = text
681 704 cmd = line.split(None,1)[0]
682 705 event.command = cmd
683 706 event.text_until_cursor = self.text_until_cursor
684 707
685 708 #print "\ncustom:{%s]\n" % event # dbg
686 709
687 710 # for foo etc, try also to find completer for %foo
688 711 if not cmd.startswith(self.magic_escape):
689 712 try_magic = self.custom_completers.s_matches(
690 713 self.magic_escape + cmd)
691 714 else:
692 715 try_magic = []
693 716
694 717 for c in itertools.chain(self.custom_completers.s_matches(cmd),
695 718 try_magic,
696 719 self.custom_completers.flat_matches(self.text_until_cursor)):
697 720 #print "try",c # dbg
698 721 try:
699 722 res = c(event)
700 723 if res:
701 724 # first, try case sensitive match
702 725 withcase = [r for r in res if r.startswith(text)]
703 726 if withcase:
704 727 return withcase
705 728 # if none, then case insensitive ones are ok too
706 729 text_low = text.lower()
707 730 return [r for r in res if r.lower().startswith(text_low)]
708 731 except TryNext:
709 732 pass
710 733
711 734 return None
712 735
713 736 def complete(self, text=None, line_buffer=None, cursor_pos=None):
714 """Return the state-th possible completion for 'text'.
737 """Find completions for the given text and line context.
715 738
716 739 This is called successively with state == 0, 1, 2, ... until it
717 740 returns None. The completion should begin with 'text'.
718 741
719 742 Note that both the text and the line_buffer are optional, but at least
720 743 one of them must be given.
721 744
722 745 Parameters
723 746 ----------
724 747 text : string, optional
725 748 Text to perform the completion on. If not given, the line buffer
726 749 is split using the instance's CompletionSplitter object.
727 750
728 751 line_buffer : string, optional
729 752 If not given, the completer attempts to obtain the current line
730 753 buffer via readline. This keyword allows clients which are
731 754 requesting for text completions in non-readline contexts to inform
732 755 the completer of the entire text.
733 756
734 757 cursor_pos : int, optional
735 758 Index of the cursor in the full line buffer. Should be provided by
736 759 remote frontends where kernel has no access to frontend state.
760
761 Returns
762 -------
763 text : str
764 Text that was actually used in the completion.
765
766 matches : list
767 A list of completion matches.
737 768 """
738 769 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
739 770
740 771 # if the cursor position isn't given, the only sane assumption we can
741 772 # make is that it's at the end of the line (the common case)
742 773 if cursor_pos is None:
743 774 cursor_pos = len(line_buffer) if text is None else len(text)
744 775
745 776 # if text is either None or an empty string, rely on the line buffer
746 777 if not text:
747 778 text = self.splitter.split_line(line_buffer, cursor_pos)
748 779
749 780 # If no line buffer is given, assume the input text is all there was
750 781 if line_buffer is None:
751 782 line_buffer = text
752 783
753 784 self.line_buffer = line_buffer
754 785 self.text_until_cursor = self.line_buffer[:cursor_pos]
755 786 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
756 787
757 788 # Start with a clean slate of completions
758 789 self.matches[:] = []
759 790 custom_res = self.dispatch_custom_completer(text)
760 791 if custom_res is not None:
761 792 # did custom completers produce something?
762 793 self.matches = custom_res
763 794 else:
764 795 # Extend the list of completions with the results of each
765 796 # matcher, so we return results to the user from all
766 797 # namespaces.
767 798 if self.merge_completions:
768 799 self.matches = []
769 800 for matcher in self.matchers:
770 801 try:
771 802 self.matches.extend(matcher(text))
772 803 except:
773 804 # Show the ugly traceback if the matcher causes an
774 805 # exception, but do NOT crash the kernel!
775 sys.excepthook()
806 sys.excepthook(*sys.exc_info())
776 807 else:
777 808 for matcher in self.matchers:
778 809 self.matches = matcher(text)
779 810 if self.matches:
780 811 break
781 812 # FIXME: we should extend our api to return a dict with completions for
782 813 # different types of objects. The rlcomplete() method could then
783 814 # simply collapse the dict into a list for readline, but we'd have
784 815 # richer completion semantics in other evironments.
785 816 self.matches = sorted(set(self.matches))
786 817 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
787 818 return text, self.matches
788 819
789 820 def rlcomplete(self, text, state):
790 821 """Return the state-th possible completion for 'text'.
791 822
792 823 This is called successively with state == 0, 1, 2, ... until it
793 824 returns None. The completion should begin with 'text'.
794 825
795 826 Parameters
796 827 ----------
797 828 text : string
798 829 Text to perform the completion on.
799 830
800 831 state : int
801 832 Counter used by readline.
802 833 """
803 834 if state==0:
804 835
805 836 self.line_buffer = line_buffer = self.readline.get_line_buffer()
806 837 cursor_pos = self.readline.get_endidx()
807 838
808 839 #io.rprint("\nRLCOMPLETE: %r %r %r" %
809 840 # (text, line_buffer, cursor_pos) ) # dbg
810 841
811 842 # if there is only a tab on a line with only whitespace, instead of
812 843 # the mostly useless 'do you want to see all million completions'
813 844 # message, just do the right thing and give the user his tab!
814 845 # Incidentally, this enables pasting of tabbed text from an editor
815 846 # (as long as autoindent is off).
816 847
817 848 # It should be noted that at least pyreadline still shows file
818 849 # completions - is there a way around it?
819 850
820 851 # don't apply this on 'dumb' terminals, such as emacs buffers, so
821 852 # we don't interfere with their own tab-completion mechanism.
822 853 if not (self.dumb_terminal or line_buffer.strip()):
823 854 self.readline.insert_text('\t')
824 855 sys.stdout.flush()
825 856 return None
826 857
827 858 # Note: debugging exceptions that may occur in completion is very
828 859 # tricky, because readline unconditionally silences them. So if
829 860 # during development you suspect a bug in the completion code, turn
830 861 # this flag on temporarily by uncommenting the second form (don't
831 862 # flip the value in the first line, as the '# dbg' marker can be
832 863 # automatically detected and is used elsewhere).
833 864 DEBUG = False
834 865 #DEBUG = True # dbg
835 866 if DEBUG:
836 867 try:
837 868 self.complete(text, line_buffer, cursor_pos)
838 869 except:
839 870 import traceback; traceback.print_exc()
840 871 else:
841 872 # The normal production version is here
842 873
843 874 # This method computes the self.matches array
844 875 self.complete(text, line_buffer, cursor_pos)
845 876
846 877 try:
847 878 return self.matches[state]
848 879 except IndexError:
849 880 return None
@@ -1,115 +1,158 b''
1 1 """Tests for the IPython tab-completion machinery.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # stdlib
8 import os
8 9 import sys
9 10 import unittest
10 11
11 12 # third party
12 13 import nose.tools as nt
13 14
14 15 # our own packages
15 16 from IPython.core import completer
17 from IPython.utils.tempdir import TemporaryDirectory
16 18
17 19 #-----------------------------------------------------------------------------
18 20 # Test functions
19 21 #-----------------------------------------------------------------------------
20 22 def test_protect_filename():
21 23 pairs = [ ('abc','abc'),
22 24 (' abc',r'\ abc'),
23 25 ('a bc',r'a\ bc'),
24 26 ('a bc',r'a\ \ bc'),
25 27 (' bc',r'\ \ bc'),
26 28 ]
27 29 # On posix, we also protect parens and other special characters
28 30 if sys.platform != 'win32':
29 31 pairs.extend( [('a(bc',r'a\(bc'),
30 32 ('a)bc',r'a\)bc'),
31 33 ('a( )bc',r'a\(\ \)bc'),
32 34 ('a[1]bc', r'a\[1\]bc'),
33 35 ('a{1}bc', r'a\{1\}bc'),
34 36 ('a#bc', r'a\#bc'),
35 37 ('a?bc', r'a\?bc'),
36 38 ('a=bc', r'a\=bc'),
37 39 ('a\\bc', r'a\\bc'),
38 40 ('a|bc', r'a\|bc'),
39 41 ('a;bc', r'a\;bc'),
40 42 ('a:bc', r'a\:bc'),
41 43 ("a'bc", r"a\'bc"),
42 44 ('a*bc', r'a\*bc'),
43 45 ('a"bc', r'a\"bc'),
44 46 ('a^bc', r'a\^bc'),
45 47 ('a&bc', r'a\&bc'),
46 48 ] )
47 49 # run the actual tests
48 50 for s1, s2 in pairs:
49 51 s1p = completer.protect_filename(s1)
50 52 nt.assert_equals(s1p, s2)
51 53
52 54
53 55 def check_line_split(splitter, test_specs):
54 56 for part1, part2, split in test_specs:
55 57 cursor_pos = len(part1)
56 58 line = part1+part2
57 59 out = splitter.split_line(line, cursor_pos)
58 60 nt.assert_equal(out, split)
59 61
60 62
61 63 def test_line_split():
62 64 """Basice line splitter test with default specs."""
63 65 sp = completer.CompletionSplitter()
64 66 # The format of the test specs is: part1, part2, expected answer. Parts 1
65 67 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
66 68 # was at the end of part1. So an empty part2 represents someone hitting
67 69 # tab at the end of the line, the most common case.
68 70 t = [('run some/scrip', '', 'some/scrip'),
69 71 ('run scripts/er', 'ror.py foo', 'scripts/er'),
70 72 ('echo $HOM', '', 'HOM'),
71 73 ('print sys.pa', '', 'sys.pa'),
72 74 ('print(sys.pa', '', 'sys.pa'),
73 75 ("execfile('scripts/er", '', 'scripts/er'),
74 76 ('a[x.', '', 'x.'),
75 77 ('a[x.', 'y', 'x.'),
76 78 ('cd "some_file/', '', 'some_file/'),
77 79 ]
78 80 check_line_split(sp, t)
79 81 # Ensure splitting works OK with unicode by re-running the tests with
80 82 # all inputs turned into unicode
81 83 check_line_split(sp, [ map(unicode, p) for p in t] )
82 84
83 85
84 86 def test_unicode_completions():
85 87 ip = get_ipython()
86 88 # Some strings that trigger different types of completion. Check them both
87 89 # in str and unicode forms
88 90 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
89 91 for t in s + map(unicode, s):
90 92 # We don't need to check exact completion values (they may change
91 93 # depending on the state of the namespace, but at least no exceptions
92 94 # should be thrown and the return value should be a pair of text, list
93 95 # values.
94 96 text, matches = ip.complete(t)
95 97 nt.assert_true(isinstance(text, basestring))
96 98 nt.assert_true(isinstance(matches, list))
97 99
98 100
99 101 class CompletionSplitterTestCase(unittest.TestCase):
100 102 def setUp(self):
101 103 self.sp = completer.CompletionSplitter()
102 104
103 105 def test_delim_setting(self):
104 106 self.sp.set_delims(' ')
105 107 nt.assert_equal(self.sp.get_delims(), ' ')
106 108 nt.assert_equal(self.sp._delim_expr, '[\ ]')
107 109
108 110 def test_spaces(self):
109 111 """Test with only spaces as split chars."""
110 112 self.sp.delims = ' '
111 113 t = [('foo', '', 'foo'),
112 114 ('run foo', '', 'foo'),
113 115 ('run foo', 'bar', 'foo'),
114 116 ]
115 117 check_line_split(self.sp, t)
118
119
120 def test_has_open_quotes1():
121 for s in ["'", "'''", "'hi' '"]:
122 nt.assert_equal(completer.has_open_quotes(s), "'")
123
124
125 def test_has_open_quotes2():
126 for s in ['"', '"""', '"hi" "']:
127 nt.assert_equal(completer.has_open_quotes(s), '"')
128
129
130 def test_has_open_quotes3():
131 for s in ["''", "''' '''", "'hi' 'ipython'"]:
132 nt.assert_false(completer.has_open_quotes(s))
133
134
135 def test_has_open_quotes4():
136 for s in ['""', '""" """', '"hi" "ipython"']:
137 nt.assert_false(completer.has_open_quotes(s))
138
139
140 def test_file_completions():
141
142 ip = get_ipython()
143 with TemporaryDirectory() as tmpdir:
144 prefix = os.path.join(tmpdir, 'foo')
145 suffixes = map(str, [1,2])
146 names = [prefix+s for s in suffixes]
147 for n in names:
148 open(n, 'w').close()
149
150 # Check simple completion
151 c = ip.complete(prefix)[1]
152 nt.assert_equal(c, names)
153
154 # Now check with a function call
155 cmd = 'a = f("%s' % prefix
156 c = ip.complete(prefix, cmd)[1]
157 comp = [prefix+s for s in suffixes]
158 nt.assert_equal(c, comp)
General Comments 0
You need to be logged in to leave comments. Login now