completer: handle spaces, .., and .

This commit is contained in:
adam j hartz 2015-04-03 18:25:44 -04:00
parent b085ccd9ca
commit 0ec02095ac

View file

@ -69,30 +69,47 @@ class Completer(object):
""" """
space = ' ' # intern some strings for faster appending space = ' ' # intern some strings for faster appending
slash = '/' slash = '/'
ctx = ctx or {}
cmd = line.split(' ', 1)[0]
if begidx == 0: if begidx == 0:
# the first thing we're typing; could be python or subprocess, so
# anything goes.
rtn = self.cmd_complete(prefix) rtn = self.cmd_complete(prefix)
elif line.split(' ', 1)[0] in self.bash_complete_funcs: elif cmd in self.bash_complete_funcs:
rtn = set() rtn = set()
for s in self.bash_complete(prefix, line, begidx, endidx): for s in self.bash_complete(prefix, line, begidx, endidx):
if os.path.isdir(s.rstrip()): if os.path.isdir(s.rstrip()):
s = s.rstrip() + slash s = s.rstrip() + slash
rtn.add(s) rtn.add(s)
if len(rtn) == 0: if len(rtn) != 0:
rtn = self.path_complete(prefix) rtn = self.path_complete(prefix)
return sorted(rtn) return sorted(rtn)
elif cmd not in ctx and cmd not in XONSH_TOKENS:
# subproc mode; do path completions
return sorted(self.path_complete(prefix))
else: else:
# if we're here, we're definitely python?
rtn = set() rtn = set()
rtn |= {s for s in XONSH_TOKENS if s.startswith(prefix)} rtn |= {s for s in XONSH_TOKENS if s.startswith(prefix)}
if ctx is not None: if ctx is not None:
rtn |= {s for s in ctx if s.startswith(prefix)} rtn |= {s for s in ctx if s.startswith(prefix)}
rtn |= {s for s in dir(builtins) if s.startswith(prefix)} rtn |= {s for s in dir(builtins) if s.startswith(prefix)}
rtn |= {s + space for s in builtins.aliases if s.startswith(prefix)} rtn |= {s + space for s in builtins.aliases if s.startswith(prefix)}
self._add_env(rtn, prefix)
rtn |= self.path_complete(prefix)
return sorted(rtn)
def _add_env(self, paths, prefix):
if prefix.startswith('$'): if prefix.startswith('$'):
env = builtins.__xonsh_env__ env = builtins.__xonsh_env__
key = prefix[1:] key = prefix[1:]
rtn |= {'$' + k for k in env if k.startswith(key)} paths.update({'$' + k for k in env if k.startswith(key)})
rtn |= self.path_complete(prefix)
return sorted(rtn) def _add_dots(self, paths, prefix):
if prefix in {'', '.'}:
paths.update({'./', '../'})
if prefix == '..':
paths.add('../')
def cmd_complete(self, cmd): def cmd_complete(self, cmd):
"""Completes a command name based on what is on the $PATH""" """Completes a command name based on what is on the $PATH"""
@ -123,6 +140,8 @@ class Completer(object):
if tilde in prefix: if tilde in prefix:
home = os.path.expanduser(tilde) home = os.path.expanduser(tilde)
paths = {s.replace(home, tilde) for s in paths} paths = {s.replace(home, tilde) for s in paths}
self._add_env(paths, prefix)
self._add_dots(paths, prefix)
return paths return paths
def bash_complete(self, prefix, line, begidx, endidx): def bash_complete(self, prefix, line, begidx, endidx):