Skip to content

Commit 46e28b5

Browse files
author
a
committed
Add xonsh lexer
1 parent 75c603c commit 46e28b5

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='xontrib-argcomplete',
12-
version='0.2.10',
12+
version='0.3.0',
1313
license='BSD',
1414
author='anki-code',
1515
author_email='[email protected]',

xontrib/argcomplete.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,52 @@ def _get_subproc_output(cmds, debug=False):
1111
result.rtn # workaround https://github.com/xonsh/xonsh/issues/3394
1212
return result.output
1313

14+
def _get_executor(arg):
15+
m = re.match('^(python[0-9.]*|xonsh)$', arg)
16+
return m.group(1) if m else None
17+
18+
def _get_filepath(arg):
19+
return arg.strip("'").rstrip('\n')
20+
1421
def _xontrib_argcomplete_completer(prefix, line, begidx, endidx, ctx):
1522
"""
16-
Adding support of argcomplete to xonsh.
23+
Argcomplete support to tab completion of python and xonsh scripts in xonsh shell.
1724
"""
1825
debug = __xonsh__.env.get('XONTRIB_ARGCOMPLETE_DEBUG', False)
1926
py = None
2027
file = None
21-
m = re.match('^(python[0-9.]*|xonsh) ([\']*.+?(\\.py[0-9.]*|\\.xsh)[\']*)', line)
22-
if m:
23-
py = m.group(1)
24-
file = m.group(2)
28+
29+
args = __xonsh__.execer.parser.lexer.split(line)
30+
31+
if len(args) == 0:
32+
return None
33+
34+
py = _get_executor(args[0])
35+
if py:
36+
file = _get_filepath(args[1])
2537
else:
26-
m = re.match('^([\']*.+?(\\.py|\\.xsh)[\']*)', line)
27-
if m:
28-
file = m.group(1)
29-
py = 'xonsh' if file.endswith('.xsh') else 'python'
38+
file = _get_filepath(args[0])
39+
40+
if file.endswith('.xsh'):
41+
py = 'xonsh'
42+
elif file.endswith('.py'):
43+
py = 'python'
44+
45+
if not Path(file).exists():
46+
which_maybe_file = which(file)
47+
if which_maybe_file and Path(which_maybe_file).exists():
48+
file = str(which_maybe_file)
3049
else:
31-
m = re.match('^(.+?)(\\s+|$)', line)
32-
if m:
33-
if not m.group(2):
34-
return None
35-
36-
maybe_file = m.group(1)
37-
if Path(maybe_file).exists():
38-
file = str(maybe_file)
39-
else:
40-
which_maybe_file = which(maybe_file)
41-
if which_maybe_file and Path(which_maybe_file).exists():
42-
file = str(which_maybe_file)
43-
else:
44-
try:
45-
which_maybe_file = _get_subproc_output(['which', maybe_file], debug).strip()
46-
except:
47-
return None
48-
if which_maybe_file and Path(which_maybe_file).exists():
49-
file = which_maybe_file
50+
try:
51+
which_maybe_file = _get_subproc_output(['which', file], debug).strip()
52+
except:
53+
return None
54+
if which_maybe_file and Path(which_maybe_file).exists():
55+
file = which_maybe_file
5056

5157
if not file:
52-
return None if not debug else ((prefix, 'xontrib-argcomplete DEBUG: filename not found'), len(prefix))
58+
return None if not debug else ((prefix, 'xontrib-argcomplete DEBUG: path to file not found'), len(prefix))
5359

54-
file = file[1:-1] if file[0] == "'" and file[-1] == "'" else file
5560
if not Path(file).exists():
5661
return None if not debug else ((prefix, 'xontrib-argcomplete DEBUG: file does not exists'), len(prefix))
5762

0 commit comments

Comments
 (0)