MCPcopy Create free account
hub / github.com/ipython/ipython / cd_completer

Function cd_completer

IPython/core/completerlib.py:298–350  ·  view source on GitHub ↗

Completer function for cd, which only returns directories.

(self, event)

Source from the content-addressed store, hash-verified

296
297
298def cd_completer(self, event):
299 """Completer function for cd, which only returns directories."""
300 ip = get_ipython()
301 relpath = event.symbol
302
303 #print(event) # dbg
304 if event.line.endswith('-b') or ' -b ' in event.line:
305 # return only bookmark completions
306 bkms = self.db.get('bookmarks', None)
307 if bkms:
308 return bkms.keys()
309 else:
310 return []
311
312 if event.symbol == '-':
313 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
314 # jump in directory history by number
315 fmt = '-%0' + width_dh +'d [%s]'
316 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
317 if len(ents) > 1:
318 return ents
319 return []
320
321 if event.symbol.startswith('--'):
322 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
323
324 # Expand ~ in path and normalize directory separators.
325 relpath, tilde_expand, tilde_val = expand_user(relpath)
326 relpath = relpath.replace('\\','/')
327
328 found = []
329 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
330 if os.path.isdir(f)]:
331 if ' ' in d:
332 # we don't want to deal with any of that, complex code
333 # for this is elsewhere
334 raise TryNext
335
336 found.append(d)
337
338 if not found:
339 if os.path.isdir(relpath):
340 return [compress_user(relpath, tilde_expand, tilde_val)]
341
342 # if no completions so far, try bookmarks
343 bks = self.db.get('bookmarks',{})
344 bkmatches = [s for s in bks if s.startswith(event.symbol)]
345 if bkmatches:
346 return bkmatches
347
348 raise TryNext
349
350 return [compress_user(p, tilde_expand, tilde_val) for p in found]
351
352def reset_completer(self, event):
353 "A completer for %reset magic"

Callers

nothing calls this directly

Calls 3

get_ipythonFunction · 0.90
expand_userFunction · 0.85
compress_userFunction · 0.70

Tested by

no test coverage detected