add fish shell based completer (#4569)

* feat: add fish shell based completer

* refactor: use filter-func to filter results
This commit is contained in:
Noorhteen Raja NJ 2021-11-29 21:02:32 +05:30 committed by GitHub
parent d5c6c9e4e6
commit b031047beb
Failed to generate hash of commit
2 changed files with 66 additions and 0 deletions

23
news/fish-completer.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* `xontrib.fish_completer` is available to complete using `fish` shell.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

43
xontrib/fish_completer.py Normal file
View file

@ -0,0 +1,43 @@
from xonsh.completers import completer
from xonsh.completers.tools import (
RichCompletion,
contextual_command_completer,
get_filter_function,
)
import subprocess as sp
from xonsh.built_ins import XSH
from xonsh.parsers.completion_context import CommandContext
def create_rich_completion(line: str):
line = line.strip()
if "\t" in line:
cmd, desc = map(str.strip, line.split("\t", maxsplit=1))
else:
cmd, desc = line, ""
return RichCompletion(
str(cmd),
description=str(desc),
append_space=True,
)
@contextual_command_completer
def fish_proc_completer(ctx: CommandContext):
"""Populate completions using fish shell and remove bash-completer"""
args = [arg.value for arg in ctx.args] + [ctx.prefix]
line = " ".join(args)
args = ["fish", "-c", f"complete -C '{line}'"]
env = XSH.env.detype()
output = sp.check_output(args, env=env).decode()
filter_func = get_filter_function()
if output:
for line in output.strip().splitlines(keepends=False):
comp = create_rich_completion(line)
if filter_func(comp, ctx.prefix):
yield comp
completer.add_one_completer("fish", fish_proc_completer, "<bash")