Skip to content

autocompletions

CompletionType

Constants for common completion types

Source code in /home/runner/work/arc/arc/arc/autocompletions.py
class CompletionType:
    """Constants for common completion types"""

    FILE = "file"
    DIR = "dir"
    USERS = "users"
    GROUPS = "groups"
    PLAIN = "plain"

ShellCompletion

Source code in /home/runner/work/arc/arc/arc/autocompletions.py
class ShellCompletion:
    template: t.ClassVar[str]
    shells: dict[str, type["ShellCompletion"]] = {}

    def __init__(self, command: Command, info: CompletionInfo):
        self.command = command
        self.info = info
        self.command_name = self.command.name

    def __init_subclass__(cls, name: str) -> None:
        ShellCompletion.shells[name] = cls

    @property
    def completion_vars(self) -> dict[str, t.Any]:
        return {
            "name_exe": "python cli.py"
            if os.getenv("ARC_DEVELOPMENT")
            else self.command_name,
            "name_com": self.command_name,
            "func_name": f"_{self.command_name}_completions".replace("-", "_"),
            "completion_var": self.completion_var,
        }

    @property
    def completion_var(self) -> str:
        return f"_{self.command_name}_complete".upper().replace("-", "_")

    def should_complete(self) -> bool:
        return os.getenv(self.completion_var) is not None

    def source(self) -> str:
        """Returns the script for the paricular lanuage"""
        return self.template.format(**self.completion_vars)

    def complete(self) -> str:
        """Actually provides the completions"""
        return ""

    def format_completion(self, comp: Completion) -> str:
        return ""

    @classmethod
    def run(cls, shell: str, command: Command) -> str:
        info = CompletionInfo.from_env()
        if shell not in cls.shells:
            raise errors.ArgumentError(
                f"Unsupported shell: {shell}. "
                f"Supported shells: {Join.with_comma(cls.shells)}"
            )
        comp: ShellCompletion = cls.shells[shell](command, info)

        with open("completions.log", "w+") as f, redirect_stderr(f):
            res = comp.complete() if comp.should_complete() else comp.source()
            f.write("\n")
            f.write(" ".join(info.words))
            f.write("\n")
            f.write(res)
            f.write("\n\n")

        return res

complete()

Actually provides the completions

Source code in /home/runner/work/arc/arc/arc/autocompletions.py
def complete(self) -> str:
    """Actually provides the completions"""
    return ""

source()

Returns the script for the paricular lanuage

Source code in /home/runner/work/arc/arc/arc/autocompletions.py
def source(self) -> str:
    """Returns the script for the paricular lanuage"""
    return self.template.format(**self.completion_vars)

get_completions(obj, info, *args, **kwargs)

Gets the completions for a particular object that supports the CompletionProtocol

Source code in /home/runner/work/arc/arc/arc/autocompletions.py
def get_completions(
    obj: CompletionProtocol,
    info: CompletionInfo,
    *args: t.Any,
    **kwargs: t.Any,
) -> list[Completion]:
    """Gets the completions for a particular object that supports the `CompletionProtocol`"""
    comps = obj.__completions__(info, *args, **kwargs)
    if comps is None:
        comps = []
    else:
        comps = list(comps)
    return comps