Skip to content

Commit

Permalink
Update argcomplete.py
Browse files Browse the repository at this point in the history
  • Loading branch information
anki-code authored Sep 17, 2020
1 parent c127a8e commit 4582b07
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions xontrib/argcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
from pathlib import Path
from shutil import which

def _get_subproc_output(cmds):
result = __xonsh__.subproc_captured_object(cmds)
result.rtn # workaround https://github.com/xonsh/xonsh/issues/3394
return result.output

def _xontrib_argcomplete_completer(prefix, line, begidx, endidx, ctx):
"""
Adding support of kislyuk/argcomplete to xonsh.
Adding support of argcomplete to xonsh.
"""
py = None
file = None
Expand All @@ -20,34 +25,41 @@ def _xontrib_argcomplete_completer(prefix, line, begidx, endidx, ctx):
file = m.group(1)
py = 'xonsh' if file.endswith('.xsh') else 'python'
else:
m = re.match('^(.+?)\s+', line)
m = re.match('^(.+?)(\s+|$)', line)
if m:
if not m.group(2):
return None

maybe_file = m.group(1)
if Path(maybe_file).exists():
file = str(maybe_file)
else:
maybe_file = which(maybe_file)
if maybe_file:
maybe_file = Path(maybe_file)
if maybe_file.exists():
file = str(maybe_file)
if maybe_file and Path(maybe_file).exists():
file = str(maybe_file)
else:
maybe_file = _get_subproc_output(['bash', '-c','which proto']).strip()
if maybe_file and Path(maybe_file).exists():
file = maybe_file

if not file:
if __xonsh__.env.get('XONTRIB_ARGCOMPLETE_DEBUG', False):
return ((prefix, 'xontrib-argcomplete DEBUG: filename not found'), len(prefix))
return None

file = file[1:-1] if file[0] == "'" and file[-1] == "'" else file
filep = Path(file)
if not filep.exists():
return ((prefix, 'xontrib-argcomplete: file does not exists'), len(prefix))
if not Path(file).exists():
if __xonsh__.env.get('XONTRIB_ARGCOMPLETE_DEBUG', False):
return ((prefix, 'xontrib-argcomplete DEBUG: file does not exists'), len(prefix))
return None

# Check this is a text file
result = __xonsh__.subproc_captured_object(['file', file])
result.rtn # workaround https://github.com/xonsh/xonsh/issues/3394
if 'text' not in result.output:
if 'text' not in _get_subproc_output(['file', file]):
if __xonsh__.env.get('XONTRIB_ARGCOMPLETE_DEBUG', False):
return ((prefix, 'xontrib-argcomplete DEBUG: file type is not text'), len(prefix))
return None

found_argcomplete = False
with open(filep) as f:
with open(file) as f:
for x in range(10):
fline = next(f)
if x == 0 and not py:
Expand All @@ -63,13 +75,13 @@ def _xontrib_argcomplete_completer(prefix, line, begidx, endidx, ctx):

if found_argcomplete and py:
with __xonsh__.env.swap(_ARGCOMPLETE=str(1), _ARGCOMPLETE_IFS='\n', COMP_LINE=str(line), COMP_POINT=str(begidx)):
result = __xonsh__.subproc_captured_object(['bash', '-c', f"{py} '{file}' 8>&1"])
result.rtn # workaround https://github.com/xonsh/xonsh/issues/3394
tokens = set([t.replace(r'\ ', ' ') for t in result.output.split('\n') if prefix in t])
output = _get_subproc_output(['bash', '-c', f"{py} '{file}' 8>&1"])
tokens = set([t.replace(r'\ ', ' ') for t in output.split('\n') if prefix in t])

if len(tokens) == 0:
if __xonsh__.env.get('XONTRIB_ARGCOMPLETE_DEBUG', False):
return ((prefix, 'xontrib-argcomplete DEBUG: completions not found'), len(prefix))
return None
#return ((prefix, 'xontrib-argcomplete: completions not found'), len(prefix))

return (tokens, len(prefix))

Expand Down

0 comments on commit 4582b07

Please sign in to comment.