bash autocomplete space deduplication bugfix

This commit is contained in:
Anthony Scopatz 2018-04-02 13:43:46 -04:00
parent 75120dfd20
commit c44206564f
2 changed files with 75 additions and 3 deletions

13
news/complete_line.rst Normal file
View file

@ -0,0 +1,13 @@
**Added:** None
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:**
* Fixed issue where xonsh would deduplicate spaces from bash autocompletions.
**Security:** None

View file

@ -2,6 +2,8 @@
from bash.
"""
# developer note: this file should not perform any action on import.
# This file comes from https://github.com/xonsh/py-bash-completion
# and should be edited there!
import os
import re
import sys
@ -237,7 +239,7 @@ COMP_COUNT={end}
COMP_CWORD={n}
$_func {cmd} {prefix} {prev}
for ((i=0;i<${{#COMPREPLY[*]}};i++)) do echo ${{COMPREPLY[i]}}; done
for ((i=0;i<${{#COMPREPLY[*]}};i++)) do echo "${{COMPREPLY[i]}}"; done
"""
@ -274,8 +276,8 @@ def bash_completions(prefix, line, begidx, endidx, env=None, paths=None,
Returns
-------
rtn : list of str
Possible completions of prefix, sorted alphabetically.
rtn : set of str
Possible completions of prefix
lprefix : int
Length of the prefix to be replaced in the completion.
"""
@ -340,3 +342,60 @@ def bash_completions(prefix, line, begidx, endidx, env=None, paths=None,
out = set([x.rstrip() for x in out])
return out, len(prefix) - strip_len
def bash_complete_line(line, return_line=True, **kwargs):
"""Provides the completion from the end of the line.
Parameters
----------
line : str
Line to complete
return_line : bool, optional
If true (default), will return the entire line, with the completion added.
If false, this will instead return the strings to append to the original line.
kwargs : optional
All other keyword arguments are passed to the bash_completions() function.
Returns
-------
rtn : set of str
Possible completions of prefix
"""
# set up for completing from the end of the line
split = line.split()
if len(split) > 1 and not line.endswith(' '):
prefix = split[-1]
begidx = len(line.rsplit(prefix)[0])
else:
prefix = ''
begidx = len(line)
endidx = len(line)
# get completions
out, lprefix = bash_completions(prefix, line, begidx, endidx, **kwargs)
# reformat output
if return_line:
preline = line[:-lprefix]
rtn = {preline + o for o in out}
else:
rtn = {o[lprefix:] for o in out}
return rtn
def _bc_main(args=None):
"""Runs complete_line() and prints the output."""
from argparse import ArgumentParser
p = ArgumentParser('bash_completions')
p.add_argument('--return-line', action='store_true', dest='return_line', default=True,
help='will return the entire line, with the completion added')
p.add_argument('--no-return-line', action='store_false', dest='return_line',
help='will instead return the strings to append to the original line')
p.add_argument('line', help='line to complete')
ns = p.parse_args(args=args)
out = bash_complete_line(ns.line, return_line=ns.return_line)
for o in sorted(out):
print(o)
if __name__ == '__main__':
_bc_main()